Modifying the model
Describes modification of a model in the Java API.
An important feature of CPLEX is that you can modify a previously created model to consider different scenarios. Furthermore, depending on the optimization model and algorithm used, CPLEX will save as much information from a previous solution as possible when optimizing a modified model.
The most important modification method is IloModel.add,
for adding modeling objects to the active model. Conversely, you can
use IloModel.remove to remove a modeling
object from a model, if you have previously added that object.
When you add a modeling object such as a ranged constraint
to a model, all the variables used by that modeling object implicitly
become part of the model as well. However, when you remove a modeling
object, no variables are implicitly removed from the model. Instead,
variables can only be explicitly removed from a model by calling IloMPModeler.delete.
(The interface IloMPModeler derives from
the class IloModel, among others. It is
implemented by the class IloCplex.) This
call will cause the specified variables to be deleted from the model,
and thus from all modeling objects in the model that are using these
variables. In other words, deleting variables from a model may implicitly
modify other modeling objects in that model.
The API of specific modeling objects may provide modification
methods. For example, you can change variable bounds by using the
methods IloNumVar.setLB and IloNumVar.setUB.
Similarly, you can change the bounds of ranged constraints by using IloRange.setLB and IloRange.setUB.
Because not all the optimizers that implement the IloModeler interface
support the ability to modify a model, modification methods are implemented
in IloMPModeler. These methods are for
manipulating the linear expressions in ranged constraints and objective
functions used with IloCplex. The methods IloMPModeler.setLinearCoef, IloMPModeler.setLinearCoefs,
and IloMPModeler.addToExpr apply in this
situation.
The type of a variable cannot be changed. However, it
can be overwritten for a particular model by adding an IloConversion object,
which allows you to specify new types for variables within that model.
When CPLEX finds a conversion object in the active model, it uses
the variable types specified in the conversion object instead of the
original type specified for the optimization. For example, in a model
containing the following lines, CPLEX will only generate solutions
where variable x is an integer (within
tolerances), yet the type returned by x.getType will
remain IloNumVarType.Float.
IloNumVar x = cplex.numVar(0.0, 1.0);
cplex.add(cplex.conversion(x, IloNumVarType.Int));
A variable can be used only in at most one conversion
object, or the model will no longer be unambiguously defined. This
convention does not imply that the type of a variable can be changed
only once and never again after that. Instead, you can remove the
conversion object and add a new one to implement consecutive variable
type changes. To remove the conversion object, use the method IloModel.remove .