Topic
  • 5 replies
  • Latest Post - ‏2013-12-07T08:53:13Z by AlexFleischer
ReneGusmao
ReneGusmao
2 Posts

Pinned topic How to update values of variables in opl main script

‏2013-10-03T13:49:34Z |

Hi, I have my model implemented in opl and I added a constraint, this constraint means rejected demand d if it is selected. I am using main script, I would like to iteratively change the vector selected so different demands will be rejected at each iteration. How can I do it? I've tried some approaches, but I can't change this vector. It only can be processed once in pre-processing execute blocks or if it is in external data file, but no success in changing it. Someone can help me?

int selected[Demands]; //1 if demands will be rejected at front or 0 let the cplex decide

minimize sum xd[d]*d.bd;

forall(d in Demands: selected[d]==1)

xd[d]==1;

A similar approach is

sum(d in Demands: selected[d]==1)

xd[d] == RejectedCount;

 

Thanks in Advance!

  • AlexFleischer
    AlexFleischer
    1475 Posts

    Re: How to update values of variables in opl main script

    ‏2013-10-03T14:23:31Z  

    Hi,

     

    you can do something like iterative solving in a main but then you regenerate the model at each iteration:

    var prj= thisOplModel

    varmod = prj.modelDefinition

    var dat= prj.dataElements

    var capFlour= dat.capacity["flour"]

    for (var i=1; i<=4; ++i) {

      cplex.clearModel()

       prj= new IloOplModel(mod,cplex)

       prj.addDataSource(dat)

       dat.capacity["flour"] = capFlour++

       prj.generate()

       cplex.solve()

       writeln("Flour capacity ", capFlour,

          ", Objective function:", cplex.getObjValue())

    }

    or you may do full incremental change like in

     

     

    dvar float+ Gas;

     
    dvar float+ Chloride;  
     
     
    maximize


      40 * Gas+ 50 * Chloride;


    subjectto {


      ctMaxTotal:    


        Gas+ Chloride<= 50;


      ctMaxTotal2:   


        3 * Gas+ 4 * Chloride<= 180;


      ctMaxChloride:


        Chloride<= 40;


       ctVide:


         Gas<=infinity;
    }
     
     main


    {
       thisOplModel.generate();


       cplex.solve();


       writeln(thisOplModel.Gas," ",thisOplModel.Chloride);


       thisOplModel.ctVide.UB=5;


       thisOplModel.ctVide.setCoef(thisOplModel.Gas,-1);


       thisOplModel.ctVide.setCoef(thisOplModel.Chloride,1);


       cplex.solve();


       writeln(thisOplModel.Gas," ",thisOplModel.Chloride);
     
     }


    regards

    Updated on 2013-10-03T14:26:17Z at 2013-10-03T14:26:17Z by AlexFleischer
  • ReneGusmao
    ReneGusmao
    2 Posts

    Re: How to update values of variables in opl main script

    ‏2013-10-03T14:39:57Z  

    Hi,

     

    you can do something like iterative solving in a main but then you regenerate the model at each iteration:

    var prj= thisOplModel

    varmod = prj.modelDefinition

    var dat= prj.dataElements

    var capFlour= dat.capacity["flour"]

    for (var i=1; i<=4; ++i) {

      cplex.clearModel()

       prj= new IloOplModel(mod,cplex)

       prj.addDataSource(dat)

       dat.capacity["flour"] = capFlour++

       prj.generate()

       cplex.solve()

       writeln("Flour capacity ", capFlour,

          ", Objective function:", cplex.getObjValue())

    }

    or you may do full incremental change like in

     

     

    dvar float+ Gas;

     
    dvar float+ Chloride;  
     
     
    maximize


      40 * Gas+ 50 * Chloride;


    subjectto {


      ctMaxTotal:    


        Gas+ Chloride<= 50;


      ctMaxTotal2:   


        3 * Gas+ 4 * Chloride<= 180;


      ctMaxChloride:


        Chloride<= 40;


       ctVide:


         Gas<=infinity;
    }
     
     main


    {
       thisOplModel.generate();


       cplex.solve();


       writeln(thisOplModel.Gas," ",thisOplModel.Chloride);


       thisOplModel.ctVide.UB=5;


       thisOplModel.ctVide.setCoef(thisOplModel.Gas,-1);


       thisOplModel.ctVide.setCoef(thisOplModel.Chloride,1);


       cplex.solve();


       writeln(thisOplModel.Gas," ",thisOplModel.Chloride);
     
     }


    regards

    Very thanks for you response!!

    Like I've tried now, I am manipulating a variable initialized externally. Is there some way to change global variables in main script?

    Updated on 2013-10-03T14:46:10Z at 2013-10-03T14:46:10Z by ReneGusmao
  • AlexFleischer
    AlexFleischer
    1475 Posts

    Re: How to update values of variables in opl main script

    ‏2013-10-03T15:10:23Z  

    Very thanks for you response!!

    Like I've tried now, I am manipulating a variable initialized externally. Is there some way to change global variables in main script?

    Hi,

     

    that's possible.

     

    Take the small model sub.mod

    float maxOfx = ...;
    dvar float x;

    maximize x;
    subject to {
      x<=maxOfx;
    }

     

     

     

    You can use in some other .mod

    main {
      var source = new IloOplModelSource("sub.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
      var opl = new IloOplModel(def,cplex);
     
     
      for(var k=11;k<=20;k++)
      {
      var opl = new IloOplModel(def,cplex);
        
      var data2= new IloOplDataElements();
      data2.maxOfx=k;
      opl.addDataSource(data2);
      opl.generate();

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

     

  • 6A0Q_Hartioun_Arzoumanian
    6 Posts

    Re: How to update values of variables in opl main script

    ‏2013-12-06T20:09:34Z  

    Hi,

     

    that's possible.

     

    Take the small model sub.mod

    float maxOfx = ...;
    dvar float x;

    maximize x;
    subject to {
      x<=maxOfx;
    }

     

     

     

    You can use in some other .mod

    main {
      var source = new IloOplModelSource("sub.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
      var opl = new IloOplModel(def,cplex);
     
     
      for(var k=11;k<=20;k++)
      {
      var opl = new IloOplModel(def,cplex);
        
      var data2= new IloOplDataElements();
      data2.maxOfx=k;
      opl.addDataSource(data2);
      opl.generate();

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

     

    How could I add two vectors in the main script,

    for example I need to create a new two dimensional array and assign it an opl data array and then add another two dimensional array to it.

    How can this be done in OPL?

  • AlexFleischer
    AlexFleischer
    1475 Posts

    Re: How to update values of variables in opl main script

    ‏2013-12-07T08:53:13Z  

    How could I add two vectors in the main script,

    for example I need to create a new two dimensional array and assign it an opl data array and then add another two dimensional array to it.

    How can this be done in OPL?

    We discuss this at

    https://www.ibm.com/developerworks/community/forums/html/topic?id=21dcd01f-0b92-4a8e-8385-12a1aa233ea8