Docsity

Tutorial on CPLEX Linear Programming, Study notes of Linear Programming

Partial preview of the text

Download Tutorial on CPLEX Linear Programming and more Study notes Linear Programming in PDF only on Docsity!

Tutorial on CPLEXLinear Programming

Combinatorial Problem Solving (CPS)

Enric Rodr´

ıguez-Carbonell

April 28, 2021

LP with CPLEX

■

Among other things, CPLEX allows one to deal with: ◆

Real linear progs(all vars are in

R

min

Tc x A

x 1

b

1

A

x 2

b

2

A

x 3

b

3

x

R

n

◆

Mixed integer linear progs(some vars are in

Z

min

Tc x A

x 1

b

1

A

x 2

b

2

A

x 3

b

3

i^

I

x

i^

Z

i^

I

x

i^

R

Concert Technology

■

Two kinds of objects: ◆

Modeling objects

for defining the optimization problem

(constraints, objective function, etc.)They are grouped into an

IloModel

object representing the complete

optimization problem (recall: here,

model = problem

◆

Solving objects

for solving problems represented by modeling objects.

An

IloCplex

object reads a model, extracts its data, solves the

problem and answers queries on solution.

Creating the Environment:

IloEnv

■

The class

IloEnv

constructs a CPLEX environment.

■

The environment is the first object created in an application.

■

To create an environment named

env

, you do this:

IloEnv

env

■

The environment object needs to be available to the constructor of allother Concert Technology classes

■

IloEnv

is a handle class: variable

env

is a pointer to an implementation

object, which is created at the same time

■

Before terminating destroy the implementation object with^ env

end

for just

ONE

of its

IloEnv

handles

Creating a Model:

IloModel

■

After an

IloModel

object has been constructed, it can be populated with

objects of classes: ◆

IloNumVar

, representing modeling variables;

◆

IloRange

, which define constraints of the form

l

E

u

where

E

is a linear expression;

◆

IloObjective

, representing an objective function.

■

Any object

obj

can be added to the model by calling:

model

add

obj

■

No need to explicitly add the variable objects to a model, as they areimplicitly added when they are used in range constraints (instances of IloRange

) or in the objective.

■

At most one objective function can added to a model.

Creating a Model:

IloModel

■

Modeling variables are constructed as objects of class

IloNumVar

, e.g.:

IloNumVar

x^

(^

env

ILOFLOAT

This definition creates the modeling variable

x

with lower bound

upper bound

and type

ILOFLOAT

■

Variable types: ◆

ILOFLOAT

: continuous variable

◆

ILOINT

: integer variable

◆

ILOBOOL

: Boolean variable

■

Note that even for variables of type

ILOBOOL

the lower bound (0) and the upper bound (1) have to be specified.

■

One may have arrays of variables:

IloNumVarArray

Creating a Model:

IloModel

■

Actually in^ model

add

x

+^

y

z^

an object of class

IloExpr

(a linear expression) is also implicitly created

before the object of class

IloRange

is created

■

Sometimes it is convenient to create objects of class

IloExpr

explicitly.

E.g., when expressions cannot be spelled out in source code but have to bebuilt up dynamically. Operators like

provide an efficient way to do this.

■

Example:

IloEnv

env

IloModel

model

env

IloNumVarArray

V

(^

env

n^

,^

ILOBOOL

IloExpr

expr

env

for

(^

int

i^

=^

i^

n

i

expr

V^

[

i^

];

model

add

expr

expr

end

Creating a Model:

IloModel

IloEnv

env

IloModel

model

env

IloNumVarArray

V

env

n

ILOBOOL

IloExpr

expr

env

for

int

i^

i

n

i

)^

expr

V

[^

i^

];

model

add

expr

expr

end

Notice

this!

■

IloExpr

objects are handles.

So

if an

IloExpr

object has been created explicitly

then method

end()

must be called when it is no longer needed.

■

Example of explicitly defining an

IloRange

object

IloRange

c

env

-^

IloInfinity

x

x

+^

x

■

One may have arrays of constraints:

IloRangeArray

Solving the Model:

IloCplex

■

More precise information about the outcome of the last call to themethod

solve

can be obtained by calling:

cplex

getStatus

■

Returned value tells what CPLEX found out: whether ◆

it found the optimal solution or only a feasible one; or

◆

it proved the model to be unbounded or infeasible; or

◆

nothing at all has been proved at this point.

■

More info is available with method

getCplexStatus

Querying Results

■

Query methods access information about the solution.

■

Numbers in solution, etc. are of type

IloNum

double

■

To query the solution value for a variable:^ IloNum

v

=^

cplex

getValue

x

■

Warning!

Sometimes for integer variables the value is not integer

but just “almost” integer (e.g. 1e-9 instead of 0).Explicit rounding necessary(use functions

round

of

<math.h>

or

IloRound

■

To query the solution value for an array of variables:^ IloNumVarArray

x

env

... IloNumArray

v

env

cplex

getValues

v^

,^

x

Querying Results

■

To get the values of the reduced costs of an array of variables:^ IloNumVarArray

x

env

... IloNumArray

v

env

cplex

getReducedCosts

v

x^

■

To avoid logging messages by CPLEX on screen:^ cplex

setOut

(^

env

getNullStream

cplex

setError

env

getNullStream

Querying Results

■

Output operator

is defined for type

IloAlgorithm::Status

returned

by

getStatus

, as well as for

IloNum

,^

IloNumVar

is also defined for any array of elements if the output operator is defined for the elements.

■

Default names are of the form

IloNumVar

n

)[

u

]

for variables, and

similarly for constraints, e.g.,^ IloNumVar

(1)[0..9]

+^

IloNumVar

(3)[0..

inf

]

<=

20

■

One can set names to variables and constraints:

x^

.^

setName

(

" x "

);

c^

.^

setName

(

" c "

);

Languages for Linear Programs

■

MPS ◆

Very

old

format (

age of punched cards!) by IBM

◆

Has become

industry standard

over the years

◆

Column-oriented

◆

Not

really human-

readable

nor

comfortable

for writing

◆

All

LP solvers

support

this language

■

LP ◆

CPLEX

specific file format

◆

Row-oriented

◆

Very

readable

, close to mathematical formulation

◆

Supported by CPLEX

,^

GUROBI

,^

GLPK

,^

LP

SOLVE

(which can translate from one format to the other!)

Example: Product Mix Problem

■

A company can produce

6 different products

P

,... , P 1

6

■

Production requires

labour

,^

energy

and

machines

, which are all limited

■

The company wants to

maximize revenue

■

The next table describes the requirements of producing one unit of eachproduct, the corresponding revenue and the availability of resources:

P

1

P

2

P

3

P

4

P

5

P

6

Limit

Revenue

Machine

Labour

Energy