4 Model for solving the multi-commodity transportation problem:
5 minimize the total transportation cost for satisfying demand at
6 customers, from capacitated facilities.
8 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
11 from pyscipopt
import Model, quicksum, multidict
15 """mctransp -- model for solving the Multi-commodity Transportation Problem
18 - J: set of facilities
19 - K: set of commodities
20 - c[i,j,k]: unit transportation cost on arc (i,j) for commodity k
21 - d[i][k]: demand for commodity k at node i
23 Returns a model, ready to be solved.
26 model = Model(
"multi-commodity transportation")
32 x[i, j, k] = model.addVar(vtype=
"C", name=
"x(%s,%s,%s)" % (i, j, k))
37 model.addCons(sum(x[i, j, k]
for j
in J
if (i, j, k)
in x) == d[i, k],
"Demand(%s,%s)" % (i, k))
41 model.addCons(sum(x[i, j, k]
for (i, j2, k)
in x
if j2 == j) <= M[j],
"Capacity(%s)" % j)
44 model.setObjective(
quicksum(c[i, j, k] * x[i, j, k]
for (i, j, k)
in x),
"minimize")
52 """creates example data set 1"""
53 d = {(1, 1): 80, (1, 2): 85, (1, 3): 300, (1, 4): 6,
54 (2, 1): 270, (2, 2): 160, (2, 3): 400, (2, 4): 7,
55 (3, 1): 250, (3, 2): 130, (3, 3): 350, (3, 4): 4,
56 (4, 1): 160, (4, 2): 60, (4, 3): 200, (4, 4): 3,
57 (5, 1): 180, (5, 2): 40, (5, 3): 150, (5, 4): 5
59 I =
set([i
for (i, k)
in d])
60 K =
set([k
for (i, k)
in d])
61 J, M =
multidict({1: 3000, 2: 3000, 3: 3000})
63 produce = {1: [2, 4], 2: [1, 2, 3], 3: [2, 3, 4]}
64 weight = {1: 5, 2: 2, 3: 3, 4: 4}
65 cost = {(1, 1): 4, (1, 2): 6, (1, 3): 9,
66 (2, 1): 5, (2, 2): 4, (2, 3): 7,
67 (3, 1): 6, (3, 2): 3, (3, 3): 4,
68 (4, 1): 8, (4, 2): 5, (4, 3): 3,
69 (5, 1): 10, (5, 2): 8, (5, 3): 4
75 c[i, j, k] = cost[i, j] * weight[k]
77 return I, J, K, c, d, M
81 """creates example data set 2"""
87 I =
set([i
for (i, k)
in d])
88 K =
set([k
for (i, k)
in d])
90 produce = {1: [1], 2: [1], 3: [1]}
92 cost = {(1, 1): 8, (1, 2): 9, (1, 3): 14,
93 (2, 1): 6, (2, 2): 12, (2, 3): 9,
94 (3, 1): 10, (3, 2): 13, (3, 3): 16,
95 (4, 1): 9, (4, 2): 7, (4, 3): 5,
101 c[i, j, k] = cost[i, j] * weight[k]
103 return I, J, K, c, d, M
107 """creates example data set 3"""
108 d = {(1, 1): 40, (1, 2): 30, (1, 3): 10,
109 (2, 1): 70, (2, 2): 100, (2, 3): 100,
110 (3, 1): 0, (3, 2): 0, (3, 3): 250,
111 (4, 1): 60, (4, 2): 100, (4, 3): 0,
112 (5, 1): 180, (5, 2): 0, (5, 3): 0
114 I =
set([i
for (i, k)
in d])
115 K =
set([k
for (i, k)
in d])
116 J, M =
multidict({1: 500, 2: 500, 3: 500})
118 produce = {1: [2, 4], 2: [1, 2, 3], 3: [2, 3, 4]}
119 weight = {1: 5, 2: 2, 3: 3, 4: 4}
120 cost = {(1, 1): 4, (1, 2): 6, (1, 3): 9,
121 (2, 1): 5, (2, 2): 4, (2, 3): 7,
122 (3, 1): 6, (3, 2): 3, (3, 3): 4,
123 (4, 1): 8, (4, 2): 5, (4, 3): 3,
124 (5, 1): 10, (5, 2): 8, (5, 3): 4
130 c[i, j, k] = cost[i, j] * weight[k]
132 return I, J, K, c, d, M
135 if __name__ ==
"__main__":
138 model.writeProblem(
"transp.lp")
141 print(
"Optimal value:", model.getObjVal())
147 if model.getVal(x[i, j, k]) > EPS:
148 print(
"sending %10s units of %3s from plant %3s to customer %3s" % (model.getVal(x[i, j, k]), k, j, i))