From 920153631b00b826654d6dda74e2b4921232e9e4 Mon Sep 17 00:00:00 2001 From: r0c0y <184822225+r0c0y@users.noreply.github.com> Date: Sat, 5 Dec 2026 21:25:03 +0530 Subject: [PATCH 1/2] Keep Learner1D's loss infinite while a bound is still pending #271 made loss() infinite until the bounds are evaluated, so that a parallel run whose middle points come back first cannot reach its loss goal without them. Checking _missing_bounds() left out pending bounds, and in a runner both bounds are pending from the first ask, so the loss was finite in exactly that case: BlockingRunner returned above its goal and AsyncRunner cancelled the bounds. Treat a bound as missing until it is in data. Fixes #316 --- adaptive/learner/learner1D.py | 5 ++++- adaptive/tests/test_learner1d.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/adaptive/learner/learner1D.py b/adaptive/learner/learner1D.py index 289ced0d..7868f83d 100644 --- a/adaptive/learner/learner1D.py +++ b/adaptive/learner/learner1D.py @@ -421,7 +421,10 @@ def npoints(self) -> int: # type: ignore[override] @cache_latest def loss(self, real: bool = True) -> float: - if self._missing_bounds(): + # A bound that is only pending counts as missing too: a parallel + # runner often gets the middle points back first, and a finite loss + # would let its goal stop it before the bounds are evaluated. + if any(bound not in self.data for bound in self.bounds): return np.inf losses = self.losses if real else self.losses_combined if not losses: diff --git a/adaptive/tests/test_learner1d.py b/adaptive/tests/test_learner1d.py index a2cee877..309d81ed 100644 --- a/adaptive/tests/test_learner1d.py +++ b/adaptive/tests/test_learner1d.py @@ -414,3 +414,23 @@ def test_inf_loss_with_missing_bounds(): BlockingRunner(learner, loss_goal=0.01) assert learner.npoints >= 5 + + +def test_inf_loss_while_bounds_are_pending(): + # 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 + assert learner.loss(real=False) == np.inf + + learner.tell(-1, 0) + assert learner.loss() == np.inf + + learner.tell(1, 0) + assert np.isfinite(learner.loss()) From b0bb809b3bf3501c0cbc6f85828260b8a29a4f0f Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Dec 2026 15:40:04 -0800 Subject: [PATCH 2/2] Preserve expected Learner1D loss while bounds are pending Keep real loss infinite until both bounds are evaluated. Use the original missing-bound check for expected loss so an in-flight bound does not starve sibling learners under loss-based balancing. Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol --- adaptive/learner/learner1D.py | 11 +++++++---- adaptive/tests/test_balancing_learner.py | 12 ++++++++++++ adaptive/tests/test_learner1d.py | 25 ++++++++++++++++++++---- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/adaptive/learner/learner1D.py b/adaptive/learner/learner1D.py index 7868f83d..dfc3f81c 100644 --- a/adaptive/learner/learner1D.py +++ b/adaptive/learner/learner1D.py @@ -421,10 +421,13 @@ def npoints(self) -> int: # type: ignore[override] @cache_latest def loss(self, real: bool = True) -> float: - # A bound that is only pending counts as missing too: a parallel - # runner often gets the middle points back first, and a finite loss - # would let its goal stop it before the bounds are evaluated. - if any(bound not in self.data for bound in self.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 309d81ed..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 @@ -416,7 +417,8 @@ def test_inf_loss_with_missing_bounds(): assert learner.npoints >= 5 -def test_inf_loss_while_bounds_are_pending(): +@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). @@ -427,10 +429,25 @@ def test_inf_loss_while_bounds_are_pending(): learner.tell(x, 0) assert learner.pending_points == {-1, 1} assert learner.loss() == np.inf - assert learner.loss(real=False) == np.inf - learner.tell(-1, 0) + 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(1, 0) + 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))