From edef7302d4332ea46c8cd7697e21464b077cfe9a Mon Sep 17 00:00:00 2001 From: r0c0y <184822225+r0c0y@users.noreply.github.com> Date: Sun, 6 Dec 2026 06:07:53 +0530 Subject: [PATCH 1/2] Keep LearnerND's loss infinite until its bound points are evaluated Learner1D and Learner2D report an infinite loss until their bounds are evaluated, so a parallel run whose interior points come back first cannot reach a loss goal without them. LearnerND.loss() was the largest simplex loss of whatever had been told, so it was finite as soon as a few interior points triangulated, with every corner still pending: BlockingRunner returned above its goal and AsyncRunner returned without the corners and a loss below the goal. Return inf until bounds_are_done, which LearnerND has had all along without using it. --- adaptive/learner/learnerND.py | 5 +++++ adaptive/tests/test_learnernd.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/adaptive/learner/learnerND.py b/adaptive/learner/learnerND.py index 8c4b5dde..e57e7e7d 100644 --- a/adaptive/learner/learnerND.py +++ b/adaptive/learner/learnerND.py @@ -986,6 +986,11 @@ def _update_range(self, new_output): @cache_latest def loss(self, real=True): + # Infinite until every bound point is evaluated, as in Learner1D and + # Learner2D: a parallel runner often gets interior points back first, + # and a finite loss would let its goal stop it without the bounds. + if not self.bounds_are_done: + return float("inf") # XXX: compute pending loss if real == False losses = self._losses if self.tri is not None else {} return max(losses.values()) if losses else float("inf") diff --git a/adaptive/tests/test_learnernd.py b/adaptive/tests/test_learnernd.py index f3205c5e..7a74c299 100644 --- a/adaptive/tests/test_learnernd.py +++ b/adaptive/tests/test_learnernd.py @@ -144,3 +144,30 @@ def f(xy): assert learner.npoints == 30 assert all(learner.inside_bounds(p) for p in learner.data) + + +@pytest.mark.parametrize( + "bounds", + [ + [(-1, 1), (-1, 1)], + scipy.spatial.ConvexHull([(-1, -1), (1, -1), (0, 1)]), + ], +) +def test_inf_loss_while_bound_points_are_pending(bounds): + # As in Learner1D (#316): interior points that come back before the + # bound points must not give a finite loss, or a loss goal stops the + # runner without them. + learner = LearnerND(lambda xy: 0.0, bounds=bounds) + n_bounds = len(learner._bounds_points) + bound_points, _ = learner.ask(n_bounds) + for point in [(0, 0), (0.3, 0), (0, 0.3), (0.2, 0.2)]: + learner.tell(point, 0.0) + assert learner.loss() == np.inf + assert learner.loss(real=False) == np.inf + + for point in bound_points[:-1]: + learner.tell(point, 0.0) + assert learner.loss() == np.inf + + learner.tell(bound_points[-1], 0.0) + assert np.isfinite(learner.loss()) From 6f66d1887e8bc6f5f21a86abb14468a230b82992 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 5 Dec 2026 17:27:43 -0800 Subject: [PATCH 2/2] Allow finite LearnerND expected loss with pending bounds Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol --- adaptive/learner/learnerND.py | 11 ++++--- adaptive/tests/test_learnernd.py | 50 +++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/adaptive/learner/learnerND.py b/adaptive/learner/learnerND.py index e57e7e7d..e8a5b5f0 100644 --- a/adaptive/learner/learnerND.py +++ b/adaptive/learner/learnerND.py @@ -986,10 +986,13 @@ def _update_range(self, new_output): @cache_latest def loss(self, real=True): - # Infinite until every bound point is evaluated, as in Learner1D and - # Learner2D: a parallel runner often gets interior points back first, - # and a finite loss would let its goal stop it without the bounds. - if not self.bounds_are_done: + # As in Learner1D, real loss checks bounds_are_done so a parallel + # runner's loss goal cannot stop it while bound points are pending. + # Expected loss checks _bounds_available so pending bounds don't force + # it to be infinite once a triangulation exists. + if real and not self.bounds_are_done: + return float("inf") + if not real and self._bounds_available: return float("inf") # XXX: compute pending loss if real == False losses = self._losses if self.tri is not None else {} diff --git a/adaptive/tests/test_learnernd.py b/adaptive/tests/test_learnernd.py index 7a74c299..4b2eba7c 100644 --- a/adaptive/tests/test_learnernd.py +++ b/adaptive/tests/test_learnernd.py @@ -2,7 +2,7 @@ import pytest import scipy.spatial -from adaptive.learner import LearnerND +from adaptive.learner import BalancingLearner, LearnerND from adaptive.learner.learnerND import choose_point_in_simplex from adaptive.learner.triangulation import point_in_simplex from adaptive.runner import BlockingRunner, replay_log, simple @@ -146,28 +146,62 @@ def f(xy): assert all(learner.inside_bounds(p) for p in learner.data) -@pytest.mark.parametrize( - "bounds", - [ +@pytest.fixture( + params=[ [(-1, 1), (-1, 1)], scipy.spatial.ConvexHull([(-1, -1), (1, -1), (0, 1)]), ], + ids=["box", "hull"], ) -def test_inf_loss_while_bound_points_are_pending(bounds): +def box_or_hull_bounds(request): + return request.param + + +def test_inf_loss_while_bound_points_are_pending(box_or_hull_bounds): # As in Learner1D (#316): interior points that come back before the - # bound points must not give a finite loss, or a loss goal stops the + # bound points must not give a finite real loss, or a loss goal stops the # runner without them. - learner = LearnerND(lambda xy: 0.0, bounds=bounds) + learner = LearnerND(lambda xy: 0.0, bounds=box_or_hull_bounds) n_bounds = len(learner._bounds_points) bound_points, _ = learner.ask(n_bounds) for point in [(0, 0), (0.3, 0), (0, 0.3), (0.2, 0.2)]: learner.tell(point, 0.0) assert learner.loss() == np.inf - assert learner.loss(real=False) == np.inf + assert np.isfinite(learner.loss(real=False)) for point in bound_points[:-1]: learner.tell(point, 0.0) assert learner.loss() == np.inf + assert np.isfinite(learner.loss(real=False)) learner.tell(bound_points[-1], 0.0) assert np.isfinite(learner.loss()) + + +def test_expected_loss_with_pending_bounds(box_or_hull_bounds): + learner = LearnerND(lambda xy: 0.0, bounds=box_or_hull_bounds) + for point in [(0, 0), (0.3, 0), (0, 0.3), (0.2, 0.2)]: + learner.tell(point, 0.0) + assert learner.loss() == np.inf + assert learner.loss(real=False) == np.inf + + learner.ask(len(learner._bounds_points)) + assert learner.loss() == np.inf + assert np.isfinite(learner.loss(real=False)) + + learner.remove_unfinished() + assert learner.loss(real=False) == np.inf + + +def test_balancing_loss_with_pending_corner(): + bounds = [(0, 1)] * 2 + corners = [(0, 0), (0, 1), (1, 0), (1, 1)] + slow = LearnerND(lambda xy: 0.0, bounds=bounds) + ready = LearnerND(sum, bounds=bounds) + slow.tell_many(corners[:-1], [0.0] * 3) + slow.tell_pending(corners[-1]) + ready.tell_many(corners, map(sum, corners)) + + bl = BalancingLearner([slow, ready], strategy="loss") + assert slow.loss(real=False) < ready.loss(real=False) + assert bl.loss(real=False) == ready.loss(real=False)