Source code for scm.glompo.stoppers.maxfunctioncalls

from .basestopper import BaseStopper
from ..core.optimizerlogger import BaseLogger
from ...plams.core.settings import Settings

__all__ = ("MaxFunctionCalls",)


[docs]class MaxFunctionCalls(BaseStopper): """Keeps an optimizer alive for a minimum number of function evaluations. :Parameters: min_pts Minimum number of points for which an optimizer should be kept alive. :Returns: bool Returns ``True`` after the function has been evaluated at least ``min_pts`` times. """ def __init__(self, min_pts: int): super().__init__() if min_pts > 0 and isinstance(min_pts, int): self.min_pts = min_pts else: raise ValueError("min_pts must be a positive integer.") def __call__(self, log: BaseLogger, best_opt_id: int, tested_opt_id: int) -> bool: self.last_result = log.len(tested_opt_id) >= self.min_pts return self.last_result def __amssettings__(self, s: Settings) -> Settings: s.input.ams.Stopper.Type = "MaxFunctionCalls" s.input.ams.Stopper.MaxFunctionCalls = self.min_pts return s