Skip to content

Commit

Permalink
allow None to override variable value, but log warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbybp committed Jul 15, 2024
1 parent ded5de9 commit c6ddbc9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions pyomo/core/plugins/transform/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

import logging
from pyomo.common.collections import ComponentMap
from pyomo.core.base import Block, Var, Constraint, Objective, Suffix, value
from pyomo.core.plugins.transform.hierarchy import Transformation
Expand All @@ -17,6 +18,8 @@
from pyomo.core.expr import replace_expressions
from pyomo.util.components import rename_components

logger = logging.getLogger("pyomo.core.plugins.transform.scaling")


@TransformationFactory.register(
'core.scale_model', doc="Scale model variables, constraints, and objectives."
Expand Down Expand Up @@ -313,9 +316,14 @@ def propagate_solution(self, scaled_model, original_model):
original_v = original_model.find_component(original_v_path)

for k in scaled_v:
if scaled_v[k].value is not None:
# NOTE: if the variable is set to None in the scaled model,
# we don't attempt to change its value in the original model
if scaled_v[k].value is None and original_v[k].value is not None:
logger.warning(
"Variable with value None in the scaled model is replacing"
f" value of variable {original_v[k].name} in the original"
f" model with None (was {original_v[k].value})."
)
original_v[k].set_value(None, skip_validation=True)
elif scaled_v[k].value is not None:
original_v[k].set_value(
value(scaled_v[k]) / component_scaling_factor_map[scaled_v[k]],
skip_validation=True,
Expand Down
6 changes: 3 additions & 3 deletions pyomo/core/tests/transform/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,9 @@ def test_propagate_solution_uninitialized_variable(self):
scaled_model, m
)
self.assertAlmostEqual(m.x[1].value, 2.0, delta=1e-8)
# Note that because x[2] was None in the scaled model, its value is unchanged
# (and has not been overridden and set to None).
self.assertEqual(m.x[2].value, 1.0)
# Note that value of x[2] in original model *has* been overriddeen to None.
# In this case, a warning has been raised.
self.assertIs(m.x[2].value, None)


if __name__ == "__main__":
Expand Down

0 comments on commit c6ddbc9

Please sign in to comment.