Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion adaptive/learner/learner1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions adaptive/tests/test_balancing_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions adaptive/tests/test_learner1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Loading