Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Tuple with set element - add() problem

    Posted 12/11/18 11:36 AM

    Originally posted by: K__U


    Hi,

    I defined a tuple in my master problem (outside the main script):

    tuple sub_prob_tup {

                    string dc;

                    {string} demand;

    }

    {sub_prob_tup} sub_prob_control;

     

    I would like to update (add) my sub problem solutions to this master tuple with below line in the main script:

    thisOplModel.sub_prob_control.add(opl3.sub_prob_control_tmp);

     

    Additionally, in the sub model the elements are defined as below:

    {string} DC=...;                     

    {string} Demand_N=...;                  

     

    tuple sub_prob_tup {

                    string dc;

                    {string} demand;

    }

    {sub_prob_tup} sub_prob_control_tmp;

    string tmp1 = first(DC);

     

    execute{

    sub_prob_control_tmp.add(tmp1,Demand_N);

    writeln("sub_prob_control_tmp:", sub_prob_control_tmp)

    };

    The sub tuple seems to be OK, and it's giving me the results that I want, but the problem is I cannot update the master tuple.

    I have other tuples which I can add with the same method, but this master is not working. (It is deleting previously added elements, deleting the sets etc.)

    Is it related to set {} type that I am using? Any suggestions?

     

    >>>>>

     

    I added my results and tried to explain the problem:

    First iteration everything seems to be OK:

    opl3.sub_prob_control_tmp >>> 

    {<"6037" {"6019" "6029" "6037" "6059" "6065" "6071" "6073" "6111" "32003"}>}

     

    thisOplModel.sub_prob_control >>> 

    {<"6037"  {"6019" "6029" "6037" "6059" "6065" "6071" "6073" "6111" "32003"}>}

     

    In the second iteration sub problem solution:

    opl3.sub_prob_control_tmp >>>

    {<"4013" {"4013" "4019"}>}

     

    While I was expecting the main tuple as below:

    thisOplModel.sub_prob_control >>> 

    {<"6037" {"6019" "6029" "6037" "6059" "6065" "6071" "6073" "6111" "32003"}>

    <"4013" {"4013" "4019"}>}

    it returns:

    thisOplModel.sub_prob_control >>> 

    {<"6037" {"4013" "4019"}> <"4013" {"4013" "4019"}>}

     

    First element is added properly (dc) however second element {demand} is updated in both.

    This problem is continuing for all iterations. (it adds dc properly but replaces all {demand} with the latest added one)

     

     

    Thanks,


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Tuple with set element - add() problem

    Posted 12/11/18 02:46 PM

    Hi

    let me share a small example that works fine:

    sub.mod

    tuple t
    {
    int a;
    int b;
    }

    {t} y=...;

    execute
    {
     writeln("y=",y);
    }

    dvar float x;

    maximize x;
    subject to {
          x<=sum(i in y)i.a*i.b;
    }

    execute
    {
        writeln("x=",x);
    }

     

    and then main.mod

    tuple t
    {
    int a;
    int b;
    }

        {t} s={};

        main {
          var source = new IloOplModelSource("sub.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         
          for(var k=1;k<=5;k++)
          {
          var opl = new IloOplModel(def,cplex);
            
          var data2= new IloOplDataElements();
         
        data2.y=thisOplModel.s;
        data2.y.add(k,k+1);
          opl.addDataSource(data2);
          opl.generate();

          if (cplex.solve()) {
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
        data2.end();
         opl.end();
         
         
        }  
         
        }

     

    gives

     

    y= {<1 2>}
    OBJ = 2
    y= {<1 2> <2 3>}
    OBJ = 8
    y= {<1 2> <2 3> <3 4>}
    OBJ = 20
    y= {<1 2> <2 3> <3 4> <4 5>}
    OBJ = 40
    y= {<1 2> <2 3> <3 4> <4 5> <5 6>}
    OBJ = 70

     

    regards

     

    https://www.linkedin.com/pulse/how-opl-alex-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Tuple with set element - add() problem

    Posted 12/11/18 03:11 PM

    Originally posted by: K__U


    Hi Alex,

     

    Thanks you for the example, but it is not exactly the same problem. 

    In your example only the sub problem's tuple (y) is updated, but not the tuple in the main problem (s).

     

    What I am trying to do is adding a new element (which is a solution of sub problem) to the main tuple and use this tuple in another sub problem.

    The problem is I am able to do similar additions/updates with other tuples in my model but only this tuple (sub_prob_control) is not working.

     

    Thanks


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Tuple with set element - add() problem

    Posted 12/12/18 04:37 AM

    Hi,

    then

    tuple t
    {
    int a;
    int b;
    }

        {t} s={};

        main {
          var source = new IloOplModelSource("sub.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         
          for(var k=1;k<=5;k++)
          {
          var opl = new IloOplModel(def,cplex);
            
          var data2= new IloOplDataElements();
         
        data2.y=thisOplModel.s;
        data2.y.add(k,k+1);
          opl.addDataSource(data2);
          opl.generate();

          if (cplex.solve()) {
             writeln("OBJ = " + cplex.getObjValue());
             opl.postProcess();
             data2.y.add(opl.x,1);
          } else {
             writeln("No solution");
          }
        data2.end();
         opl.end();
         
         
        }  
         
        }

     

    which gives

     

    y= {<1 2>}
    OBJ = 2
    x=2
    y= {<1 2> <2 1> <2 3>}
    OBJ = 10
    x=10
    y= {<1 2> <2 1> <2 3> <10 1> <3 4>}
    OBJ = 32
    x=32
    y= {<1 2> <2 1> <2 3> <10 1> <3 4> <32 1> <4 5>}
    OBJ = 84
    x=84
    y= {<1 2> <2 1> <2 3> <10 1> <3 4> <32 1> <4 5> <84 1> <5 6>}
    OBJ = 198
    x=198

    and uses the result of the submodel to change the tuple set s

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Tuple with set element - add() problem

    Posted 12/12/18 04:37 PM

    Originally posted by: K__U


    Hi Alex,

     

    I am still having trouble with updating the set defined in the tuple.

    How can I define a variable for a set in the main script  (instead of a string) ?

    I have tried :

    var settmp = new Set();

    but as expected it didn't work.

     

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Tuple with set element - add() problem

    Posted 12/13/18 03:49 AM

    Hi,

    since we cannot create a new OPL set in scripting you can book them in advance:

    sub.mod


    tuple t
    {
     int e1;
     int e2;    
    }
        
    {t} y=...;

    execute
    {
      writeln("y=",y);
    }

    dvar float x;

    maximize x;
    subject to {
      x<=sum(i in y)i.e2;
    }

    execute
    {
      writeln("x=",x);
    }

    and then

     

        tuple t
        {
        int a;
        int b;
        }

            {t} s={};
            {t} storeds[1..5]; // book sets in advance

            main {
              var source = new IloOplModelSource("sub.mod");
              var cplex = new IloCplex();
              var def = new IloOplModelDefinition(source);
             
             
              for(var k=1;k<=5;k++)
              {
              var opl = new IloOplModel(def,cplex);
                
              var data2= new IloOplDataElements();
             
            data2.y=thisOplModel.s;
            data2.y.add(k,k+1);
              opl.addDataSource(data2);
              opl.generate();

              if (cplex.solve()) {
                 writeln("OBJ = " + cplex.getObjValue());
                 opl.postProcess();
                 data2.y.add(opl.x,1);
                 for(var i in data2.y) thisOplModel.storeds[k].add(i);
              } else {
                 writeln("No solution");
              }
            data2.end();
             opl.end();
             
             
            }  
            
            writeln("storeds = ",thisOplModel.storeds);
            }

     

    that gives

    y= {<1 2>}
    OBJ = 2
    x=2
    y= {<1 2> <2 1> <2 3>}
    OBJ = 6
    x=6
    y= {<1 2> <2 1> <2 3> <6 1> <3 4>}
    OBJ = 11
    x=11
    y= {<1 2> <2 1> <2 3> <6 1> <3 4> <11 1> <4 5>}
    OBJ = 17
    x=17
    y= {<1 2> <2 1> <2 3> <6 1> <3 4> <11 1> <4 5> <17 1> <5 6>}
    OBJ = 24
    x=24
    storeds =  [{<1 2> <2 1>} {<1 2> <2 1> <2 3> <6 1>} {<1 2> <2 1> <2 3>
             <6 1> <3 4> <11 1>} {<1 2> <2 1> <2 3> <6 1> <3 4>
             <11 1> <4 5> <17 1>} {<1 2> <2 1> <2 3> <6 1> <3 4>
             <11 1> <4 5> <17 1> <5 6> <24 1>}]

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer