PySCIPOpt  5.1.1
Python Interface for the SCIP Optimization Suite
transp.py
Go to the documentation of this file.
1 
3 """
4 Model for solving a 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 from pyscipopt import Model, quicksum, multidict
11 
12 
13 def transp(I, J, c, d, M):
14  """transp -- model for solving the transportation problem
15  Parameters:
16  I - set of customers
17  J - set of facilities
18  c[i,j] - unit transportation cost on arc (i,j)
19  d[i] - demand at node i
20  M[j] - capacity
21  Returns a model, ready to be solved.
22  """
23 
24  model = Model("transportation")
25 
26  # Create variables
27  x = {}
28 
29  for i in I:
30  for j in J:
31  x[i, j] = model.addVar(vtype="C", name="x(%s,%s)" % (i, j))
32 
33  # Demand constraints
34  for i in I:
35  model.addCons(quicksum(x[i, j] for j in J if (i, j) in x) == d[i], name="Demand(%s)" % i)
36 
37  # Capacity constraints
38  for j in J:
39  model.addCons(quicksum(x[i, j] for i in I if (i, j) in x) <= M[j], name="Capacity(%s)" % j)
40 
41  # Objective
42  model.setObjective(quicksum(c[i, j] * x[i, j] for (i, j) in x), "minimize")
43 
44  model.optimize()
45 
46  model.data = x
47  return model
48 
49 
50 def make_inst1():
51  """creates example data set 1"""
52  I, d = multidict({1: 80, 2: 270, 3: 250, 4: 160, 5: 180}) # demand
53  J, M = multidict({1: 500, 2: 500, 3: 500}) # capacity
54  c = {(1, 1): 4, (1, 2): 6, (1, 3): 9, # cost
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, c, d, M
61 
62 
63 def make_inst2():
64  """creates example data set 2"""
65  I, d = multidict({1: 45, 2: 20, 3: 30, 4: 30}) # demand
66  J, M = multidict({1: 35, 2: 50, 3: 40}) # capacity
67  c = {(1, 1): 8, (1, 2): 9, (1, 3): 14, # {(customer,factory) : cost<float>}
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,
71  }
72  return I, J, c, d, M
73 
74 
75 if __name__ == "__main__":
76  I, J, c, d, M = make_inst1();
77  # I,J,c,d,M = make_inst2();
78  model = transp(I, J, c, d, M)
79  model.optimize()
80 
81  print("Optimal value:", model.getObjVal())
82 
83  EPS = 1.e-6
84  x = model.data
85 
86  for (i, j) in x:
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))
pyscipopt.expr.quicksum
def quicksum(termlist)
Definition: expr.pxi:357
transp.make_inst1
def make_inst1()
Definition: transp.py:50
transp.transp
def transp(I, J, c, d, M)
Definition: transp.py:13
transp
Definition: transp.py:1
transp.make_inst2
def make_inst2()
Definition: transp.py:63
pyscipopt.Multidict.multidict
def multidict(D)
Definition: Multidict.py:3