Hi there,
I have this OPL model:
range I = 1..3;
dvar boolean x[I];
maximize sum(i in I) x[i];
subject to {
c1: x[1] + x[2] + x[3] <= 3;
c2: sum(i in I) x[i] <= 3;
}
main {
thisOplModel.generate();
cplex.solve();
writeln(thisOplModel.x[1], " " , thisOplModel.x[2], " " ,thisOplModel.x[3]);
// Changing c1 works without a problem
thisOplModel.c1.UB = 2;
thisOplModel.c1.setCoef(thisOplModel.x[1], 2);
thisOplModel.c1.setCoef(thisOplModel.x[2], 2);
thisOplModel.c1.setCoef(thisOplModel.x[3], 2);
// Changing c2 throws "You can not change the coefficient of an aggregate expression"
thisOplModel.c2.UB = 3;
for (var i in thisOplModel.I) {
thisOplModel.c2.setCoef(thisOplModel.x[i], 3); // this line throws
}
cplex.solve();
writeln(thisOplModel.x[1], " " , thisOplModel.x[2], " " ,thisOplModel.x[3]);
}
As you can see, I am trying to dynamically change coefficients and right-hand sides of constraints c1 and c2. This works well for c1 (seemingly because I just explicitly list all terms of the sum). For c2 I get a "You can not change the coefficient of an aggregate expression" error. So my question is: how can I avoid this error and change the coefficients of c2 in the script? I looked at the reference documentation of setCoef() of class IloConstraint but that did not even mention any limitation. According to this section in the manual, "The method IloCplex.setCoef is available for all CPLEX linear constraints." Clearly c2 is a linear constraint, so I am wondering what is the right way to use setCoef() here?
Thank you for any help.
#DecisionOptimization#OPLusingCPLEXOptimizer