Search code examples
optimizationiterationcplexoplquadratic-programming

CPLEX OPL, Iterative solves problem: the variable has already been set


I'm trying to solve some quadratic integer programming problem with IBM ILOG CPLEX Optimization studio. So I would like to do solve the following quadratic integer programming problem, all the parameters has been defined.

float q = 0;

maximize
    sum(i in RR) sum(j in RR) (Num[i][j]*x[i]*x[j]  - q*Den[i][j]*x[i]*x[j]);

subject to {
    forall (i in R) sum(j in R) x[I*i + j] == 1;
    forall (i in R) sum(j in R) x[I*j + i] == 1;
}

The program manage to find the solution. But I also would like to solve for several values of q. So I setup the following main script loop, updating q for every iteration and solve.

main {
  var count = 1;
  thisOplModel.generate();
  var mdl = thisOplModel;
  while (1) {
    writeln("Running with q = " + mdl.q);

    cplex.solve();
    count = count + 1;
    if (count > 20) break;

    // prepare next iteration
    var def = mdl.modelDefinition;
    var data = mdl.dataElements;
  
    if ( mdl!=thisOplModel ) {
       mdl.end();
    }

    mdl = new IloOplModel(def,cplex);
    data.q = count;
    mdl.addDataSource(data);
    mdl.generate();
  }
}

But when I tried to run this, I got the error: enter image description here I think the idea of what I'm trying to do should be clear, could anyone advice me how to do it properly? Thank you!


Solution

  • in Easy Optimization see Flow control and changes with regenerate

    main
    {
      var source = new IloOplModelSource("sub.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
     
      var opl1 = new IloOplModel(def,cplex);
      var data1=new IloOplDataElements();
      data1.costBus40=500;
      data1.costBus30=400;
      data1.maxBus40=100;
      data1.maxBus30=100;
      data1.nbKids=300;
     
     
      opl1.addDataSource(data1);
        
      opl1.generate();
      cplex.solve();
      opl1.postProcess();
     
    //    //now 350 kids instead of 300
      writeln("now 350 kids instead of 300");
     
     
      data1.nbKids=350;
      var opl2 = new IloOplModel(def,cplex);
      opl2.addDataSource(data1);
        
      opl2.generate();
      cplex.solve();
      opl2.postProcess();
    
    // no more than 4 buses 40 seats
      writeln("no more than 4 buses 40 seats");
      data1.maxBus40=4;
     
      var opl3 = new IloOplModel(def,cplex);
      opl3.addDataSource(data1);
        
      opl3.generate();
      cplex.solve();
      opl3.postProcess();
     
    // change the objective so that cost for 40 seats is 450
    // and remove the limit on the number of buses 40 seats
    
      writeln("change the objective so that cost for 40 seats is 450");
      writeln("and remove the limit on the number of buses 40 seats");    
     
      data1.maxBus40=100;
      data1.costBus40=450;
     
      var opl4 = new IloOplModel(def,cplex);
      opl4.addDataSource(data1);
        
      opl4.generate();
      cplex.solve();
      opl4.postProcess();
    
    
        }