我试图尽量减少成本制造的产品有两个机器。 成本的机为30美元/产品和成本的机B40美元/产品。
有两个限制:
- 我们必须涵盖的需求的50产品的每月(x+y>=50)
- 廉价机(A)只能制造40产品的每月(x<=40)
所以我创造了以下Pyomo码:
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())
它的工作确定,具有明显的解决方案x=40;y=10(成本=1600)
然而,如果我们开始使用的机B,将有一个固定的罚款300美元的费用。
我试着用
def production_cost(m):
if (m.y > 0):
return 30*m.x + 40*m.y + 300
else:
return 30*m.x + 40*m.y
但我得到的以下错误消息
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.
我不如何执行条件以包括罚款到的目标功能,通过Pyomo代码。