PySCIPOpt  5.1.1
Python Interface for the SCIP Optimization Suite
transp_nofn.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 Data:
9  I - set of customers
10  J - set of facilities
11  c[i,j] - unit transportation cost on arc (i,j)
12  d[i] - demand at node i
13  M[j] - capacity
14 
15 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
16 """
17 
18 from pyscipopt import Model, quicksum
19 
20 d = {1: 80, 2: 270, 3: 250, 4: 160, 5: 180} # demand
21 I = d.keys()
22 
23 M = {1: 500, 2: 500, 3: 500} # capacity
24 J = M.keys()
25 
26 c = {(1, 1): 4, (1, 2): 6, (1, 3): 9, # cost
27  (2, 1): 5, (2, 2): 4, (2, 3): 7,
28  (3, 1): 6, (3, 2): 3, (3, 3): 4,
29  (4, 1): 8, (4, 2): 5, (4, 3): 3,
30  (5, 1): 10, (5, 2): 8, (5, 3): 4,
31  }
32 
33 model = Model("transportation")
34 
35 # Create variables
36 x = {}
37 
38 for i in I:
39  for j in J:
40  x[i, j] = model.addVar(vtype="C", name="x(%s,%s)" % (i, j))
41 
42 # Demand constraints
43 for i in I:
44  model.addCons(sum(x[i, j] for j in J if (i, j) in x) == d[i], name="Demand(%s)" % i)
45 
46 # Capacity constraints
47 for j in J:
48  model.addCons(sum(x[i, j] for i in I if (i, j) in x) <= M[j], name="Capacity(%s)" % j)
49 
50 # Objective
51 model.setObjective(quicksum(c[i, j] * x[i, j] for (i, j) in x), "minimize")
52 
53 model.optimize()
54 
55 print("Optimal value:", model.getObjVal())
56 
57 EPS = 1.e-6
58 
59 for (i, j) in x:
60  if model.getVal(x[i, j]) > EPS:
61  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