2

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
    The link you provide for linear constraints only applies to the `kernel` interface. That interface is a specialized, low-level programming API that has been proposed for efficiently generating models in a programmatic context. At the moment (as of Pyomo 5.3), it does not necessarily apply to "normal" Pyomo models. – jsiirola Jan 31 '18 at 17:43

2 Answers2

5

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))
jsiirola
  • 2,259
  • 2
  • 9
  • 12
  • 2
    Note that for pyomo version 6.x, the import statement would be like: ```from pyomo.core.expr.current import identify_variables``` – CharlieNeutron Dec 12 '21 at 17:48
0

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

lymbot
  • 31
  • 6
  • There is a pending expression system revamp that may cause such usage to no longer behave as expected. – Qi Chen Jan 31 '18 at 18:31