Skip to content
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
53bbc30
Move bounds calculation to separate, cached method
jas-yao Mar 20, 2026
b6a03aa
Clear any cached bounds at set validation
jas-yao Mar 20, 2026
65e9564
Add tests checking caching.
jas-yao Mar 20, 2026
a483ab6
Fix _fbbt_parameter_bounds bound value issues
jas-yao Mar 20, 2026
549d5b8
Run black
jas-yao Mar 20, 2026
bb7fca5
Fix test comments
jas-yao Mar 20, 2026
a6ef4f5
Merge branch 'main' into pyros-cache-computed-param-bounds
jas-yao Mar 22, 2026
3a955f6
Update _solve_bounds_optimization docstring
jas-yao Mar 26, 2026
d003145
Merge branch 'main' into pyros-cache-computed-param-bounds
jsiirola Apr 2, 2026
c573512
Merge branch 'main' into pyros-cache-computed-param-bounds
jas-yao May 6, 2026
f0e86b6
Update bounds optimization caching
jas-yao May 6, 2026
2af61c8
Add test_solve_exact_bounds_optimization
jas-yao May 6, 2026
bb53c9d
Add test_fbbt_values
jas-yao May 6, 2026
be21c04
Move cache clearing to before/after solving
jas-yao May 6, 2026
7619f3e
Add tests for PyROS caching
jas-yao May 6, 2026
81a98ab
Run black
jas-yao May 6, 2026
4f78486
Run updated black
jas-yao May 6, 2026
581a9f7
Update CHANGELOG
jas-yao May 6, 2026
7aeeef5
Merge branch 'main' into pyros-cache-computed-param-bounds
jsiirola May 8, 2026
6de83b7
Merge branch 'main' into pyros-cache-computed-param-bounds
jas-yao May 11, 2026
31e96fd
Update caching with custom dict
jas-yao May 11, 2026
43f09d8
Simplify cache clearing setup
jas-yao May 11, 2026
ee87245
Update caching tests
jas-yao May 11, 2026
c1145cd
Run black
jas-yao May 11, 2026
d905ab4
Merge branch 'main' into pyros-cache-computed-param-bounds
jas-yao May 12, 2026
5518ee8
Use `var.lb` and `var.ub` for numerical bounds
jas-yao May 12, 2026
d954aba
Add assertion check for empty _cache
jas-yao May 12, 2026
c242b6a
Update caching tests for assertion error test
jas-yao May 12, 2026
e0863b0
Run black
jas-yao May 12, 2026
4ebca37
Merge branch 'main' into pyros-cache-computed-param-bounds
jsiirola May 14, 2026
6259595
Merge branch 'main' into pyros-cache-computed-param-bounds
jas-yao May 15, 2026
43079be
Apply suggestion from @shermanjasonaf
jas-yao May 15, 2026
32b278b
Apply descriptive TestPyROSCacheUncertaintySetBounds name and docstring
jas-yao May 15, 2026
15c0218
Update caching unit tests.
jas-yao May 15, 2026
d12f066
Merge branch 'main' into pyros-cache-computed-param-bounds
jas-yao May 15, 2026
acecc15
Update CHANGELOG
jas-yao May 15, 2026
60d2491
Update uncertainty set caching documentation
jas-yao May 15, 2026
f55f97c
Run black
jas-yao May 15, 2026
c152970
Update uncertaintyset cache manager design to support CartesianProduc…
jsiirola May 15, 2026
1632ba1
NFC: fix typo
jsiirola May 15, 2026
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
6 changes: 6 additions & 0 deletions pyomo/contrib/pyros/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ PyROS CHANGELOG
===============


-------------------------------------------------------------------------------
PyROS 1.3.14 20 Mar 2026
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After PR #3951 is either merged or closed, you may update the PyROS version number (in pyros.py) and further update the changelog here according to this comment. In particular, if #3951 is closed rather than merged, then the final version number here will be 1.3.14.

-------------------------------------------------------------------------------
- Add caching for uncertainty set parameter bounds


-------------------------------------------------------------------------------
PyROS 1.3.13 16 Jan 2026
-------------------------------------------------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions pyomo/contrib/pyros/pyros.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,13 @@ def solve(
)

model_data = ModelData(original_model=model, timing=TimingData(), config=None)
with time_code(
timing_data_obj=model_data.timing,
code_block_name="main",
is_main_timer=True,
with (
uncertainty_set._cache,
time_code(
timing_data_obj=model_data.timing,
code_block_name="main",
is_main_timer=True,
),
):
kwds.update(
dict(
Expand Down
267 changes: 266 additions & 1 deletion pyomo/contrib/pyros/tests/test_grcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3899,7 +3899,7 @@ def solve(self, model, *args, **kwargs):

class TestPyROSUnavailableSubsolvers(unittest.TestCase):
"""
Check that appropriate exceptionsa are raised if
Check that appropriate exceptions are raised if
PyROS is invoked with unavailable subsolvers.
"""

Expand Down Expand Up @@ -5146,5 +5146,270 @@ def test_discrete_set_subsolver_error_recovery(self, name, sec_con_UB):
)


# @SolverFactory.register("slow_solver")
class SlowSolver:
"""
Solver which sleeps for a specified time before solving.
"""

def __init__(self, sleep_time, sub_solver):
self.sleep_time = sleep_time
self.sub_solver = sub_solver

self.options = Bunch()

def available(self, exception_flag=True):
return True

def license_is_valid(self):
return True

def __enter__(self):
return self

def __exit__(self, et, ev, tb):
pass

def solve(self, model, **kwargs):
"""
Sleep, then solve a model.

Parameters
----------
model : ConcreteModel
Model of interest.

Returns
-------
results : SolverResults
Solver results.
"""

# ensure only one active objective
active_objs = [
obj for obj in model.component_data_objects(Objective, active=True)
]
assert len(active_objs) == 1

# sleep for specified time
time.sleep(self.sleep_time)

print("I slept. Now, I will solve.")
# invoke subsolver
results = self.sub_solver.solve(model, **kwargs)

return results


class CustomExactBoundsUncertaintySet(BoxSet):
"""
Custom uncertainty set that always solves optimization bounding problems.
Comment thread
jas-yao marked this conversation as resolved.
Outdated
"""

def __init__(self, bounds, sleep_time, cache):
super().__init__(bounds)
self.sleep_time = sleep_time
self.cache = cache

@property
def parameter_bounds(self):
"""
Solve bounding problems to calculate exact parameter bounds.
"""
solver = SlowSolver(
sub_solver=SolverFactory("baron"), sleep_time=self.sleep_time
Comment thread
jas-yao marked this conversation as resolved.
Outdated
)
bounds = self._compute_exact_parameter_bounds(solver=solver)

if not self.cache:
self._cache.clear()

return bounds


@unittest.skipUnless(ipopt_available, "IPOPT is not available.")
class TestPyROSCache(unittest.TestCase):
"""
Test PyROS cache creation and clearing.
"""
Comment thread
jas-yao marked this conversation as resolved.
Outdated

def test_pyros_cache_creation(self):
"""
Check that PyROS creates a cache for storing computed exact parameter bounds.
"""
m = build_leyffer_two_cons()

# Define the uncertainty set
interval = CustomExactBoundsUncertaintySet(
bounds=[(0.25, 2)], sleep_time=0, cache=True
)

# Instantiate the PyROS solver
pyros_solver = SolverFactory("pyros")

# Define subsolvers utilized in the algorithm
local_subsolver = SolverFactory("ipopt")
global_subsolver = SolverFactory("ipopt")

# check cache exists
self.assertTrue(hasattr(interval, "_cache"))

# Call the PyROS solver
results = pyros_solver.solve(
Comment thread
jas-yao marked this conversation as resolved.
Outdated
model=m,
first_stage_variables=[m.x1],
second_stage_variables=[m.x2],
uncertain_params=[m.u],
uncertainty_set=interval,
local_solver=local_subsolver,
global_solver=global_subsolver,
options={
"objective_focus": ObjectiveType.worst_case,
"solve_master_globally": True,
},
)

# check cache has been cleared after solve
self.assertTrue(hasattr(interval, "_cache"))
self.assertEqual(
interval._cache, {}, msg="Did not clear uncertainty set cache after solve."
)

def test_pyros_cache_time(self):
"""
Check that caching improves solve time.
Comment thread
jas-yao marked this conversation as resolved.
Outdated
"""
m = build_leyffer_two_cons()

# Define the uncertainty set
interval_cache = CustomExactBoundsUncertaintySet(
bounds=[(0.25, 2)], sleep_time=0.1, cache=True
)
interval_no_cache = CustomExactBoundsUncertaintySet(
bounds=[(0.25, 2)], sleep_time=0.1, cache=False
)

# Instantiate the PyROS solver
pyros_solver = SolverFactory("pyros")

# Define subsolvers utilized in the algorithm
local_subsolver = SolverFactory("ipopt")
global_subsolver = SolverFactory("ipopt")

# Call the PyROS solver
results_cache = pyros_solver.solve(
model=m,
first_stage_variables=[m.x1],
second_stage_variables=[m.x2],
uncertain_params=[m.u],
uncertainty_set=interval_cache,
local_solver=local_subsolver,
global_solver=global_subsolver,
options={
"objective_focus": ObjectiveType.worst_case,
"solve_master_globally": True,
},
)

results_no_cache = pyros_solver.solve(
model=m,
first_stage_variables=[m.x1],
second_stage_variables=[m.x2],
uncertain_params=[m.u],
uncertainty_set=interval_no_cache,
local_solver=local_subsolver,
global_solver=global_subsolver,
options={
"objective_focus": ObjectiveType.worst_case,
"solve_master_globally": True,
},
)

# caching should always result in less time,
# as not caching reruns the slow solver multiple times
self.assertGreater(results_no_cache.time, results_cache.time)

def test_pyros_cache_solutions(self):
"""
Check that PyROS clears cache before/after and yields accurate results.
"""
Comment thread
jas-yao marked this conversation as resolved.
Outdated
m = build_leyffer_two_cons()

# Define the uncertainty set
interval = CustomExactBoundsUncertaintySet(
bounds=[(25, 200)], sleep_time=0, cache=True
)
self.assertEqual(interval.parameter_bounds, [(25, 200)])

# change set attributes, leading to outdated parameter bounds
interval.bounds = [(0.25, 2)]
self.assertEqual(interval.parameter_bounds, [(25, 200)])

# Instantiate the PyROS solver
pyros_solver = SolverFactory("pyros")

# Define subsolvers utilized in the algorithm
local_subsolver = SolverFactory("ipopt")
global_subsolver = SolverFactory("ipopt")

# Solve with PyROS
results = pyros_solver.solve(
model=m,
first_stage_variables=[m.x1],
second_stage_variables=[m.x2],
uncertain_params=[m.u],
uncertainty_set=interval,
local_solver=local_subsolver,
global_solver=global_subsolver,
options={
"objective_focus": ObjectiveType.worst_case,
"solve_master_globally": True,
},
)

# check results, which should use the correct parameter bounds
self.assertEqual(results.iterations, 3)
self.assertAlmostEqual(results.final_objective_value, 0.531, places=2)
self.assertAlmostEqual(m.x1.value, 3.518, places=2)
self.assertAlmostEqual(m.x2.value, 1.547, places=2)
self.assertAlmostEqual(m.x3.value, 9.684, places=2)
self.assertEqual(
results.pyros_termination_condition,
pyrosTerminationCondition.robust_optimal,
)

Comment thread
jas-yao marked this conversation as resolved.
# modify the cache
interval._cache[0, minimize] = 25
interval._cache[0, maximize] = 200

self.assertEqual(interval.parameter_bounds, [(25, 200)])

# Solve with PyROS
results = pyros_solver.solve(
model=m,
first_stage_variables=[m.x1],
second_stage_variables=[m.x2],
uncertain_params=[m.u],
uncertainty_set=interval,
local_solver=local_subsolver,
global_solver=global_subsolver,
options={
"objective_focus": ObjectiveType.worst_case,
"solve_master_globally": True,
},
)

# check results, which should not change
self.assertEqual(results.iterations, 3)
self.assertAlmostEqual(results.final_objective_value, 0.531, places=2)
self.assertAlmostEqual(m.x1.value, 3.518, places=2)
self.assertAlmostEqual(m.x2.value, 1.547, places=2)
self.assertAlmostEqual(m.x3.value, 9.684, places=2)
self.assertEqual(
results.pyros_termination_condition,
pyrosTerminationCondition.robust_optimal,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add here a test based on a problem with a CartesianProductSet, to formally check that CartesianProductSet._cache_manager() works according to expectations. Something like:

diff --git a/pyomo/contrib/pyros/tests/test_grcs.py b/pyomo/contrib/pyros/tests/test_grcs.py
index 410593c82..8f577d879 100644
--- a/pyomo/contrib/pyros/tests/test_grcs.py
+++ b/pyomo/contrib/pyros/tests/test_grcs.py
@@ -75,6 +75,7 @@ from pyomo.contrib.pyros.uncertainty_sets import (
     FactorModelSet,
     Geometry,
     IntersectionSet,
+    PolyhedralSet,
     UncertaintyQuantification,
     UncertaintySet,
 )
@@ -5468,6 +5469,71 @@ class TestPyROSCacheUncertaintySetBounds(unittest.TestCase):
         self.assertAlmostEqual(interval._cache[0, minimize], 25, places=2)
         self.assertAlmostEqual(interval._cache[0, maximize], 200, places=2)
 
+    def test_solve_cartesian_product_set_bounds_cache(self):
+        """
+        Test management of uncertainty set bounds caches
+        is carried out as expected in the context of a PyROS solve.
+        """
+        # deterministic model
+        m = ConcreteModel()
+        m.q = Param(range(4), initialize=0, mutable=True)
+        m.x = Var(initialize=0, bounds=(0, 100))
+        m.obj = Objective(expr=m.x, sense=minimize)
+        m.con = Constraint(expr=m.x >= sum(m.q.values()))
+
+        # uncertainty set(s)
+        poly_set = PolyhedralSet(
+            # this is just the cube [-1, 1]^3
+            lhs_coefficients_mat=np.vstack([np.eye(3), -np.eye(3)]),
+            rhs_vec=[1] * 6,
+        )
+        # inclusion of PolyhedralSet means bounds caching takes place
+        cpset = CartesianProductSet([BoxSet([[0, 1]]), poly_set])
+        res1 = SolverFactory("pyros").solve(
+            model=m,
+            first_stage_variables=m.x,
+            second_stage_variables=[],
+            uncertain_params=m.q,
+            uncertainty_set=cpset,
+            local_solver=SolverFactory("ipopt"),
+            global_solver=SolverFactory("ipopt"),
+            objective_focus="worst_case",
+            solve_master_globally=True,
+        )
+
+        # check caches cleared
+        self.assertEqual(cpset._cache, {})
+        self.assertEqual(cpset._all_sets[0]._cache, {})
+        self.assertEqual(cpset._all_sets[1]._cache, {})
+        # check results: worst case objective is just sum of the
+        #                uncertainty set upper bounds
+        self.assertEqual(res1.iterations, 2)
+        self.assertAlmostEqual(res1.final_objective_value, 4.0, places=6)
+        self.assertAlmostEqual(m.x.value, 4.0, places=6)
+
+        # expand the polyhedralset to the cube [-2, 2]^3
+        poly_set.rhs_vec = [2] * 6
+        # solve again. since caches cleared, PyROS should work normally
+        res2 = SolverFactory("pyros").solve(
+            model=m,
+            first_stage_variables=m.x,
+            second_stage_variables=[],
+            uncertain_params=m.q,
+            uncertainty_set=cpset,
+            local_solver=SolverFactory("ipopt"),
+            global_solver=SolverFactory("ipopt"),
+            objective_focus="worst_case",
+            solve_master_globally=True,
+        )
+        # check caches cleared
+        self.assertEqual(cpset._cache, {})
+        self.assertEqual(cpset._all_sets[0]._cache, {})
+        self.assertEqual(cpset._all_sets[1]._cache, {})
+        # results have changed, since the polyhedral set was expanded
+        self.assertEqual(res2.iterations, 2)
+        self.assertAlmostEqual(res2.final_objective_value, 7.0, places=6)
+        self.assertAlmostEqual(m.x.value, 7.0, places=6)
+
 
 if __name__ == "__main__":
     unittest.main()


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