""" Abstract stopper classes used to construct the stop criteria. """
import logging
from abc import abstractmethod
from ..common.corebase import _AndCore, _CoreBase, _OrCore
from ..core.optimizerlogger import BaseLogger
__all__ = ("BaseStopper",)
[docs]class BaseStopper(_CoreBase):
"""Base stopper from which all stoppers must inherit to be compatible with GloMPO.
:Attributes:
logger : logging.Logger
:class:`logging.Logger` instance into which status messages may be added.
"""
AMS_KEY = "Stopper"
def __init__(self):
super().__init__()
self.logger = logging.getLogger("glompo.stopper")
[docs] @abstractmethod
def __call__(self, log: BaseLogger, best_opt_id: int, tested_opt_id: int) -> bool:
"""Compares two optimizers and returns if one should be terminated according to some condition.
When called, this method may check any values within the logs of both the best and the tested optimizer.
:Parameters:
log
Instance of :class:`.BaseLogger` that contains the iteration history of every optimizer.
best_opt_id
ID number of the optimizer which has found the lowest function value.
tested_opt_id
ID number of the optimizer which the Stopper is testing to stop or not.
:Returns:
bool
``True`` if the condition in the method is met, ``False`` otherwise.
:Notes:
For proper functionality, the result of this method must be saved to ``last_result`` before
returning.
"""
def __or__(self, other: "BaseStopper") -> "_OrStopper":
return _OrStopper(self, other)
def __and__(self, other: "BaseStopper") -> "_AndStopper":
return _AndStopper(self, other)
class _OrStopper(_OrCore, BaseStopper):
def __call__(self, *args, **kwargs) -> bool:
return super().__call__(*args, **kwargs)
class _AndStopper(_AndCore, BaseStopper):
def __call__(self, *args, **kwargs) -> bool:
return super().__call__(*args, **kwargs)