Construct environment
The class IloEnv constructs a CPLEX environment.
An environment, that is, an instance of IloEnv is typically the first object created in any Concert Technology application.
You construct an IloEnv object by declaring a variable of type IloEnv . For example, to create an environment named env , you do this:
IloEnv env;
Note: The environment object created in a Concert Technology application is different from the environment created in the CPLEX C library by calling the routine CPXopenCPLEX.
When terminating the Concert Technology application, the implementation object must be destroyed as well. This must be done explicitly by the user by calling
env.end();
Creating a model: IloModel
IloModel model(env);
You create objects of these classes for each variable, constraint, and objective function of your optimization problem. Then you add the objects to the model by calling:
model.add(object);
add variables
IloNumVar x1(env, 0.0, 40.0, ILOFLOAT);
This definition creates the modeling variable x1 with lower bound 0.0, upper bound 40.0 and type ILOFLOAT , which indicates the variable is continuous. Other possible variable types include ILOINT for integer variables and ILOBOOL for Boolean variables.
add object
IloObjective obj = IloMinimize(env, x1 + 2*x2 + 3*x3);
or
model.add(IloMinimize(env, x1 + 2*x2 + 3*x3));
add constraints
model.add(-x1 + x2 + x3 <= 20);
The part -x1 + x2 + x3 <= 20 creates an object of class IloRange that is immediately added to the model by passing it to the method IloModel::add . Again, if a reference to the IloRange object is needed later, an IloRange handle object must be stored for it
Solving the model: IloCplex
After the optimization problem has been created in an IloModel object, it is time to create the IloCplex object for solving the problem by creating an instance of the class IloCplex. For example, to create an object named cplex, write the following line:
IloCplex cplex(env);
The CPLEX object can then be used to extract the model to be solved. One way to extract the model is to call cplex.extract(model). However, experienced Concert users practice a shortcut that performs the construction of the cplex object and the extraction of the model in one line:
IloCplex cplex(model);
After this line, the object cplex is ready to solve the optimization problem defined by model. To solve the model, call:
cplex.solve ();
This method returns an IloBool value, where IloTrue indicates that cplex successfully found a feasible (yet not necessarily optimal) solution, and IloFalse specifies that no solution was found. More precise information about the outcome of the last call to the method IloCplex::solve can be obtained by calling:
cplex.getStatus ();
The returned value tells you what CPLEX found out about the model: whether it found the optimal solution or only a feasible solution, whether it proved the model to be unbounded or infeasible, or whether nothing at all has been proved at this point.
Query results
IloNum IloCplex::getValue (IloNumVar var) const;
void IloCplex::getValues (IloNumArray val,
const IloNumVarArray var) const;
For example:
IloNum val1 = cplex.getValue(x1);
to get the variable value
and
IloNum objval = cplex.getObjValue ();
to get the object value