from abc import ABC, abstractmethod
from ..core.nosettingsobject import NoSettingsObject
from ...plams.core.settings import Settings
__all__ = ("IterSpawnStop", "NOptimizersSpawnStop")
[docs]class BaseController(ABC, NoSettingsObject):
"""Conditions to stop new optimizers being started even if there are available worker slots."""
[docs] @abstractmethod
def __call__(self, mng: "GloMPOManager") -> bool:
"""Returns ``True`` if the Selector may start a new optimizer, ``False`` otherwise.
Evaluated everytime the selector is called.
"""
[docs]class IterSpawnStop(BaseController):
"""Controls spawning based on the number of function calls used thus far.
:Parameters:
max_calls
Maximum number of function calls allowed, after which no more optimizers will be started.
"""
def __init__(self, max_calls: int):
self.max_calls = max_calls
def __amssettings__(self, s: Settings) -> Settings:
s.input.ams.ControlOptimizerSpawning.MaxEvaluations = self.max_calls
return s
def __call__(self, mng: "GloMPOManager"):
if mng.f_counter >= self.max_calls:
return False
return True
class _AlwaysSpawn(BaseController):
def __call__(self, *args, **kwargs):
return True
def __amssettings__(self, s: Settings) -> Settings:
return s