Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Scale named expressions in scaling transformation #3346

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions pyomo/core/plugins/transform/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# ___________________________________________________________________________

from pyomo.common.collections import ComponentMap
from pyomo.core.base import Block, Var, Constraint, Objective, Suffix, value
from pyomo.core.base import Block, Var, Constraint, Objective, Suffix, value, Expression
from pyomo.core.plugins.transform.hierarchy import Transformation
from pyomo.core.base import TransformationFactory
from pyomo.core.base.suffix import SuffixFinder
Expand Down Expand Up @@ -174,7 +174,7 @@ def _apply_to(self, model, rename=True):

already_scaled = set()
for component in model.component_objects(
ctype=(Constraint, Objective), descend_into=True
ctype=(Constraint, Objective, Expression), descend_into=True
):
if component.is_reference():
# Skip any references - these should get picked up when
Expand All @@ -187,13 +187,13 @@ def _apply_to(self, model, rename=True):
continue
already_scaled.add(id(c))
# perform the constraint/objective scaling and variable sub
scaling_factor = component_scaling_factor_map[c]
if c.ctype is Constraint:
scaling_factor = component_scaling_factor_map[c]
body = scaling_factor * replace_expressions(
expr=c.body,
substitution_map=variable_substitution_dict,
descend_into_named_expressions=True,
remove_named_expressions=True,
descend_into_named_expressions=False,
remove_named_expressions=False,
)

# scale the rhs
Expand All @@ -218,11 +218,22 @@ def _apply_to(self, model, rename=True):
c.set_value((lower, body, upper))

elif c.ctype is Objective:
scaling_factor = component_scaling_factor_map[c]
c.expr = scaling_factor * replace_expressions(
expr=c.expr,
substitution_map=variable_substitution_dict,
descend_into_named_expressions=True,
remove_named_expressions=True,
descend_into_named_expressions=False,
remove_named_expressions=False,
)
elif c.ctype is Expression:
# Note that we don't have a scaling factor for Expression objects.
# If we want to add this, Expressions would need to be added to
# component_scaling_factor_map, defined above.
c.expr = replace_expressions(
expr=c.expr,
substitution_map=variable_substitution_dict,
descend_into_named_expressions=False,
remove_named_expressions=False,
)
else:
raise NotImplementedError(
Expand Down
15 changes: 15 additions & 0 deletions pyomo/core/tests/transform/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,21 @@ def test_get_float_scaling_factor_intermediate_level(self):
sf = ScaleModel()._get_float_scaling_factor(m.b1.b2.b3.v3)
assert sf == float(0.3)

def test_scaled_named_expressions(self):
m = pyo.ConcreteModel()
m.x = pyo.Var(initialize=10)
m.y = pyo.Var(initialize=0.1)
m.inner_expr = pyo.Expression(expr=3 + m.x**2 + m.y)
m.sum = pyo.Expression(expr=m.x + m.y + m.inner_expr)
m.scaling_factor = pyo.Suffix(direction=pyo.Suffix.EXPORT)
m.scaling_factor[m.x] = 0.1
m.scaling_factor[m.y] = 10
m_scaled = pyo.TransformationFactory('core.scale_model').create_using(
m, rename=False
)
self.assertAlmostEqual(pyo.value(m.sum), 113.2)
self.assertAlmostEqual(pyo.value(m_scaled.sum), 113.2)


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