Using IloModeler

Describes the class IloModeler in the Java API.

IloModeler defines an interface for building optimization models. This interface defines methods for constructing variable, constraint, and objective function objects.

Variables in a model

A modeling variable in Concert Technology is represented by an object of type IloNumVar or one of its extensions. You can choose from a variety of methods defined in IloModeler and IloMPModeler to create one or multiple modeling variable objects. An example of the method is:


IloNumVar x = cplex.numVar(lb, ub, IloNumVarType.Float, "xname");

This constructor method allows you to set all the attributes of a variable: its lower and upper bounds, its type, and its name. Names are optional in the sense that null strings are considered to be valid as well.

The other constructor methods for variables are provided mainly for ease of use. For example, because names are not frequently assigned to variables, all variable constructors come in pairs, where one variant requires a name string as the last parameter and the other one does not (defaulting to a null string).

Integer variables can be created by the intVar methods, and do not require the type IloNumVarType.Int to be passed, as this is implied by the method name. The bound parameters are also specified more consistently as integers. These methods return objects of type IloIntVar, an extension of interface IloNumVar that allows you to query and set bounds consistently using integers, rather than doubles as used for IloNumVar .

Frequently, integer variables with 0/1 bounds are used as decision variables. To help create such variables, the boolVar methods are provided. In the Boolean type, 0 (zero) and 1 (one) are implied, so these methods do not need to accept any bound values.

For all these constructive methods, there are also equivalent methods for creating a complete array of modeling variables at one time. These methods are called numVarArray, intVarArray, and boolVarArray .

Expressions

Modeling variables are typically used in expressions that define constraints or objective functions. Expressions are represented by objects of type IloNumExpr. They are built using methods such as sum, prod, diff, negative, and square. For example, the expression


x1 + 2*x2

where x1 and x2 are IloNumVar objects, is constructed by this call:


IloNumExpr expr = cplex.sum(x1, cplex.prod(2.0, x2));

It follows that a single variable is a special case of an expression, since IloNumVar is an extension of IloNumExpr.

The special case of linear expressions is represented by objects of type IloLinearNumExpr. Such expressions can be edited, especially convenient when linear expressions are being built in a loop, like this:


IloLinearNumExpr lin = cplex.linearNumExpr();
   for (int i = 0; i < num; ++i)
     lin.addTerm(value[i], variable[i]);

The special case of the scalar product of an array of values with an array of variables is directly supported through the method scalProd. Thus that loop can be rewritten as:


IloLinearNumExpr lin = cplex.scalProd(value, variable);

It is recommended that you build expressions in terms of data that is either integer or double-precision (64-bit) floating-point. Single-precision (32 bit) floating-point data should be avoided as it can result in unnecessarily ill-conditioned problems. For more information, refer to Numeric difficulties.

Ranged constraints

Ranged constraints are constraints of the form: lb expression ub and are represented in Concert Technology by objects of type IloRange. The most general constructor is:


IloRange rng = cplex.range(lb, expr, ub, name);

where lb and ub are double values, expr is of type IloNumExpr, and name is a string.

By choosing the range bounds appropriately, ranged constraints can be used to model any of the more commonly found constraints of the form:


expr  relation  rhs

where relation is the relation =, , or . The following table shows how to choose lb and ub for modeling these relations:

relation lb ub method
= rhs rhs eq
-Double.MAX_VALUE rhs le
rhs Double.MAX_VALUE ge

The last column contains the method provided with IloModeler to use directly to create the appropriate ranged constraint, when you specify the expression and righthand side (RHS). For example, the constraint expr 1.0 is created by the call:


IloRange le = cplex.le(expr, 1.0);

Again, all constructors for ranged constraints come in pairs, one constructor with and one without an argument for the name.

The objective function

The objective function in Concert Technology is represented byan object of type IloObjective. Such objects are defined by an optimization sense, an expression, and an optional name. The objective expression is represented by an IloNumExpr. The objective sense is represented by an object of class IloObjectiveSense and can take two values, IloObjectiveSense.Maximize or IloObjectiveSense.Minimize. The most general constructor for an objective function object is:

where sense is of type IloObjectiveSense, expr is of type IloNumExpr, and name is a string.

For convenience, the methods maximize and minimize are provided to create a maximization or minimization objective respectively, without using an IloObjectiveSense parameter. Names for objective function objects are optional, so all constructor methods come in pairs, one with and one without the name parameter.