GLPK Notes
GLPK Notes
GLPK Notes
Pawe Zielinski
Institute of Mathematics and Computer Science, Wrocaw University of Technology, Poland
General informations
Webpages: My home page: http://www.im.pwr.wroc.pl/ pziel/ 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): http://gnuwin32.sourceforge.net/packages/glpk.htm,
GLPK (GNU Linear Programming Kit) sources (without
IDE): http://www.gnu.org/software/glpk/glpk.html
cj xj min(max)
j =1 n
aij xj = (, )bi ,
j =1
xj 0,
Parameters (input data): cj , j = 1, . . . , n, objective function coefcients, bi , i = 1, . . . , m, the right hand side vector coefcients, aij , i = 1, . . . , m, j = 1, . . . , n, matrix coefcients. xj , j = 1, . . . , n, decision variables (output).
Solve the above model (feasible.mod) in glpsol. glpsol --model feasible.mod glpsol --model feasible.mod --output feasible.txt
/* objective function represents profit */ maximize profit: 3*cub + 9*quick + 25*vip; /* constraints determine composition manufacturing cameras */ s.t. s.t. s.t. s.t. s.t. s.t. end; time_manufacture: 0.1*cub + 0.2*quick + time_assemble: 0.2*cub + 0.35*quick + time_inspect: 0.1*cub + 0.2*quick + requirements_cub: cub requirements_quick: quick requirements_vip: 0.7*vip <= 250; 0.1*vip <= 350; 0.3*vip <= 150; >= 250; >= 375; vip >= 150;
Towards isolating the data from the model - arrays and sets
/* Arrays and sets */ /* enumerated set Cameras represents the set of cameras manufactured by the company */ set Cameras; /* array of three decision variables: production[cub], production[quick] and production[vip] */ var production{Cameras} >=0; /* objective function represents profit */ maximize profit: 3*production[cub] + 9*production[quick] + 25*production[vip]; /* constraints determine composition manufacturing cameras */ s.t. man: 0.1*production[cub] + 0.2*production[quick] +0.7*production[vip] <= 250; s.t. ass: 0.2*production[cub] + 0.35*production[quick] +0.1*production[vip] <= 350; s.t. insp: 0.1*production[cub] +0.2*production[quick] +0.3*production[vip] <= 150; s.t. requirements_cub: s.t. requirements_quick: s.t. requirements_vip: production[cub] production[quick] production[vip] >= 250; >= 375; >= 150;
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
param capacity:= manufacture 250 assemble param demand:=cub 250 quick 375 vip
param consumption: cub quick vip:= manufacture 0.1 0.2 0.7 assemble 0.2 0.35 0.1 inspect 0.1 0.2 0.3; end;
Model: camera_isolation.mod
/* Variables */ var production{j in Cameras} >=demand[j]; # decision variables plus trivial bounds /* objective function represents profit */ maximize Profit: sum{j in Cameras} profit[j]*production[j]; /* constraints determine composition manufacturing cameras */ s.t. time{i in Times}: sum{j in Cameras} consumption[i,j]*production[j] <=capacity[i];
Model: camera_isolation.mod
One may add the bounds to the constraints using universal quantier:
requirements{j in Cameras} s.t. requirements{j in Cameras}: production[j]>=demand[j];
Checking the model without solving it and writing the generated model
to the le:
glpsol --check --model camera_isolation.mod --wcpxlp camera_isolation.lp \* Problem: camera_isolation *\ Maximize Profit: + 3 production(cub) + 9 production(quick) + 25 production(vip) 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: maximize Profit: sum{j in Cameras} profit[j]*production[j]; s.t. time{i in Times}: sum{j in Cameras} consumption[i,j]*production[j] <=capacity[i]; solve; /* solve command is needed !!!*/ display production; display -----------more elegant way -------------; display profit =, sum{j in Cameras} profit[j]*production[j]; display{j in Cameras} production[j];
the data section in a separated le camera_isolation1.dat data; set Times:= manufacture assemble inspect; param: Cameras: profit demand := cub 3 250 quick 9 375 vip 25 150; param capacity:= manufacture 250 assemble 350 inspect 150; param consumption: cub quick vip:= manufacture 0.1 0.2 0.7 assemble 0.2 0.35 0.1 inspect 0.1 0.2 0.3; end;
cj xj min(max)
j =1 n
aij = (, )bi ,
j =1
xj 0, xj integer, (binary)
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
x4
var x1 >= 0; 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: s.t. label2: end;
x1 - 2*x2 + x3 2*x1 + x2
Solve the above model (mip.mod) in glpsol. glpsol --model mip.mod glpsol --model mip.mod --output mip.txt
ci xi min
i =1 n
xi = p
i =1
xi {0, 1}, i = 1, . . . , n xi is a binary decision variable that takes value 1 if and only if i -th item belongs to X .
/* The objective function */ minimize TotalCost: sum{i in E} c[i]*x[i]; /* The constraint */ s.t. exact_p: sum{i in E} x[i] = p; solve; /* 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] end;
1 [9] 2 [10] 6;
pj xi max
j =1 n
rij xj ci ,
j =1
i = 1, . . . , m j = 1, . . . , n
/* Constraints */ s.t. ResourceConstraints{i in Resources}: sum{j in Items} consumption[i,j]*choose[j] <= capaci solve; display{j in Items: choose[j]=1} choose[j];
DLS - a model
The Dynamic lot sizing with backorders can be formulated as follows:
T t =1 (ct xt
t = 1, . . . , T , t = 1, . . . , T , t = 1, . . . , T .
It - inventory amount carried from period t to period t + 1, Bt - backordering amount carried from period t + 1 to period t .
xij
j S (i ) j P (i )
xji =
ai 0 bi
i V1 , i V2 , i V3 ,
0 xij uij , (i , j ) A.
Example 9: We are given G = (V , A) with distinguished nodes s and t , s, t V . A nonnegative cost cij is given for each arc (i , j ) A. We wish to nd a path from s to t whose total cost is minimal. It is sufcient to set V1 = {s}, V3 = {t }, a1 = 1, bn = 1, uij = 1 for (i , j ) A in the model of the minimum cost ow problem (see Example 6) and so: cij xij min
(i ,j )A
xij
j S (i ) j P (i )
xji =
1 0 1
i = s, i = s, i = t , i = t,
0 xij 1, (i , j ) A.
param n, integer, >= 2; # the number of nodes set V:={1..n}; # the set of nodes set A within V cross V; # the set of arcs param c{(i,j) in A} >= 0; # cij the cost of arc (i,j) param s in V, default 1; # source s param t in V, != s, default n; # sink t var x{(i,j) in A}, >= 0, <= 1; /* x[i,j] =1 if arc belongs to the shortest path, 0 otherwise*/ minimize Cost: sum{(i,j) in A} c[i,j]*x[i,j]; s.t. node{i in V}: sum{(j,i) in A} x[j,i] + (if i = s then 1)= sum{(i,j) in A} x[i,j] + (if i = t then 1);
Example 11: Given m different items that are to be routed through n machines. Each item must be processed rst on machine 1, then on machine 2, and nally on machine n. The sequence of items may differ for each machine. Assume that the times pij required to perform the work on item i by machine j are known. Our objective is to minimize the total time necessary to process all the items called makespan.
Decision variables: tij is the earliest starting time of processing item i on machine j , yjik = 1 if and only if on machine j item i is processed before item k , ms is the makespan. B is a big number, for instance: B = 1 +
m i n j =1
pij .
Exercises
Exercise 1: Generalize the model presented in Example 1 to m constraints and n variables. Isolate the model from data. Input data: n, m, A Rmn (the constraint matrix), b Rm (the right hand side vector), c Rn (the vector of objective function coefcients) Solve the model with the data provided in Example 1. Exercise 2:(C.H. Papadimitriou, K. Steiglitz, 1998). Consider the problem faced by a homemaker when buying food to meet certain nutritional requirements. He has a set of different kinds of foods F (for example F = {potato, carrot, bread, butter, apple}) and a set of nutrients N (for example N = {vitamin A, vitamin B, vitamin C}. Each food from F has some of each nutrient from N . Namely, he has some information about amount aij of i -th nutrient, i N , in a unit of the j -th food, j F . The requirements of i -th nutrient are given by ri , i N . The cost per unit of the j -th food is cj , j F . The homemaker has to decide how many units of each food to buy in order to satisfy the nutritional requirements. Formulate a linear programming model for nding the least expensive diet (which foods to buy) and implement in GNU MathProg and solve it for a sample data by glpk. The model must isolated from data. Hint : See Example 3.
Exercises
Exercise 3: (The constrained shortest path) Given a directed graph G = (V , A) with distinguished nodes s and t , s, t V and cost cij , length lij for each arc (i , j ) A and length L. We wish to nd a path from s to t whose total cost is minimal and total length is at most L. Formulate an integer programming model for nding the problem and implement in GNU MathProg and solve it for a sample data by glpk. The model must isolated from data. Hint : Modify Example 9. Exercise 4: Scheduling a set of jobs on identical parallel machines to minimize the makespan is one of the basic and most extensively studied scheduling problems . We are given a set of jobs J = {1, . . . , n} which must be processed on m identical machines M1 , . . . , Mm . Each machine can process at most one job at a time. Each job i J has a processing time pi . We wish to assign each job to exactly one machine so that the maximum job completion time of the resulting schedule, called a makespan, is minimal. Formulate an integer programming model for nding the problem and implement in GNU MathProg and solve it for a sample data by glpk. The model must isolated from data.