Contents - School of Mathematics, Statistics and Operations Research
Contents - School of Mathematics, Statistics and Operations Research
Contents - School of Mathematics, Statistics and Operations Research
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>School</strong> <strong>of</strong> <strong>Mathematics</strong>, <strong>Statistics</strong> <strong>and</strong> <strong>Operations</strong> <strong>Research</strong><br />
Te Kura Mātai Tatauranga, Rangahau Pūnaha<br />
OPRE 355 Model Building with GMPL Weeks 1 <strong>and</strong> 2<br />
Outline. We revise the GMPL modelling language (from OPRE253) <strong>and</strong> extend its use on<br />
a number <strong>of</strong> further applications. The transportation problem is a good example <strong>of</strong> a problem<br />
class, i.e., it is a generic problem structure <strong>of</strong> which there are many problem instances with<br />
particular costs, supplies <strong>and</strong> dem<strong>and</strong>s. Hence, we will also show how to separate the general<br />
model <strong>of</strong> the problem class from the problem instance data in GMPL.<br />
MSOR Student Account Activation. Please follow the “register register” instructions on<br />
the lab door.<br />
<strong>Contents</strong><br />
1 Introduction 2<br />
1.1 GMPL: A Modelling Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2<br />
1.2 GLPSOL: A Model Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3<br />
2 Application: Assigning Jobs to Machines 5<br />
2.1 A First GMPL Model using Subscripts . . . . . . . . . . . . . . . . . . . . . . . . 7<br />
2.2 A Second GMPL Model using Summations <strong>and</strong> Families <strong>of</strong> Constraints . . . . . 8<br />
2.3 A General Model <strong>of</strong> the “Assignment Problem” . . . . . . . . . . . . . . . . . . . 10<br />
2.4 GMPL: Model + Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11<br />
3 Application: A Transportation Problem 13<br />
3.1 GMPL: Model + Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14<br />
4 Application: Solving Two-Person Zero-Sum Games 18<br />
4.1 GMPL: Model + Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20<br />
5 Practice Problems 22<br />
OPRE 355, 2012 1 Lab 1
1 Introduction<br />
Example: A Diet Problem<br />
My diet requires that all the food I eat come from one <strong>of</strong> the four “basic food groups” (chocolate<br />
cake, ice cream, s<strong>of</strong>t drink, <strong>and</strong> cheesecake). Each (large) slice <strong>of</strong> chocolate cake costs 50c, each<br />
scoop <strong>of</strong> chocolate ice cream costs 20c, each bottle <strong>of</strong> cola costs 30c, <strong>and</strong> each piece <strong>of</strong> pineapple<br />
cheesecake costs 80c. Each day, I must ingest at least 500 calories, 6 oz <strong>of</strong> chocolate, 10 oz<br />
<strong>of</strong> sugar, <strong>and</strong> 8 oz <strong>of</strong> fat. The nutritional content per unit <strong>of</strong> each food is shown in the table<br />
below. Formulate a linear programming model that can be used to satisfy my daily nutritional<br />
requirement at minimum cost.<br />
Type <strong>of</strong> Calories Chocolate Sugar Fat<br />
Food (ounces) (ounces) (ounces)<br />
Chocolate Cake (1 slice) 400 3 2 2<br />
Chocolate ice cream (1 scoop) 200 2 2 4<br />
Cola (1 bottle) 150 0 4 1<br />
Pineapple cheesecake (1 piece) 500 0 4 5<br />
Here is an LP model for this diet problem.<br />
##<br />
## GMPL Model: Diet Problem<br />
##<br />
var x1>=0; # number <strong>of</strong> slices <strong>of</strong> chocolate cake eaten daily<br />
var x2>=0; # number <strong>of</strong> scoops <strong>of</strong> chocolate ice cream eaten daily<br />
var x3>=0; # number <strong>of</strong> bottles <strong>of</strong> cola drunk daily<br />
var x4>=0; # number <strong>of</strong> pieces <strong>of</strong> pineapple cheesecake eaten daily<br />
minimize DailyCost: 50*x1 + 20*x2 + 30*x3 + 80*x4;<br />
subject to Calories: 400*x1 + 200*x2 + 150*x3 + 500*x4 >= 500;<br />
subject to Chocolate: 3*x1 + 2*x2 >= 6;<br />
subject to Sugar: 2*x1 + 2*x2 + 4*x3 + 4*x4 >= 10;<br />
subject to Fat: 2*x1 + 4*x2 + x3 + 5*x4 >= 8;<br />
end;<br />
1.1 GMPL: A Modelling Language<br />
The GNU Mathematical Programming Language (GMPL or “GNU MathProg”) is a mathematical<br />
modelling language that is part <strong>of</strong> the GNU Linear Programming Kit (GLPK). 1 GMPL is a<br />
subset <strong>of</strong> a more well-known language called A Mathematical Programming Language (AMPL)<br />
which is expensive (see www.ampl.com).<br />
1 GLPK is free s<strong>of</strong>tware <strong>and</strong> you can download it from http://www.gnu.org/s<strong>of</strong>tware/glpk/glpk.html.<br />
OPRE 355, 2012 2 Lab 1
A mathematical modelling language allows you to define or construct a mathematical model. It<br />
is not the same as a programming language (such as Java) or a statistical computing language<br />
(such as R). A separate solver is used to solve the model.<br />
The amazing thing is that the diet problem formulation above is written exactly the input<br />
format that GMPL uses.<br />
Tasks<br />
• Each line ends with a semicolon ‘;’.<br />
• Also, the hash character ‘#’ represents a comment (the rest <strong>of</strong> the line is ignored).<br />
• Each variable is defined using the keyword ‘var’.<br />
• Note that the objective function <strong>and</strong> each constraint are named.<br />
• All white space is ignored but is useful for clarity <strong>of</strong> reading.<br />
• The file ends with the line ‘end;’.<br />
1. Createadirectory calledopre355inyourhomedirectory. Todothisclick onthe“Terminal<br />
program” icon at the bottom <strong>of</strong> the desktop. Then type<br />
mkdir opre355<br />
cd opre355<br />
2. Type the formulation from diet problem example into a text file <strong>and</strong> save it as diet.mod<br />
in your opre355 directory. To do this you will need to use a text editor, e.g., use KWrite<br />
from the icons at the bottom <strong>of</strong> the desktop.<br />
Modelling with Integer <strong>and</strong> Binary Variables<br />
GMPL can also solve Integer Programming (IP) models.<br />
We can specify that any variable is restricted to integer or binary, i.e., {0,1} values using the<br />
notation below:<br />
var x1 integer;<br />
var x2 integer >= 0;<br />
var x3 binary;<br />
1.2 GLPSOL: A Model Solver<br />
The solver we will be using is called glpsol which is available in the Statistical Computing Lab<br />
(Cotton 535). It is capable <strong>of</strong> solving LP models <strong>and</strong> IP models but not nonlinear models.<br />
OPRE 355, 2012 3 Lab 1
Tasks<br />
1. In your “terminal program” (usually just called a “shell”) make sure you change the<br />
directory to the place where your file diet.mod is saved. You probably want to type<br />
something like<br />
cd ~/opre355<br />
ls<br />
<strong>and</strong> make sure that diet.mod has been listed.<br />
2. To solve a model you simply type<br />
glpsol --model diet.mod<br />
which will produce alot <strong>of</strong> output something like<br />
Reading model section from diet.mod...<br />
17 lines were read<br />
Generating DailyCost...<br />
Generating Calories...<br />
Generating Chocolate...<br />
Generating Sugar...<br />
Generating Fat...<br />
Model has been successfully generated<br />
lpx_simplex: original LP has 5 rows, 4 columns, 18 non-zeros<br />
lpx_simplex: presolved LP has 4 rows, 4 columns, 14 non-zeros<br />
lpx_adv_basis: size <strong>of</strong> triangular part = 4<br />
0: objval = 0.000000000e+00 infeas = 1.000000000e+00 (0)<br />
4: objval = 2.200000000e+02 infeas = 0.000000000e+00 (0)<br />
* 4: objval = 2.200000000e+02 infeas = 0.000000000e+00 (0)<br />
* 7: objval = 9.000000000e+01 infeas = 0.000000000e+00 (0)<br />
OPTIMAL SOLUTION FOUND<br />
Time used: 0.0 secs<br />
Memory used: 0.1M (151281 bytes)<br />
The output above tells us that an optimal solution has been found <strong>and</strong> that the optimal<br />
objective function value is 90. Unfortunately it does not tell us the optimal values <strong>of</strong> the<br />
decision variables.<br />
3. In your file diet.mod add the following two lines before the end; line.<br />
solve;<br />
display x1,x2,x3,x4;<br />
Running the solver again gives the full optimal solution.<br />
• What is the optimal solution?<br />
• Interpret this in terms <strong>of</strong> the original word problem.<br />
4. Modify some <strong>of</strong> the coefficients in the LP model <strong>and</strong> resolve. What is the effect on the<br />
optimal solution? Can change the problem so that it is (a) infeasible, or (b) unbounded?<br />
OPRE 355, 2012 4 Lab 1
2 Application: Assigning Jobs to Machines<br />
Suggested Reading: Hillier <strong>and</strong> Lieberman §8.3<br />
We have seen that LP models <strong>of</strong>ten belong to a problem class, e.g., production models, diet<br />
models, blending models, multiple period models, distribution models, etc. A problem class is<br />
simply a generic problem structure <strong>of</strong> which there are many problem instances with particular<br />
costs, resourcesavailable, etc. Thefirstobjective<strong>of</strong>thislabistoshowhowtoseparatethegeneral<br />
model <strong>of</strong> the problem class from the problem instance data in GMPL. The second objective <strong>of</strong><br />
this lab is solve two-person zero-sum games, i.e., to determine the optimal strategies for each<br />
player <strong>and</strong> the value <strong>of</strong> the game.<br />
Example<br />
MachineCo has four machines <strong>and</strong> four jobs to be completed. Each machine must be assigned<br />
to complete one job. The time required to set up each machine for completing each job is shown<br />
in the table below. MachineCo wants to minimize the total setup time needed to complete the<br />
four jobs.<br />
Time (hours)<br />
Machine Job 1 Job 2 Job 3 Job 4<br />
1 14 5 8 7<br />
2 2 12 6 5<br />
3 7 8 3 9<br />
4 2 4 6 10<br />
Formulate this problem as an optimization model.<br />
OPRE 355, 2012 5 Lab 1
Solution<br />
Let<br />
Warning<br />
xij =<br />
1 if job j is assigned to machine i<br />
0 otherwise<br />
• Note carefully here that xij can only take the value 0 or 1, so is called a binary variable.<br />
• The resulting optimization model is not an LP model but rather it is an Integer Programming<br />
(IP) model. Fortunately GMPL is very good at solving IP models.<br />
Model Formulation<br />
min 14x11 +5x12 +8x13 +7x14 +2x21 +12x22 +6x23 +5x24<br />
+7x31 +8x32 +3x33 +9x34 +2x41 +4x42 +6x43 +10x44<br />
s.t. x11 +x12 +x13 +x14 ≤ 1 (machine 1)<br />
Explanation <strong>of</strong> Model Formulation<br />
x21 +x22 +x23 +x24 ≤ 1 (machine 2)<br />
x31 +x32 +x33 +x34 ≤ 1 (machine 3)<br />
x41 +x42 +x43 +x44 ≤ 1 (machine 4)<br />
x11 +x21 +x31 +x41 ≥ 1 (job 1)<br />
x12 +x22 +x32 +x42 ≥ 1 (job 2)<br />
x13 +x23 +x33 +x43 ≥ 1 (job 3)<br />
x14 +x24 +x34 +x44 ≥ 1 (job 4)<br />
xij ∈ {0,1} for all i,j<br />
• The first four constraints ensure that each machine is assigned to at most one job.<br />
• The last four constraints ensure that each job is completed by at least one machine.<br />
• In general, there may be more machines that jobs, so that some machines have no job<br />
assigned, i.e., some machines are not used.<br />
OPRE 355, 2012 6 Lab 1
2.1 A First GMPL Model using Subscripts<br />
###<br />
### GMPL Model: MachineCo Assignment Problem<br />
###<br />
var x{1..4,1..4} binary;<br />
minimize totalcost: 14*x[1,1] + 5*x[1,2] + 8*x[1,3] + 7*x[1,4] +<br />
2*x[2,1] + 12*x[2,2] + 6*x[2,3] + 5*x[2,4] +<br />
7*x[3,1] + 8*x[3,2] + 3*x[3,3] + 9*x[3,4] +<br />
2*x[4,1] + 4*x[4,2] + 6*x[4,3] + 10*x[4,4];<br />
subject to machine1: x[1,1] + x[1,2] + x[1,3] + x[1,4] = 1;<br />
solve;<br />
display x;<br />
end;<br />
New Ideas<br />
• Here var x{1..4,1..4} binary; defines a family <strong>of</strong> variables with subscripts. We can<br />
then write x[1,1] instead <strong>of</strong> x11, etc.<br />
• Also, each variable is a binary variable, i.e., the only feasible values are 0 <strong>and</strong> 1.<br />
Tasks<br />
1. TypetheMachineComodelformulationaboveintoatextfile<strong>and</strong>saveitasmachineco1.mod<br />
in your opre355 directory.<br />
2. In your “terminal program” (usually just called a “shell”) make sure you change the<br />
directory to the place where your file machineco1.mod is saved. You probably want to<br />
type something like cd ~/opre355 followed by ls <strong>and</strong> make sure that machineco1.mod<br />
has been listed.<br />
3. Solve this GMPL model using<br />
glpsol --model machineco1.mod<br />
<strong>and</strong> interpret your output.<br />
OPRE 355, 2012 7 Lab 1
2.2 A Second GMPL Model using Summations <strong>and</strong> Families <strong>of</strong> Constraints<br />
The GMPL model machineco1.mod requires quite a lot <strong>of</strong> repetitive typing.<br />
Summations<br />
Consider the constraint for machine 1.<br />
x11 +x12 +x13 +x14 ≤ 1<br />
We would be quite happy writing (in summation notation)<br />
4<br />
x1j ≤ 1 (machine 1)<br />
j=1<br />
Similarly, for the other machine constraints we would be happy writing<br />
4<br />
x2j ≤ 1 (machine 2)<br />
j=1<br />
4<br />
x3j ≤ 1 (machine 3)<br />
j=1<br />
4<br />
x4j ≤ 1 (machine 4)<br />
j=1<br />
In GMPL, we can use a summation in a constraint, e.g.<br />
subject to machine1: sum{j in 1..4} x[1,j] = 1;<br />
Families <strong>of</strong> Constraints<br />
Actually, we can be even more brief by writing<br />
4<br />
xij ≤ 1 for machine i ∈ {1,2,3,4}<br />
j=1<br />
which defines one constraint for each machine i ∈ {1,2,3,4} <strong>and</strong><br />
4<br />
xij ≥ 1 for job j ∈ {1,2,3,4}<br />
i=1<br />
which defines one constraint for each machine j ∈ {1,2,3,4}.<br />
OPRE 355, 2012 8 Lab 1
• In GMPL, we can define a family <strong>of</strong> constraints<br />
subject to machine{i in 1..4}: sum{j in 1..4} x[i,j] = 1;<br />
In each such constraint, the value <strong>of</strong> the index i is fixed.<br />
• Similarly,<br />
subject to job{j in 1..4}: sum{i in 1..4} x[i,j] = 1;<br />
In each such constraint, the value <strong>of</strong> the index j is fixed.<br />
In this way, we follow very closely (<strong>and</strong> powerfully) the general formulation with the Σ notation<br />
<strong>and</strong> the indexed constraints.<br />
Full GMPL Model<br />
###<br />
### GMPL Model: MachineCo Assignment Problem<br />
###<br />
var x{1..4,1..4} binary;<br />
minimize totalcost: 14*x[1,1] + 5*x[1,2] + 8*x[1,3] + 7*x[1,4] +<br />
2*x[2,1] + 12*x[2,2] + 6*x[2,3] + 5*x[2,4] +<br />
7*x[3,1] + 8*x[3,2] + 3*x[3,3] + 9*x[3,4] +<br />
2*x[4,1] + 4*x[4,2] + 6*x[4,3] + 10*x[4,4];<br />
subject to machine{i in 1..4}: sum{j in 1..4} x[i,j] = 1;<br />
solve;<br />
display x;<br />
end;<br />
Tasks<br />
1. TypethisnewMachineComodelformulation intoatextfile<strong>and</strong>save itasmachineco2.mod<br />
in your opre355 directory. You may wish to copy-<strong>and</strong>-paste most <strong>of</strong> the model from your<br />
machineco1.mod text file.<br />
2. Solve this GMPL model using<br />
glpsol --model machineco2.mod<br />
<strong>and</strong> check that you get the same solution as before.<br />
OPRE 355, 2012 9 Lab 1
2.3 A General Model <strong>of</strong> the “Assignment Problem”<br />
The MachineCo example above is a particular instance <strong>of</strong> a general class <strong>of</strong> problem called the<br />
“Assignment Problem”. It involves the optimal matching or pairing <strong>of</strong> objects <strong>of</strong> two distinct<br />
types — jobs to machines, sales personnel to customers, <strong>and</strong> so on.<br />
• Suppose there are m machines <strong>and</strong> n jobs, where m ≥ n<br />
• Each job must be done by at least one machine.<br />
• Each machine can do at most one job.<br />
• The cost <strong>of</strong> machine i doing job j is cij.<br />
• The problem is to assign the machines to the jobs so as to minimize the total cost <strong>of</strong><br />
completing all <strong>of</strong> the jobs.<br />
We introduce binary variables xij, i = 1,...,m, j = 1,...,n<br />
<br />
1 if job j is assigned to machine i<br />
xij =<br />
0 otherwise<br />
• Since each machine j can do no more than one job, we have the machine constraints<br />
n<br />
xij ≤ 1 for i = 1,...,m<br />
j=1<br />
• Since at least one machine must do job j, we also have the job constraints<br />
• The objective function is<br />
m<br />
xij ≥ 1 for j = 1,...,n<br />
i=1<br />
In summary, the assignment problem is<br />
min<br />
s.t.<br />
m<br />
n<br />
i=1 j=1<br />
cijxij<br />
min<br />
m<br />
n<br />
i=1 j=1<br />
cijxij<br />
n<br />
xij ≤ 1 for machine i = 1,...,m<br />
j=1<br />
m<br />
xij ≥ 1 for job j = 1,...,n<br />
i=1<br />
xij ∈ {0,1} for all i,j<br />
OPRE 355, 2012 10 Lab 1
2.4 GMPL: Model + Data<br />
We are now ableto separate (in GMPL)the general model <strong>of</strong> theproblem class fromthe problem<br />
instance data. Rather than using numbers for the machines <strong>and</strong> the jobs, we can now use any<br />
labels we wish, e.g., people’s names. These will be defined as sets below.<br />
GMPL Model<br />
###<br />
### GMPL Model: Assignment Problem<br />
###<br />
# Set definitions<br />
set machines;<br />
set jobs;<br />
# Parameter definitions<br />
param cost{machines,jobs};<br />
# Variable definitions<br />
var x{machines,jobs} binary;<br />
# Objective function<br />
minimize totalcost:<br />
sum{i in machines,j in jobs} cost[i,j]*x[i,j];<br />
# Constraints<br />
subject to machine_constraint{i in machines}:<br />
sum{j in jobs} x[i,j] = 1;<br />
# Solve <strong>and</strong> display results<br />
solve;<br />
display x;<br />
end;<br />
Sets <strong>and</strong> Parameters<br />
The model starts with the reserved word set to identify the constraints <strong>and</strong> variables <strong>of</strong> the<br />
problem generically using the user-generated names machines <strong>and</strong> jobs. Next, we use the<br />
reserved word param to name the elements <strong>of</strong> the objective function <strong>and</strong> constraints, again using<br />
user-generated name cost. Thus cost defines the later coefficients in the objective function as<br />
a function <strong>of</strong> the sets machines <strong>and</strong> jobs. Once the set <strong>and</strong> param segments have been defined,<br />
the model itself can be developed.<br />
OPRE 355, 2012 11 Lab 1
Problem Instance <strong>and</strong> GMPL Data<br />
A particular problem instance would be specified by supplying the cij cost data for the general<br />
model. The equivalent in GMPL is to specify the values that the sets <strong>and</strong> parameters take.<br />
For the MachineCo example, the data we require is as follows. Note that this is very readable.<br />
###<br />
### GMPL Data: MachineCo Assignment Problem<br />
###<br />
set machines := m1 m2 m3 m4;<br />
set jobs := j1 j2 j3 j4;<br />
param cost : j1 j2 j3 j4 :=<br />
m1 14 5 8 7<br />
m2 2 12 6 5<br />
m3 7 8 3 9<br />
m4 2 4 6 10;<br />
Observe the use <strong>of</strong> := to flag the start <strong>of</strong> the data elements <strong>of</strong> a set or param. The data is<br />
stored in a separate file, in this case machineco.dat (see the tasks below).<br />
Tasks<br />
1. Type the GMPL model formulation <strong>of</strong> the assignment problem into a text file <strong>and</strong> save<br />
it as assignment.mod in your opre355 directory. You may wish to copy-<strong>and</strong>-paste some<br />
<strong>of</strong> the model from your machineco2.mod text file.<br />
2. Type the GMPL data into a text file <strong>and</strong> save it as machineco.dat in your opre355<br />
directory.<br />
3. We can solve the GMPL model in assignment.mod together with the GMPL data in<br />
machineco.dat using the following comm<strong>and</strong>.<br />
glpsol --model assignment.mod --data machineco.dat<br />
Compare the solution with solutions to previous tasks.<br />
In this way the same GMPL model can be used with many different problem<br />
instances, each <strong>of</strong> which has its own GMPL data file.<br />
OPRE 355, 2012 12 Lab 1
3 Application: A Transportation Problem<br />
Example<br />
The Fly-by-Night Airline must decide on the amounts <strong>of</strong> jet fuel to buy from three possible<br />
vendors. The airline refuels its aircraft regularly at the four airports it serves. Theoil companies<br />
have said that they can supply up to the following amounts <strong>of</strong> fuel during the coming month:<br />
275,000 gallons for BP; 350,000 gallons for Caltex; <strong>and</strong> 345,000 gallons for Shell. The required<br />
amount <strong>of</strong> jet fuel is 110,000 gallons at Dunedin; 235,000 gallons at Christchurch; 185,000 gallons<br />
at Wellington; <strong>and</strong> 440,000 gallons at Auckl<strong>and</strong>.<br />
When transportation costs are added to the price per gallon supplied, the combined cost per<br />
gallon for jet fuel from each vendor supplying a specific airport is shown in the table below.<br />
GMPL Model<br />
Dunedin Christchurch Wellington Auckl<strong>and</strong><br />
BP 10 10 9 11<br />
Caltex 7 11 12 13<br />
Shell 8 14 4 9<br />
##<br />
## GMPL: Fly-by-Night Airlines model formulation<br />
##<br />
var x{1..3,1..4} >= 0;<br />
minimize total_cost: 10*x[1,1] + 10*x[1,2] + 9*x[1,3] + 11*x[1,4]<br />
+ 7*x[2,1] + 11*x[2,2] + 12*x[2,3] + 13*x[2,4]<br />
+ 8*x[3,1] + 14*x[3,2] + 4*x[3,3] + 9*x[3,4];<br />
subject to supply_BP: sum{j in 1..4} x[1,j] = 185000;<br />
subject to dem<strong>and</strong>_Auckl<strong>and</strong>: sum{i in 1..3} x[i,4] >= 440000;<br />
solve;<br />
display x;<br />
end;<br />
This model has 12 variables <strong>and</strong> 7 constraints. Note that you can download flybynight.mod<br />
from the OPRE355 webpage, i.e., you do not need to type this in yourself.<br />
OPRE 355, 2012 13 Lab 1
Subscripts <strong>and</strong> Summation<br />
In the GMPL model above we have introduce a few new things.<br />
var x{1..3,1..4} >= 0;<br />
defines a family <strong>of</strong> variables with subscripts. We can then write x[1,1] instead <strong>of</strong> x11, etc.<br />
Also, we have summations in the constraints, e.g.<br />
sum{j in 1..4} x[1,j]
GMPL Model<br />
###<br />
### GMPL Model: Transportation Problem<br />
###<br />
### Set definitions<br />
set sources;<br />
set destinations;<br />
### Parameter definitions<br />
param supply{sources};<br />
param dem<strong>and</strong>{destinations};<br />
param cost{sources,destinations};<br />
### Variable definitions<br />
var x{sources,destinations} >= 0;<br />
### Objective function<br />
minimize total_cost:<br />
sum {i in sources,j in destinations} cost[i,j]*x[i,j];<br />
### Constraints<br />
supply_constraint{i in sources}:<br />
sum{j in destinations} x[i,j] = dem<strong>and</strong>[j];<br />
solve;display x;<br />
end;<br />
Sets <strong>and</strong> Parameters<br />
The model starts with the reserved word set to identify the constraints <strong>and</strong> variables <strong>of</strong> the<br />
problem generically using the user-generated names sources <strong>and</strong> destinations. Next, we use<br />
the reserved word param to name the elements <strong>of</strong> the objective function <strong>and</strong> constraints, again<br />
using user-generated names supply, dem<strong>and</strong> <strong>and</strong> cost. Thus supply defines the later righth<strong>and</strong>-sides<br />
<strong>of</strong> the supply constraints as a function <strong>of</strong> the set sources. Once the set <strong>and</strong> param<br />
segments have been defined, the model itself can be developed.<br />
Families <strong>of</strong> Constraints<br />
Notice also that we can specify whole families <strong>of</strong> constraints using the notation:<br />
supply_constraint{i in sources}:<br />
sum{j in destinations} x[i,j]
Problem Instance <strong>and</strong> GMPL Data<br />
A particular problem instance would be specified by supplying the cij, si <strong>and</strong> dj data for the<br />
general model. The equivalent in GMPL is to specify the values that the sets <strong>and</strong> parameters<br />
take.<br />
For the Fly-By-Night Airlines example, the data we require is as follows. Note that this is very<br />
readable.<br />
###<br />
### GMPL Data: Fly-By-Night Airlines transportation problem<br />
###<br />
### Set values<br />
set sources := BP Caltex Shell;<br />
set destinations := Dunedin Christchurch Wellington Auckl<strong>and</strong>;<br />
### Parameter values<br />
param supply :=<br />
BP 275000<br />
Caltex 350000<br />
Shell 345000;<br />
param dem<strong>and</strong> :=<br />
Dunedin 110000<br />
Christchurch 235000<br />
Wellington 185000<br />
Auckl<strong>and</strong> 440000;<br />
param cost : Dunedin Christchurch Wellington Auckl<strong>and</strong> :=<br />
BP 10 10 9 11<br />
Caltex 7 11 12 13<br />
Shell 8 14 4 9 ;<br />
Observe the use <strong>of</strong> := to flag the start <strong>of</strong> the data elements <strong>of</strong> a set or param. The data is<br />
stored in a separate file, in this case flybynight.dat. In this way the same GMPL model can<br />
be used with many different problem instances, each <strong>of</strong> which has its own data file.<br />
Solution <strong>of</strong> the Problem Instance<br />
Task<br />
WecansolvetheGMPLmodelintransport.modtogetherwiththeGMPLdatainflybynight.dat<br />
using the following comm<strong>and</strong>.<br />
glpsol --model transport.mod --data flybynight.dat<br />
OPRE 355, 2012 16 Lab 1
Practice Problem<br />
Tom would like 3 pints <strong>of</strong> home brew today <strong>and</strong> an additional 4 pints <strong>of</strong> home brew tomorrow.<br />
Dick is willing to sell a maximum <strong>of</strong> 5 pints total at a price <strong>of</strong> $3.00 per pint today <strong>and</strong> $2.70<br />
per pint tomorrow. Harry is willing to sell a maximum <strong>of</strong> 4 pints total at a price <strong>of</strong> $2.90 per<br />
pint today <strong>and</strong> $2.80 per pint tomorrow. Tom wishes to know what his purchases should be to<br />
minimize his cost while satisfying his thirst requirements.<br />
Task<br />
Formulate this problem as a transportation problem by writing a GMPL data file <strong>and</strong> call it<br />
tomdickharry.dat. Solve your model using<br />
glpsol --model transport.mod --data tomdickharry.dat<br />
<strong>and</strong> interpret your results.<br />
OPRE 355, 2012 17 Lab 1
4 Application: Solving Two-Person Zero-Sum Games<br />
Suggested Reading: Hillier <strong>and</strong> Lieberman Chapter 14<br />
We will see that a two-person zero-sum game can be solved by solving two LP models, one for<br />
the row player <strong>and</strong> one for the column player.<br />
Example. The Birthday<br />
Frank is hurrying home late, after a particularly grueling day, when it pops into his mind that<br />
today is Kitty’s birthday! Or is it? Everything is closed except the florist.<br />
If it is not her birthday <strong>and</strong> he brings no gift, the situation will be neutral, i.e., pay<strong>of</strong>f 0. If it is<br />
not her birthday <strong>and</strong> he comes in bursting with roses, <strong>and</strong> obviously confused, she may be very<br />
suspicious, a pay<strong>of</strong>f <strong>of</strong> say −1. If it is her birthday <strong>and</strong> he has, clearly, remembered it, that is<br />
worth say 2. If he has forgotten it, he is down like a stone, say, −10. So Frank mentally forms<br />
the pay<strong>of</strong>f matrix.<br />
Nature<br />
Not Birthday Birthday<br />
Frank Nothing 0 −10<br />
Flowers −1 2<br />
OPRE 355, 2012 18 Lab 1
###<br />
### GMPL: Frank’s LP Model<br />
###<br />
var x{1..2} >=0;<br />
var v;<br />
maximize value<strong>of</strong>game: v;<br />
subject to col1: v = (0)*y[1] + (-10)*y[2];<br />
subject to row2: w >= (-1)*y[1] + (2)*y[2];<br />
subject to prob: y[1]+y[2] = 1;<br />
solve;<br />
display w;<br />
display y;<br />
end;<br />
Tasks<br />
1. Type Frank’s LP model above into a text file <strong>and</strong> save is as frank.mod in your opre355<br />
directory.<br />
2. Solve Frank’s LP model using glpsol --model frank.mod<br />
3. Type the Nature’s LP model above into a text file <strong>and</strong> save is as nature.mod in your<br />
opre355 directory.<br />
4. Solve Nature’s LP model using glpsol --model nature.mod<br />
5. Interpret the optimal strategies <strong>and</strong> the value <strong>of</strong> the game.<br />
OPRE 355, 2012 19 Lab 1
4.1 GMPL: Model + Data<br />
As in Section 2.4 we can formulate a general model in a GMPL Model file (with sets <strong>and</strong><br />
parameters) <strong>and</strong> give the problem instance data in a GMPL Data file.<br />
General GMPL Model<br />
###<br />
### GMPL Model: Two-Person Zero-Sum Game<br />
###<br />
### Model <strong>and</strong> solve the row player’s LP<br />
###<br />
set ROWS;<br />
set COLS;<br />
param pay<strong>of</strong>f {ROWS,COLS};<br />
var x {ROWS} >=0;<br />
var v;<br />
maximize value<strong>of</strong>game: v;<br />
subject to constraint {j in COLS}:<br />
v
Tasks<br />
1. Download the file rowplayer.mod from the OPRE355 Labs webpage <strong>and</strong> save it in your<br />
opre355 directory.<br />
2. Type the GMPL Data above into a text file <strong>and</strong> save is as birthday.dat in your opre355<br />
directory.<br />
3. Solve Frank’s LP model using glpsol --model rowplayer.mod --data birthday.dat<br />
<strong>and</strong> compare with the previous solution.<br />
4. Adapt the GMPL model given above to solve the column player’s LP. Save it in a<br />
different file called columnplayer.mod in your opre355 directory. You may wish to copy<strong>and</strong>-paste<br />
some <strong>of</strong> the model from rowplayer.mod.<br />
5. Solve the Nature’s LP usingglpsol --model columnplayer.mod --data birthday.dat<br />
6. Suppose Frank is in a whole lot more trouble than he expected if he does not buy flowers<br />
on Kitty’s birthday, i.e., supposethe pay<strong>of</strong>f is now −100. What is the effect on the optimal<br />
strategy <strong>and</strong> the value <strong>of</strong> the game?<br />
OPRE 355, 2012 21 Lab 1
5 Practice Problems<br />
1 Metalworking shop. A metalworking shop needs to cut at least 37 large disks <strong>and</strong> 211 small<br />
ones from sheet metal rectangles <strong>of</strong> a st<strong>and</strong>ard size. Three cutting patterns are available. One<br />
yields 2 large disks with 34% waste, the second gives 5 small disks with 22% waste, <strong>and</strong> the last<br />
produces 1 large <strong>and</strong> 3 small disks with 27% waste. The shop seeks a minimum waste way to<br />
fulfill its requirements.<br />
(a) Formulate an LP model to choose an optimal cutting plan.<br />
(b) Enter <strong>and</strong> solve your model with GMPL.<br />
(c) What happens if we insist that all variables are integers?<br />
2 Knights, knaves <strong>and</strong> werewolves. Suppose you are visiting a forest in which every inhabitant<br />
is either a knight or a knave. Knights always tell the truth <strong>and</strong> knaves always lie. In addition<br />
some <strong>of</strong> the inhabitants are werewolves <strong>and</strong> have the annoying habit <strong>of</strong> sometimes turning into<br />
wolves at knight <strong>and</strong> devouring people. A werewolf can be either a knight or a knave.<br />
You are interviewing three inhabitants, A, B, <strong>and</strong> C, <strong>and</strong> it is known that exactly one <strong>of</strong> them<br />
is a werewolf. They make the following statements:<br />
A: I am a werewolf.<br />
B: I am a werewolf.<br />
C: At most one <strong>of</strong> us is a knight.<br />
Formulate(<strong>and</strong>solve) anIPmodelinGMPLtodetermineacompleteclassification <strong>of</strong>inhabitants<br />
A, B <strong>and</strong> C.<br />
3 Swim team. The coach needs to assign swimmers to a 50-metre medley relay team to send<br />
to the University Games. Since most <strong>of</strong> his best swimmers are very fast in more than one<br />
stroke, it is not clear which swimmer should be assigned to each <strong>of</strong> the four strokes. The five<br />
fastest swimmers <strong>and</strong> the best time (in seconds) they have achieved in each <strong>of</strong> the strokes (for<br />
50 metres) are given below.<br />
Stroke Danyon Anthony Moss Paul Malcolm<br />
Backstroke 37.7 32.9 33.8 37.0 35.4<br />
Breaststroke 43.4 33.1 42.2 34.7 41.8<br />
Butterfly 33.3 28.5 38.9 30.4 33.6<br />
Freestyle 29.2 26.4 29.6 28.5 31.1<br />
The coach wishes to determine how to assign four swimmers to the four different strokes to<br />
minimize the sum <strong>of</strong> the corresponding best times.<br />
OPRE 355, 2012 22 Lab 1<br />
lp165<br />
ip027
(a) Formulate the coach’s assignment problem by writing a suitable GMPL data file (please<br />
call it swimteam.dat) to be used with the GMPL model assignment.mod.<br />
Hint. First decide what are the machines <strong>and</strong> what are the jobs.<br />
(b) Solve the problem using glpsol <strong>and</strong> interpret your solution.<br />
4 <strong>School</strong> Buses. The city <strong>of</strong> Springfield is taking bids from six bus companies (A, B, C, D, E<br />
<strong>and</strong> F) on the eight routes (R1, R2, R3, R4, R5, R6, R7 <strong>and</strong> R8) that must be driven in the<br />
surrounding school district. Each company enters a bid (in $) on how much it will charge to<br />
drive selected routes, although not all companies bid on all routes. Blank cells indicate routes<br />
on which a company does not bid.<br />
Company R1 R2 R3 R4 R5 R6 R7 R8<br />
A 8200 7800 5400 3900<br />
B 7800 8200 6300 3300 4900<br />
C 3800 4400 5600 3600<br />
D 8000 5000 6800 6700 4200<br />
E 7200 6400 3900 6400 2800 3000<br />
F 7000 5800 7500 4500 5600 6000 4200<br />
Thecity needs to select which companies to assign to which routes with the following conditions.<br />
(1) If a company does not bid on a route, it cannot by assigned to that route.<br />
(2) Exactly one company must be assigned to each route.<br />
(3) A company can be assigned to at most two routes.<br />
The objective is to minimize the total cost <strong>of</strong> covering all routes.<br />
Tasks<br />
(a) Briefly explain why Springfield’s problem is not an assignment problem.<br />
(b) Adapt theGMPL modelassignment.modto make thechanges required in part (a). Please<br />
call your new model doubleassignment.mod.<br />
(c) Write a suitable GMPL data file called springfield.dat.<br />
(d) Solve Springfield’s problem using<br />
glpsol --model doubleassignment.mod --data springfield.dat<br />
<strong>and</strong> interpret your solution.<br />
OPRE 355, 2012 23 Lab 1<br />
ip023<br />
ip024
5 Two armies are advancing on two cities. The first army is comm<strong>and</strong>ed by General Custard<br />
<strong>and</strong> has four regiments; the second army is comm<strong>and</strong>ed by General Peabody <strong>and</strong> has three<br />
regiments. At each city, the army that sends more regiments to the city captures both the city<br />
<strong>and</strong> the opposing army’s regiments. If both armies send the same number <strong>of</strong> regiments to a city,<br />
then the battle at the city is a draw. Each army scores 1 point per city captured <strong>and</strong> 1 point<br />
per captured regiment. Assume that each army wants to maximize the difference between its<br />
reward <strong>and</strong> its opponent’s reward.<br />
(a) Formulate this situation as a two-person zero-sum game.<br />
(b) Determine whether this game is strictly determined.<br />
(c) Solve (using GMPL or R) for the value <strong>of</strong> the game <strong>and</strong> each player’s optimal strategies.<br />
game001<br />
6 Morra. Each player extends some fingers <strong>and</strong>, simultaneously, guesses how many the enemy<br />
is extending. Thenumberhemay extend is 1, 2, or 3. If only one player guesses the enemy digits<br />
successfully, the pay<strong>of</strong>f to him is the total number <strong>of</strong> digits extended on the play. Otherwise,<br />
the pay<strong>of</strong>f is zero. (Payments are usually in coin, rather than fingers.) Thus if Blue holds out 3<br />
fingers <strong>and</strong> guesses 1, while Red holds out 1 <strong>and</strong> guesses 2, Blue will win because he is correct;<br />
he will win $4.<br />
Each player may extend 1, 2, or 3 fingers<strong>and</strong> may guess, reasonably, that the enemy is extending<br />
1, 2, or 3. So there are nine possibilities open to him, i.e., nine pure strategies. Letting 〈i,j〉<br />
represent the strategy <strong>of</strong> putting out i fingers <strong>and</strong> guessing the opponent has put out j fingers,<br />
the appropriate reward matrix is shown below.<br />
Blue’s Red’s Strategy<br />
Strategy 〈1,1〉 〈1,2〉 〈1,3〉 〈2,1〉 〈2,2〉 〈2,3〉 〈3,1〉 〈3,2〉 〈3,3〉<br />
〈1,1〉 0 2 2 −3 0 0 −4 0 0<br />
〈1,2〉 −2 0 0 0 3 3 −4 0 0<br />
〈1,3〉 −2 0 0 −3 0 0 0 4 4<br />
〈2,1〉 3 0 3 0 −4 0 0 −5 0<br />
〈2,2〉 0 −3 0 4 0 4 0 −5 0<br />
〈2,3〉 0 −3 0 0 −4 0 5 0 5<br />
〈3,1〉 4 4 0 0 0 −5 0 0 −6<br />
〈3,2〉 0 0 −4 5 5 0 0 0 −6<br />
〈3,3〉 0 0 −4 0 0 −5 6 6 0<br />
(a) Solve the game by writing a GMPL Data file <strong>and</strong> solving both the row player’s <strong>and</strong> the<br />
column player’s LP models.<br />
(b) Interpret the optimal solution. game020a<br />
OPRE 355, 2012 24 Lab 1