Skip to content

Commit

Permalink
Merge branch 'incidence-compute-values' into incidence-amplrepn
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbybp committed Dec 13, 2023
2 parents 9236f4f + bcd2435 commit 4e79fb3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
15 changes: 9 additions & 6 deletions pyomo/contrib/incidence_analysis/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ class IncidenceMethod(enum.Enum):
standard_repn = 1
"""Use ``pyomo.repn.standard_repn.generate_standard_repn``"""

ampl_repn = 2
standard_repn_compute_values = 2
"""Use ``pyomo.repn.standard_repn.generate_standard_repn`` with
``compute_values=True``
"""

ampl_repn = 3
"""Use ``pyomo.repn.plugins.nl_writer.AMPLRepnVisitor``"""


Expand All @@ -42,11 +47,10 @@ class IncidenceMethod(enum.Enum):
_linear_only = ConfigValue(
default=False,
domain=bool,
description="Identify variables that participate linearly",
description="Identify only variables that participate linearly",
doc=(
"Flag indicating whether only variables that participate linearly should"
" be included. Note that these are included even if they participate"
" nonlinearly as well."
" be included."
),
)

Expand All @@ -64,8 +68,7 @@ class IncidenceMethod(enum.Enum):
- ``include_fixed`` -- Flag indicating whether fixed variables should be included
in the incidence graph
- ``linear_only`` -- Flag indicating whether only variables that participate linearly
should be included. Note that these are included even if they participate
nonlinearly as well
should be included.
- ``method`` -- Method used to identify incident variables. Must be a value of the
``IncidenceMethod`` enum.
Expand Down
16 changes: 13 additions & 3 deletions pyomo/contrib/incidence_analysis/incidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def _get_incident_via_identify_variables(expr, include_fixed):
return list(identify_variables(expr, include_fixed=include_fixed))


def _get_incident_via_standard_repn(expr, include_fixed, linear_only):
def _get_incident_via_standard_repn(
expr, include_fixed, linear_only, compute_values=False
):
if include_fixed:
to_unfix = [
var for var in identify_variables(expr, include_fixed=True) if var.fixed
Expand All @@ -41,7 +43,9 @@ def _get_incident_via_standard_repn(expr, include_fixed, linear_only):
context = nullcontext()

with context:
repn = generate_standard_repn(expr, compute_values=False, quadratic=False)
repn = generate_standard_repn(
expr, compute_values=compute_values, quadratic=False
)

linear_vars = []
# Check coefficients to make sure we don't include linear variables with
Expand Down Expand Up @@ -182,7 +186,13 @@ def get_incident_variables(expr, **kwds):
if method is IncidenceMethod.identify_variables:
return _get_incident_via_identify_variables(expr, include_fixed)
elif method is IncidenceMethod.standard_repn:
return _get_incident_via_standard_repn(expr, include_fixed, linear_only)
return _get_incident_via_standard_repn(
expr, include_fixed, linear_only, compute_values=False
)
elif method is IncidenceMethod.standard_repn_compute_values:
return _get_incident_via_standard_repn(
expr, include_fixed, linear_only, compute_values=True
)
elif method is IncidenceMethod.ampl_repn:
return _get_incident_via_ampl_repn(expr, linear_only, visitor=visitor)
else:
Expand Down
13 changes: 12 additions & 1 deletion pyomo/contrib/incidence_analysis/tests/test_incidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_basic_incidence(self):

def test_incidence_with_fixed_variable(self):
m = pyo.ConcreteModel()
m.x = pyo.Var([1, 2, 3])
m.x = pyo.Var([1, 2, 3], initialize=1.0)
expr = m.x[1] + m.x[1] * m.x[2] + m.x[1] * pyo.exp(m.x[3])
m.x[2].fix()
variables = self._get_incident_variables(expr)
Expand Down Expand Up @@ -247,5 +247,16 @@ def _get_incident_variables(self, expr, **kwds):
return get_incident_variables(expr, method=method, **kwds)


class TestIncidenceStandardRepnComputeValues(
unittest.TestCase,
_TestIncidence,
_TestIncidenceLinearOnly,
_TestIncidenceLinearCancellation,
):
def _get_incident_variables(self, expr, **kwds):
method = IncidenceMethod.standard_repn_compute_values
return get_incident_variables(expr, method=method, **kwds)


if __name__ == "__main__":
unittest.main()

0 comments on commit 4e79fb3

Please sign in to comment.