PySCIPOpt  5.1.1
Python Interface for the SCIP Optimization Suite
mctransp.py
Go to the documentation of this file.
1 
3 """
4 Model for solving the multi-commodity transportation problem:
5 minimize the total transportation cost for satisfying demand at
6 customers, from capacitated facilities.
7 
8 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
9 """
10 
11 from pyscipopt import Model, quicksum, multidict
12 
13 
14 def mctransp(I, J, K, c, d, M):
15  """mctransp -- model for solving the Multi-commodity Transportation Problem
16  Parameters:
17  - I: set of customers
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
22  - M[j]: capacity
23  Returns a model, ready to be solved.
24  """
25 
26  model = Model("multi-commodity transportation")
27 
28  # Create variables
29  x = {}
30 
31  for (i, j, k) in c:
32  x[i, j, k] = model.addVar(vtype="C", name="x(%s,%s,%s)" % (i, j, k))
33 
34  # Demand constraints
35  for i in I:
36  for k in 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))
38 
39  # Capacity constraints
40  for j in J:
41  model.addCons(sum(x[i, j, k] for (i, j2, k) in x if j2 == j) <= M[j], "Capacity(%s)" % j)
42 
43  # Objective
44  model.setObjective(quicksum(c[i, j, k] * x[i, j, k] for (i, j, k) in x), "minimize")
45 
46  model.data = x
47 
48  return model
49 
50 
51 def make_inst1():
52  """creates example data set 1"""
53  d = {(1, 1): 80, (1, 2): 85, (1, 3): 300, (1, 4): 6, # {(customer,commodity):demand}}
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
58  }
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}) # capacity
62 
63  produce = {1: [2, 4], 2: [1, 2, 3], 3: [2, 3, 4]} # products that can be produced in each facility
64  weight = {1: 5, 2: 2, 3: 3, 4: 4} # {commodity: weight}
65  cost = {(1, 1): 4, (1, 2): 6, (1, 3): 9, # {(customer,factory): cost}
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
70  }
71  c = {}
72  for i in I:
73  for j in J:
74  for k in produce[j]:
75  c[i, j, k] = cost[i, j] * weight[k]
76 
77  return I, J, K, c, d, M
78 
79 
80 def make_inst2():
81  """creates example data set 2"""
82  d = {(1, 1): 45, # {(customer,commodity):demand}}
83  (2, 1): 20,
84  (3, 1): 30,
85  (4, 1): 30,
86  }
87  I = set([i for (i, k) in d])
88  K = set([k for (i, k) in d])
89  J, M = multidict({1: 35, 2: 50, 3: 40}) # {factory: capacity}}
90  produce = {1: [1], 2: [1], 3: [1]} # products that can be produced in each facility
91  weight = {1: 1} # {commodity: weight}
92  cost = {(1, 1): 8, (1, 2): 9, (1, 3): 14, # {(customer,factory): cost}
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,
96  }
97  c = {}
98  for i in I:
99  for j in J:
100  for k in produce[j]:
101  c[i, j, k] = cost[i, j] * weight[k]
102 
103  return I, J, K, c, d, M
104 
105 
107  """creates example data set 3"""
108  d = {(1, 1): 40, (1, 2): 30, (1, 3): 10, # {(customer,commodity):demand}}
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
113  }
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}) # capacity
117 
118  produce = {1: [2, 4], 2: [1, 2, 3], 3: [2, 3, 4]} # products that can be produced in each facility
119  weight = {1: 5, 2: 2, 3: 3, 4: 4} # {commodity: weight}
120  cost = {(1, 1): 4, (1, 2): 6, (1, 3): 9, # {(customer,factory): cost}
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
125  }
126  c = {}
127  for i in I:
128  for j in J:
129  for k in produce[j]:
130  c[i, j, k] = cost[i, j] * weight[k]
131 
132  return I, J, K, c, d, M
133 
134 
135 if __name__ == "__main__":
136  I, J, K, c, d, M = make_inst3();
137  model = mctransp(I, J, K, c, d, M)
138  model.writeProblem("transp.lp")
139  model.optimize()
140 
141  print("Optimal value:", model.getObjVal())
142 
143  EPS = 1.e-6
144  x = model.data
145 
146  for (i, j, k) in x:
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))
pyscipopt.expr.quicksum
def quicksum(termlist)
Definition: expr.pxi:357
set
mctransp.make_inst2
def make_inst2()
Definition: mctransp.py:80
pyscipopt.Multidict.multidict
def multidict(D)
Definition: Multidict.py:3
mctransp.make_inst3
def make_inst3()
Definition: mctransp.py:106
mctransp.make_inst1
def make_inst1()
Definition: mctransp.py:51
mctransp
Definition: mctransp.py:1
mctransp.mctransp
def mctransp(I, J, K, c, d, M)
Definition: mctransp.py:14