PySCIPOpt  4.3.0
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 def mctransp(I,J,K,c,d,M):
14  """mctransp -- model for solving the Multi-commodity Transportation Problem
15  Parameters:
16  - I: set of customers
17  - J: set of facilities
18  - K: set of commodities
19  - c[i,j,k]: unit transportation cost on arc (i,j) for commodity k
20  - d[i][k]: demand for commodity k at node i
21  - M[j]: capacity
22  Returns a model, ready to be solved.
23  """
24 
25  model = Model("multi-commodity transportation")
26 
27  # Create variables
28  x = {}
29 
30  for (i,j,k) in c:
31  x[i,j,k] = model.addVar(vtype="C", name="x(%s,%s,%s)" % (i,j,k))
32 
33  # Demand constraints
34  for i in I:
35  for k in K:
36  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))
37 
38  # Capacity constraints
39  for j in J:
40  model.addCons(sum(x[i,j,k] for (i,j2,k) in x if j2 == j) <= M[j], "Capacity(%s)" % j)
41 
42  # Objective
43  model.setObjective(quicksum(c[i,j,k]*x[i,j,k] for (i,j,k) in x), "minimize")
44 
45  model.data = x
46 
47  return model
48 
49 
50 def make_inst1():
51  """creates example data set 1"""
52  d = {(1,1):80, (1,2):85, (1,3):300, (1,4):6, # {(customer,commodity):demand}}
53  (2,1):270, (2,2):160, (2,3):400, (2,4):7,
54  (3,1):250, (3,2):130, (3,3):350, (3,4):4,
55  (4,1):160, (4,2):60, (4,3):200, (4,4):3,
56  (5,1):180, (5,2):40, (5,3):150, (5,4):5
57  }
58  I = set([i for (i,k) in d])
59  K = set([k for (i,k) in d])
60  J,M = multidict({1:3000, 2:3000, 3:3000}) # capacity
61 
62  produce = {1:[2,4], 2:[1,2,3], 3:[2,3,4]} # products that can be produced in each facility
63  weight = {1:5, 2:2, 3:3, 4:4} # {commodity: weight}
64  cost = {(1,1):4, (1,2):6, (1,3):9, # {(customer,factory): cost}
65  (2,1):5, (2,2):4, (2,3):7,
66  (3,1):6, (3,2):3, (3,3):4,
67  (4,1):8, (4,2):5, (4,3):3,
68  (5,1):10, (5,2):8, (5,3):4
69  }
70  c = {}
71  for i in I:
72  for j in J:
73  for k in produce[j]:
74  c[i,j,k] = cost[i,j] * weight[k]
75 
76  return I,J,K,c,d,M
77 
78 def make_inst2():
79  """creates example data set 2"""
80  d = {(1,1):45, # {(customer,commodity):demand}}
81  (2,1):20,
82  (3,1):30,
83  (4,1):30,
84  }
85  I = set([i for (i,k) in d])
86  K = set([k for (i,k) in d])
87  J,M = multidict({1:35, 2:50, 3:40}) # {factory: capacity}}
88  produce = {1:[1], 2:[1], 3:[1]} # products that can be produced in each facility
89  weight = {1:1} # {commodity: weight}
90  cost = {(1,1):8, (1,2):9, (1,3):14, # {(customer,factory): cost}
91  (2,1):6, (2,2):12, (2,3):9 ,
92  (3,1):10, (3,2):13, (3,3):16,
93  (4,1):9, (4,2):7, (4,3):5 ,
94  }
95  c = {}
96  for i in I:
97  for j in J:
98  for k in produce[j]:
99  c[i,j,k] = cost[i,j] * weight[k]
100 
101  return I,J,K,c,d,M
102 
103 
105  """creates example data set 3"""
106  d = {(1,1):40, (1,2):30, (1,3):10, # {(customer,commodity):demand}}
107  (2,1):70, (2,2):100, (2,3):100,
108  (3,1):0, (3,2):0, (3,3):250,
109  (4,1):60, (4,2):100, (4,3):0,
110  (5,1):180, (5,2):0, (5,3):0
111  }
112  I = set([i for (i,k) in d])
113  K = set([k for (i,k) in d])
114  J,M = multidict({1:500, 2:500, 3:500}) # capacity
115 
116  produce = {1:[2,4], 2:[1,2,3], 3:[2,3,4]} # products that can be produced in each facility
117  weight = {1:5, 2:2, 3:3, 4:4} # {commodity: weight}
118  cost = {(1,1):4, (1,2):6, (1,3):9, # {(customer,factory): cost}
119  (2,1):5, (2,2):4, (2,3):7,
120  (3,1):6, (3,2):3, (3,3):4,
121  (4,1):8, (4,2):5, (4,3):3,
122  (5,1):10, (5,2):8, (5,3):4
123  }
124  c = {}
125  for i in I:
126  for j in J:
127  for k in produce[j]:
128  c[i,j,k] = cost[i,j] * weight[k]
129 
130  return I,J,K,c,d,M
131 
132 
133 if __name__ == "__main__":
134  I,J,K,c,d,M = make_inst3();
135  model = mctransp(I,J,K,c,d,M)
136  model.writeProblem("transp.lp")
137  model.optimize()
138 
139  print("Optimal value:",model.getObjVal())
140 
141  EPS = 1.e-6
142  x = model.data
143 
144  for (i,j,k) in x:
145  if model.getVal(x[i,j,k]) > EPS:
146  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:78
pyscipopt.Multidict.multidict
def multidict(D)
Definition: Multidict.py:3
mctransp.make_inst3
def make_inst3()
Definition: mctransp.py:104
mctransp.make_inst1
def make_inst1()
Definition: mctransp.py:50
mctransp
Definition: mctransp.py:1
mctransp.mctransp
def mctransp(I, J, K, c, d, M)
Definition: mctransp.py:13