Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SKOptLearner for multi variate domain (issue #233) #234

Merged
merged 5 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 11 additions & 5 deletions adaptive/learner/skopt_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
from skopt import Optimizer
from collections import OrderedDict

from adaptive.learner.base_learner import BaseLearner
from adaptive.notebook_integration import ensure_holoviews
Expand All @@ -26,18 +27,23 @@ class SKOptLearner(Optimizer, BaseLearner):
def __init__(self, function, **kwargs):
self.function = function
self.pending_points = set()
self.data = {}
self.data = OrderedDict()
super().__init__(**kwargs)

def tell(self, x, y, fit=True):
self.pending_points.discard(x)
self.data[x] = y
super().tell([x], y, fit)
if hasattr(x, '__iter__'):
self.pending_points.discard(tuple(x))
self.data[tuple(x)] = y
super().tell(x, y, fit)
else:
self.pending_points.discard(x)
self.data[x] = y
super().tell([x], y, fit)

def tell_pending(self, x):
# 'skopt.Optimizer' takes care of points we
# have not got results for.
self.pending_points.add(x)
self.pending_points.add(tuple(x))

def remove_unfinished(self):
pass
Expand Down
20 changes: 20 additions & 0 deletions adaptive/tests/test_skopt_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ def g(x, noise_level=0.1):
for _ in range(11):
(x,), _ = learner.ask(1)
learner.tell(x, learner.function(x))


@pytest.mark.skipif(not with_scikit_optimize, reason="scikit-optimize is not installed")
def test_skopt_learner_4D_runs():
"""The SKOptLearner provides very few guarantees about its
behaviour, so we only test the most basic usage
In this case we test also for 4D domain
"""

def g(x, noise_level=0.1):
return np.sin(5 * (x[0] + x[1] + x[2] + x[3])) * (
1 - np.tanh(x[0] ** 2 + x[1] ** 2 + x[2] ** 2 + x[3] ** 2)
) + np.random.randn() * noise_level

learner = SKOptLearner(g, dimensions=[(-2.0, 2.0), (-2.0, 2.0),
(-2.0, 2.0), (-2.0, 2.0)])

for _ in range(11):
(x,), _ = learner.ask(1)
learner.tell(x, learner.function(x))