Source code for pyrevs.strategies.base.termination
"""Defines the interface and simple implementations of termination criteria."""
from __future__ import annotations
from math import isclose
from typing import TYPE_CHECKING
from typing import Protocol
from typing import TypeVar
from pyrevs.trajectory import TrajectoryStateType
if TYPE_CHECKING:
from pyrevs.core import ForwardModelBaseClass
from pyrevs.trajectory import Trajectory
[docs]
T_Noise = TypeVar("T_Noise")
[docs]
T_State = TypeVar("T_State")
[docs]
class TerminationCriterion(Protocol):
"""Termination criterion interface."""
[docs]
def should_terminate(
self,
model: ForwardModelBaseClass[T_Noise, T_State],
trajectory: Trajectory,
) -> TrajectoryStateType:
"""Check if the trajectory should terminate.
Args:
model: the forward model
trajectory: the trajectory
Returns:
One of the TrajectoryStateType
"""
raise NotImplementedError
[docs]
class TimeTerminationCriterion(TerminationCriterion):
"""Termination criterion based on time.
Will trigger termination if the current time is greater than or equal
to the end time.
"""
def __init__(self, end_time: float) -> None:
"""Initialize the termination criterion.
Args:
end_time: the end time
"""
self._end_time = end_time
[docs]
def should_terminate(
self,
model: ForwardModelBaseClass[T_Noise, T_State],
trajectory: Trajectory,
) -> TrajectoryStateType:
"""Check if the trajectory should terminate."""
_ = model
if (
isclose(trajectory.current_time(), self._end_time, abs_tol=1e-9)
or trajectory.current_time() >= self._end_time
):
return TrajectoryStateType.TERMINATED
return TrajectoryStateType.ONGOING
[docs]
class TimeInterruptionCriterion(TerminationCriterion):
"""Interruption criterion based on time.
Will trigger interruption if the current time is greater than or equal
to the end time.
"""
def __init__(self, end_time: float) -> None:
"""Initialize the termination criterion.
Args:
end_time: the end time
"""
self._end_time = end_time
[docs]
def should_terminate(
self,
model: ForwardModelBaseClass[T_Noise, T_State],
trajectory: Trajectory,
) -> TrajectoryStateType:
"""Check if the trajectory should terminate."""
_ = model
if (
isclose(trajectory.current_time(), self._end_time, abs_tol=1e-9)
or trajectory.current_time() >= self._end_time
):
return TrajectoryStateType.INTERRUPTED
return TrajectoryStateType.ONGOING
[docs]
class LowScoreTerminationCriterion(TerminationCriterion):
"""Termination criterion based on score.
Will trigger termination if the current score is less than or equal
to a threshold.
"""
def __init__(self, score_threshold: float) -> None:
"""Initialize the termination criterion.
Args:
score_threshold: the score threshold
"""
self._score_threshold = score_threshold
[docs]
def should_terminate(
self,
model: ForwardModelBaseClass[T_Noise, T_State],
trajectory: Trajectory,
) -> TrajectoryStateType:
"""Check if the trajectory should terminate."""
_ = trajectory
if model.score() <= self._score_threshold:
return TrajectoryStateType.TERMINATED
return TrajectoryStateType.ONGOING
[docs]
class ModelTerminationCriterion(TerminationCriterion):
"""Termination criterion based on model.
Will trigger termination if the forward model has decides to.
"""
[docs]
def should_terminate(
self,
model: ForwardModelBaseClass[T_Noise, T_State],
trajectory: Trajectory,
) -> TrajectoryStateType:
"""Check if the trajectory should terminate."""
if model.check_termination(trajectory.current_step(), trajectory.current_time()):
return TrajectoryStateType.TERMINATED
return TrajectoryStateType.ONGOING
[docs]
class TargetScoreConvergenceCriterion(TerminationCriterion):
"""Convergence criterion based on a target score.
Will trigger convergence if the forward model decides that the current score
reached the target score (by default, if the score is greater than or equal
to the target score).
"""
def __init__(self, target_score: float) -> None:
"""Initialize the convergence criterion.
Args:
target_score: the target score
"""
self._target_score = target_score
[docs]
def should_terminate(
self,
model: ForwardModelBaseClass[T_Noise, T_State],
trajectory: Trajectory,
) -> TrajectoryStateType:
"""Check if the trajectory has converged."""
if model.check_convergence(
trajectory.current_step(), trajectory.current_time(), trajectory.current_score(), self._target_score
):
return TrajectoryStateType.CONVERGED
return TrajectoryStateType.ONGOING