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