Criterion that stops after a specified maximum runtime (in seconds).
| 2 | |
| 3 | |
| 4 | class MaxRuntime: |
| 5 | """ |
| 6 | Criterion that stops after a specified maximum runtime (in seconds). |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, max_runtime: float): |
| 10 | if max_runtime < 0: |
| 11 | raise ValueError("max_runtime < 0 not understood.") |
| 12 | |
| 13 | self._max_runtime = max_runtime |
| 14 | self._start_runtime: float | None = None |
| 15 | |
| 16 | def __call__(self, best_cost: float) -> bool: |
| 17 | if self._start_runtime is None: |
| 18 | self._start_runtime = time.perf_counter() |
| 19 | |
| 20 | return time.perf_counter() - self._start_runtime > self._max_runtime |
no outgoing calls