# !pip install pulp
from pulp import *
# 1. Specify optimization problem type
prob = LpProblem("SomeLPProblem", LpMinimize)
# 2. Define the decision variables for each of 12 shelters
x1 = LpVariable("A: 2671Islington", 0, None, LpInteger)
x2 = LpVariable("B1:26Vaughan", 0, None, LpInteger)
x3 = LpVariable("B2:14Vaughan", 0, None, LpInteger)
x4 = LpVariable("C:850Bloor", 0, None, LpInteger)
x5 = LpVariable("D1:38Bathurst", 0, None, LpInteger)
x6 = LpVariable("D2:38Bathurst", 0, None, LpInteger)
x7 = LpVariable("E1:135Sherbourne", 0, None, LpInteger)
x8 = LpVariable("E2:339George", 0, None, LpInteger)
x9 = LpVariable("E3:339George", 0, None, LpInteger)
x10 = LpVariable("E4:339George", 0, None, LpInteger)
x11 = LpVariable("E5:339George", 0, None, LpInteger)
x12 = LpVariable("E6:339George", 0, None, LpInteger)
# 3. Define the first objective function -- minimize monetary cost
# prob += 15*x1 + 30*x2 + 10*x3 + 5*x4 + 5*x5 + 5*x6 + 2*(x7 + x8 + x9 + x10 + x11 + x12)
# 4. Define the second objective function -- minimize time cost
prob += x1 + 1.2*(x2 + x3) + x4 + 1.8*(x5) + 1.7*(x6) + 1.2*(x7 + x8 + x9) + 1.4*(x10 + x11 + x12)
# 5. Add monetary cost constraint (result of first optimization layer)
prob += 15*x1 + 30*x2 + 10*x3 + 5*x4 + 5*x5 + 5*x6 + 2*(x7 + x8 + x9 + x10 + x11 + x12) <= 123
# 6. Add Capacity/Supply constraints at cluster level: How much is available at the NBHD level
prob += x1 <= 1
prob += x2 + x3 <= 10
prob += x4 <= 1
prob += x5 + x6 <= 3
prob += x7 + x8 + x9 + x10 + x11 + x12 <= 30
# 7. Add Demand constraints at cluster level: How many beds are needed at the NBHD level
prob += x1 >= 1
prob += x2 + x3 >= 5
prob += x4 >= 1
prob += x5 + x6 >= 3
prob += x7 + x8 + x9 + x10 + x11 + x12 >= 19
# 8. Add Capacity/Supply for each shelter: Shelter-level capacity
prob += x1 <= 1
prob += x2 <= 5
prob += x3 <= 5
prob += x4 <= 1
prob += x5 <= 2
prob += x6 <= 1
prob += x7 <= 6
prob += x8 <= 5
prob += x9 <= 4
prob += x10 <= 2
prob += x11 <= 1
prob += x12 <= 12
# 9. Solve optimization problem
prob.solve()
# 10. Print the variables optimized value and optimized objective function value
for v in prob.variables():
print(v.name, ": ", int(v.varValue), "additional bed(s).")
print("\nOptimal Value of Objective Function: ", value(prob.objective))
view raw lp_code.py hosted with ❤ by GitHub