Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

python - Access all variables occurring in a pyomo constraint

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...