我想從 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 undefined中。每次迭代我都有相同的 .mod 檔案,我在 .mod 檔案中定義 xnew 如下:
{float} xnew [s][N]=[];
你能幫我解決這個問題嗎?
非常感謝您的意見。
uj5u.com熱心網友回復:
讓我從示例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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/536633.html
標籤:变量复杂的opl
上一篇:C中沒有變數的函式的列印值
下一篇:谷歌圖表時間表日期可變
