Access all variables occurring in a pyomo constraint

偶尔善良 提交于 2019-12-19 04:51:32

问题


I am working on an algorithm in python that needs to modify concrete (mixed-integer nonlinear) pyomo models. In particular, I need to know which variables are present in a general algebraic constraint. E.g. for a constraint

model.con1 = Constraint(expr=exp(model.x_1) + 2*model.x_2 <= 2)

I would like to make a a query (like model.con1.variables ) which returns (a list of) the variables ([model.x_1,model.x_2]).

In this documentation I find that for linear constraints, the parameter variables exactly serves my purpose. However, the models I'm working with will also contain general algebraic constraints.

Chapter 14 of this book provides several details for the development of high-level algorithms using pyomo, but I did not find an answer to my question there. The only very clumsy approach I can think of is to use the to_string() method of the expression (returning exp( x_1 ) + x_2 <= 1.0 in our example), and then to search this string for the appearence of all variables. I'm sure there is a much better way using pyomo to access the variables occurring in a constraint.

Note that a similar, but less detailed and unanswered question has already been asked here.


回答1:


You do not want to directly query the _args attribute of the expression returned by model.con1.body. Methods and attributes beginning with an underscore are considered private and should not be used by general users (they are undocumented and subject to change with no notice or deprecation warning). Second, the _args attribute only returns the children of that node in the expression tree. For linear expressions, there is a high probability that those are the variables, but it is not guaranteed. For nonlinear expressions (and general expressions), the members of _args are almost guaranteed to be other expression objects.

You can get the variables that appear in any Pyomo expression using the identify_variables generator:

from pyomo.environ import *
from pyomo.core.base.expr import identify_variables

m = ConcreteModel()
m.x_1 = Var()
m.x_2 = Var()
m.c = Constraint(expr=exp(model.x_1) + 2*model.x_2 <= 2)
vars = list(identify_variables(m.c.body))



回答2:


model.con1.body._args gives you just this list of variables.



来源:https://stackoverflow.com/questions/48538945/access-all-variables-occurring-in-a-pyomo-constraint

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