The smaller exercises embedded in Transportation and Transshipment, Set Covering and Shift Scheduling, and Machine Scheduling check what you just read. This notebook collects the homework exercises for Lecture 9: independent problems worth more time.
Homework Exercises¶
Exercise 1¶
Solution to Exercise 1 #
a. Define as the decision variable for the number of hours worker works on task , for and . The LO is
The objective is the total number of hours worked. The first three constraints make sure no worker works more than their available hours, and the last two that each task gets at least its required effective hours.
b. The restriction is .
c. Define as a binary decision variable that is 1 if worker works on task 1, and 0 otherwise, for . Let be a big constant, for example (any works, since no worker has more than 20 hours). Add to the LO of part a the constraints
Constraint gives its meaning: if worker works on task 1 (), then must be 1, and if then . The second constraint then allows exactly one worker on task 1.
d. Start from the LO of part a. Define as a binary decision variable that is 1 if workers 2 and 3 both work on task 1, and 0 otherwise. To require 10 extra effective hours for task 1 when , replace constraint by
Now we make sure that gets the value of its definition. First, add the constraints and of part c for (but not ), so that and have the same meaning as in part c. Then add
If , this forces . Otherwise the constraint allows , and since a larger only requires more work, minimizing the total hours sets (“less work”). For the same reason the optimizer never sets for a worker who does not work on task 1.
Exercise 2¶
Solution to Exercise 2 #
a. The universe is , the 5 cities. For each location , let be the set of cities that are covered from location , that is, reachable within 5 minutes: , , , . Finding the smallest number of sets that together cover is then exactly the ambulance placement problem.
b. Define as a binary decision variable that is 1 if an ambulance is placed at location , and 0 otherwise, for . Then the ILO is
The constraint for city sums the variables of the locations that reach within 5 minutes, so it says that at least one of them has an ambulance.
c. Filling in and violates two constraints: for city 3, , and for city 4, . Cities 3 and 4 are not covered, so this allocation is infeasible.
d. The constraints for cities 1, 4 and 5 contain a single variable each, so every feasible solution has , and at least 3 ambulances are needed. Setting (with ) is feasible, since city 2 is covered from A and city 3 from D, and it uses exactly this minimum of 3 ambulances. So it is optimal: place ambulances at A, C and D.
Exercise 3¶
Solution to Exercise 3 #
a. Define as the decision variable for the quantity transported from source S to destination D, for and . The LO is
The inequality signs may be replaced by equality signs here: total supply equals total demand (), so the only way to meet all demand is to ship every unit of supply, and then every constraint holds with equality.
b. Define as a binary decision variable that is 1 if arc is used, and 0 otherwise, for and . Let be a big constant, for example (any works, since no arc carries more than a demand of 25). Add to the LO of part a
The first constraint gives its meaning: an arc that carries goods has . The second then limits the number of arcs used to 3.
This model is correct, but this particular instance has no feasible solution. As shown in part a, every unit of supply must be shipped. S1 has 30 units, more than either destination needs, so it uses both of its arcs, and S2 and S3 each use at least one arc: at least 4 arcs are needed.
c. The sources are the students and the destinations are the rooms. The supply of every source and the demand of every destination is 1. The cost from student to room is the rank student gives room : if student ranks room as number , the cost of arc is , so a more preferred room costs less. Since all supplies and demands are integer, the LO optimum is integer, so it really assigns each student to one room.
d. In the formulation of part c, redefine the cost of arc as 0 if room is in student ’s top 3, and 1 otherwise. Then the total cost of an assignment is the number of students who do not get a top-3 room. Solve the transportation problem: if the minimal total cost is 0, the solution is an assignment in which every student gets a top-3 room. If it is positive, no such assignment exists, since any such assignment would have cost 0.
Exercise 4¶
Solution to Exercise 4 #
a. Any choice of 2 sets is optimal. No single set covers (each set misses one element), so at least 2 sets are needed. Each set misses a different element, so any 2 sets together cover .
b. Define as a binary decision variable that is 1 if set is chosen, and 0 otherwise, for . The ILO is
c. The LO relaxation is the model of part b with replaced by , for . An optimal solution is for all , with objective value : it satisfies every cover constraint with equality. No solution does better, since adding the four cover constraints gives .
d. Now is the number of times set is chosen, and the covering constraints are
Adding these four constraints gives , so the objective value is at least 6. By trial and error, we find the solution , which satisfies every constraint (with equality: , , , ) and has objective value 6. So it is optimal.
Exercise 5¶
Solution to Exercise 5 #
a. A couple can be formed if is on ’s list and is on ’s list. This gives the couples , , , and . For example, student 1 lists student 4, but student 4 does not list student 1, so cannot be formed. Student 6 cannot be part of any couple.
b. The couple is the same as the couple . To have only one decision variable per couple, we use variables with only; this breaks the symmetry between and . Define as a binary decision variable that is 1 if the couple of students and is formed, and 0 otherwise, for the couples with from part a. A couple that cannot be formed gets no variable. The ILO is
The objective counts the couples formed. The constraint for student sums the variables of all couples that contain , whether is the first index () or the second (), so every student is in at most one couple. The constraints for students 4 and 5 already follow from the binary constraints, and student 6 needs no constraint, since no variable contains student 6.
In general, with if students and listed each other (and 0 otherwise), the model is to maximize subject to for , and for every student , with .
c. An optimal solution is and all other variables 0: the couples and , which is feasible since no student is in both. It is optimal because student 6 cannot be paired, so at most 5 students are in a couple, and 5 students form at most 2 couples. (Another optimal solution is .)
Exercise 6¶
Solution to Exercise 6 #
On the exam, the answer consists of the code only. The printed output below is not part of the answer: it is reported out of curiosity and for completeness.
a. The completed code (compare Transportation and Transshipment):
import pulp
supply = {"A": 60, "B": 45}
demand = {"C1": 20, "C2": 25, "C3": 15}
cost = {
("A", "C1"): 2,
("A", "C2"): 4,
("A", "C3"): 5,
("B", "C1"): 3,
("B", "C2"): 1,
("B", "C3"): 2,
}
transport = pulp.LpProblem(name="transportation", sense=pulp.LpMinimize)
ship = {
(i, j): pulp.LpVariable(name=f"x_{i}_{j}", lowBound=0) for (i, j) in cost
}
transport += pulp.lpSum(cost[i, j] * ship[i, j] for (i, j) in cost)
for i, cap in supply.items():
shipped = pulp.lpSum(ship[i, j] for j in demand)
transport += shipped <= cap, f"supply_{i}"
for j, need in demand.items():
received = pulp.lpSum(ship[i, j] for i in supply)
transport += received >= need, f"demand_{j}"
transport.solve(pulp.PULP_CBC_CMD(msg=False))
print(pulp.LpStatus[transport.status])
for (i, j), var in ship.items():
if var.value() > 0:
print(i, "->", j, var.value())
print("total cost:", transport.objective.value())It prints:
Optimal
A -> C1 20.0
B -> C2 25.0
B -> C3 15.0
total cost: 95.0b. Add one constraint before the solve line:
transport += ship["B", "C2"] <= 10, "limit_B_C2"Now B can deliver only 10 of the 25 units for C2, and the other 15 come from A at 4 per unit instead of 1, so the cost rises by :
Optimal
A -> C1 20.0
A -> C2 15.0
B -> C2 10.0
B -> C3 15.0
total cost: 140.0c. Let be the fixed cost of using warehouse , so and . Define as a binary decision variable that is 1 if warehouse is used, and 0 otherwise, for . Add the fixed costs to the objective, and change the supply constraint to : if the warehouse ships nothing, and if it is the original supply constraint. This is the indicator-variable trick from Machine Scheduling, with the supply as big M. The changed and added lines:
fixed_cost = {"A": 100, "B": 150}
# ...
used = {i: pulp.LpVariable(name=f"used_{i}", cat="Binary") for i in supply}
shipping_cost = pulp.lpSum(cost[i, j] * ship[i, j] for (i, j) in cost)
opening_cost = pulp.lpSum(fixed_cost[i] * used[i] for i in supply)
transport += shipping_cost + opening_cost
for i, cap in supply.items():
shipped = pulp.lpSum(ship[i, j] for j in demand)
transport += shipped <= cap * used[i], f"supply_{i}"
# ... (demand constraints and solve as in part a)
print("used:", {i: used[i].value() for i in supply})Using both warehouses costs . Warehouse A alone can supply all 60 units, at transport cost plus 100 fixed cost, which is 315 and cheaper. B alone is infeasible (supply 45 < 60). The code prints:
Optimal
A -> C1 20.0
A -> C2 25.0
A -> C3 15.0
used: {'A': 1.0, 'B': 0.0}
total cost: 315.0