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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <ilcplex/ilocplex.h>
#include <stdio.h>
using namespace std;

ILOSTLBEGIN
int
main(void*) {
IloEnv env;
try {
IloModel model(env);
IloNumVarArray vars(env);
vars.add(IloNumVar(env, 0.0, 40.0)); // 0 <= x1 <= 40
vars.add(IloNumVar(env)); // 0 <= x2
vars.add(IloNumVar(env)); // 0 <= x3
model.add(IloMaximize(env, vars[0] + 2 * vars[1] + 3 * vars[2])); //maximize x1 + 2 x2 + 3 x3
model.add(-vars[0] + vars[1] + vars[2] <= 20);//subject to -x1 + x2 + x3 <= 20
model.add(vars[0] - 3 * vars[1] + vars[2] <= 30);//x1 - 3 x2 + x3 <=30
IloCplex cplex(model);
if (!cplex.solve()) {
env.error() << "Failed to optimize LP." << endl;
throw(-1);
}
IloNumArray vals(env);
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
cplex.getValues(vals, vars);
env.out() << "Values = " << vals << endl;
}
catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; }
catch (...) { cerr << "Unknown exception caught" << endl; }
env.end();
system("pause");
return 0;
}

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 and CPLEX C++ API Reference Manual.

1. The design of CPLEX in Concert Technology C++ applications

  1. These objects can be divided into two categories:
  • Modeling objects: define the optimization problem
  • IloCplex 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
IloEnv env;
  • When terminating the Concert Technology application, the implementation object must be destroyed as well
1
env.end();

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
IloModel model(env);

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
2
//objects can be variable, constraint, and objective function of your optimization problem. 
model.add(object);

Modeling variables are constructed as objects of class IloNumVar, by defining variables of type IloNumVar .

1
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

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
IloObjective obj = IloMinimize(env, x1 + 2*x2 + 3*x3);

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
model.add(IloMinimize(env, x1 + 2*x2 + 3*x3));

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
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 .

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
IloCplex cplex(model);

To solve the mode

1
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:

1
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
  • 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
IloNum val1 = cplex.getValue(x1);

the objective function value of the solution can be accessed using

1
IloNum objval = cpelx.getObjValue();

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
2
3
4
5
6
7
IloEnv env;
try {
// ...
}
catch (IloException& e) { cerr << "Concert exception caught: " << e << endl; }
catch (...) { cerr << "Unknown exception caught" << endl; }
env.end();

3. Building and solving a small LP model in C++

The sample illustrates three alternative approaches: modeling by rows, columns, or nonzero elements.

1
\CPLEX_Studio1210\cplex\examples\src\cpp\ilolpex1.cpp

env.out which is initialized to cout by default

IloNumArray, the Concert Technology class for an array of numerical values

Reference

Q&A


本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!