CPLEX OPL,迭代求解问题:变量已设置

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

我正在尝试使用 IBM ILOG CPLEX Optimization Studio 解决一些二次整数规划问题。所以我想解决下面的二次整数规划问题,所有的参数都已经定义好了。

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;
}

程序设法找到了解决方案。但我也想解决 q 的几个值。所以我设置了以下主脚本循环,为每次迭代更新 q 并求解。

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();
  }
}

但是当我尝试 运行 这个时,我得到了错误: 我认为我正在尝试做的事情的想法应该很清楚,有人可以建议我如何正确地做吗?谢谢!

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();


    }