diff --git a/adaptive/learner/learnerND.py b/adaptive/learner/learnerND.py index 8c4b5dde..e8a5b5f0 100644 --- a/adaptive/learner/learnerND.py +++ b/adaptive/learner/learnerND.py @@ -986,6 +986,14 @@ def _update_range(self, new_output): @cache_latest def loss(self, real=True): + # 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 {} 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..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 @@ -144,3 +144,64 @@ def f(xy): assert learner.npoints == 30 assert all(learner.inside_bounds(p) for p in learner.data) + + +@pytest.fixture( + params=[ + [(-1, 1), (-1, 1)], + scipy.spatial.ConvexHull([(-1, -1), (1, -1), (0, 1)]), + ], + ids=["box", "hull"], +) +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 real loss, or a loss goal stops the + # runner without them. + 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 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)