【问题标题】:freeze solution value for next iteration冻结下一次迭代的解决方案值
【发布时间】:2022-11-18 17:35:12
【问题描述】:

我想从 iter=1 为 iter=2 等保存一些解决方案。 主要块的一部分放置在这里。

var x = opl.x.solutionValue;
  for (var k in data2.M){
    for (var r in data2.Links){
      if (x[k][r.N]==1){
       x[k][r.N]= opl.x[k][r.N].solutionValue;
       var data3 = new IloOplDataElements();
       var xnew =opl.x[k][r.N].solutionValue;
       xnew = x[k][r.N];
       data3.xnew = x[k][r.N];
       opl.addDataSource(data3);
       writeln("x[",k,"]","[",r.N,"]"," = ",x [k][r.N]);
       writeln("xnew[",k,"]","[",r.N,"]"," = ",data3.xnew [k][r.N]);
       }
       
       }}

当我运行这个模型时;没有任何错误,xnew未在脚本日志中更新和打印xnew 未定义. 每次迭代我都有相同的 .mod 文件,我在 .mod 文件中定义 xnew 如下:

{float} xnew [s][N]=[];

你能帮我解决这个问题吗?

非常感谢您的 cmets。

【问题讨论】:

    标签: variables cplex opl


    【解决方案1】:

    让我从示例https://github.com/AlexFleischerParis/howtowithoplchange/blob/master/change2darray.mod 开始,展示如何冻结下一次迭代。

    if (k!=11)
          {opl.x.LB=output;
          opl.x.UB=output;
          }  
    

    正在做冻结

    int a[1..2][1..2];
    
        main {
          var source = new IloOplModelSource("sub2d.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         var output=0;
         
          for(var k=11;k<=15;k++)
          {
          var opl = new IloOplModel(def,cplex);
         
         
            
          var data2= new IloOplDataElements();
         
          data2.y=thisOplModel.a;
          data2.y[1][1]=k;
          opl.addDataSource(data2);
         
          opl.generate();
          // if k!=11 then freeze x to the output value from last time
          if (k!=11)
          {opl.x.LB=output;
          opl.x.UB=output;
          }      
    
          if (cplex.solve()) {
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
          opl.postProcess();
          output=opl.x.solutionValue;
          
          data2.end();
         opl.end();
         
         
        }  
         
        }
    

    sub2d.mod 在哪里

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

    如果您将 sub2d.mod 更改为

     int y[1..2][1..2]=...;
    
        execute
        {
        writeln("y=",y);
        }
    
        dvar boolean x[1..2][1..2];
    
        maximize sum(i in 1..2,j in 1..2)x[i][j];
        subject to {
          sum(i in 1..2,j in 1..2)  x[i][j]<=(sum(i in 1..2, j  in 1..2) y[i][j]) ;
        }  
    

    然后你可以写

    int a[1..2][1..2];
    
        main {
          var source = new IloOplModelSource("sub2d.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         var output=0;
         
          for(var k=11;k<=15;k++)
          {
          var opl = new IloOplModel(def,cplex);
         
         
            
          var data2= new IloOplDataElements();
         
          data2.y=thisOplModel.a;
          data2.y[1][1]=k;
          opl.addDataSource(data2);
         
          opl.generate();
          // if k!=11 then freeze x to the output value from last time
          if (k!=11)
          {opl.x[1][1].LB=output;
          opl.x[1][1].UB=output;
          }      
    
          if (cplex.solve()) {
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
          opl.postProcess();
          output=opl.x[1][1].solutionValue;
          
          writeln("x[1][1]=",opl.x[1][1].solutionValue);
          writeln("x[2][1]=",opl.x[2][1].solutionValue);
          
          data2.end();
         opl.end();
         
         
        }  
         
        }
    

    【讨论】:

    • 感谢您的宝贵意见;但是我收到以下错误。脚本运行时错误:无法向该值添加属性,“[[0 1 1 0 0 0 0]]”。
    • 你能给我一个用这个变量保持下一次迭代的解决方案值的例子吗dvar 布尔值 x[i][j]?再次感谢您所做的一切。我真的需要你的帮助
    • 我更新了我的答案购买尝试不添加这样的新问题。最好开个新问题
    猜你喜欢
    • 2013-07-22
    • 2020-10-25
    • 2016-03-19
    • 2017-12-22
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多