4 Model for solving a 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
10 from pyscipopt
import Model, quicksum, multidict
14 """transp -- model for solving the transportation problem
18 c[i,j] - unit transportation cost on arc (i,j)
19 d[i] - demand at node i
21 Returns a model, ready to be solved.
24 model = Model(
"transportation")
31 x[i, j] = model.addVar(vtype=
"C", name=
"x(%s,%s)" % (i, j))
35 model.addCons(
quicksum(x[i, j]
for j
in J
if (i, j)
in x) == d[i], name=
"Demand(%s)" % i)
39 model.addCons(
quicksum(x[i, j]
for i
in I
if (i, j)
in x) <= M[j], name=
"Capacity(%s)" % j)
42 model.setObjective(
quicksum(c[i, j] * x[i, j]
for (i, j)
in x),
"minimize")
51 """creates example data set 1"""
52 I, d =
multidict({1: 80, 2: 270, 3: 250, 4: 160, 5: 180})
53 J, M =
multidict({1: 500, 2: 500, 3: 500})
54 c = {(1, 1): 4, (1, 2): 6, (1, 3): 9,
55 (2, 1): 5, (2, 2): 4, (2, 3): 7,
56 (3, 1): 6, (3, 2): 3, (3, 3): 4,
57 (4, 1): 8, (4, 2): 5, (4, 3): 3,
58 (5, 1): 10, (5, 2): 8, (5, 3): 4,
64 """creates example data set 2"""
65 I, d =
multidict({1: 45, 2: 20, 3: 30, 4: 30})
67 c = {(1, 1): 8, (1, 2): 9, (1, 3): 14,
68 (2, 1): 6, (2, 2): 12, (2, 3): 9,
69 (3, 1): 10, (3, 2): 13, (3, 3): 16,
70 (4, 1): 9, (4, 2): 7, (4, 3): 5,
75 if __name__ ==
"__main__":
81 print(
"Optimal value:", model.getObjVal())
87 if model.getVal(x[i, j]) > EPS:
88 print(
"sending quantity %10s from factory %3s to customer %3s" % (model.getVal(x[i, j]), j, i))