You are on page 1of 33

Operational Research

Lab:
Using a solver
Patrick Meyer
& Mehrdad Mohammadi
IMT Atlantique
Part 1 : Introduction to GLPK and the GNU MathProg modeling
language

OR: Lab
P. Meyer & M. Mohammadi
GLPK 3
Various solvers available

- Commercial ;

- Free for academic use ;

- Open source.

OR: Lab
P. Meyer & M. Mohammadi
GLPK 4
GLPK

- The GLPK package is intended for solving large-scale linear


programming (LP), mixed integer linear programming (MIP), and
other related problems ;
- It includes among other things :
- an implementation of the simplex method ;
- the GNU MathProg modeling language (a subset of AMPL) ;
- GLPSOL, a stand-alone LP/MIP solver ;
- an application program interface (API) ;

OR: Lab
P. Meyer & M. Mohammadi
GLPK 5
GLPK

- Documentation available on your IMT Atlantique PC :

- /usr/share/doc/glpk-doc/glpk.pdf : the API and some info on


the stand-alone solver ;
- /usr/share/doc/glpk-doc/gmpl.pdf : full documentation of the
GNU MathProg modeling language.

OR: Lab
P. Meyer & M. Mohammadi
GLPK 6
GLPK

- GLPK on your personnal computers :


- Ubuntu (Linux) : type
sudo apt-get install glpk-utils
in a terminal window
- Windows : Download from here
https://sourceforge.net/projects/winglpk/
- Mac : via Homebrew (https://brew.sh/)
https://formulae.brew.sh/formula/glpk
- In any case : no support from us, use the IMT Atlantique PCs ! !

OR: Lab
P. Meyer & M. Mohammadi
GLPK 7
Example 1

- Model and solve the following problem :




 max z = f (x1 , x2 ) = 3x1 + 5x2
 s.t. x1 ≤ 4


x2 ≤ 6
3x1 + 2x2 ≤ 18




x1 , x2 ≥ 0

- Create a file called “example1” containing the GMPL modelization


of this problem.

OR: Lab
P. Meyer & M. Mohammadi
GLPK 8
Example 1

example1 :
var x1 >= 0;
var x2 >= 0;

maximize obj : 3* x1 + 5* x2 ;

s . t . c1 : x1 <= 4;

s . t . c2 : x2 <= 6;

s . t . c3 : 3* x1 +2* x2 <= 18;

end ;

OR: Lab
P. Meyer & M. Mohammadi
GLPK 9
Example 1
- Solve the problem by calling the solver from a terminal window :
glpsol --math example1
- Gives the following output :
[...]
Model has been successfully generated
GLPK Simplex Optimizer , v4 .45
4 rows , 2 columns , 6 non - zeros
[...]
Constructing initial basis ...
Size of triangular part = 1
0: obj = 4.50 e +01 infeas = 3.00 e +00 (0)
* 1: obj = 3.60 e +01 infeas = 0.00 e +00 (0)
OPTIMAL SOLUTION FOUND
Time used : 0.0 secs
Memory used : 0.1 Mb (114472 bytes )

OR: Lab
P. Meyer & M. Mohammadi
GLPK 10
Example 1
- For more information on the solution add for example (before the
end; statement :
[...]
printf " - - - - - - Start solving - - - - -\ n ";

solve ;

printf " - - - - - - End solving - - - - -\ n ";

display x1 ;

display x2 ;

display obj ;

end ;

OR: Lab
P. Meyer & M. Mohammadi
GLPK 11
Example 1

- Which gives :

[...]
------ End solving -----
Display statement at line 18
x1 . val = 2
Display statement at line 20
x2 . val = 6
Display statement at line 22
obj . val = 36
Model has been successfully processed

OR: Lab
P. Meyer & M. Mohammadi
GLPK 12
Example 2 (Dantzig 1963)

- Write the first cannery problem in GMPL format and solve it with
the glpk stand-alone solver

OR: Lab
P. Meyer & M. Mohammadi
GLPK 13
Example 2 (solution)
var x11 >= 0;
var x12 >= 0;
var x13 >= 0;
var x21 >= 0;
var x22 >= 0;
var x23 >= 0;

minimize cost : 225 * x11 + 153 * x12 + 162 * x13 + 225 *


x21 + 162 * x22 + 126 * x23 ;

s . t . supply1 : x11 + x12 + x13 <= 350;


s . t . supply2 : x21 + x22 + x23 <= 650;

s . t . demand1 : x11 + x21 >= 300;


s . t . demand2 : x12 + x22 >= 300;
s . t . demand3 : x13 + x23 >= 300;

[...]

OR: Lab
P. Meyer & M. Mohammadi
GLPK 14
Example 2 (solution)

[...]

solve ;

display x11 ;
display x12 ;
display x13 ;
display x21 ;
display x22 ;
display x23 ;
display cost ;

end ;

OR: Lab
P. Meyer & M. Mohammadi
GLPK 15
In real life

- The data can change from one problem instance to the other ;

- Necessity to write general models, independent of the particular


problem instance ⇒ parametric version

OR: Lab
P. Meyer & M. Mohammadi
GLPK 16
Example 3 (Dantzig 1963)

- Write the parametric version of the initial cannery problem to allow


for easy changes in the parameter values.

OR: Lab
P. Meyer & M. Mohammadi
GLPK 17
Example 3 : solution

- Let I be the set of canning plants ;

- Let J be the set of markets ;

- Let ai (i ∈ I) be the capacity of plant i (in cases) ;

- Let bj (j ∈ J) be the demand of market j (in cases) ;

- Let dij (i ∈ I, j ∈ J) be the distance between i and j (in thousands


of miles) ;

- Let f be the freight in USD per case per thousand miles.

OR: Lab
P. Meyer & M. Mohammadi
GLPK 18
Example 3 : solution

- Transport cost in thousands of USD per case :

cij := f · dij , (∀i ∈ I, ∀j ∈ J);

- Decision variables : xij ≥ 0, the shipment quantities in cases


between i and j ;
P
- Objective function : min cij · xij , the total cost.
i∈I,j∈J

OR: Lab
P. Meyer & M. Mohammadi
GLPK 19
Example 3 : solution

- Constraints :
P
- ∀i ∈ I : xij ≤ ai (supply limit at plant i) ;
j∈J
P
- ∀j ∈ J : xij ≥ bj (demand at market j).
i∈I

OR: Lab
P. Meyer & M. Mohammadi
GLPK 20
Example 3 : solution

Parameters :
set I ; # canning plants
set J ; # markets
param a { i in I };
/* capacity of plant i in cases */
param b { j in J };
/* demand at market j in cases */
param d { i in I , j in J };
/* distance in thousands of miles */
param f ;
/* freight in USD per case per thousand miles */
param c { i in I , j in J } := f * d [i , j ];
/* transport cost in thousands of USD per case */

OR: Lab
P. Meyer & M. Mohammadi
GLPK 21
Example 3 : solution

Decision variables :
var x { i in I , j in J } >= 0;
/* shipment quantities in cases */

Objective function :
minimize cost : sum { i in I , j in J } c [i , j ]* x [i , j ];
/* total transportation costs in thousands of dollars */

OR: Lab
P. Meyer & M. Mohammadi
GLPK 22
Example 3 : solution

Constraints :
s . t . supply { i in I }: sum { j in J } x [i , j ] <= a [ i ];
/* observe supply limit at plant i */
s . t . demand { j in J }: sum { i in I } x [i , j ] >= b [ j ];
/* satisfy demand at market j */

Solve and display :


solve ;

display x ;

display cost ;

OR: Lab
P. Meyer & M. Mohammadi
GLPK 23
Example 3 : solution

The data :
data ;
set I := Seattle San - Diego ;
set J := New - York Chicago Kansas ;
param a := Seattle 350
San - Diego 650;
param b := New - York 300
Chicago 300
Kansas 300;
param d : New - York Chicago Kansas :=
Seattle 2.5 1.7 1.8
San - Diego 2.5 1.8 1.4;
param f := 90;
end ;

OR: Lab
P. Meyer & M. Mohammadi
GLPK 24
Example 3 : solution

Solution :
x [ Seattle , New - York ]. val = 50
x [ Seattle , Chicago ]. val = 300
x [ Seattle , Kansas ]. val = 0
x [ San - Diego , New - York ]. val = 250
x [ San - Diego , Chicago ]. val = 0
x [ San - Diego , Kansas ]. val = 300
Display statement at line 38
cost . val = 151 200

OR: Lab
P. Meyer & M. Mohammadi
Part 2 : Exercises

OR: Lab
P. Meyer & M. Mohammadi
Exercises 26
Exercise 1 : cannery++

In this exercise, you will implement the extended version of the


canneries problem in GMPL.
- Take the same assumptions as provided for you during the course
and write down the parametric model of this problem.
- Consider that only P number of plants can be selected.
- Define first the decision variables,
- Next, write the objective function and finally the constraints.

OR: Lab
P. Meyer & M. Mohammadi
Exercises 27
Exercise 1 : cannery++

Use the following data to solve the model.


Demand
Distance
j Value
j i Fixed cost Capacity
1 80
i 1 2 3 4 5 6 7 8 9 10 1 684 200
2 50
1 18 14 12 16 19 17 14 18 11 13 2 977 300
3 82
2 12 15 17 19 13 10 15 14 16 14 3 563 150
4 43
3 11 12 12 20 19 20 12 15 13 16 4 612 180
5 96
4 11 18 10 10 11 15 13 15 10 15 5 950 280
6 107
5 10 14 20 10 10 16 18 16 15 15 6 928 270
7 88
6 12 14 20 14 15 10 12 15 17 13 7 750 200
8 42
7 13 17 19 15 12 12 12 14 20 14 8 766 220
9 65
8 11 10 12 18 18 17 19 20 15 10
10 38

- Change the value of P from 1 to 8.


- Interpret your solutions.

OR: Lab
P. Meyer & M. Mohammadi
Exercises 28
Exercise 2

- A cargo plane has three compartments for storing cargo : front,


centre and rear. These compartments have the following limits on
both weight and space :
Compartment Weight capacity (tonnes) Space capacity (cubic metres)
Front 10 6800
Centre 16 8700
Rear 8 5300

- The weight of the cargo in the respective compartments must be


the same proportion of that compartment’s weight capacity to
maintain the balance of the plane.

OR: Lab
P. Meyer & M. Mohammadi
Exercises 29
Exercise 2

- The following four cargoes are available for shipment on the next
flight :
Cargo Weight (tonnes) Volume (cubic metres/tonne) Profit (USD/tonne)
C1 18 480 310
C2 15 650 380
C3 23 580 350
C4 12 390 285

- Any proportion of these cargoes can be accepted.

OR: Lab
P. Meyer & M. Mohammadi
Exercises 30
Exercise 2

- Determine how much (if any) of each cargo C1, C2, C3 and C4
should be accepted and how to distribute each among the
compartments so that the total profit for the flight is maximized :

- Formulate the above problem as a linear program and solve it using


GLPK ;
- What assumptions are made in formulating this problem as a linear
program ?

OR: Lab
P. Meyer & M. Mohammadi
Exercises 31
Exercise 3

- A company assembles four products (1, 2, 3, 4) from delivered


components. The profit per unit for each product (1, 2, 3, 4) is 10,
15, 22 and 17 USD respectively. The maximum demand in the
next week for each product (1, 2, 3, 4) is 50, 60, 85 and 70 units
respectively.

OR: Lab
P. Meyer & M. Mohammadi
Exercises 32
Exercise 3

- There are three stages (A, B, C) in the manual assembly of each


product and the man-hours needed for each stage per unit of
product are shown below :

Stage \ Product 1 2 3 4
A 2 2 1 1
B 2 4 1 2
C 3 6 1 5

- The nominal time available in the next week for assembly at each
stage (A, B, C) is 160, 200 and 80 man-hours respectively.

OR: Lab
P. Meyer & M. Mohammadi
Exercises 33
Exercise 3

- It is possible to vary the man-hours spent on assembly at each


stage such that workers previously employed on stage B assembly
could spend up to 20% of their time on stage A assembly and
workers previously employed on stage C assembly could spend up
to 30% of their time on stage A assembly ;

- Production constraints also require that the ratio (product 1 units


assembled)/(product 4 units assembled) must lie between 0.9 and
1.15 ;

- Formulate the problem of deciding how much to produce next


week as a linear program.

OR: Lab
P. Meyer & M. Mohammadi

You might also like