2

是否可以(如果可以)使用具有条件表达式的目标函数?

更改文档中的示例,我想要如下表达式:

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 认为我想获得一个值,而不是带有变量的表达式。

4

1 回答 1

1

一种表达方式是使用逻辑析取。您可以查看 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)

您还可以使用互补性约束来制定这一点。

于 2017-09-25T03:51:45.037 回答