From ff0d0351b687d95678181f07f401f5deb8ee6f17 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Thu, 1 Feb 2024 15:15:03 -0700 Subject: [PATCH 1/2] Fix RangeSet.__len__ when defined by floats --- pyomo/core/base/set.py | 2 +- pyomo/core/tests/unit/test_set.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pyomo/core/base/set.py b/pyomo/core/base/set.py index ba7fdd52446..d820ae8d933 100644 --- a/pyomo/core/base/set.py +++ b/pyomo/core/base/set.py @@ -2668,7 +2668,7 @@ def __len__(self): if r.start == r.end: return 1 else: - return (r.end - r.start) // r.step + 1 + return int((r.end - r.start) // r.step) + 1 else: return sum(1 for _ in self) diff --git a/pyomo/core/tests/unit/test_set.py b/pyomo/core/tests/unit/test_set.py index a1072e7156c..2154c02e659 100644 --- a/pyomo/core/tests/unit/test_set.py +++ b/pyomo/core/tests/unit/test_set.py @@ -1238,6 +1238,9 @@ def __len__(self): # Test types that cannot be case to set self.assertNotEqual(SetOf({3}), 3) + # Test floats + self.assertEqual(RangeSet(0.0, 2.0), RangeSet(0.0, 2.0)) + def test_inequality(self): self.assertTrue(SetOf([1, 2, 3]) <= SetOf({1, 2, 3})) self.assertFalse(SetOf([1, 2, 3]) < SetOf({1, 2, 3})) From 714edcd069677c9d1ac09a67f9e10e4dc2871ff4 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Thu, 1 Feb 2024 15:22:06 -0700 Subject: [PATCH 2/2] Expand the test to mixed-type RangeSets --- pyomo/core/tests/unit/test_set.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyomo/core/tests/unit/test_set.py b/pyomo/core/tests/unit/test_set.py index 2154c02e659..72231bb08d7 100644 --- a/pyomo/core/tests/unit/test_set.py +++ b/pyomo/core/tests/unit/test_set.py @@ -1240,6 +1240,7 @@ def __len__(self): # Test floats self.assertEqual(RangeSet(0.0, 2.0), RangeSet(0.0, 2.0)) + self.assertEqual(RangeSet(0.0, 2.0), RangeSet(0, 2)) def test_inequality(self): self.assertTrue(SetOf([1, 2, 3]) <= SetOf({1, 2, 3}))