Skip to content

Commit 2c95367

Browse files
authored
Merge pull request #234 from caenrigen/master
Fix SKOptLearner for multi variate domain (issue #233)
2 parents c084704 + 454a186 commit 2c95367

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

adaptive/learner/skopt_learner.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
import collections
4+
35
import numpy as np
46
from skopt import Optimizer
57

@@ -26,18 +28,23 @@ class SKOptLearner(Optimizer, BaseLearner):
2628
def __init__(self, function, **kwargs):
2729
self.function = function
2830
self.pending_points = set()
29-
self.data = {}
31+
self.data = collections.OrderedDict()
3032
super().__init__(**kwargs)
3133

3234
def tell(self, x, y, fit=True):
33-
self.pending_points.discard(x)
34-
self.data[x] = y
35-
super().tell([x], y, fit)
35+
if isinstance(x, collections.abc.Iterable):
36+
self.pending_points.discard(tuple(x))
37+
self.data[tuple(x)] = y
38+
super().tell(x, y, fit)
39+
else:
40+
self.pending_points.discard(x)
41+
self.data[x] = y
42+
super().tell([x], y, fit)
3643

3744
def tell_pending(self, x):
3845
# 'skopt.Optimizer' takes care of points we
3946
# have not got results for.
40-
self.pending_points.add(x)
47+
self.pending_points.add(tuple(x))
4148

4249
def remove_unfinished(self):
4350
pass

adaptive/tests/test_skopt_learner.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,26 @@ def g(x, noise_level=0.1):
2525
for _ in range(11):
2626
(x,), _ = learner.ask(1)
2727
learner.tell(x, learner.function(x))
28+
29+
30+
@pytest.mark.skipif(not with_scikit_optimize, reason="scikit-optimize is not installed")
31+
def test_skopt_learner_4D_runs():
32+
"""The SKOptLearner provides very few guarantees about its
33+
behaviour, so we only test the most basic usage
34+
In this case we test also for 4D domain
35+
"""
36+
37+
def g(x, noise_level=0.1):
38+
return (
39+
np.sin(5 * (x[0] + x[1] + x[2] + x[3]))
40+
* (1 - np.tanh(x[0] ** 2 + x[1] ** 2 + x[2] ** 2 + x[3] ** 2))
41+
+ np.random.randn() * noise_level
42+
)
43+
44+
learner = SKOptLearner(
45+
g, dimensions=[(-2.0, 2.0), (-2.0, 2.0), (-2.0, 2.0), (-2.0, 2.0)]
46+
)
47+
48+
for _ in range(11):
49+
(x,), _ = learner.ask(1)
50+
learner.tell(x, learner.function(x))

0 commit comments

Comments
 (0)