Snažím minimalizovať náklady na výrobu výrobku s dvoma strojmi. Náklady na stroj je $30/výrobku a náklady na stroj B je $40/produkt.
Tam sú dve obmedzenia:
- musíme pokryť dopyt 50 produktov za mesiac (x+y >= 50)
- lacný stroj (A) možno len vtedy, výroba 40 výrobkov za mesiac (x<=40)
Tak som vytvoril nasledujúce Pyomo kód:
from pyomo.environ import *
model = ConcreteModel()
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)
def production_cost(m):
return 30*m.x + 40*m.y
# Objective
model.mycost = Objective(expr = production_cost, sense=minimize)
# Constraints
model.demand = Constraint(expr = model.x + model.y >= 50)
model.maxA = Constraint(expr = model.x <= 40)
# Let's solve it
results = SolverFactory('glpk').solve(model)
# Display the solution
print('Cost=', model.mycost())
print('x=', model.x())
print('y=', model.y())
Funguje to ok, s zrejmé, riešenie x=40;y=10 (Cena = 1600)
Ak sa však začneme používať stroj B, tam bude stanovená pokuta vo výške 300 $nad nákladmi.
Snažil som sa s
def production_cost(m):
if (m.y > 0):
return 30*m.x + 40*m.y + 300
else:
return 30*m.x + 40*m.y
Ale ja si nasledujúce chybové hlásenie
Rule failed when generating expression for Objective mycost with index
None: PyomoException: Cannot convert non-constant Pyomo expression (0 <
y) to bool. This error is usually caused by using a Var, unit, or mutable
Param in a Boolean context such as an "if" statement, or when checking
container membership or equality. For example,
>>> m.x = Var() >>> if m.x >= 1: ... pass
and
>>> m.y = Var() >>> if m.y in [m.x, m.y]: ... pass
would both cause this exception.
Neviem, ako implementovať stav, patrí pokutu do cieľa funkciu prostredníctvom Pyomo kód.