Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
2.02k stars 518 forks source link

Unable to extract sets from constraints #2999

Open codykarcher opened 1 year ago

codykarcher commented 1 year ago

Summary

For the latex printer, I am unable to extract set information from a constraint object using identify_components

Rationale

This is necessary for printing individual constraints using the latex printer

Description

The following code:

import pyomo.environ as pyo
from pyomo.common.collections.component_set import ComponentSet
from pyomo.core.expr.template_expr import templatize_constraint
from pyomo.core.expr.visitor import identify_components
from pyomo.core.base.set import _SetData, _SetDataBase

m = pyo.ConcreteModel(name='basicFormulation')

m.I = pyo.Set(initialize=[1, 2, 3, 4, 5])

m.c = pyo.Param(m.I, initialize=1.0, mutable=True)

m.u = pyo.Var(m.I * m.I)
m.v = pyo.Var(m.I)

def ruleMaker(m, j):
    return sum(m.c[i]*m.v[i] + m.u[i, j] ** 2 for i in m.I) <= 0

m.constraint = pyo.Constraint(m.I, rule=ruleMaker)

temp_comp, temp_indexes = templatize_constraint(m.constraint)

setList = []
for s in identify_components(temp_comp, [_SetData]) :
    if s not in ComponentSet(setList):
        setList.append(s)

print([s.__str__() for s in setList])

prints:

[]

and should print:

[I]

Similar code for variables and parameters works fine.

Additional information

N/A

codykarcher commented 1 year ago

Its possible I just have the wrong data type in the identify_components call, but I tried all the ones I could find that made sense.