Professional Documents
Culture Documents
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 ;
- Open source.
OR: Lab
P. Meyer & M. Mohammadi
GLPK 4
GLPK
OR: Lab
P. Meyer & M. Mohammadi
GLPK 5
GLPK
OR: Lab
P. Meyer & M. Mohammadi
GLPK 6
GLPK
OR: Lab
P. Meyer & M. Mohammadi
GLPK 7
Example 1
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;
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 ;
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;
[...]
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 ;
OR: Lab
P. Meyer & M. Mohammadi
GLPK 16
Example 3 (Dantzig 1963)
OR: Lab
P. Meyer & M. Mohammadi
GLPK 17
Example 3 : solution
OR: Lab
P. Meyer & M. Mohammadi
GLPK 18
Example 3 : solution
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 */
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++
OR: Lab
P. Meyer & M. Mohammadi
Exercises 27
Exercise 1 : cannery++
OR: Lab
P. Meyer & M. Mohammadi
Exercises 28
Exercise 2
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
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 :
OR: Lab
P. Meyer & M. Mohammadi
Exercises 31
Exercise 3
OR: Lab
P. Meyer & M. Mohammadi
Exercises 32
Exercise 3
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
OR: Lab
P. Meyer & M. Mohammadi