diff --git a/adaptive/learner/learner1D.py b/adaptive/learner/learner1D.py index 289ced0d..dfc3f81c 100644 --- a/adaptive/learner/learner1D.py +++ b/adaptive/learner/learner1D.py @@ -421,7 +421,13 @@ def npoints(self) -> int: # type: ignore[override] @cache_latest def loss(self, real: bool = True) -> float: - if self._missing_bounds(): + # Real loss checks self.data so a parallel runner's loss goal cannot + # stop while bounds are pending. Expected loss checks _missing_bounds() + # so BalancingLearner(strategy="loss") doesn't starve siblings while + # a bound is in flight. + if real and any(bound not in self.data for bound in self.bounds): + return np.inf + if not real and self._missing_bounds(): return np.inf losses = self.losses if real else self.losses_combined if not losses: diff --git a/adaptive/tests/test_balancing_learner.py b/adaptive/tests/test_balancing_learner.py index 6c27d1e0..b7ad7066 100644 --- a/adaptive/tests/test_balancing_learner.py +++ b/adaptive/tests/test_balancing_learner.py @@ -178,6 +178,18 @@ def test_loss_strategy_with_empty_average_learners() -> None: assert len(learner.ask(7)[0]) == 7 +def test_loss_strategy_with_pending_bound() -> None: + slow, ready = [Learner1D(lambda x: x, bounds=(0, 1)) for _ in range(2)] + for child in (slow, ready): + child.tell_many([0, 0.5], [0, 0.5]) + slow.tell_pending(1) + ready.tell(1, 1) + + bl = BalancingLearner([slow, ready], strategy="loss") + points, _ = bl.ask(8) + assert {index for index, _ in points} == {0, 1} + + def test_loss_strategy_refreshes_learner2d_loss_after_tell() -> None: def f(xy, amplitude): x, y = xy diff --git a/adaptive/tests/test_learner1d.py b/adaptive/tests/test_learner1d.py index a2cee877..bff116c6 100644 --- a/adaptive/tests/test_learner1d.py +++ b/adaptive/tests/test_learner1d.py @@ -5,6 +5,7 @@ import flaky import numpy as np +import pytest from adaptive.learner import Learner1D from adaptive.learner.learner1D import curvature_loss_function @@ -414,3 +415,39 @@ def test_inf_loss_with_missing_bounds(): BlockingRunner(learner, loss_goal=0.01) assert learner.npoints >= 5 + + +@pytest.mark.parametrize("first_bound", [-1, 1]) +def test_inf_loss_while_bounds_are_pending(first_bound): + # A parallel runner often gets the middle points back before the bounds. + # The loss must stay infinite until both bounds are in, or a loss goal + # stops the runner without them (#316). + learner = Learner1D(lambda x: 0, bounds=(-1, 1)) + points, _ = learner.ask(4) + for x in points: + if x not in learner.bounds: + learner.tell(x, 0) + assert learner.pending_points == {-1, 1} + assert learner.loss() == np.inf + + learner.tell(first_bound, 0) + assert learner.loss() == np.inf + if first_bound == -1: + assert learner.loss(real=False) == pytest.approx(1 / 3) + + learner.tell(-first_bound, 0) + assert np.isfinite(learner.loss()) + + +def test_expected_loss_with_pending_bounds(): + learner = Learner1D(lambda x: x, bounds=(-1, 1)) + points, _ = learner.ask(4, tell_pending=False) + for x in points: + if x not in learner.bounds: + learner.tell(x, x) + assert learner.loss(real=False) == np.inf + + learner.ask(2) + assert learner.pending_points == {-1, 1} + assert learner.loss() == np.inf + assert np.isfinite(learner.loss(real=False))