pyomo icon indicating copy to clipboard operation
pyomo copied to clipboard

Clarify Output of `generate_standard_repn(compute_values=False)` for Expressions with Mutable Params

Open shermanjasonaf opened this issue 2 years ago • 0 comments

Summary

Consider the following setup:

>>> import pyomo.environ as pyo
>>> from pyomo.repn.standard_repn import generate_standard_repn
>>> m.v = pyo.Var()
>>> m.p = pyo.Param(initialize=1, mutable=True)
>>> m.con = pyo.Constraint(expr=m.v * m.v * m.p == 0)
>>> print(generate_standard_repn(m.con.body, compute_values=False))
constant:       p*0*0
linear vars:    []
linear var ids: []
linear coef:    []
quadratic vars:    [('v', 'v')]
quadratic var ids: [(139757446926000, 139757446926000)]
quadratic coef:    [<pyomo.core.base.param.ScalarParam object at 0x7f1bd157d680>]
nonlinear expr:
p*(p*0)*v + p*(p*0)*v
nonlinear vars: ['v']

As the snippet shows, generate_standard_repn returns a representation with a nonempty list of nonlinear Vars. This may lead one to believe that the nonlinearities of m.con.body are beyond quadratic in nature. The accuracy of contrib.pyros.util.coefficient_matching, which utilizes generate_standard_repn to analyze constraint expressions, may be affected.

I originally expected generate_standard_repn would return

>>> print(generate_standard_repn(m.con.body, compute_values=False))
constant:       0
linear vars:    []
linear var ids: []
linear coef:    []
quadratic vars:    [('v', 'v')]
quadratic var ids: [(139757446926000, 139757446926000)]
quadratic coef:    [<pyomo.core.base.param.ScalarParam object at 0x7f1bd157d680>]
nonlinear expr: None
nonlinear vars: []

At the very least, it would be nice to add a docstring to the function generate_standard_repn() to clarify the meanings of the function's parameters.

Information on your system

Pyomo version: 6.7.1dev0 @ 1e259f6c1 Python version: 3.9.17 Operating system: Ubuntu 20.04 How Pyomo was installed (PyPI, conda, source): source Solver (if applicable): N/A

shermanjasonaf avatar Feb 08 '24 18:02 shermanjasonaf