ILOG CPLEX 11.0 Getting Started > Tutorials > Concert Technology Tutorial for C++ Users > Building and Solving a Small LP Model in C++ |
Building and Solving a Small LP Model in C++ |
INDEX PREVIOUS NEXT |
A complete example of building and solving a small LP model can now be presented. This example demonstrates:
Example ilolpex1.cpp
, which is one of the example programs in the standard ILOG CPLEX distribution, is an extension of the example presented in Introducing ILOG CPLEX. It shows three different ways of creating an ILOG Concert Technology LP model, how to solve it using IloCplex
, and how to access the solution. Here is the problem that the example optimizes:
Maximize |
x1 + 2x2 + 3x3 |
subject to |
-x1 + x2 + x3 20
x1 - 3x2 + x3 30 |
with these bounds |
0 x1 40
0 x2 +
0 x3 + |
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 cplex.getStatus
. It is also defined for IloNumArray
, the ILOG Concert Technology class for an array of numerical values, as returned by the calls to cplex.getValues
, cplex.getDuals
, cplex.getSlacks
, and cplex.getReducedCosts
. In general, the output operator is defined for any ILOG 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.
Copyright © 1987-2007 ILOG S.A. All rights reserved. Legal terms. | PREVIOUS NEXT |