Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Displaying 2 dimensions objects in the OPL IDE

  • 1.  Displaying 2 dimensions objects in the OPL IDE

    Posted 08/30/13 05:31 AM

    Hi,

    many users heard about

    Viewing the results of scheduling problems in the IDE

    in IDE and OPL > CPLEX Studio IDE > Getting Started with the IDE > Getting Started Tutorial > Examining a solution to the model,

    but many do not realize that even for CPLEX problems that are not scheduling problems, this can be very useful.

    Let me consider the game of life example.

     

    int n=6;
    int Half=n div 2;
    range FirstHalf = 1..Half;
    range LastHalf = n-Half+1..n;
    range States = 0..1;
    range Bord = 0..(n+1);
    range Interior = 1..n;

    range obj = 0..(n*n);

    tuple neighbors {
       int row;
       int col;
    }

    {neighbors} Neighbor =
      {<(-1),(-1)>,<(-1),0>,<(-1),1>,<0,(-1)>,<0,1>,<1,(-1)>,<1,0>,<1,1>};

    dvar int Life[Bord][Bord] in States;
    dvar int Obj in obj;

    maximize Obj;

    subject to {
      ct1:
        Obj == sum( i , j in Bord )
          Life[i][j];
          
      forall( i , j in Interior ) {
        ct21:
          2*Life[i][j] - sum( nb in Neighbor )
            Life[i+nb.row][j+nb.col] <= 0;
        ct22:
          3*Life[i][j] + sum( nb in Neighbor )
            Life[i+nb.row][j+nb.col] <= 6;
        forall( ordered n1 , n2 , n3 in Neighbor ) {
          ct23:
            -Life[i][j]+Life[i+n1.row][j+n1.col]
                       +Life[i+n2.row][j+n2.col]
                       +Life[i+n3.row][j+n3.col]
            -sum( nb in Neighbor : nb!=n1 && nb!=n2 && nb!=n3 )
              Life[i+nb.row][j+nb.col] <= 2;
        }
      }
      forall( j in Bord ) {
        ct31:
          Life[0][j] == 0;
        ct32:   
          Life[j][0] == 0;
        ct33:   
          Life[j][n+1] == 0;
        ct34:   
          Life[n+1][j] == 0;
      }
      forall( i in Bord : i<n ) {
        ct41:
          Life[i][1]+Life[i+1][1]+Life[i+2][1] <= 2;
        ct42:
          Life[1][i]+Life[1][i+1]+Life[1][i+2] <= 2;
        ct43:
          Life[i][n]+Life[i+1][n]+Life[i+2][n] <= 2;
        ct44:
          Life[n][i]+Life[n][i+1]+Life[n][i+2] <= 2;
      }
      ct5:
        sum( i in FirstHalf , j in Bord )
          Life[i][j] >=
        sum( i in LastHalf , j in Bord )
          Life[i][j];
      ct6:
        sum( i in Bord , j in FirstHalf )
          Life[i][j] >=
        sum( i in Bord , j in LastHalf )
          Life[i][j];   
    }

    if I click on Life I see gameoflife1.jpg

    but now if I add in the model

    tuple sequence_like {
       int start;
       int end;
       string label;
       int type;
     };   
     
    {sequence_like} array2[i in 1..n] = {<j-1,j," ",Life[i][j]> | j in 1..n};

     
      execute noname {
        
       array2;
    }

    then I can see the result as gameoflife2.jpg, which is much nicer.

    Many use cases when you deal with 2 dimensions objects like which warehouse should serve which store ...

    Regards

    Alex

     

    Alex Fleischer

    PS:

    Many how to with OPL at https://www.linkedin.com/pulse/how-opl-alex-fleischer/

    Many examples from a very good book : https://www.linkedin.com/pulse/model-building-oplcplex-alex-fleischer/

    Making optimization simple : https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Displaying 2 dimensions objects in the OPL IDE

    Posted 01/02/18 08:29 AM

    Hi,

    I posted this 4 years ago and I have to reckon now that using Gantt chart in order to display 2D objects is slighly far-fetched.

    With CPLEX 12.8 and external calls like https://www.ibm.com/developerworks/community/forums/html/topic?id=4bef7847-9ac0-4402-bd3d-74eba89a03f8&ps=25

    this gets easier.

    The idea is simply to generate a python file that will do the display and call that python file from OPL!

    The OPL file will be:

    int n=6;
    int Half=n div 2;
    range FirstHalf = 1..Half;
    range LastHalf = n-Half+1..n;
    range States = 0..1;
    range Bord = 0..(n+1);
    range Interior = 1..n;

    range obj = 0..(n*n);

    tuple neighbors {
       int row;
       int col;
    }

    {neighbors} Neighbor =
      {<(-1),(-1)>,<(-1),0>,<(-1),1>,<0,(-1)>,<0,1>,<1,(-1)>,<1,0>,<1,1>};

    dvar int Life[Bord][Bord] in States;
    dvar int Obj in obj;

    maximize Obj;

    subject to {
      ct1:
        Obj == sum( i , j in Bord )
          Life[i][j];
         
      forall( i , j in Interior ) {
        ct21:
          2*Life[i][j] - sum( nb in Neighbor )
            Life[i+nb.row][j+nb.col] <= 0;
        ct22:
          3*Life[i][j] + sum( nb in Neighbor )
            Life[i+nb.row][j+nb.col] <= 6;
        forall( ordered n1 , n2 , n3 in Neighbor ) {
          ct23:
            -Life[i][j]+Life[i+n1.row][j+n1.col]
                       +Life[i+n2.row][j+n2.col]
                       +Life[i+n3.row][j+n3.col]
            -sum( nb in Neighbor : nb!=n1 && nb!=n2 && nb!=n3 )
              Life[i+nb.row][j+nb.col] <= 2;
        }
      }
      forall( j in Bord ) {
        ct31:
          Life[0][j] == 0;
        ct32:  
          Life[j][0] == 0;
        ct33:  
          Life[j][n+1] == 0;
        ct34:  
          Life[n+1][j] == 0;
      }
      forall( i in Bord : i<n ) {
        ct41:
          Life[i][1]+Life[i+1][1]+Life[i+2][1] <= 2;
        ct42:
          Life[1][i]+Life[1][i+1]+Life[1][i+2] <= 2;
        ct43:
          Life[i][n]+Life[i+1][n]+Life[i+2][n] <= 2;
        ct44:
          Life[n][i]+Life[n][i+1]+Life[n][i+2] <= 2;
      }
      ct5:
        sum( i in FirstHalf , j in Bord )
          Life[i][j] >=
        sum( i in LastHalf , j in Bord )
          Life[i][j];
      ct6:
        sum( i in Bord , j in FirstHalf )
          Life[i][j] >=
        sum( i in Bord , j in LastHalf )
          Life[i][j];   
    }


    tuple LifeSolutionT{
        int Bord1;
        int Bord2;
        int value;
    };
    {LifeSolutionT} LifeSolution = {<i0,i1,Life[i0][i1]> | i0 in Bord,i1 in Bord};

    execute DISPLAY
    {

    var python=new IloOplOutputFile("c:/display.py");
    python.writeln("import matplotlib.pyplot as plt");
    python.writeln("import numpy as np");
    python.writeln("grid=np.array(");

    python.writeln("[");
    for(var i in Bord)
    {
        python.writeln("[");
        for(var j in Bord) python.write(Life[i][j],",");
        python.writeln("],");
        ;
    }
    python.writeln("]");

    python.writeln(")");
    python.writeln("im = plt.imshow(grid, cmap='hot')");
    python.writeln("im.axes.get_xaxis().set_visible(False)");
    python.writeln("im.axes.get_yaxis().set_visible(False)");
    python.writeln("plt.show()");
    python.close();

    IloOplExec("C:\\Python36\\python.exe c:\\display.py");
    }

    which will give

    after generating display.py

    import matplotlib.pyplot as plt
    import numpy as np
    grid=np.array(
    [
    [
    0,0,0,0,0,0,0,0,],
    [
    0,1,1,0,0,1,1,0,],
    [
    0,1,0,0,1,0,1,0,],
    [
    0,0,1,0,1,1,0,0,],
    [
    0,1,1,0,0,0,0,0,],
    [
    0,1,0,0,1,1,0,0,],
    [
    0,0,1,1,0,1,0,0,],
    [
    0,0,0,0,0,0,0,0,],
    ]
    )
    im = plt.imshow(grid, cmap='hot')
    im.axes.get_xaxis().set_visible(False)
    im.axes.get_yaxis().set_visible(False)
    plt.show()

    regards


    #DecisionOptimization