from time import time
from typing import Optional
from .baseexitcondition import BaseExitCondition
from ...plams.core.settings import Settings
__all__ = ("TimeLimit",)
[docs]class TimeLimit(BaseExitCondition):
"""Time based exit criteria.
Differentiates between session time and overall optimisation time, the difference is only relevant in the case where
checkpointing is being used.
:Parameters:
session_max
Maximum time in seconds which may elapse from the time :meth:`.GloMPOManager.start_manager` is called.
overall_max
Maximum time in seconds which may elapse in total over all optimisation sessions. In other words the
total time used previously is loaded from the checkpoint. In the case where checkpoint is not used, both
quantities are equivalent.
:Notes:
For the avoidance of doubt, both times cannot be used simultaneously. If required rather initialise two
instances with one condition each.
"""
def __init__(self, *, session_max: Optional[float] = None, overall_max: Optional[float] = None):
assert bool(session_max) ^ bool(overall_max)
super().__init__()
self.session_max = session_max
self.overall_max = overall_max
def __amssettings__(self, s: Settings) -> Settings:
if self.session_max:
s.input.ams.ExitCondition.Type = "TimeLimit"
s.input.ams.ExitCondition.TimeLimit = self.session_max
else:
s.input.ams.ExitCondition.Type = "TimeLimitThroughRestarts"
s.input.ams.ExitCondition.TimeLimitThroughRestarts = self.overall_max
return s
def __call__(self, manager: "GloMPOManager") -> bool:
t_total = time() - manager.t_start if manager.t_start else 0
cond = self.session_max
if self.overall_max:
t_total += manager.t_used
cond = self.overall_max
self.last_result = t_total >= cond
return self.last_result