Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
51c9a51
Add `UncertaintySet` method for checking positive measure
shermanjasonaf Nov 19, 2026
ef02ba6
Perform PyROS coeff matching only for known positive measure sets
shermanjasonaf Nov 19, 2026
bc142f4
Fix master problem DR variable fixing and polishing rules
shermanjasonaf Nov 19, 2026
da65ab5
Merge branch 'Pyomo:main' into pyros-fix-dr-treatment-coeff-matching
shermanjasonaf Nov 22, 2026
9641cf2
Extend sufficient for positive `FactorModelSet` measure
shermanjasonaf Nov 22, 2026
1c5789b
Clarify documentation of `UncertaintySet.dim`
shermanjasonaf Nov 22, 2026
5332cba
Remove loose "todo" from `CartesianProductSet._has_positive_measure()`
shermanjasonaf Nov 22, 2026
72b9292
Update comment under `MasterProblemData.solve_dr_polishing()`
shermanjasonaf Nov 22, 2026
6213634
Enhance PyROS `get_master_dr_degree()` function
shermanjasonaf Nov 22, 2026
f0a92e3
Fix code comment in `get_master_dr_degree()` function
shermanjasonaf Nov 22, 2026
24746d6
Simplify quadratic DR variable fixing rule
shermanjasonaf Nov 23, 2026
bbd9042
Make PyROS DR freezing rule account for single unique scenario
shermanjasonaf Nov 23, 2026
5decca9
Add comment on DR variable fixing application to subproblems
shermanjasonaf Nov 23, 2026
7f6f34b
Fix typos in PyROS code edits
shermanjasonaf Nov 23, 2026
a34168b
Tweak DR fixing rule test assertion
shermanjasonaf Nov 23, 2026
5d97668
Fix numpy import
shermanjasonaf Nov 23, 2026
9546266
Merge branch 'main' into pyros-fix-dr-treatment-coeff-matching
shermanjasonaf Nov 23, 2026
3e77b4e
Fix DR fixing rule test solver licensing check
shermanjasonaf Nov 25, 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
116 changes: 85 additions & 31 deletions pyomo/contrib/pyros/master_problem_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Functions for construction and solution of the PyROS master problem.
"""

from pyomo.common.dependencies import numpy as np
from pyomo.common.collections import ComponentMap, ComponentSet
from pyomo.common.modeling import unique_component_name
from pyomo.core import TransformationFactory
Expand Down Expand Up @@ -579,24 +580,16 @@ def minimize_dr_vars(master_data):

def get_master_dr_degree(master_data):
"""
Determine DR polynomial degree to enforce based on
the iteration number and/or the presence of first-stage
equality constraints that depend on the decision rule variables.
Determine degree of the decision rule polynomials that should
be enforced given the current state of the master model's
uncertain parameter realizations.

If there are first-stage equality constraints that depend
Also, if there are first-stage equality constraints that depend
on the decision rule variables, such as equalities derived
from coefficient matching or discretization of state-variable
independent equalities, then the degree is set to
independent equalities, then the degree is always set to
``config.decision_rule_order``.

Otherwise, the degree is set to:

- 0 if iteration number is 0
- min(1, config.decision_rule_order) if iteration number
otherwise does not exceed number of effective
uncertain parameters
- min(2, config.decision_rule_order) otherwise.

Parameters
----------
master_data : MasterProblemData
Expand All @@ -611,31 +604,65 @@ def get_master_dr_degree(master_data):
if nom_scenario_blk.first_stage.dr_dependent_equality_cons:
return master_data.config.decision_rule_order

if master_data.iteration == 0:
if not len(nom_scenario_blk.first_stage.decision_rule_vars):
return 0
elif master_data.iteration <= len(nom_scenario_blk.effective_uncertain_params):
return min(1, master_data.config.decision_rule_order)

scenario_blks = master_data.master_model.scenarios
if len(scenario_blks) == 1 or master_data.config.decision_rule_order == 0:
return 0
elif master_data.config.decision_rule_order == 1:
return 1
elif master_data.config.decision_rule_order == 2:
# restricting quadratic decision rules to affine rules
# is nonconservative if (and only if)
# the unique sampled problem scenarios are affinely independent.
# to avoid conservatism, it is safer to err on the side of
# allowing quadratic decision rules

# note: we are erring on the side of caution by
# using a zero deduplication tolerance.
# allowing scenarios that are nearly equal increases the
# scenario count and leads rank-based checks
# to err on side of affirming affine dependence
unique_scenarios = np.unique(
[
[param.value for param in blk.effective_uncertain_params]
for blk in scenario_blks.values()
],
axis=0,
)

return 1 + (
# singleton set is always affinely independent
len(unique_scenarios) > 1
and (
# too many "unique" scenarios for affine independence
len(unique_scenarios)
>= 2 + len(nom_scenario_blk.effective_uncertain_dimensions)
or
# rank check reveals affine dependence
np.linalg.matrix_rank(unique_scenarios[1:] - unique_scenarios[0])
!= len(unique_scenarios) - 1
)
)
else:
return min(2, master_data.config.decision_rule_order)
raise ValueError(
f"Decision rule order {master_data.config.decision_rule_order} not supported."
)


def higher_order_decision_rule_efficiency(master_data):
"""
Enforce DR coefficient variable efficiencies for
master problem-like formulation.
"Fix" a subset of the higher-order decision rule coefficients,
based on the current state of the master problem's added
scenarios and whether there are decision-rule-variable-dependent
equality constraints that were derived by the preprocessor's
equality constraint reformulations.

Parameters
----------
master_data : MasterProblemData
Master problem data.

Note
----
The DR coefficient variable efficiencies consist of
setting the degree of the DR polynomial expressions
by fixing the appropriate variables to 0. The degree
to be set depends on the iteration number;
see ``get_master_dr_degree``.
"""
order_to_enforce = get_master_dr_degree(master_data)
enforce_dr_degree(
Expand Down Expand Up @@ -775,8 +802,6 @@ def solver_call_master(master_data):
solve_mode = "global" if config.solve_master_globally else "local"
config.progress_logger.debug("Solving master problem")

higher_order_decision_rule_efficiency(master_data)

for idx, opt in enumerate(solvers):
if idx > 0:
config.progress_logger.debug(
Expand Down Expand Up @@ -874,6 +899,13 @@ def solve_master(master_data):
master_soln : MasterResults
Master problem solve results.
"""
# apply decision rule variable fixing here.
# note: the master feasibility and DR polishing models
# are later cloned from the master model,
# so the decision rule variable fixing
# is implicitly applied to those, as well.
higher_order_decision_rule_efficiency(master_data)

feasibility_problem_results = None
time_out_after_feasibility = False
if master_data.iteration > 0:
Expand Down Expand Up @@ -937,6 +969,28 @@ def solve_master(self):

def solve_dr_polishing(self):
"""
Solve the DR polishing problem.
"Polish" the decision rules, as needed.

Returns
-------
polishing_solver_res : SolverResults or None
If the polishing problem was set up and solved,
the SolverResults object produced by the subsolver
is returned. Otherwise, None is returned.
polishing_successful : bool
True if polishing was deemed successful, False otherwise.
"""
return minimize_dr_vars(self)
nom_scenario_blk = self.master_model.scenarios[0, 0]
if (
# there are DR variables in the model
nom_scenario_blk.first_stage.decision_rule_vars
and (
# there are unfixed non-static DR variables
len(self.master_model.scenarios) > 1
or nom_scenario_blk.first_stage.dr_dependent_equality_cons
)
):
return minimize_dr_vars(self)
else:
# static DRs are considered always already "polished"
return None, True
9 changes: 1 addition & 8 deletions pyomo/contrib/pyros/pyros_algorithm_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,7 @@ def ROSolver_iterative_solve(model_data):
iterations=k + 1,
)

polishing_successful = True
polish_master_solution = (
config.decision_rule_order != 0
and nominal_master_blk.first_stage.decision_rule_vars
and k != 0
)
if polish_master_solution:
_, polishing_successful = master_data.solve_dr_polishing()
_, polishing_successful = master_data.solve_dr_polishing()

# track variable values
current_iter_var_data = get_variable_value_data(
Expand Down
130 changes: 130 additions & 0 deletions pyomo/contrib/pyros/tests/test_grcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,46 @@ def test_higher_order_decision_rules(self):
msg="Returned termination condition is not return robust_optimal.",
)

@unittest.skipUnless(baron_license_is_valid, "BARON is not available and licensed.")
def test_quadratic_dr_fixing_rule_correct(self):
m = ConcreteModel()
m.q = Param(range(2), initialize=0, mutable=True)
# test consideration for effective uncertain parameters
m.qfix = Param(initialize=0, mutable=True)
m.z = Var()
m.obj = Objective(expr=m.z)
m.con1 = Constraint(expr=m.z <= sum(q**2 for q in m.q.values()))
m.con2 = Constraint(expr=m.z >= -sum((q + 2) ** 2 for q in m.q.values()) + 1)
m.con3 = Constraint(expr=m.z >= -sum((q - 2) ** 2 for q in m.q.values()) + 1)
res = SolverFactory("pyros").solve(
model=m,
first_stage_variables=[],
second_stage_variables=m.z,
uncertain_params=[m.q, m.qfix],
# define set to make only q effectively uncertain
uncertainty_set=BoxSet([[-2, 2]] * len(m.q) + [[0, 0]]),
local_solver="ipopt",
global_solver="baron",
decision_rule_order=2,
objective_focus="worst_case",
solve_master_globally=True,
bypass_local_separation=True,
robust_feasibility_tolerance=1e-4,
# to confirm the DR fixing rule is correct,
# just need to confirm robust infeasible termination
# does not occur before this many iterations.
# test is otherwise expensive
max_iter=5,
)
self.assertIn(
res.pyros_termination_condition,
{
pyrosTerminationCondition.max_iter,
pyrosTerminationCondition.robust_optimal,
},
)
self.assertLessEqual(res.iterations, 5)

@unittest.skipUnless(scip_available, "Global NLP solver is not available.")
def test_coefficient_matching_solve(self):
# Write the deterministic Pyomo model
Expand Down Expand Up @@ -2138,6 +2178,96 @@ def test_coefficient_matching_singleton_set(self):
self.assertEqual(results2.iterations, 0)
self.assertEqual(results2.final_objective_value, None)

@unittest.skipUnless(scip_available, "SCIP is not available.")
def test_coefficient_matching_measure_0_set(self):
ndim = 2
m = ConcreteModel()
m.q = Param(range(ndim), initialize=0, mutable=True)
m.x = Var(range(ndim), initialize=0, bounds=(-2, 2))
m.obj = Objective(expr=m.x[0])
m.certain_eq = Constraint(expr=sum(m.x.values()) == 0)
m.p = Param(initialize=0, mutable=True)
m.uncertain_eq = Constraint(
# this constraint is subject to coefficient matching,
# since there are no state variables and there are
# uncertain parameters.
expr=sum(m.q[idx] * m.x[idx] for idx in range(ndim))
== 0
)

with LoggingIntercept(level=logging.DEBUG) as LOG:
res = SolverFactory("pyros").solve(
model=m,
first_stage_variables=m.x,
second_stage_variables=[],
uncertain_params=m.q,
# this set is a line segment (1D), so it
# has no interior points.
# therefore, coefficient matching
# of the above equality constraint
# should not take place
uncertainty_set=PolyhedralSet(
lhs_coefficients_mat=np.vstack(
[
np.hstack(
[np.ones(ndim - 1)[:, np.newaxis], -np.eye(ndim - 1)]
),
-np.hstack(
[np.ones(ndim - 1)[:, np.newaxis], -np.eye(ndim - 1)]
),
np.eye(ndim),
-np.eye(ndim),
]
),
rhs_vec=np.hstack(
[
np.zeros(ndim - 1),
np.zeros(ndim - 1),
np.ones(ndim),
np.zeros(ndim),
]
),
),
local_solver="scip",
global_solver="scip",
)
debug_msgs = LOG.getvalue()
self.assertRegex(
debug_msgs,
(
r".*Equality constraint.*eq_con.*state.*independent.*as a polynomial.*"
r".*but the specified uncertainty set does not meet conditions"
),
)
self.assertEqual(
res.pyros_termination_condition, pyrosTerminationCondition.robust_feasible
)
# if coefficient matching occurred, the objective would have
# been equal to 0
self.assertEqual(res.final_objective_value, -2)
self.assertEqual(res.iterations, 1)
self.assertEqual(m.x[0].value, -2)
self.assertEqual(m.x[1].value, 2)

# run again, now with box set,
# to confirm coefficient matching causes x = 0
res = SolverFactory("pyros").solve(
model=m,
first_stage_variables=m.x,
second_stage_variables=[],
uncertain_params=m.q,
uncertainty_set=BoxSet([[0, 1]] * len(m.q)),
local_solver="scip",
global_solver="scip",
)
self.assertEqual(
res.pyros_termination_condition, pyrosTerminationCondition.robust_feasible
)
self.assertEqual(res.iterations, 1)
self.assertEqual(res.final_objective_value, 0)
self.assertEqual(m.x[0].value, 0)
self.assertEqual(m.x[1].value, 0)

@unittest.skipUnless(ipopt_available, "IPOPT not available")
def test_pyros_certain_params_ipopt_degrees_of_freedom(self):
"""
Expand Down
Loading
Loading