Source code for mpcrl.core.errors
"""Error and warning classes for signaling failure in the MPC solver and in the
updates, as well as convenience functions for raising these errors and warnings."""
from warnings import warn as _warn
[docs]
class MpcSolverError(RuntimeError):
"""Exception class for raising errors when an MPC solver fails."""
[docs]
class MpcSolverWarning(RuntimeWarning):
"""Warning class for raising errors when an MPC solver fails."""
[docs]
class UpdateError(RuntimeError):
"""Exception class for raising errors when a learning agent's updates fails."""
[docs]
class UpdateWarning(RuntimeWarning):
"""Warning class for raising errors when a learning agent's updates fails."""
[docs]
def raise_or_warn_on_mpc_failure(msg: str, raises: bool) -> None:
"""Raises an error or warning with the given message due to an MPC solver's failure.
Parameters
----------
msg : str
The exception or warning message.
raises : bool
If ``True``, raises an exception; otherwise, throws a warning.
Raises
------
UpdateError
Raises :class:`MpcSolverError` if ``raises=True``; otherwise raises
:class:`MpcSolverWarning`.
"""
if raises:
raise MpcSolverError(msg)
_warn(msg, MpcSolverWarning, 2)
[docs]
def raise_or_warn_on_update_failure(msg: str, raises: bool) -> None:
"""Raises an error or warning with the given message due to an update failure.
Parameters
----------
msg : str
The exception or warning message.
raises : bool
If ``True``, raises an exception; otherwise, throws a warning.
Raises
------
UpdateError
Raises :class:`UpdateError` if ``raises=True``; otherwise raises
:class:`UpdateWarning`.
"""
if raises:
raise UpdateError(msg)
_warn(msg, UpdateWarning, 2)