Debian Codegraph @

doc/KPyCon2009/code/beerdistribution.py

raw symbols SCIP
1 """
2 The Beer Distribution Problem for the PuLP Modeller
3
4 Authors: Antony Phillips, Dr Stuart Mitchell 2007
5 """
6
7 # Import PuLP modeler functions
8 import pulp
9
10 # Creates a list of all the supply nodes
11 warehouses = ["A", "B"]
12
13 # Creates a dictionary for the number of units of supply for each supply node
14 supply = {"A": 1000, "B": 4000}
15
16 # Creates a list of all demand nodes
17 bars = ["1", "2", "3", "4", "5"]
18
19 # Creates a dictionary for the number of units of demand for each demand node
20 demand = {
21 "1": 500,
22 "2": 900,
23 "3": 1800,
24 "4": 200,
25 "5": 700,
26 }
27
28 # Creates a list of costs of each transportation path
29 costs = [ # Bars
30 # 1 2 3 4 5
31 [2, 4, 5, 2, 1], # A Warehouses
32 [3, 1, 3, 2, 3], # B
33 ]
34
35 # The cost data is made into a dictionary
36 costs = pulp.makeDict([warehouses, bars], costs, 0)
37
38 # Creates the 'prob' variable to contain the problem data
39 prob = pulp.LpProblem("Beer Distribution Problem", pulp.LpMinimize)
40
41 # Creates a list of tuples containing all the possible routes for transport
42 routes = [(w, b) for w in warehouses for b in bars]
43
44 # A dictionary called x is created to contain quantity shipped on the routes
45 x = pulp.LpVariable.dicts("route", (warehouses, bars), lowBound=0, cat=pulp.LpInteger)
46
47 # The objective function is added to 'prob' first
48 prob += sum([x[w][b] * costs[w][b] for (w, b) in routes]), "Sum_of_Transporting_Costs"
49
50 # Supply maximum constraints are added to prob for each supply node (warehouse)
51 for w in warehouses:
52 prob += (
53 sum([x[w][b] for b in bars]) <= supply[w],
54 "Sum_of_Products_out_of_Warehouse_%s" % w,
55 )
56
57 # Demand minimum constraints are added to prob for each demand node (bar)
58 for b in bars:
59 prob += (
60 sum([x[w][b] for w in warehouses]) >= demand[b],
61 "Sum_of_Products_into_Bar%s" % b,
62 )
63
64 # The problem data is written to an .lp file
65 prob.writeLP("BeerDistributionProblem.lp")
66
67 # The problem is solved using PuLP's choice of Solver
68 prob.solve()
69
70 # The status of the solution is printed to the screen
71 print("Status:", pulp.LpStatus[prob.status])
72
73 # Each of the variables is printed with it's resolved optimum value
74 for v in prob.variables():
75 print(v.name, "=", v.varValue)
76
77 # The optimised objective function value is printed to the screen
78 print("Total Cost of Transportation = ", prob.objective.value())