Skip to content

Update PyROS Uncertainty Set Validation Methods #3558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

jas-yao
Copy link
Contributor

@jas-yao jas-yao commented Apr 11, 2025

Summary/Motivation:

This PR provides updates to PyROS uncertainty set validation methods and related tests.
Here, a validate method replaces the is_valid method (which solves 2N bounding problems to check for set boundedness) in all uncertainty sets, with each set having its own custom validate method that efficiently checks set-specific attributes and raises informative exceptions if any issues are found.

Changes proposed in this PR:

  • Update is_bounded and is_nonempty methods in base UncertaintySet class
  • Provide a _solve_feasibility method in base UncertaintySet class
  • Replace is_valid with validate method that runs is_bounded and is_nonempty in the base UncertaintySet class
  • Override validate in subclass uncertainty sets to check set-specific attributes
  • Remove attribute setter checks in uncertainty sets that have been moved to the validate method
  • Update unit tests for is_bounded, is_nonempty, _solve_feasibility, and validate methods

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

jas-yao added 30 commits April 7, 2025 15:31
@jas-yao
Copy link
Contributor Author

jas-yao commented Apr 11, 2025

@shermanjasonaf

@jas-yao
Copy link
Contributor Author

jas-yao commented Apr 11, 2025

Thanks for all the feedback and suggestions @jsiirola !
Regarding FBBT, I was able to reproduce the issue with the following short script.

"""Reproducing FBBT error."""
from pyomo.contrib.pyros.uncertainty_sets import PolyhedralSet
from pyomo.contrib.fbbt.fbbt import fbbt

# create a polyhedral uncertainty set that is not bounded
lhs_coefficients_mat = [[1, 2, 3], [4, 5, 6]]
rhs_vec = [1, 3]
pset = PolyhedralSet(lhs_coefficients_mat, rhs_vec)

# create pyomo model and pprint
bounding_model = pset._create_bounding_model()
bounding_model.pprint()

# try to use fbbt and pprint results
fbbt(bounding_model)
bounding_model.pprint()

with traceback

Traceback (most recent call last):
  File "<string>", line 17, in __PYTHON_EL_eval
  File "/Users/yletian/Documents/projects/pyomo/pyomo/contrib/pyros/temp_fbbt_error.py", line 15, in <module>
    fbbt(bounding_model)
    ~~~~^^^^^^^^^^^^^^^^
  File "/Users/yletian/Documents/projects/pyomo/pyomo/contrib/fbbt/fbbt.py", line 1543, in fbbt
    _new_var_bounds = _fbbt_block(comp, config)
  File "/Users/yletian/Documents/projects/pyomo/pyomo/contrib/fbbt/fbbt.py", line 1429, in _fbbt_block
    _new_var_bounds = _fbbt_con(c, config)
  File "/Users/yletian/Documents/projects/pyomo/pyomo/contrib/fbbt/fbbt.py", line 1334, in _fbbt_con
    visitorA.walk_expression(con.expr)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/Users/yletian/Documents/projects/pyomo/pyomo/core/expr/visitor.py", line 276, in walk_expression
    result = self._process_node(root, RECURSION_LIMIT)
  File "/Users/yletian/Documents/projects/pyomo/pyomo/core/expr/visitor.py", line 472, in _process_node_bx
    tmp = self.beforeChild(node, child, child_idx)
  File "/Users/yletian/Documents/projects/pyomo/pyomo/contrib/fbbt/fbbt.py", line 1160, in beforeChild
    return _before_child_handlers[child.__class__](self, child)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/yletian/Documents/projects/pyomo/pyomo/contrib/fbbt/fbbt.py", line 1108, in _register_new_before_child_handler
    if child.is_variable_type():
       ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'numpy.int64' object has no attribute 'is_variable_type'

This was fixed with your suggested changes:

@@ -19,7 +19,7 @@ from pyomo.core.expr.visitor import (
     identify_variables,
     StreamBasedExpressionVisitor,
 )
-from pyomo.core.expr.numvalue import nonpyomo_leaf_types, value
+from pyomo.common.numeric_types import nonpyomo_leaf_types, native_numeric_types, value
 from pyomo.core.expr.numvalue import is_fixed
 import pyomo.contrib.fbbt.interval as interval
 import math
@@ -1105,7 +1105,9 @@ def _before_external_function(visitor, child):
 def _register_new_before_child_handler(visitor, child):
     handlers = _before_child_handlers
     child_type = child.__class__
-    if child.is_variable_type():
+    if child_type in native_numeric_types:
+        handlers[child_type] = _before_constant
+    elif child.is_variable_type():
         handlers[child_type] = _before_var
     elif not child.is_potentially_variable():
         handlers[child_type] = _before_NPV

With this fix, I'll take a closer look into possibly replacing the 2N bounding problems of is_bounded with FBBT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant