是否可以(如果可以的话如何)使用具有条件表达式的目标函数?
更改文档中的示例,我想要一个如下表达式:
def objective_function(model):
return model.x[0] if model.x[1] < const else model.x[2]
model.Obj = Objective(rule=objective_function, sense=maximize)
可以像这样直接建模吗?或者我是否必须考虑某种转换(如果是的话,这会是什么样子)?
只需执行上面的命令就会出现如下错误消息:
Evaluating Pyomo variables in a Boolean context, e.g.
if expression <= 5:
is generally invalid. If you want to obtain the Boolean value of the
expression based on the current variable values, explicitly evaluate the
expression using the value() function:
if value(expression) <= 5:
or
if value(expression <= 5):
我认为这是因为 Pyomo 认为我想获取一个值,而不是带有变量的表达式。
表达这一点的一种方法是使用逻辑析取。您可以查看 Pyomo.GDP 文档以了解用法,但它看起来像:
m.helper_var = Var()
m.obj = Objective(expr=m.helper_var)
m.lessthan = Disjunct()
m.lessthan.linker = Constraint(expr=m.helper_var == m.x[0])
m.lessthan.constr = Constraint(expr=m.x[1] < const)
m.greaterthan = Disjunct()
m.greaterthan.linker = Constraint(expr=m.helper_var == m.x[2])
m.greaterthan.constr = Constraint(expr=m.x[1] >= const)
m.lessthanorgreaterthan = Disjunction(expr=[m.lessthan, m.greaterthan])
# some kind of transformation (convex hull or big-M)
您还可以使用互补约束来表述这一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)