Pyomo and conditional objective function

╄→гoц情女王★ 提交于 2019-12-11 08:08:05

问题


Is it possible (and if so how) to use an objective function that has a conditional expression?

Changing the example from the docs, I would like an expression like:

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)

Can this be modelled directly like this or do I have to consider some sort of transformation (and if so how would this look like)?

Just executing the above gives an error message like:

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):

which I think is because Pyomo thinks I'd like to obtain a value, instead of an expression with the variable.


回答1:


One way to formulate that is by using a logical disjunction. You can look into the Pyomo.GDP documentation for usage, but it would look like:

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)

You can also formulate this using complementarity constraints.



来源:https://stackoverflow.com/questions/46357217/pyomo-and-conditional-objective-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!