This file shows how to code up the solution to the "Museum Problem" on the Fall 2020 Midterm, Question 4, in Pyomo.
from pyomo.environ import *
from pyomo.opt import *
opt = solvers.SolverFactory("glpk")
We code up the doors of the museum as arcs of a network. The rooms are the vertices; they can be extracted automatically from the list of arcs.
A = [('A','B'), ('B','H'), ('H','I'), ('I','G'),
('G','A'), ('I','J'), ('A','F'), ('F','D'),
('A','C'), ('C','D'), ('D','E')]
R = {a[0] for a in A} | {a[1] for a in A}
The rest is a straightforward implementation of the model as specified in the sample solutions.
model = ConcreteModel()
model.x = Var(A, within=Boolean)
model.z = Objective(
expr = sum(model.x[a] for a in A), sense=minimize)
def all_rooms_are_guarded_rule (model, i):
return sum(model.x[a] for a in A if i in a) >= 1
model.c = Constraint(R, rule=all_rooms_are_guarded_rule)
results = opt.solve(model)
model.x.get_values()
model.z.expr()