Modifying the CPLEX matrix incrementally
How to change the bounds of a CPLEX® constraint or variable. How to change the coefficient of a variable.
In this tutorial, you have learned how to solve a sequence of modified OPL models by changing data in OPL. This is a useful technique, however you need to generate the CPLEX model again after each modification. Sometimes, when the number of iterations is high and generating the new iteration takes a long time compared to solving it, you may prefer to have a direct interaction with the generated optimization model and be able to work incrementally on the result of the previous iteration. You can do so by taking advantage of the API of IBM® ILOG Script extensions for OPL.
The mulprod production planning
example illustrates how to change the bounds of a CPLEX constraint.
You can also:
change the bounds of a variable
change the coefficient of a variable in a CPLEX constraint or in the CPLEX objective
Changing bounds
Of a CPLEX constraint
To
change the bound of a constraint, you can directly change the LB and UB properties on
the IloConstraint class.
It is important to understand what happens when these methods are used: the optimization model is directly modified but the OPL model is not. Therefore, the solution given by CPLEX corresponds to the modified optimization model, but not to the original OPL model any more. On the other hand, the advantage is that the CPLEX matrix is directly modified (not rebuilt from scratch) and any new search can take advantage of the previous ones.
You can see in the mulprod_change_main.mod file how the example can be modified to change the optimization model directly. In particular, the important line is the one that changes the bound of a constraint:
Changing the bound of a CPLEX constraint
for(var t in thisOplModel.Periods)
thisOplModel.ctCapacity["flour"][t].UB = capFlour;
}
Of a variable
To change the bound of a variable, you can directly change
the lower-bound (LB) and upper-bound (UB) properties on the IloNumVar class.
This does not change the bound of the variable in the OPL model but only in the CPLEX matrix.
This change is taken into account incrementally by the CPLEX engine.
Changing the coefficient of a variable
You can use the method IloConstraint.setCoef to change the coefficient of a variable in the invoking constraint and the method IloObjective.setCoef to change the coefficient of a variable in the invoking objective. The coefficient is changed only in the CPLEX matrix and in the Concert extracted model. The OPL model is not affected. On the other hand, the change is taken into account incrementally by the CPLEX engine.
The method IloCplex.setCoef is available for all CPLEX linear constraints. It changes the engine representation directly without going through Concert.