Getting Started with CPLEX
本文最后更新于:2020年9月21日 下午
内容来源:https://www.ibm.com/support/knowledgecenter/zh/SSSA5P
Chapter 2. Solving an LP with CPLEX
1. Problem statement
Maximize
$x_1+x_2+3x_3$
subject to
$-x_1+x_2+x_3 \leq 20$
$x_1+3x_2+x_3 \leq 30$
with these bounds
$0 \leq x_1 \leq 40$
$0 \leq x \leq infinity$
$0 \leq x_3 \leq infinity$
2. Using Concert Technology in c++
1 |
|
Chapter 4. C++ tutorial
This brief tutorial introduces the modeling and solution classes provided by Concert Technology and CPLEX.
More information about the algorithm class IloCplex and its nested classes can be found in the
CPLEX User’s Manual
andCPLEX C++ API Reference Manual
.
1. The design of CPLEX in Concert Technology C++ applications
- These objects can be divided into two categories:
Modeling objects
: define the optimization problemIloCplex objects
: solve the problems that have been created with the modeling objects
2. The anatomy of a Concert Technology C++ application
Constructing the environment: IloEnv
The class IloEnv constructs a CPLEX environment.
the first object created in any Concert Technology application
- it provides optimized memory management for objects of Concert Technology classes
IloEnv
is a handle class- the variable
env
is a pointer to an implementation object
1 |
|
- When terminating the Concert Technology application, the implementation object must be destroyed as well
1 |
|
Creating a model: IloModel
- it is the implementation objects that contain the data that specifies the optimization model
- Modeling objects are also known as extractables
IloExtractable
is the base class of all classes of extractables or modeling objects.- The most fundamental extractable class is
IloModel
- Objects of this class are used to define a complete optimization model that can later be extracted to an object
1 |
|
Modeling classes
- representing modeling variables;
- defining constraints of the form l <= expr <= u, where expr is a linear expression; and representing an objective function
1 |
|
Modeling variables are constructed as objects of class IloNumVar
, by defining variables of type IloNumVar .
1 |
|
This definition creates the modeling variable x1 with lower bound 0.0, upper bound 40.0 and type ILOFLOAT
ILOFLOAT: the variable is continuous
ILOINT: integer variables
ILOBOOL: Boolean variables
After all the modeling variables have been constructed, they can be used to build expressions, which in turn are used to define objects of class IloObjective
and IloRange
.
1 |
|
After an objective extractable is created, it must be added to the model.
If this is all that the variable obj is needed for, it can be written more compactly, like this:
1 |
|
If in contrast, the objective function is needed later, for example, to change it and re-optimize the model when doing scenario analysis, the variable obj must be created in order to refer to the objective function.
Creating constraints and adding them to the model
1 |
|
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
.
The importance of the class IloExpr
becomes clear when expressions can no longer be fully spelled out in the source code but need instead to be built up in a loop.
Solving the model: IloCplex
experienced Concert users practice a shortcut that performs the construction of the cplex object and the extraction of the model in one line:
1 |
|
To solve the mode
1 |
|
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:
1 |
|
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
- whether nothing at all has been proved at this point
Querying results
The following methods can be used to query the solution value for a variable or a set of variables:
1 |
|
the objective function value of the solution can be accessed using
1 |
|
Handling errors
The first kind covers simple programming errors
The second kind of error is more complex and cannot generally be avoided by correct programming.
Concert Technology provides a hierarchy of exception classes that all derive from the common base class IloException
1 |
|
3. Building and solving a small LP model in C++
The sample illustrates three alternative approaches: modeling by rows, columns, or nonzero elements.
1 |
|
env.out
which is initialized to cout by default
IloNumArray
, the Concert Technology class for an array of numerical values
Reference
- Getting Started with CPLEX
- CPLEX User’s Manual
- CPLEX C++ API Reference Manual
- CPLEX c++ example
- Examples in C++ using Concert Technology
Q&A
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!