I want to save some solution from iter=1 for iter=2 and so on. A part of the main block is placed here.
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]);
}
}}
when I run this model; Without any error, the xnew is not update and printed in scripting log xnew undefined. I have the same .mod file for each iteration and I defined xnew in the .mod file as follows:
{float} xnew [s][N]= [];
could you help me to solve this problem?
A really appreciate your comments.
CodePudding user response:
Let me start from the example https://github.com/AlexFleischerParis/howtowithoplchange/blob/master/change2darray.mod to show how to freeze for next iteration.
if (k!=11)
{opl.x.LB=output;
opl.x.UB=output;
}
is doing the freeze
in
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();
}
}
where sub2d.mod is
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];
}
and if you change sub2d.mod to
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]) ;
}
then you can write
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();
}
}