Skip to content
Snippets Groups Projects

More efficient 'tell_many'

Merged Bas Nijholt requested to merge efficient_tell_many into master
1 file
+ 70
0
Compare changes
  • Side-by-side
  • Inline
@@ -235,3 +235,73 @@ def test_ask_does_not_return_known_points_when_returning_bounds():
learner.tell(0, 0)
points, _ = learner.ask(3)
assert 0 not in points
def test_efficient_tell_many():
def f(x, offset=0.123214):
a = 0.01
return (np.sin(x**2) + np.sin(x**5)
+ a**2 / (a**2 + (x - offset)**2)
+ x**2 + 1e-5 * x**3)
def f_vec(x, offset=0.123214):
a = 0.01
y = x + a**2 / (a**2 + (x - offset)**2)
return [y, 0.5 * y, y**2]
def test_equal(learner, learner2):
assert learner2.neighbors == learner.neighbors
assert learner2.data == learner.data
assert learner2._scale == learner._scale
assert learner2._bbox[0] == learner._bbox[0]
assert (np.array(learner2._bbox[1]) == np.array(learner._bbox[1])).all()
assert not learner.losses_combined.keys() - learner2.losses_combined.keys()
assert sum(learner.losses_combined.values()) - sum(learner2.losses_combined.values()) == 0
assert learner2.neighbors_combined == learner.neighbors_combined
assert learner2.losses == learner.losses
for function in [f, f_vec]:
learner = Learner1D(function, bounds=(-1, 1))
simple(learner, goal=lambda l: l.npoints > 200)
learner2 = Learner1D(function, bounds=(-1, 1))
learner2.tell_many(*zip(*learner.data.items()))
test_equal(learner, learner2)
# Test non-determinism. We keep a list of points that will be
# evaluated later to emulate parallel execution.
def _random_run(learner, tell_many, seed):
stash = []
random.seed(seed)
for i in range(10):
xs, _ = learner.ask(10)
# Save 5 random points out of `xs` for later
random.shuffle(xs)
for _ in range(5):
stash.append(xs.pop())
ys = [learner.function(x) for x in xs]
if tell_many:
learner.tell_many(xs, ys)
else:
for x, y in zip(xs, ys):
learner.tell(x, y)
# Evaluate and add 5 random points from `stash`
random.shuffle(stash)
xs = [stash.pop() for _ in range(5)]
ys = [learner.function(x) for x in xs]
if tell_many:
learner.tell_many(xs, ys)
else:
for x, y in zip(xs, ys):
learner.tell(x, learner.function(x))
print(xs)
learner = Learner1D(f, bounds=(-1, 1))
learner2 = Learner1D(f, bounds=(-1, 1))
seed = random.randint(0, 1000)
_random_run(learner, tell_many=False, seed=seed)
_random_run(learner2, tell_many=True, seed=seed)
test_equal(learner, learner2) # is not possible because floating points operations are not associative
Loading