Machineco has four machines and four jobs to be completed. Each machine must be assigned to complete one job. The time required to set up each machine for completing each job is shown in Table 43. Machineco wants to minimize the total setup time needed to complete the four jobs. Use linear programming to solve this problem.
Job1 | Job2 | Job3 | Job4 | |
---|---|---|---|---|
Machine 1 | 14 | 5 | 8 | 7 |
Machine 2 | 2 | 12 | 6 | 5 |
Machine 3 | 7 | 8 | 3 | 9 |
Machine 4 | 2 | 4 | 6 | 10 |
In [10]:
from gurobipy import *
m = Model("Assignment Prolem")
NUMBER_OF_MACHINES = 4
NUMBER_OF_JOBS = 4
cost_matrix = [
[14,5,8,7],[2,12,6,5], [7,8,3,9], [2,4,6,10]
]
variables = [
[m.addVar(vtype=GRB.BINARY,
obj = cost_matrix[machine][job],
name="(Machine - %d , Job %d)" % (machine+1, job+1))
for job in range(NUMBER_OF_JOBS) ]
for machine in range(NUMBER_OF_MACHINES)
]
In [11]:
variables
Out[11]:
In [12]:
m.modelSense = GRB.MINIMIZE
m.update()
In [13]:
m.getObjective()
Out[13]:
In [14]:
for machine in range(NUMBER_OF_MACHINES):
m.addConstr(
quicksum(variables[machine][job] for job in range(NUMBER_OF_JOBS)) == 1,
"Job requirment for machine %d" % (machine + 1))
In [15]:
for job in range(NUMBER_OF_JOBS):
m.addConstr(
quicksum(variables[machine][job] for machine in range(NUMBER_OF_MACHINES)) == 1,
"Machine requirment for machine %d" % (job + 1))
In [16]:
m.optimize()
for v in m.getVars():
print (v.varName, v.x)
print (m.getObjective().getValue())