Reputation: 47
My question is simple. Given a Pyomo constraint, how can I easily extract the variables that appear in the constraint?
This question has been asked a few times already. I believe the Pyomo internals have been modified and the proposed solutions no longer work.
How to get variables of a constraint in Pyomo
Access all variables occurring in a pyomo constraint
Minimum working test problem:
from pyomo.environ import *
m = ConcreteModel()
m.I = Set(initialize=[i for i in range(5)])
m.x = Var(m.I,bounds=(-10,10),initialize=1.0)
m.z = Var(bounds=(-100,100), initialize=5.0)
m.con1 = Constraint(expr=m.x[0] + m.x[1] - m.x[3] >= 10)
m.con2 = Constraint(expr=m.x[0]*m.x[3] + m.x[1] >= 0)
m.con3 = Constraint(expr=m.x[4]*m.x[3] + m.x[0]*m.x[3] - m.x[4] == 0)
m.obj = Objective(expr=sum(m.x[i]**2 for i in m.I))
m.pprint()
opt = SolverFactory('ipopt')
opt.options['max_iter'] = 0
opt.solve(m, tee=True)
In this example, I would like to programmatically inspect the variables in con1
.
Upvotes: 0
Views: 175
Reputation: 1718
The second link has the correct solution: Access all variables occurring in a pyomo constraint
identify_variables()
still exists, but it looks like it was moved to pyomo.core.expr.visitor
.
It might be worth promoting it into the pyomo.core.expr
namespace.
Upvotes: 1