Pyomo:约束与if语句
问题描述:
我目前正试图解决这个问题。我需要最大化该公司的利润。Pyomo:约束与if语句
这就是我目前拥有的代码:
from pyomo.environ import *
from pyomo.opt import *
opt = solvers.SolverFactory("ipopt")
model = ConcreteModel()
model.x1 = Var(within=NonNegativeIntegers)
model.x2 = Var(within=NonNegativeIntegers)
model.y1 = Var(within=NonNegativeIntegers)
model.y2 = Var(within=NonNegativeIntegers)
model.b1 = Var(within=Boolean)
model.b2 = Var(within=Boolean)
model.c1 = Constraint(expr = model.x1 + model.x2 + model.y1 + model.y2 <= 7000)
model.c2 = Constraint(expr = 2*model.x1 + 2*model.x2 + model.y1 + model.y2 <= 10000)
model.c3 = Constraint(expr = model.x1 <= 2000)
model.c4 = Constraint(expr = model.x2 <= 1000)
model.c5 = Constraint(expr = model.y1 <= 2000)
model.c6 = Constraint(expr = model.y2 <= 3000)
model.z = Objective(expr= (150*model.x1 + 180*model.x2*model.b1 + 100*model.y1 + 110*model.y2*model.b2), sense=maximize)
results = opt.solve(model)
这是我试着写我的约束,然后只使用第一斜率,只要不超过2000产品代码:
def ObjRule(model):
if model.x1 >= 2000:
return model.b1==1
if model.x2 >= 2000:
return model.b2 == 1`
如果有人会有提示,我该如何继续下去会很棒。
预先感谢您, 帕特里克
答
在Pyomo,规则不是发送到求解器的回调。他们被调用一次为每个索引获取一组静态表达式。这组表达式是发送给解算器的。在规则中使用的任何if逻辑都不应包含变量的值(除非它基于变量的初始值,在这种情况下,无论您在主表达式之外使用它,只要将变量包含在value()中这是返回)。
如果你想建立一个分段函数的模型,你需要应用某种建模技巧来做到这一点。在一些情况下,这涉及引入离散变量(参见examples为分段成分),在其它情况下它不(例如最大化可表示为有限数量的仿射函数的最小分段功能时)。
谢谢你的回答。 – pat96