| 1 |
""" |
| 2 |
The Beer Distribution Problem for the PuLP Modeller |
| 3 |
|
| 4 |
Authors: Antony Phillips, Dr Stuart Mitchell 2007 |
| 5 |
""" |
| 6 |
|
| 7 |
|
| 8 |
import pulp |
| 9 |
|
| 10 |
|
| 11 |
warehouses = ["A", "B"] |
| 12 |
|
| 13 |
|
| 14 |
supply = {"A": 1000, "B": 4000} |
| 15 |
|
| 16 |
|
| 17 |
bars = ["1", "2", "3", "4", "5"] |
| 18 |
|
| 19 |
|
| 20 |
demand = { |
| 21 |
"1": 500, |
| 22 |
"2": 900, |
| 23 |
"3": 1800, |
| 24 |
"4": 200, |
| 25 |
"5": 700, |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
costs = [ |
| 30 |
|
| 31 |
[2, 4, 5, 2, 1], |
| 32 |
[3, 1, 3, 2, 3], |
| 33 |
] |
| 34 |
|
| 35 |
|
| 36 |
costs = pulp.makeDict([warehouses, bars], costs, 0) |
| 37 |
|
| 38 |
|
| 39 |
prob = pulp.LpProblem("Beer Distribution Problem", pulp.LpMinimize) |
| 40 |
|
| 41 |
|
| 42 |
routes = [(w, b) for w in warehouses for b in bars] |
| 43 |
|
| 44 |
|
| 45 |
x = pulp.LpVariable.dicts("route", (warehouses, bars), lowBound=0, cat=pulp.LpInteger) |
| 46 |
|
| 47 |
|
| 48 |
prob += sum([x[w][b] * costs[w][b] for (w, b) in routes]), "Sum_of_Transporting_Costs" |
| 49 |
|
| 50 |
|
| 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 |
|
| 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 |
|
| 65 |
prob.writeLP("BeerDistributionProblem.lp") |
| 66 |
|
| 67 |
|
| 68 |
prob.solve() |
| 69 |
|
| 70 |
|
| 71 |
print("Status:", pulp.LpStatus[prob.status]) |
| 72 |
|
| 73 |
|
| 74 |
for v in prob.variables(): |
| 75 |
print(v.name, "=", v.varValue) |
| 76 |
|
| 77 |
|
| 78 |
print("Total Cost of Transportation = ", prob.objective.value()) |