PySCIPOpt  5.1.1
Python Interface for the SCIP Optimization Suite
flp.py
Go to the documentation of this file.
1 
3 """
4 minimize the total (weighted) travel cost from n customers
5 to some facilities with fixed costs and capacities.
6 
7 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
8 """
9 from pyscipopt import Model, quicksum, multidict
10 
11 
12 def flp(I, J, d, M, f, c):
13  """flp -- model for the capacitated facility location problem
14  Parameters:
15  - I: set of customers
16  - J: set of facilities
17  - d[i]: demand for customer i
18  - M[j]: capacity of facility j
19  - f[j]: fixed cost for using a facility in point j
20  - c[i,j]: unit cost of servicing demand point i from facility j
21  Returns a model, ready to be solved.
22  """
23 
24  model = Model("flp")
25 
26  x, y = {}, {}
27  for j in J:
28  y[j] = model.addVar(vtype="B", name="y(%s)" % j)
29  for i in I:
30  x[i, j] = model.addVar(vtype="C", name="x(%s,%s)" % (i, j))
31 
32  for i in I:
33  model.addCons(quicksum(x[i, j] for j in J) == d[i], "Demand(%s)" % i)
34 
35  for j in M:
36  model.addCons(quicksum(x[i, j] for i in I) <= M[j] * y[j], "Capacity(%s)" % i)
37 
38  for (i, j) in x:
39  model.addCons(x[i, j] <= d[i] * y[j], "Strong(%s,%s)" % (i, j))
40 
41  model.setObjective(
42  quicksum(f[j] * y[j] for j in J) +
43  quicksum(c[i, j] * x[i, j] for i in I for j in J),
44  "minimize")
45  model.data = x, y
46 
47  return model
48 
49 
50 def make_data():
51  """creates example data set"""
52  I, d = multidict({1: 80, 2: 270, 3: 250, 4: 160, 5: 180}) # demand
53  J, M, f = multidict({1: [500, 1000], 2: [500, 1000], 3: [500, 1000]}) # capacity, fixed costs
54  c = {(1, 1): 4, (1, 2): 6, (1, 3): 9, # transportation costs
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,
59  }
60  return I, J, d, M, f, c
61 
62 
63 if __name__ == "__main__":
64  I, J, d, M, f, c = make_data()
65  model = flp(I, J, d, M, f, c)
66  model.optimize()
67 
68  EPS = 1.e-6
69  x, y = model.data
70  edges = [(i, j) for (i, j) in x if model.getVal(x[i, j]) > EPS]
71  facilities = [j for j in y if model.getVal(y[j]) > EPS]
72 
73  print("Optimal value:", model.getObjVal())
74  print("Facilities at nodes:", facilities)
75  print("Edges:", edges)
76 
77  try: # plot the result using networkx and matplotlib
78  import networkx as NX
79  import matplotlib.pyplot as P
80 
81  P.clf()
82  G = NX.Graph()
83 
84  other = [j for j in y if j not in facilities]
85  customers = ["c%s" % i for i in d]
86  G.add_nodes_from(facilities)
87  G.add_nodes_from(other)
88  G.add_nodes_from(customers)
89  for (i, j) in edges:
90  G.add_edge("c%s" % i, j)
91 
92  position = NX.drawing.layout.spring_layout(G)
93  NX.draw(G, position, node_color="y", nodelist=facilities)
94  NX.draw(G, position, node_color="g", nodelist=other)
95  NX.draw(G, position, node_color="b", nodelist=customers)
96  P.show()
97  except ImportError:
98  print("install 'networkx' and 'matplotlib' for plotting")
pyscipopt.expr.quicksum
def quicksum(termlist)
Definition: expr.pxi:357
flp.flp
def flp(I, J, d, M, f, c)
Definition: flp.py:12
flp.make_data
def make_data()
Definition: flp.py:50
pyscipopt.Multidict.multidict
def multidict(D)
Definition: Multidict.py:3
flp
Definition: flp.py:1