You are on page 1of 20

Business Information Systems

Adam Kasperski
Discrete Optimization and Network Flows (laboratory 15 h)

Project co-financed by the European Union within


the European Social Fund

A mixed integer linear programming problem is of the following form:


min(max)

n
X

j=1
n
X

c j xj

[linear objective function]

aij xj = (, )bi i = 1, . . . , m [linear constraints]

j=1

x j Dj

j = 1, . . . , n [domains of variables]

The problem consists of:


1. Decision variables x1 , . . . , xn . Each variable xj has some domain Dj , where Dj
can be the set of reals, integers or binary values.
2. An objective function, which has to be minimized or maximized.
3. A set of m linear constraints, which can take the form of equalities or inequalities.
4. Input data, that is all the constants cj , aij and bi .
Together, the decision variables, objective function and constraints define a model.
The model can be solved and an optimal solution can be reported after providing
some input data. In modern optimization languages, the model can be separated
from the input data. So we can use the same model for many different instances of
input data.

During the laboratory we will use the GNU GLPK optimization package, which
contains the glpsol solver and the GNU MathProg modeling language. This package
was designed by Andrew Makhorin from the Department for Applied Informatics,
Moscow Aviation Institute, Moscow, Russia. The software is free and can be downloaded from http://www.gnu.org/software/glpk/. We will also use the
free editor GUSEK (GLPK Under SciTE Extended Kit), which can be downloaded
from: http://gusek.sourceforge.net/gusek.html
Exercise. Download and install GLPK and GUSEK. Read the user guide Modeling
Language GNU MathProg provided with GLPK.

Example 1. Solve the following problem:


min 3x1 + 2x2 + 3x3
2x1 + 4.5x2 + 2x3 120
2x1 + 3x2 x3 100
x1 , x2 0
x3 0 integer
We can input this model directly in the following way:
#decision variables
var x1 >=0;
var x2 >=0;
var x3 >=0, integer;
#objective function
minimize obj: 3*x1+2*x2+3*x3;
#constraints
c1: 2*x1+4.5*x2+2*x3<=120;
c2: 2*x1+3*x2-x3>=100;
#solve the model and display the results
solve;
display x1, x2, x3;
end;
Exercise. Input and solve the model using GUSEK and glpsol invoked from the command line (remark: the input file should have the extension .mod).

We can separate the model from the input data in the following way:
#input data
param c{1..3};
param a1{1..3};
param a2{1..3};
param b{1..2};
#decision variables
var x1 >=0;
var x2 >=0;
var x3 >=0, integer;
#objective function
minimize obj: c[1]*x1+c[2]*x2+c[3]*x3;
#constraints
c1: a1[1]*x1+a1[2]*x2+a1[3]*x3<=b[1];
c2: a2[1]*x1+a2[2]*x2+a2[3]*x3>=b[2];
solve;
display x1, x2, x3;
end;
#input data (can be placed in a separate file)
data;
param c:= 1 3
2 2
3 3;
param a1:= 1 2
2 4.5
3 2;
param a2:= 1 2
2 3
3 -1;
param b:= 1 120
2 100;
end;
Exercise. Solve the model with the input data provided in a separate file.

Example 2. Suppose that we have to choose precisely p items out of n available. The
ith item has a value of vi . Which items should we choose to maximize the total
value? This model has the following form:
max

n
X

v i xi

i=1

n
X

xi = p

i=1

xi {0, 1} for i = 1, . . . , n

The binary variable xi takes the value 1 if and only if the ith item is chosen.
#input data
param n >0, integer;
param p >0, integer;
param v{1..n};
#decision variables
var x{1..n} binary;
#objective function
maximize obj:sum{i in 1..n} v[i]*x[i];
#constraints
c1: sum{i in 1..n} x[i]=p;
#solve the model and display results
solve;
display x;
data;
#provide input data
end;
Exercise 1. Solve the model for n = 10, v = [2, 1, 4, 5, 3, 2, 8, 9, 1, 1] and p = 5.
Exercise 2. Generalize the model from Example 1 to n variables.

Example 3 [diet problem]. Consider a problem of buying foods to meet certain nutritional requirements. We have a set of different kinds of food F . The price per unit
of the jth food, j F , is pj . We also have a set of nutrients N . The nutritional
requirement for nutrient i N is bi . Let aij be the amount of the ith nutrient in one
unit of the jth food. We have to decide how many units of each food to buy in order
to satisfy the nutritional requirements.
Suppose xj units of the jth food are bought, where j F . This model has the
following form:
X
min
pj x j
F
jF
X

aij xj bi i N

F
jF

xj 0

j F

#input data
set F;
set N;
param p{F} >=0;
param b{N} >=0;
param a{N,F} >=0;
#decision variables
var x{F}>=0;
#objective function
minimize obj:sum{j in F} p[j]*x[j];
#constraints
Nutrient{i in N}: sum{j in F} a[i,j]*x[j]>=b[i];
#solve the model and display results
solve;
display x;
data;
#provide input data
end;

A sample input data are given below (notice how the matrix of coefficients aij is
defined):
set F:= milk, chocolate, bread;
set N:= vitamin A, vitamin B;
param p:= milk 10
chocolate 20
bread 30;
param b:= vitamin A 120
vitamin B 300;
param a:
milk chocolate bread :=
vitamin A
2
4
0
vitamin B
0
1
2;
Exercise. Provide some additional kinds of food and nutrients and solve the model.

Example 4. We are given a set of cities C and a symmetric square matrix d, where
dij is the distance between cities i C and j C . One of the cities is distinguished
as the capital. Find all the cities, whose distance to the capital is not greater than a
given number K. Find a central city, i.e. the city for which the sum of distances to
all other cities is minimized.
Let xi {0, 1} for i C and xi = 1 if and only if i is the central city. This model
has the following form:
X X
min
(xi
dij )
C
iC
X

C
jC

xi = 1

C
iC

xi {0, 1}

iC

# input data
set C;
param d{C,C} >=0;
param K >=0;
param capital symbolic in C;
#the set of cities close to the capital
set CM:={y in C: d[y,capital]<=K};
#decision variables
var x{C} binary;
#objective function
minimize obj:sum{i in C}(x[i] * sum{j in C} d[i,j]);
#constraints
c1: sum{i in C} x[i]=1;
#solve the model and display results
solve;
display CM, x;
data;
#sample input data
end;
Exercise. Provide sample input data and solve the model.

List of exercises 1
1. Solve the following problems:

min 3x1 + 2x2


x1 + 2x2 100
(a)
2x1 x2 180
x1 , x2 0
max
(c)

100
X

i=1
100
X

max 2x1 + 2x2 + 3x3


6.5x1 + 2x2 + x3 1300
(b)
1.6x1 3x2 + 2.3x3 = 500
x1 , x2 , x3 0
x2 , x3 integer

ixi
ixi 1300

max
(d)

i=1

x1 , . . . , x100 0, integer

10
X

xi

i=1

xi 2xi+1 i = 1, . . . , 9
x1 , . . . , x10 0, integer

2. Define sets: Z = {1, 2 . . . 20}, A = {1, 3, 6, 5, 7}, B = {5, 6, 8, 1, 9}, M =


{John, Thomas, Gregory, Mark}, F = {Ann, Mary, Eva, Susan}. Define and
display the following sets:
(a) A B , A B , Z \ B , M F
(b) {x Z : 3 x 10},
(c) {(Thomas, Mary), (Mark, Susan)} M F ,
(d) {(x, y) Z Z : x + y = 5},
3. There are n packs numbered from 1 to n. The ith pack weighs wi and its value
is equal to vi . A car can take a subset of the packs of total weight at most W .
Which packs should be chosen to maximize their total value? Build a model and
solve it for sample data.
(a) Suppose that you have a set of pairs of packs such that if packs i and j form
a pair, then they cannot be both chosen. Take this set into account in the
problem.
(b) Suppose that you have a set of pairs of packs so that when i and j form a pair
and i is chosen, then j must also be chosen. Take these pairs into account in
the problem.

4. You wish to organize a party and you want to invite some boy/girl pairs so that
every boy has one girlfriend and every girl has one boyfriend. You have two lists
of potential guests: a list of boys B and a list of girls G . You know the numbers
sij {0, . . . , 10}, which denote the degree of friendship between boy i B
and girl j G (the larger is the number the greater is the friendship). Which
pairs should be invited so that the total degree of friendship of all invited pairs
would be maximal? Build a model and solve it for sample data. Next, take into
account the following modifications:
(a) If sij < 2, then boy i and girl j cannot be a pair at the party.
(b) Find boys (girls), for whom there is no girl (boy) with degree of friendship
greater than 3 and exclude them from the party.
5. Given is a nn chessboard. How many queens can be placed on this chessboard
so that none of them is able to capture any other using the standard chess queens
moves. Namely, the queens must be placed in such a way that no two queens
would be able to attack each other.

Example 5. Solve the following minimum cost flow problem:

We start by modeling the input network. We define the set of nodes N and the set of
arcs A. Notice that the set of arcs is a subset of the Cartesian product of the set of
nodes, i.e. A N N :
#The set of nodes and the set of arcs
set N;
set A within N cross N;
Next, we define the arc costs, arc capacities and node supply/demands:
#Declaration of parameters
param c{A};
param u{A} >=0;
param b{N};
We can now define the sample network in the data section:
#Data section
data;
param N:=1,2,3,4,5;
param: A: c u := 1,2
1,3
2,3
2,4
3,1
3,4
3,5
4,5
param b:= 1 5
2 0
3 0
4 -2
5 -3

5
2
1
2
8
4
1
3

9
4
6
4
3
4
9
7

In order to solve the problem, we define the set of direct successors S(i) and the set
of direct predecessors P (i) of each node i N . In the sample network we have for
example S(2) = {3, 4} and P (2) = {1}.
#Direct successors and direct predecessors
set S{i in N}:={j in N: (i,j) in A};
set P{i in N}:={j in N: (j,i) in A};
We define nonnegative decision variables denoting the arc flows:
#The variables denoting the arc flows
var x{A} >=0;
The objective function is the following:
#Objective function
minimize obj: sum{(i,j) in A} c[i,j]*x[i,j];
Finally, we add constraints for each node:
#Constraints
c1{i in N}: sum{j in S[i]} x[i,j]-sum{j in P[i]} x[j,i]=b[i];
c2{(i,j) in A}: x[i,j]<=u[i,j];
Exercise 1. Solve the model for the sample network.
Exercise 2. Modify the model to compute a shortest path from a given node s to a
given node t.
Exercise 3. Modify the model to compute the maximum flow from a source node s
to a sink node t.

Example 6. A factory produces some product and it wants to meet prescribed demand
dj for each of K periods, j = 1, 2, . . . , K, by either producing up to aj in period j
and/or by drawing upon the inventory Ij1 carried from the previous period. The
unit production cost in the jth period is equal to cj and the unit inventory cost in the
jth period is equal to mj . We assume that up to I units can be stored between the
periods. The factory wants to establish an optimal production plan. The solution to
this problem can be represented as the following minimum cost flow problem:

W first declare the input data:


#Input data
param K>0;
param a{1..K};
param c{1..K};
param d{1..K};
param m{1..K-1};
param I>0;
We then build the network:
#Network
set V:={0..K};
set A:={0} cross {1..K} union {i in {1..K-1}, i+1};
param cc{(i,j) in A}:= if (i==0) then c[j] else m[i];
param u{(i,j) in A}:= if (i==0) then a[j] else I;
param b{i in V}:= if (i==0) then sum{j in 1..K} d[j] else
-d[i];
Exercise. Complete the model (this is a minimum cost flow model) and solve it using
sample data.

Example 7.1 We are given a labyrinth of size n n. On each square of the labyrinth
a corridor or a wall is located. We can move to the right, left, up or down using
only corridor boxes. The entrance to the labyrinth is located in square [n 1, 1] and
the exit is located in square [2, n]. We would like to find a shortest path from the
entrance to the exit of this labyrinth. A sample labyrinth of size 11 11 together
with a shortest path from the entrance to the exit is shown below:
1

...

11

1
2

...

11

We encode every labyrinth as an n n binary matrix lab, where labij = 0 if there is


a corridor on square [i, j] and labij = 1 if there is a wall on square [i, j].
We first define the size of the labyrinth n and the n n matrix describing its squares:
#The size and the squares of the labyrinth
param n >0;
param lab{n,n};
set entry:={(n-1,1)};
set exit:= {(2,n)};
Having defined the matrix lab, we build a directed network G = (N, A) whose nodes N are corridor boxes of the labyrinth and the arcs A represent all of the possible
moves between corridor boxes. We first place a node of G in every square containing
a corridor:
#The nodes
set N:={i in 1..n, j in 1..n:

lab[i,j]==0};

We then define the set of arcs A N N in the following way:


#The arcs
set S{(i,j) in N }:={(k,l) in N: i==k and l==j+1} union
{(k,l) in N: i==k and l==j-1} union
{(k,l) in N: j==l and i==k+1} union
{(k,l) in N: j==l and i==k-1};
set A:={(i,j) in N,(k,l) in S[i,j]};

1 Advanced

example

The idea of the construction of G is shown below. The node (i, j) is added to N and
the arcs (i, j, i 1, j), (i, j, i, j + 1) and (i, j, i + 1, j) are added to A.

We define decision variables xijkl 0 for all (i, j, k, l) A, where xijkl = 1 if we


move from corridor box (i, j) to corridor box (k, l):
#Decision variables
var x{A} >= 0;
The model is then the standard shortest path formulation:
#Objective function
minimize obj: sum{(i,j,k,l) in A} x[i,j,k,l];
#Constraints
r{(i,j) in N}: sum{(i,j,k,l) in A} x[i,j,k,l] - sum{(k,l,i,j)
in A} x[k,l,i,j] =
if ((i,j) in enter) then 1
else if ((i,j) in exit) then -1
else 0;
In order to see the solution we can write:
display x;

We would like to see a visualization of the path obtained. This requires a little bit
more effort. We first define the set of boxes, which are used by the path:
#The boxes (nodes) used by the path
set occupied:= entry union exit union
{(i,j) in N: exists{(k,l) in S[i,j]} x[i,j,k,l]==1};
and visualize the path in the following way:
#Printing the path
for{i in 1..n}{
for{j in 1..n} printf "%s", if (i,j) not in N then " X "
else if (i,j) in occupied then " P "
else " . ";
printf "\n"; }
After solving the sample instance, we get the following result:
X
X
X
X
X
X
X
X
X
P
X

X
.
.
.
.
.
.
.
X
P
X

X
X
.
X
X
X
.
.
P
P
X

X
.
.
X
.
X
X
X
P
.
X

X
.
.
X
P
P
P
P
P
.
X

X
.
.
X
P
X
.
.
X
.
X

X
.
X
X
P
X
X
X
X
.
X

X
.
.
X
P
.
.
.
X
.
X

X
.
X
X
P
.
X
.
X
.
X

X
P
P
P
P
X
X
.
.
.
X

X
P
X
X
X
X
X
X
X
X
X

List of excercises 2
1. We are given a set of cities and the set of all possible connections between them.
These sets are modeled as a directed network G = (N, A), where N is the set of
cities and A is the set of direct connections between cities. For each connection
(i, j) A a nonnegative distance dij is provided. We wish to find a shortest
directed path between two chosen cities. Consider the following modification of
the problem:
Suppose that for each connection (i, j) A we also know the corresponding
travel time tij and the total travel time of the computed path cannot be greater
than T .
2. We are given a set of suppliers D , a set of customers C and a set of stores M .
Each supplier i D can send si units and each customer j C requires di
units of some product.
We assume that each store has an unlimited capacity.
P
P
We assume that si =
di , so the problem is balanced. All possible connections between suppliers, customers and stores are modeled as a directed network
N = (N, A), where N = D C M . Each connection (i, j) A has a transportation cost per unit of cij and a capacity uij . We wish to find a cheapest way
of transporting the product from the suppliers to the customers.
3. A car factory wants to establish a production plan for the next K periods. In
every period the customers demand is equal to Q and it must be fully satisfied.
Producing j cars costs cj , where j = 0, . . . , 2Q. Unsold cars can be stored at a
cost of ml per unit in periods l = 1, . . . , K. We assume that up to Q cars can be
stored. How many cars should the factory produce in each period to minimize
the total cost?
4. A factory wants to open m stores. The factory considers n places, so that n > m.
In place i a store with capacity Ci can be built. The distance between places i
and j is equal to dij . The factory wants to open m stores to maximize their total
capacity. However, the stores should be located so that the distance between any
pair of open stores is not greater than K. Build a model for this problem.

Example 8. We are given an undirected network G = (N, A) with cost ci specified


for each node i N . We say that a subset W of the nodes covers all the arcs of G
if for every arc (i, j) A either i W or j W . The total cost of W is equal to
P
iW ci . We would like to find a subset that covers all the arcs at the minimum total
cost. An example is shown in the figure below. The nodes in red form an optimal
covering of total cost equal to 9.
2
A

1
B
3

7
F

3 D

G
E
1

We introduce a binary variable xi {0, 1} for each node i N . Then xi = 1 if and


only if node i belongs to the covering. The model has the following form:
X
min
ci xi
iN

xi + xj 1 for all (i, j) A


xij {0, 1} for all (i, j) A
#declare input data
set N;
set A within N cross N;
param c{N};
#declare decision variables
var x{N} binary;
#objective function
minimize obj: sum{i in N} c[i]*x[i];
#constraints
r{(i,j) in A}: x[i]+x[j] >= 1;
#solve the model and display results
solve;
display x;
#provide input data
data;
end;
Exercise. Solve the problem for a sample network.

Example 9. We are given a directed network G = (N, A). Each arc has a cost cij ,
(i, j) A. We would like to find a shortest traveling salesperson tour in G, i.e. the
the shortest directed path that starts at node 1, visits all the remaining nodes of G
exactly once and finishes at 1.
Let xij {0, 1} be a binary variable such that xij = 1 if and only if we move
from node i to node j in a given tour. The cost of the tour can be then expressed as
P
(i,j)A cij xij . Since we must enter every node exactly once and leave every node
exactly once we add the following constraints:
X
xij = 1 for all i N
(1)
{j:(i,j)A}

and
X

xji = 1 for all i N

(2)

{j:(j,i)A}

These constraints, however, are not enough. Using only them, we may get a solution
composed of several disjoint cycles. In order to prohibit such solutions, we use the
following idea. Suppose that at node 1 we have n = |N | 1 packs and exactly one
pack must be delivered from 1 to every other node in the network. Let yij 0 be the
flow (number of packs) along arc (i, j) A. Then the following constraints must be
satisfied:

X
X
n 1 if i = 1
yij
yji =
(3)
1
if i 6= 1
{j:(i,j)A}

{j:(j,i)A}

Finally, we can send the packs only along arcs used by the tour, so:
yij (n 1)xij for all (i, j) A

(4)

Exercise 1. Give an example showing that constraints (1)-(2) may not be sufficient
to describe a tour.
Exercise 2. Prove that constraints (1) - (4) are satisfied if and only if variables xij
describe a tour.
Exercise 3. Write the model in the MathProg language and solve it using sample
data.
Exercise 4. Check what sized problems (measured by the number of nodes) can be
solved in reasonable time using this model. Use complete networks with random
costs.

List of exercises 3
1. An area of some factory is divided into m n squares. Some squares are occupied by valuable packs (we assume that every square may be occupied by at
most one pack). We must place some cameras in the area to observe the packs.
Every camera can observe r squares to the left, r squares to the right, r squares
up and r squares down. It is prohibited to place cameras on the squares occupied
by packs. Where should we place cameras so that every pack is observed and
the total number of cameras is minimized?
2. In some area there is a set C of cities with a symmetric matrix D containing
travel times (in minutes) between each pair of cities. For each city i C there is
a number ui denoting the number of citizens in i. The government wants to build
k fire stations and each fire station must be placed in a different city. The fire
stations should be placed so that the traveling time from each city to the nearest
fire station is not greater than 10 minutes. Because the budget is limited, it may
be hard to satisfy this demand. Where should the fire stations be placed so that
the number of citizens for whom no fire station is reachable within 10 minutes
is minimal?
3. As a student of Wroclaw University of Technology, you wish to create a timetable for the next semester. Lectures take place from Monday to Friday, from
7:00 to 17:00 in two-hour blocks (7-9, 9-11, 11-13, 13-15, 15-17, 17-19). There
is a set of courses and you must choose at least one group for each course. You
cannot take more than 8 hours a day. The university proposes you some set of
groups for each course and you can see it as a list, e.g.: (Mathematics, Mo, 79), (Mathematics, Tu, 9-11), (Physics, We, 13-15),.... Of course, some groups
are more attractive to you than others. So, you assign each group a preference,
which is a number from 1 to 10. Build a feasible timetable of maximum total
preference.
4. We are given an undirected graph G = (V, E). A cut in G is a partition V =
V1 V2 such that V1 V2 = . Let u(V1 , V2 ) denote the number of arcs with one
endpoint in V1 and one endpoint in V2 . Determine a cut, for which the value of
u(V1 , V2 ) is maximized.
5. We are given an undirected graph G = (V, E). We wish to assign a color to each
node of the graph so that no two adjacent nodes share the same color. Find such
a coloring using the minimum number of distinct colors.

You might also like