Search code examples
cplexopl

change data and freeze solution in loop


Thank you for your reply, Alex.

but Let me ask my question more precisely.. I have this data string

{string} N = {"a", "b", "c", "d", "e", "f", "g"};

and I have to get one of the arrays from N in each iteration in cplex loop, for example, in iter 1 I have to get N = {"a"} and for the next step I have to get N = {"a", "b"} and I must continue in the same way; The rest of the data is constant in all iteration. And in each iteration, I have to save the solution of this iteration for the next step.

my data:

{string} N = {"a", "b", "c", "d", "e", "f", "g"};

{string} M = {"aa","bb","cc","dd","ee"};

range nods = 0..10;

tuple edge{

int o;

int d;

float C;

float D;

};

{edge} edges = ...;

int Tr = ...;

int cd = ...; . . .

my variables:

dvar boolean x[M][N];

dvar boolean y[M,edges];

dvar boolean w[M,N,edges];

dvar float+ T[M][nods];

I apologize for the frequent and long questions.

please help me :( thank


Solution

  • For modifying a set and solving again the same model you can see

    https://github.com/AlexFleischerParis/howtowithoplchange/blob/master/changeset.mod

    {int} s={};
    
        main {
          var source = new IloOplModelSource("subset.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);
          opl.addDataSource(data2);
          opl.generate();
    
          if (cplex.solve()) {
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
        data2.end();
         opl.end();
         
         
        }  
         
        }
    

    with subset.mod

    {int} y=...;
    
    execute
    {
    writeln("y=",y);
    }
    
    dvar float x;
    
    maximize x;
    subject to {
      x<=sum(i in y)i;
    }
    
    execute
    {
    writeln("x=",x);
    }