Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only

IBM Ponder This Challenge April 2019

  • 1.  IBM Ponder This Challenge April 2019

    Posted Fri May 10, 2019 10:14 AM

    Hi,

    why last month challenge

    https://www.research.ibm.com/haifa/ponderthis/challenges/April2019.html

    was not like any challenge ?

    Because it was written by  Reda Kebbaj  who translated my post

    What is optimization and how it can help you do more with less ? Zoo, buses, kids and optimization.

    into Arabic.

     

    I read a very good solution at https://anothercasualcoder.blogspot.com/2019/04/ibm-ponder-this-april-2019.html but let you give mine.

    And of course I used CPLEX and OPL:

    using CP;

    int m=200;
    range r=1..m;

    // array to know whether a number is prime
    int prime[i in r]=(i!=1) &&and(j in 2..ftoi(ceil(sqrt(i-1)))) (0!=i mod j); // is prime ?

    // set of primes    
    {int} primes={i | i in r : 1==prime[i]};

    // the value is each cell of the 3*3 square
    dvar int x[1..3,1..3] in 1..m;
    dvar int averagetimes3;

    subject to
    {

    // all Different
    allDifferent(x);

    // 9 primes
    forall(i,j in 1..3) x[i,j] in primes;

    // sum of rows, columns and diagonals
    forall(i in 1..3) averagetimes3==sum(j in 1..3) x[i][j];
    forall(j in 1..3) averagetimes3==sum(i in 1..3) x[i][j];
    averagetimes3==sum(i in 1..3) x[i][i];
    averagetimes3==sum(i in 1..3) x[i][4-i];

    // that sum should be a multiple of 3
    averagetimes3 mod 3==0;
    // and prime
    (averagetimes3 div 3) in primes;
    }

     

    Of course within CPLEX I relied on Constraint Programming (CPOptimizer)

    But I could also use MIP:

    int m=200;
    range r=1..m;

    // array to know whether a number is prime
    int prime[i in r]=(i!=1) &&and(j in 2..ftoi(ceil(sqrt(i-1)))) (0!=i mod j); // is prime ?

    // set of primes    
    {int} primes={i | i in r : 1==prime[i]};

    // the value is each cell of the 3*3 square
    dvar int x[1..3,1..3] in 1..m;
    dvar int averagetimes3;
    dvar int average;

    subject to
    {

    // all Different
    forall(i,j,i2,j2 in 1..3:(i!=i2) || (j!=j2)) x[i][j]!=x[i2][j2];

    // 9 primes
    forall(i,j in 1..3) 1<=sum(p in primes ) (x[i,j]==p);

    // sum of rows, columns and diagonals
    forall(i in 1..3) averagetimes3==sum(j in 1..3) x[i][j];
    forall(j in 1..3) averagetimes3==sum(i in 1..3) x[i][j];
    averagetimes3==sum(i in 1..3) x[i][i];
    averagetimes3==sum(i in 1..3) x[i][4-i];

    // that sum should be a multiple of 3
    averagetimes3 ==3 * average;
    // and prime
    1<=sum(p in primes ) (p==average);
    }

     

    Again, on this very simple problem, we can see the power of CPLEX and OPL

    regards

     

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer