Write a program that adds a feasible solution to given supply vector b ∈ R7. ˆ Model this problem as a linear program (LP) and write the corresponding .lp le.

COMP331/557 Programming Assignment

Routing and Production
Consider the following directed pipeline network that is modeled as a directed graph.
F B
C C
A F A
We route four dierent commodities a, b, x, y through the pipes. No commodity can ever ow against the indicated arc direction. Dierent commodities can be sent through the same pipe. For each arc, the total amount of all commodities that can ow through this pipe is bounded by 12 barrels.
The nodes that are labelled with A can produce arbitrary amounts of commodity a. The node that is labelled with B can produce arbitrary amounts of commodity b.
At each node that is not labelled with either A, B, C, or F , the amount of incoming commodity a equals the amount of outgoing commodity a. The same is true for the commodities b, x, y.
Nodes labelled with an F are called factories. Flow is forwarded in the same way as for other nodes, but a factory can convert commodities as follows before forwarding them.
ˆ To produce 1 barrel of commodity x, a factory uses up 3 barrels of a and 2 barrel of b.
ˆ To produce 1 barrel of commodity y, a factory uses up 1 barrel of a and 2 barrels of b.

Factories are not limited to integer barrel amounts. Moreover, factories are not limited to only one way of conversion. For example, a factory can use 3.5 barrels of a and 3 barrels of b to produce 1 barrels of x and 0.5 barrel of y.

Nodes labelled with C are called customers. A customer pays £3 for a barrel of commodity x, and £4 for a barrel of commodity y. Customers buy as much of commodity x and y as they can. Fractional barrel amounts are acceptable. Customers never buy commodity a or b.
We enumerate the 15 arcs as follows:

F B

C C
A F A1 2 3 4 5
6 7 8 9
10 11 12 13
14 15
For each are introduce 4 variables that represent how much of the 4 commodities (a, b, x, and y) is sent through that arc. For example, the 4 variables for arc 1 are a1, b1, x1, and y1. The 4 variables for arc 15 are a15, b15, x15, and y15 The LP contains the obvious ow conservation and capacity constraints.
The optimisation problem is to maximise the amount of money that the customers pay.
ˆ Model this problem as a linear program (LP) and write the corresponding assignment.lp le. [Marks: 10]
ˆ Use the glpsol to nd an optimal solution: Run the command glpsol lp assignment.lp -o out-put.txt”. [Marks: 5]

COMP331/557 Lab Exercises
These exercises prepare you for the COMP331/557 programming assignment. (This lab session was originally designed by Christian Ikenmeyer)

Lab Exercise 1 – Supply and Demand
A directed graph G = (V, A) consists of a set of vertices V and a set of arcs A V × V . Pictorially, V can be represented as points and A as arrows between points, for example like this:
1
2
4
3 5
6
7
For each edge from i to j, dene a variable xi,j with the constraints 0 xi,j 1.
For every vertex v, let inv denote the set of arcs pointing at v. For example, in3 = {(2, 3), (5, 3)}. Let outv denote the set of arcs starting at v. For every v V we have a variable bv that we call the supply of v (negative supply is possible and is called demand ). For every vertex v we impose the so-called ow balance constraints X
woutv
xw X
winv
xw = bv.
Write a program that adds a feasible solution to given supply vector b R7. You can use the following python3 snippets:
graph = {
1 : [2,4],
2 : [3],
3 : [4,5],
4 : [1],
5 : [3,5,6,7],
6 : [],
7 : [],
}
import os
import re
def run_glpsol():
os.system(“glpsol –lp supply_demand.lp -o output.txt”)
outputfile = open(“output.txt”, “r”)
opt_value = None
for line in outputfile.readlines():
if “Objective” in line:
opt_value = float((re.findall(“[0-9]+[,.]*[0-9]*” , line))[0])
if “PRIMAL SOLUTION IS INFEASIBLE” in line:
opt_value = None
outputfile.close()
if opt_value == None:
raise Exception(“LP is unsolvable.”)
return opt_value
One way of solving it is as follows.
import os
import re

graph = {

1 : [2,4],
2 : [3],
3 : [4,5],
4 : [1],
5 : [3,5,6,7],
6 : [],
7 : [],
}
b = {
1 : 1,
2 : 0,
3 : 0.5,
4 : -1,
5 : 1,
6 : -1,
7 : -0.5,
}
def run_glpsol():
os.system(“glpsol –lp supply_demand.lp -o output.txt 1> /dev/null”)
outputfile = open(“output.txt”, “r”)
opt_value = None
for line in outputfile.readlines():
if “Objective” in line:
opt_value = float((re.findall(“[0-9]+[,.]*[0-9]*” , line))[0])
if “PRIMAL SOLUTION IS INFEASIBLE” in line:
opt_value = None
outputfile.close()
if opt_value == None:
raise Exception(“LP is unsolvable.”)
return opt_value
f = open(“supply_demand.lp”, “w”)
f.write(“Maximize x0\n”)
f.write(“Subject To\n”)
f.write(“x0 = 0\n”)
s = “”
for from_vertex in graph:
for to_vertex in graph[from_vertex]:
s += “x_”+str(from_vertex)+”_”+str(to_vertex)+” >= 0\n”
s += “x_”+str(from_vertex)+”_”+str(to_vertex)+” <= 1\n”
f.write(s)
#definition of out_v
for vertex in graph:
s += “out_”+str(vertex)
for neighbor in graph[vertex]:
s += ” – x_”+str(vertex)+”_”+str(neighbor)
s += ” = 0\n”
f.write(s)
#definition of in_v
for vertex in graph:
s += “in_”+str(vertex)
for neighbor in graph:
if vertex in graph[neighbor]:
s += ” – x_”+str(neighbor)+”_”+str(vertex)
s += ” = 0\n”
f.write(s)
#flow balance constraints:
for vertex in graph:
s +=”out_”+str(vertex)+” – in_”+str(vertex)+” = “+str(b[vertex])+”\n”
f.write(s)
f.write(“End\n”)
f.close()
try:
run_glpsol()
os.system(“cat output.txt | grep x_”)
except Exception:
print(“Infeasible Supply-Demand vector”)
This is the output from which one can read the ow values:
2 x_1_2 B 1 0
3 x_1_4 NL 0 0 < eps
4 x_2_3 B 1 0
5 x_3_4 B 1 0
6 x_3_5 B 0.5 0
7 x_4_1 B 0 0
8 x_5_3 NL 0 0 < eps
9 x_5_5 NL 0 0 < eps
10 x_5_6 B 1 0
11 x_5_7 B 0.5 0

Lab Exercise 2 – Routing and Production
Consider the following directed pipeline network that is modelled as a directed graph.
F B
C C
A F A
We route four dierent commodities a, b, x, y through the pipes. No commodity can ever ow against the indicated arc direction. Dierent commodities can be sent through the same pipe. For each arc, the total amount of all commodities that can ow through this pipe is bounded by 10 barrels.
The nodes that are labelled with A can produce arbitrary amounts of commodity a. The node that is labelled with B can produce arbitrary amounts of commodity b.
At each node that is not labelled with either A, B, C, or F , the amount of incoming commodity a equals the amount of outgoing commodity a. The same is true for the commodities b, x, y.
Nodes labelled with an F are called factories. Flow is forwarded in the same way as for other nodes, but a factory can convert commodities as follows before forwarding them.
ˆ To produce 1 barrel of commodity x, a factory uses up 2 barrels of a and 1 barrel of b.
ˆ To produce 1 barrel of commodity y, a factory uses up 1 barrel of a and 3 barrels of b.

Factories are not limited to integer barrel amounts. Moreover, factories are not limited to only one way of conversion. For example, a factory can use 2 barrels of a and 3.5 barrels of b to produce 0.5 barrels of x and 1 barrel of y.
Nodes labelled with C are called customers. A customer pays £2 for a barrel of commodity x, and £3 for a barrel of commodity y. Customers buy as much of commodity x and y as they can. Fractional barrel amounts are acceptable. Customers never buy commodity a or b.
The optimisation problem is maximise the amount of money that the customers pay.
ˆ Model this problem as a linear program (LP) and write the corresponding .lp le.
ˆ Use the glpsol to nd an optimal solution.
ˆ Present your solution in a style similar to the way that the example on the next page is presented.
A feasible (but not optimal) solution is illustrated in the gure on the next page.
F B
C C
A F Aa = 2 x = 1 a = 2
b = 3.5 a = 2
a = 2, x = 1 x = 1 x = 0.5, y = 1 b = 3.5
a = 2 b = 1
b = 1
x = 0.5
y = 1
b = 1
x = 1 x = 0.5, y = 1
It is readily veried that this solution satises all constraints. In particular, on each arc the sum of the amounts of commodities never exceeds the arc capacity 10.
The customers in this example pay 1 · 2 + 0.5 · 2 + 1 · 3 = 6 pounds.
One way of solving it is as follows.
We could again use the graph structure from exercise 1. Instead we present an alternative direct approach.
We enumerate the 15 arcs, for example as follows:
F B
C C
A F A1 2 3 4 5
6 7 8 9
10 11 12 13
14 15
For each are we introduce 4 variables that represent how much of the 4 commodities is sent through that arc. The LP contains the obvious ow conservation and capacity constraints. The interesting parts are the factory nodes. They are handled as follows:

Let v be a factory node. Let Ain be the set of arcs pointing into v and let Aout be the set of arcs pointing out of v. For the sake of a simplied notation, let ain :=P eAin a(e), and analogously for aout, bin, bout, xin, xout, yin, yout. We make a base change to 8 quantities that describe the factories behaviour. They are all nonnegative (for 4 of them, nonnegativity is already implied by the nonnegativity constraints on the arcs):
ˆ aused := ain aout is the amount of a that is used up to produce commodities at v.
ˆ areroute := ain aused = aout is the amount of a that is forwarded without being used up at v.
ˆ bused := bin bout is the amount of b that is used up to produce commodities at v.
ˆ breroute := bin bused = bout is the amount of b that is forwarded without being used up at v.
ˆ xnew := xout xin is the amount of x that is produced at v.
ˆ xreroute := xout xnew = xin is the amount of x that is forwarded but not created at v.
ˆ ynew := yout yin is the amount of y that is produced at v.
ˆ yreroute := yout ynew = yin is the amount of y that is forwarded but not created at v.
Instead of the usual 4 ow conservation constraints, at a factory we have 2 degrees of freedom, so we only get 2 equality constraints:
2
1
xnew +
1
3
ynew =
aused
bused
The LP can be modeled as follows (rst constraints are the capacity constraints, then the ow conservation constraints at non-factories, then the equality constraints at factories, and then the nonnegativity constraints at factories).
Maximize
2 x14 + 3 y14 + 2 x15 + 3 y15
Subject To
a1 + b1 + x1 + y1 <= 10
a2 + b2 + x2 + y2 <= 10
a3 + b3 + x3 + y3 <= 10
a4 + b4 + x4 + y4 <= 10
a5 + b5 + x5 + y5 <= 10
a6 + b6 + x6 + y6 <= 10
a7 + b7 + x7 + y7 <= 10
a8 + b8 + x8 + y8 <= 10
a9 + b9 + x9 + y9 <= 10
a10 + b10 + x10 + y10 <= 10
a11 + b11 + x11 + y11 <= 10
a12 + b12 + x12 + y12 <= 10
a13 + b13 + x13 + y13 <= 10
a14 + b14 + x14 + y14 <= 10
a15 + b15 + x15 + y15 <= 10
a1 + a2 – a6 = 0
b1 + b2 – b6 = 0
x1 + x2 – x6 = 0
y1 + y2 – y6 = 0
a3 + a7 – a2 = 0
b3 + b7 – b2 = 0
x3 + x7 – x2 = 0
y3 + y7 – y2 = 0
a5 + a9 – a4 = 0
b5 + b9 – b4 = 0
x5 + x9 – x4 = 0
y5 + y9 – y4 = 0
a8 + a13 – a12 = 0
b8 + b13 – b12 = 0
x8 + x13 – x12 = 0
y8 + y13 – y12 = 0
a12 – a11 – a15 = 0
b12 – b11 – b15 = 0
x12 – x11 – x15 = 0
y12 – y11 – y15 = 0
a6 – a10 – a14 = 0
b6 – b10 – b14 = 0
x6 – x10 – x14 = 0
y6 – y10 – y14 = 0
b1 = 0
x1 = 0
y1 = 0
b5 = 0
x5 = 0
y5 = 0
a9 = 0
x9 = 0
y9 = 0
a13 = 0
x13 = 0
y13 = 0
a3 + a8 + 2 x3 + 2 x8 + y3 + y8 – a4 = 0
b3 + b8 + x3 + x8 + 3 y3 + 3 y8 – b4 = 0
a7 + 2 x7 + y7 – a10 – a11 = 0
b7 + x7 + 3 y7 – b10 – b11 = 0
-a7 + a10 + a11 >= 0
a7 >= 0
-b7 + b10 + b11 >= 0
b7 >= 0
x7 – x10 – x11 >= 0
x10 + x11 >= 0
y7 – y10 – y11 >= 0
y10 + y11 >= 0
– a3 – a8 + a4 >= 0
a3 + a8 >= 0
– b3 – b8 + b4 >= 0
b3 + b8 >= 0
x3 + x8 – x4 >= 0
x4 >= 0
y3 + y8 – y4 >= 0
y4 >= 0
Bounds
a1 >= 0
a2 >= 0
a3 >= 0
a4 >= 0
a5 >= 0
a6 >= 0
a7 >= 0
a8 >= 0
a9 >= 0
a10 >= 0
a11 >= 0
a12 >= 0
a13 >= 0
a14 >= 0
a15 >= 0
b1 >= 0
b2 >= 0
b3 >= 0
b4 >= 0
b5 >= 0
b6 >= 0
b7 >= 0
b8 >= 0
b9 >= 0
b10 >= 0
b11 >= 0
b12 >= 0
b13 >= 0
b14 >= 0
b15 >= 0
x1 >= 0
x2 >= 0
x3 >= 0
x4 >= 0
x5 >= 0
x6 >= 0
x7 >= 0
x8 >= 0
x9 >= 0
x10 >= 0
x11 >= 0
x12 >= 0
x13 >= 0
x14 >= 0
x15 >= 0
y1 >= 0
y2 >= 0
y3 >= 0
y4 >= 0
y5 >= 0
y6 >= 0
y7 >= 0
y8 >= 0
y9 >= 0
y10 >= 0
y11 >= 0
y12 >= 0
y13 >= 0
y14 >= 0
y15 >= 0
End
An illustration of the glpsol output looks as follows (decimal digits truncated).
F B
C C
A F Aa = 3.928 x=0.357
y=5.714 y = 2.5 a=2.5
b=7.5 a = 2.5
a=3.928,
x=0.357, y=5.714
x=0.357
y=3.214 b = 7.5
a = 3.928 b = 10 b = 10 b = 10
x=0.357
y=5.714
The objective function value at the optimum is roughly 17.857.

Write a program that adds a feasible solution to given supply vector b ∈ R7. ˆ Model this problem as a linear program (LP) and write the corresponding .lp le.
Scroll to top