Modeling in Gnu Mathprog Language - A Short Introduction: Paweł Zieli Nski
Modeling in Gnu Mathprog Language - A Short Introduction: Paweł Zieli Nski
a short introduction
Paweł Zieliński
• Webpages:
• My home page: http://www.im.pwr.wroc.pl/ ˜pziel/
• Course materials:
http://www.im.pwr.wroc.pl/ ˜pziel/lectures/toulouse/html
• The GNU Linear Programming Kit (GLPK): glpsol solver
plus the GNU MathProg modeling language The software
is free and can be downloaded:
• GUSEK (GLPK Under Scite Extended Kit): The GLPK +
IDE - Windows version: http://gusek.sourceforge.net/,
• GLPK (GNU Linear Programming Kit) for Windows (without
IDE): http://gnuwin32.sourceforge.net/packages/glpk.htm,
• GLPK (GNU Linear Programming Kit) sources (without
IDE): http://www.gnu.org/software/glpk/glpk.html
Linear programming problem
n
X
cj xj → min(max) (a linear objective function)
j=1
n
X
aij = (≤, ≥)bi , i = 1, . . . , m (linear constraints)
j=1
xj ≥ 0, j = 1, . . . , n (nonnegative variables)
end;
/* Objective function */
maximize ObjectiveFunctionLabel : 4*x1 +2*x2;
/* Constraints */
s.t. label1: x1 + x2 = 2;
s.t. label2: x1 + x2 >= 4;
end;
/* decision variables */
end;
set Cameras;
data;
/* Definition of the set Cameras */
set Cameras:= ’cub’ ’quick’ ’vip’;
end;
Model: camera_arrays.mod
Towards isolating the data from the model - arrays and
sets
• The declaration of the set of cameras manufactured by the
company:
set Cameras;
The initialization of the set Cameras
set Cameras:= ’cub’ ’quick’ ’vip’;
• The declaration of array of three nonnegative decision
variables indexed by Cameras (production[’cub’],
production[’quick’]and production[’vip’]):
var productionCameras >=0
• Other examples:
set Range:= 1..n;
set MyIntegerSet:= 4 8 9 10 ;
var array{1..m};
Isolating the data from the model - the data
/* Data section */
data;
Model: camera_isolation.mod
Isolating the data from the model - the data
set Cameras;
set Times;
/* Parameters */
/* Variables */
Model: camera_isolation.mod
Aggregate operators and quantifiers
Subject To
time(manufacture): + 0.1 production(cub)
+ 0.2 production(quick) + 0.7 production(vip) <= 250
time(assemble): + 0.2 production(cub)
+ 0.35 production(quick) + 0.1 production(vip) <= 350
time(inspect): + 0.1 production(cub)
+ 0.2 production(quick) + 0.3 production(vip) <= 150
Bounds
production(cub) >= 250
production(quick) >= 375
production(vip) >= 150
End
Displaying results, the data section in a separated file
• Displaying results:
display production;
xj ≥ 0, j = 1, . . . , n (nonnegative variables)
xj integer, (binary) j = 1, . . . , n
The integrality constraints on variables make the general
integer programming problem NP-hard and thus very hard from
computational point of view.
If there exist real nonnegative variables and integer variables in
a model, then we call the problem the Mixed Integer
programming Problem (MIP)
MIP=LP+IP
Mixed Integer programming Problem (MIP)
Example 4: Solve the following mixed integer programming
problem:
−3x1 − 2x2 + 10 → max
x1 − 2x2 + x3 = 2.5;
2x1 + x2 + x4 ≥ 1.5
x1 , x2 , x3 , x4 ≥ 0
var x1 >= 0;
x2 , x3 integer
var x4 >=0;
/* The declaration of nonnegative integer decision variables*/
var x2 integer >= 0;
var x3 integer >=0;
/* Objective function */
maximize ObjectiveFunctionLabel : -3*x1 -2*x2+10;
/* Constraints */
s.t. label1: x1 - 2*x2 + x3 = 2.5;
s.t. label2: 2*x1 + x2 +x4 >= 1.5;
end;
/* Displaying results */
display ’solution X’;
display{i in E: x[i]=1 }: x[i];
display ’total costs=’,sum{i in E} c[i]*x[i];
/* Data section */
data;
param n:=10;
param p:=6;
param c:=[1] 3 [2] 2 [3] 6 [4] 3 [5] 9 [6] 5 [7] 8 [8] 1 [9] 2 [10] 6;
end;
xj ∈ {0, 1}, j = 1, . . . , n
/* Sets */
set Items:=1..n;
set Resources:=1..m;
/* parametry */
/* Decision variables */
/* variable */
var choose{Items} >=0 binary;
/* Objective function */
/* Constraints */
s.t. ResourceConstraints{i in Resources}: sum{j in Items} consumption[i,j]*choose[j] <= capaci
solve;
Decision variables:
/* Displaying results */
display ’production plan’;
display {t in Periods}: x[t];
display ’total cost=’, sum{t in Periods} (c[t]*x[t]+cI[t]*I[t]+cB[t]*B[t]);
display {t in Periods}: I[t];
display {t in Periods}: B[t];
Exercise: Provide a separated data file and solve the problem.
DLS, a positive initial inventory (lotsizing1.mod)
If initial inventory is positive I0 , then one can append period 0 and
assign x0 = I0 and d0 = 0 with zero inventory cost.
param InitInvent>=0, default 0; # initial inventory
• Sets:
set V1 within V; the declaration of set V1 such that V1 ⊆ V
set V2:=V diff V1 diff V3; the declaration of set V2 of the form
V \ V1 \ V3
set A within V cross V; the declaration of set A such that
A ⊆ V × V (the subset of the Cartesian product)
set S{i in V}:={j in V: (i,j) in A};
S(i) = {j ∈ V | (i, j) ∈ A}
set P{i in V}:={j in V: (j,i) in A};
P(i) = {j ∈ V | (j, i) ∈ A}
• Checking a value of logical expression:
check: (V1 inter V3) within {}; test: V1 ∩ V3 = ∅; if test fail
the model translator reports error
check
P P in V1} a[i] = sum{i in V3} b[i]; test:
sum{i
i∈V1 ai = i∈V3 bi
The shortest path problem - a model
0 ≤ xij ≤ 1, (i, j) ∈ A.
The shortest path problem - an implementation
param a >=0;
param b, >a;
/* the interval of costs */
param c{(i,j) in A}, >= 0 :=Uniform(a,b); # cij the cost of arc (i,j)
/* the costs are randomly generated according to uniform distribution */
ms → min
precedence constraints:
ti,j+1 ≥ tij + pij i = 1, . . . , m, j = 1, . . . , n − 1
resource constraints:
tij + Byjik ≥ tjk + pkj j = 1, . . . , n, i = 1, . . . , m − 1, k = i + 1, . . . , m
tkj + B(1 − yjik ) ≥ tij + pij j = 1, . . . , n, i = 1, . . . , m − 1, k = i + 1, . . . , m
tin + pin ≤ ms i = 1, . . . , m
tij ≥ 0 i = 1, . . . , m, j = 1, . . . , n
yjik ∈ {0, 1} j = 1, . . . , n, i = 1, . . . , m − 1, k = i + 1, . . . , m
Exercise: Implement the flow shop problem in GNU MathProg and solve it
by glpk for the following data:
data;