from pyomo.environ import *
from pyomo.opt import *
opt = solvers.SolverFactory("glpk")
Jobs = [0, 1, 2, 3, 4]
Slots = [0, 1, 2, 3, 4]
Due = [2, 2, 3, 1, 3]
Penalty = [19, 12, 34, 30, 22]
model = ConcreteModel()
model.a = Var(Jobs, Slots, within=Boolean)
model.z = Objective(
expr = sum(model.a[j,s]*(s-Due[j])*Penalty[j]
for j in Jobs for s in Slots if s>Due[j]),
sense=minimize)
def all_jobs_assigned_rule (model, j):
return sum(model.a[j,s] for s in Slots) == 1
model.jobs = Constraint(Jobs, rule=all_jobs_assigned_rule)
def all_slots_assigned_rule (model, s):
return sum(model.a[j,s] for j in Jobs) == 1
model.slots = Constraint(Slots, rule=all_slots_assigned_rule)
results = opt.solve(model)
model.a.get_values()
model.z.expr()