Overview
The sample illustrates three alternative approaches: modeling by rows, columns, or nonzero elements.
This sample offers a complete example of building and solving a small LP model. It demonstrates:
This example ilolpex1.cpp is
one of the sample applications in the standard CPLEX distribution.
It is an extension of the example presented in Introducing CPLEX. It shows three different
ways of creating a Concert Technology LP model, as well as how to
solve it using IloCplex,
and how to access the solution. Here is the problem that the example
optimizes:
| Maximize | x 1 + 2x 2 + 3x 3 |
| subject to |
–x
1
+ x
2 + x
3
x
1
– 3x
2 + x
3
|
| with these bounds |
0
0
0
|
The first operation is to create the environment object env , and the last operation is to destroy it by
calling env.end . The rest of the code is
enclosed in a try/catch clause to gracefully
handle any errors that may occur.
First the example creates the model object and, after
checking the correctness of command line arguments, it creates empty
arrays for storing the variables and range constraints of the optimization
model. Then, depending on the command line argument, the example calls
one of the functions populatebyrow, populatebycolumn, or populatebynonzero,
to fill the model object with a representation of the optimization
problem. These functions place the variable and range objects in the
arrays var and con which
are passed to them as arguments.
After the model has been populated, the IloCplex algorithm
object cplex is created and the model is
extracted to it. The following call of the method solve invokes
the optimizer. If it fails to generate a solution, an error message
is issued to the error stream of the environment, cplex.error() ,
and the integer -1 is thrown as an exception.
IloCplex
provides the output streams out for general
logging, warning for warning messages, and error for error messages. They are preconfigured
to cout , cerr ,
and cerr respectively. Thus by default you
will see logging output on the screen when invoking the method solve. This
can be turned off by calling cplex.setOut(env.getNullStream()) ,
that is, by redirecting the out stream of
the IloCplex
object cplex to the null stream of the environment.
If a solution is found, solution information is output
through the channel, env.out which is initialized
to cout by default. The output operator << is defined for type IloAlgorithm::Status as
returned by the call to getStatus .
It is also defined for IloNumArray , the
Concert Technology class for an array of numerical values, as returned
by the calls to getValues , getDuals , getSlacks , and getReducedCosts. In
general, the output operator is defined for any Concert Technology
array of elements if the output operator is defined for the elements.
The functions named populateby *
are purely about modeling and are completely decoupled from the algorithm IloCplex.
In fact, they don’t use the cplex object,
which is created only after executing one of these functions.