是否可以(如果可以)使用具有条件表达式的目标函数?
更改文档中的示例,我想要如下表达式:
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 认为我想获得一个值,而不是带有变量的表达式。