Allocating resources
Nice work, time to solve the next problem you were introduced to.
A manager allocates 120 tasks to a senior (S), a junior (J) and an intern (I) software engineer and wants to minimize costs.
The intern requires training that costs $\$$500 prior to working on the tasks. The cost to resolve each task is c = [30, 40, 5] respectively.
\(x\) holds the number of assigned tasks and \(o\) the binary for whether the intern receives training. The total cost is
\(TC = 30x_S+40x_J+(5x_I+500)o\)
Use the BigM method to linearize this problem using the new variable \(z\):
\(z = (5x_I+500)o\)
\(-oM\leq z \leq oM\)
\(-(1-o)M \leq z- (5x_I+500)o \leq (1-o)M\)
pulp
, along with a model
, parameters c
, M
, and names
, and variables x
, z
, o
have already been imported for you.
This exercise is part of the course
Introduction to Optimization in Python
Exercise instructions
- Define objective by replacing part of the formula with
z
. - Define constraints filling the index of the intern..
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define the objective
model += c[0]*x[0] + c[1]*x[1] + ____
# Define the constraints
model += -o * M <= z
model += z <= o * M
model += -(1-o) * M <= z - (c[___]*x[____] + 500)
model += z - (c[____]*x[____] + 500) <= (1-o) * M
model += lpSum(x) >= 120
status = model.solve()
print(f"{'Optimal found' if status == 1 else 'Ignore solution'}")
for i in range(len(c)):
print(f"{names[i]} was assigned {x[i].varValue:.0f}")