Create the Model

The IloCplex object provides the functionality to create an optimization model that can be solved with IloCplex. The interface functions for doing so are defined by the Concert Technology interface IloModeler and its extension IloMPModeler. These interfaces define the constructor functions for modeling objects of the following types, which can be used with IloCplex:

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 functions for creating arrays or individual modeling variables. The documentation for IloModeler and IloMPModeler will give you the complete list.

Modeling variables are typically used to 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 IloLinearNumExpr expression. 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. 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 the above expression:

    IloObjective obj = cplex.minimize(expr);

In addition to creating an objective, IloCplex must be instructed to use it in the model it solves. This is done by adding the objective to IloCplex via:

    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 to IloCplex:

    cplex.addMinimize(expr);

Since we don't need to access the objective otherwise, we can avoid storing it in 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 expressions above could have been created in many different ways, including the use of IloLinearNumExpr.


Previous Page: The Anatomy of a Concert Technology Application  Return to Top Next Page: Solve the Model