Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
1.99k stars 512 forks source link

Allow users to attach exception to Expression.Skip and Constraint.Skip #2770

Open dallan-keylogic opened 1 year ago

dallan-keylogic commented 1 year ago

Summary

When returning Expression.Skip and Constriant.Skip, users should be able to attach an exception, or at least exception text to explain why that node is being skipped.

Rationale

This feature request relates to discussions had here and here about how to make clear to the user why an expression for a physical property is being skipped, especially when they might not be the one who wrote a property package or a property model.

Description

The simplest solution would just be to add allow the user to add a string on the frontend to add to the KeyError:

 class therm_cond_phase(object):
        """No method for thermal conductivity"""

        @staticmethod
        def build_parameters(pobj):
            """No parameters needed"""
            pass

        @staticmethod
        def return_expression(b, p):
            """Raise error when therm_cond_phase is called for in phase p"""
            _log.debug(f"Skipping construction of thermal conductivty for phase {p}")
            return Expression.Skip(raises=f"Property package does not support thermal conductivity in phase {p}")

A better solution would be to even allow exceptions other than a KeyError:

return Expression.Skip(raises=PropertyError(f"Property package does not support thermal conductivity in phase {p}"))

I have no idea what implementing these on the backend might entail, however.

Robbybp commented 1 year ago

I think this would be very useful. Encountering a skipped constraint (or Expression) is often a very confusing experience for me.