Skip to content

Commit

Permalink
remove unused _get_float_scaling_factor method
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiirola committed Aug 13, 2024
1 parent 5fbeb09 commit 84ca6ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
5 changes: 0 additions & 5 deletions pyomo/core/plugins/transform/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ def _create_using(self, original_model, **kwds):
self._apply_to(scaled_model, **kwds)
return scaled_model

def _get_float_scaling_factor(self, component):
if self._suffix_finder is None:
self._suffix_finder = SuffixFinder('scaling_factor', 1.0)
return self._suffix_finder.find(component)

def _apply_to(self, model, rename=True):
# create a map of component to scaling factor
component_scaling_factor_map = ComponentMap()
Expand Down
57 changes: 37 additions & 20 deletions pyomo/core/tests/transform/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import pyomo.common.unittest as unittest
import pyomo.environ as pyo
from pyomo.opt.base.solvers import UnknownSolver
from pyomo.core.plugins.transform.scaling import ScaleModel

from pyomo.core.plugins.transform.scaling import ScaleModel, SuffixFinder

class TestScaleModelTransformation(unittest.TestCase):
def test_linear_scaling(self):
Expand Down Expand Up @@ -600,6 +599,13 @@ def con_rule(m, i):
self.assertAlmostEqual(pyo.value(model.zcon), -8, 4)

def test_get_float_scaling_factor_top_level(self):
# Note: the transformation used to have a private method for
# finding suffix values (which this method tested). The
# transformation now leverages the SuffixFinder. To ensure that
# the SuffixFinder behaves in the same way as the original local
# method, we preserve these tests, but directly test the
# SuffixFinder

m = pyo.ConcreteModel()
m.scaling_factor = pyo.Suffix(direction=pyo.Suffix.EXPORT)

Expand All @@ -616,17 +622,23 @@ def test_get_float_scaling_factor_top_level(self):
m.scaling_factor[m.v1] = 0.1
m.scaling_factor[m.b1.v2] = 0.2

_finder = SuffixFinder('scaling_factor', 1.0, m)

# SF should be 0.1 from top level
sf = ScaleModel()._get_float_scaling_factor(m.v1)
assert sf == float(0.1)
self.assertEqual(_finder.find(m.v1), 0.1)
# SF should be 0.1 from top level, lower level ignored
sf = ScaleModel()._get_float_scaling_factor(m.b1.v2)
assert sf == float(0.2)
self.assertEqual(_finder.find(m.b1.v2), 0.2)
# No SF, should return 1
sf = ScaleModel()._get_float_scaling_factor(m.b1.b2.v3)
assert sf == 1.0
self.assertEqual(_finder.find(m.b1.b2.v3), 1.0)

def test_get_float_scaling_factor_local_level(self):
# Note: the transformation used to have a private method for
# finding suffix values (which this method tested). The
# transformation now leverages the SuffixFinder. To ensure that
# the SuffixFinder behaves in the same way as the original local
# method, we preserve these tests, but directly test the
# SuffixFinder

m = pyo.ConcreteModel()
m.scaling_factor = pyo.Suffix(direction=pyo.Suffix.EXPORT)

Expand All @@ -647,15 +659,21 @@ def test_get_float_scaling_factor_local_level(self):
# Add an intermediate scaling factor - this should take priority
m.b1.scaling_factor[m.b1.b2.v3] = 0.4

_finder = SuffixFinder('scaling_factor', 1.0, m)

# Should get SF from local levels
sf = ScaleModel()._get_float_scaling_factor(m.v1)
assert sf == float(0.1)
sf = ScaleModel()._get_float_scaling_factor(m.b1.v2)
assert sf == float(0.2)
sf = ScaleModel()._get_float_scaling_factor(m.b1.b2.v3)
assert sf == float(0.4)
self.assertEqual(_finder.find(m.v1), 0.1)
self.assertEqual(_finder.find(m.b1.v2), 0.2)
self.assertEqual(_finder.find(m.b1.b2.v3), 0.4)

def test_get_float_scaling_factor_intermediate_level(self):
# Note: the transformation used to have a private method for
# finding suffix values (which this method tested). The
# transformation now leverages the SuffixFinder. To ensure that
# the SuffixFinder behaves in the same way as the original local
# method, we preserve these tests, but directly test the
# SuffixFinder

m = pyo.ConcreteModel()
m.scaling_factor = pyo.Suffix(direction=pyo.Suffix.EXPORT)

Expand All @@ -680,15 +698,14 @@ def test_get_float_scaling_factor_intermediate_level(self):

m.b1.b2.b3.scaling_factor[m.b1.b2.b3.v3] = 0.4

_finder = SuffixFinder('scaling_factor', 1.0, m)

# v1 should be unscaled as SF set below variable level
sf = ScaleModel()._get_float_scaling_factor(m.v1)
assert sf == 1.0
self.assertEqual(_finder.find(m.v1), 1.0)
# v2 should get SF from b1 level
sf = ScaleModel()._get_float_scaling_factor(m.b1.b2.b3.v2)
assert sf == float(0.2)
self.assertEqual(_finder.find(m.b1.b2.b3.v2), 0.2)
# v2 should get SF from highest level, ignoring b3 level
sf = ScaleModel()._get_float_scaling_factor(m.b1.b2.b3.v3)
assert sf == float(0.3)
self.assertEqual(_finder.find(m.b1.b2.b3.v3), 0.3)


if __name__ == "__main__":
Expand Down

0 comments on commit 84ca6ae

Please sign in to comment.