Create the model
Java methods create a model in a Java application of CPLEX.
The IloCplex object provides
the functionality to create an optimization model that can be solved
with IloCplex. The class IloCplex implements
the Concert Technology interface IloModeler and its extensions IloMPModeler and IloCplexModeler. These interfaces
define the constructors for modeling objects of the following types,
which can be used with IloCplex :
| Modeling classes | Description |
|---|---|
| IloNumVar | modeling variables |
| IloRange | ranged constraints of the type lb <= expr <= ub |
| IloObjective | optimization objective |
| IloNumExpr | expression using variables |
Modeling variables are represented by objects implementing the IloNumVar interface
defined by Concert Technology. Here is how to create three continuous
variables, all with bounds 0 and 100 :
IloNumVar[] x = cplex.numVarArray(3, 0.0, 100.0);
There is a wealth of other methods for creating arrays or individual modeling variables. The documentation for IloModeler, IloCplexModeler, and IloMPModeler gives you the complete list.
Modeling variables build expressions, of type IloNumExpr, for use in constraints or the objective function of an optimization model. For example, the expression:
x[0] + 2*x[1] + 3*x[2]
can be created like this:
IloNumExpr expr = cplex.sum(x[0],
cplex.prod(2.0, x[1]),
cplex.prod(3.0, x[2]));
Another way of creating an object representing the same expression is to use an expression of IloLinearNumExpr. Here is how:
IloLinearNumExpr expr = cplex.linearNumExpr();
expr.addTerm(1.0, x[0]);
expr.addTerm(2.0, x[1]);
expr.addTerm(3.0, x[2]);
The advantage of using IloLinearNumExpr over the first way is that you can more easily build up your linear expression in a loop, which is what is typically needed in more complex applications. The interface IloLinearNumExpr is an extension of IloNumExpr and thus can be used anywhere an expression can be used.
As mentioned before, expressions can be used to create constraints or an objective function for a model. Here is how to create a minimization objective for that expression:
In addition to your creating an objective, you must also instruct IloCplex to use that
objective in the model it solves. To do so, add the objective
to IloCplex like this:
cplex.add(obj);
Every modeling object that is to be used in a model must be added
to the IloCplex object. The
variables need not be explicitly added as they are treated implicitly
when used in the expression of the objective. More generally, every
modeling object that is referenced by another modeling object which
itself has been added to IloCplex, is implicitly
added to IloCplex as well.
There is a shortcut notation for creating and adding the objective: cplex.addMinimize(expr);This
shortcut uses the method addMinimize
Since the objective is not otherwise accessed, it does not need
to be stored in the variable obj .
Adding constraints to the model is just as easy. For example, the constraint
-x[0] + x[1] + x[2] <= 20.0
can be added by calling:
cplex.addLe(cplex.sum(cplex.negative(x[0]), x[1], x[2]), 20);
Again, many methods are provided for adding other constraint types, including equality constraints, greater than or equal to constraints, and ranged constraints. Internally, they are all represented as IloRange objects with appropriate choices of bounds, which is why all these methods return IloRange objects. Also, note that the previous expressions could have been created in many different ways, including the use of IloLinearNumExpr.