from pyomo.environ import *
from pyomo.opt import *
opt = solvers.SolverFactory("ipopt")
P = ['P1', 'P2', 'P3']
M = ['M1', 'M2', 'M3']
s = {'P1':3, 'P2':5, 'P3':1}
d = {'M1':2, 'M2':2, 'M3':5}
c = {('P1','M1'):10, ('P1','M2'):5, ('P1','M3'):15,
('P2','M1'):15, ('P2','M2'):10, ('P2','M3'):20,
('P3','M1'):5, ('P3','M2'):5, ('P3','M3'):15}
model = ConcreteModel()
model.x = Var(P, M, within=NonNegativeReals)
model.z = Objective(expr = sum(c[i,j]*model.x[i,j] for i in P for j in M), sense=minimize)
def supply_rule (model, i):
return sum(model.x[i,j] for j in M) <= s[i]
model.supply = Constraint(P, rule=supply_rule)
def demand_rule (model, j):
return sum(model.x[i,j] for i in P) >= d[j]
model.demand = Constraint(M, rule=demand_rule)
model.dual = Suffix(direction=Suffix.IMPORT)
results = opt.solve(model)
model.x.get_values()
model.z.expr()
for j in M:
print(j, model.dual[model.demand[j]])
for i in P:
print(i, model.dual[model.supply[i]])