Skip to content
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

Improve robustness of calculate_variable_from_constraint() #2812

Merged
merged 8 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
2 changes: 1 addition & 1 deletion pyomo/core/expr/sympy_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _nondifferentiable(x):


def _external_fcn(*x):
raise ValueError(
raise TypeError(
"Expressions containing external functions are not convertible to "
f"sympy expressions (found 'f{x}')"
)
Expand Down
2 changes: 1 addition & 1 deletion pyomo/core/tests/unit/test_logical_to_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ def _fcn(*args):
m.y = BooleanVar()
m.c = LogicalConstraint(expr=(m.y.implies(m.f(m.x))))
with self.assertRaisesRegex(
ValueError,
TypeError,
"Expressions containing external functions are not convertible "
r"to sympy expressions \(found 'f\(x1",
):
Expand Down
32 changes: 27 additions & 5 deletions pyomo/util/calc_var_value.py
mrmundt marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@

logger = logging.getLogger(__name__)

_default_differentiation_mode = differentiate.Modes.sympy
_symbolic_modes = {
None,
differentiate.Modes.sympy,
differentiate.Modes.reverse_symbolic,
}


def calculate_variable_from_constraint(
variable,
Expand All @@ -25,7 +32,7 @@ def calculate_variable_from_constraint(
iterlim=1000,
linesearch=True,
alpha_min=1e-8,
diff_mode=differentiate.Modes.sympy,
diff_mode=None,
):
"""Calculate the variable value given a specified equality constraint

Expand Down Expand Up @@ -63,8 +70,9 @@ def calculate_variable_from_constraint(
[default=True]
alpha_min: `float`
The minimum fractional step to use in the linesearch [default=1e-8].
diff_mode: `pyomo.core.expr.calculus.derivatives.Modes`
The mode of differentiation
diff_mode: :py:enum:`pyomo.core.expr.calculus.derivatives.Modes`
The mode to use to differentiate the expression. If
unspecified, defaults to `Modes.sympy`

Returns:
--------
Expand Down Expand Up @@ -175,8 +183,22 @@ def calculate_variable_from_constraint(
expr = body - upper

expr_deriv = None
if diff_mode in {differentiate.Modes.sympy, differentiate.Modes.reverse_symbolic}:
expr_deriv = differentiate(expr, wrt=variable, mode=diff_mode)
if diff_mode in _symbolic_modes:
try:
expr_deriv = differentiate(
expr, wrt=variable, mode=diff_mode or _default_differentiation_mode
)
except:
if diff_mode is None:
# If the user didn't care how we differentiate, try to
# (mostly silently) revert to numeric differentiation.
logger.debug(
'Calculating symbolic derivative of expression failed. '
'Reverting to numeric differentiation'
)
diff_mode = differentiate.Modes.reverse_numeric
else:
raise

if type(expr_deriv) in native_numeric_types and expr_deriv == 0:
raise ValueError("Variable derivative == 0, cannot solve for variable")
Expand Down