Skip to content
Snippets Groups Projects
Commit 95529480 authored by Bas Nijholt's avatar Bas Nijholt
Browse files

Merge branch 'learner1D_bounds_bug' into 'master'

1D: fix the rare case where the right boundary point exists before the left bound

Closes #94

See merge request !95
parents 6a1ec632 7bc2a206
No related branches found
No related tags found
1 merge request!951D: fix the rare case where the right boundary point exists before the left bound
Pipeline #11919 passed
......@@ -261,17 +261,15 @@ class Learner1D(BaseLearner):
return [], []
# If the bounds have not been chosen yet, we choose them first.
points = [b for b in self.bounds if b not in self.data
and b not in self.pending_points]
missing_bounds = [b for b in self.bounds if b not in self.data
and b not in self.pending_points]
if len(points) == 2:
# First time
if missing_bounds:
loss_improvements = [np.inf] * n
points = np.linspace(*self.bounds, n).tolist()
elif len(points) == 1:
# Second time, if we previously returned just self.bounds[0]
loss_improvements = [np.inf] * n
points = np.linspace(*self.bounds, n + 1)[1:].tolist()
# XXX: should check if points are present in self.data or self.pending_points
points = np.linspace(*self.bounds, n + 2 - len(missing_bounds)).tolist()
if len(missing_bounds) == 1:
points = points[1:] if missing_bounds[0] == self.bounds[1] else points[:-1]
else:
def xs(x_left, x_right, n):
if n == 1:
......
......@@ -363,7 +363,7 @@ def test_learner1d_first_iteration():
"""Edge cases where we ask for a few points at the start."""
learner = Learner1D(lambda x: None, (-1, 1))
points, loss_improvements = learner.ask(2)
assert set(points) == set([-1, 1])
assert set(points) == set(learner.bounds)
learner = Learner1D(lambda x: None, (-1, 1))
points, loss_improvements = learner.ask(3)
......@@ -371,17 +371,27 @@ def test_learner1d_first_iteration():
learner = Learner1D(lambda x: None, (-1, 1))
points, loss_improvements = learner.ask(1)
assert len(points) == 1 and points[0] in [-1, 1]
assert len(points) == 1 and points[0] in learner.bounds
rest = set([-1, 0, 1]) - set(points)
points, loss_improvements = learner.ask(2)
assert set(points) == set(rest)
learner = Learner1D(lambda x: None, (-1, 1))
points, loss_improvements = learner.ask(1)
to_see = set([-1, 1]) - set(points)
to_see = set(learner.bounds) - set(points)
points, loss_improvements = learner.ask(1)
assert set(points) == set(to_see)
learner = Learner1D(lambda x: None, (-1, 1))
learner.tell(1, 0)
points, loss_improvements = learner.ask(1)
assert points == [-1]
learner = Learner1D(lambda x: None, (-1, 1))
learner.tell(-1, 0)
points, loss_improvements = learner.ask(1)
assert points == [1]
def _run_on_discontinuity(x_0, bounds):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment