8000 [MRG] Add 'copy constructor' to Space (#373) · scikit-optimize/scikit-optimize@e04e253 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit e04e253

Browse files
betatimMechCoder
authored andcommitted
[MRG] Add 'copy constructor' to Space (#373)
* Add 'copy constructor' to Space Create a new Space instance from an existing one. * Add test for fixed random seeds * Use assert_* calls instead of raw assert statements
1 parent 4c063b7 commit e04e253

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

skopt/optimizer/optimizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ def tell(self, x, y, fit=True):
309309
logits -= np.max(logits)
310310
exp_logits = np.exp(self.eta * logits)
311311
probs = exp_logits / np.sum(exp_logits)
312-
next_x = self.next_xs_[np.argmax(np.random.multinomial(1,
313-
probs))]
312+
next_x = self.next_xs_[np.argmax(self.rng.multinomial(1,
313+
probs))]
314314
else:
315315
next_x = self.next_xs_[0]
316316

skopt/space/space.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ def __repr__(self):
489489
return "Space([{}])".format(
490490
',\n '.join(map(str, dims)))
491491

492+
def __iter__(self):
493+
return iter(self.dimensions)
494+
492495
@property
493496
def is_real(self):
494497
"""

skopt/tests/test_common.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from sklearn.utils.testing import assert_almost_equal
1010
from sklearn.utils.testing import assert_array_less
1111
from sklearn.utils.testing import assert_array_equal
12+
from sklearn.utils.testing import assert_array_almost_equal
1213
from sklearn.utils.testing import assert_equal
1314
from sklearn.utils.testing import assert_raise_message
1415
from sklearn.utils.testing import assert_raises
@@ -148,6 +149,47 @@ def test_minimizer_api_random_only(minimizer):
148149
check_minimizer_bounds(result, n_calls)
149150

150151

152+
@pytest.mark.parametrize("minimizer", MINIMIZERS)
153+
def test_fixed_random_states(minimizer):
154+
# check that two runs produce exactly same results, if not there is a
155+
# random state somewhere that is not reproducible
156+
n_calls = 7
157+
n_random_starts = 4
158+
159+
space = [(-5.0, 10.0), (0.0, 15.0)]
160+
result1 = minimizer(branin, space, n_calls=n_calls,
161+
n_random_starts=n_random_starts, random_state=1)
162+
163+
dimensions = [(-5.0, 10.0), (0.0, 15.0)]
164+
result2 = minimizer(branin, dimensions, n_calls=n_calls,
165+
n_random_starts=n_random_starts, random_state=1)
166+
167+
assert_array_almost_equal(result1.x_iters, result2.x_iters)
168+
assert_array_almost_equal(result1.func_vals, result2.func_vals)
169+
170+
171+
@pytest.mark.parametrize("minimizer", MINIMIZERS)
172+
def test_minimizer_with_space(minimizer):
173+
# check we can pass a Space instance as dimensions argument and get same
174+
# result
175+
n_calls = 7
176+
n_random_starts = 4
177+
178+
space = Space([(-5.0, 10.0), (0.0, 15.0)])
179+
space_result = minimizer(branin, space, n_calls=n_calls,
180+
n_random_starts=n_random_starts, random_state=1)
181+
182+
check_minimizer_api(space_result, n_calls)
183+
check_minimizer_bounds(space_result, n_calls)
184+
185+
dimensions = [(-5.0, 10.0), (0.0, 15.0)]
186+
result = minimizer(branin, dimensions, n_calls=n_calls,
187+
n_random_starts=n_random_starts, random_state=1)
188+
189+
assert_array_almost_equal(space_result.x_iters, result.x_iters)
190+
assert_array_almost_equal(space_result.func_vals, result.func_vals)
191+
192+
151193
@pytest.mark.parametrize("n_random_starts, optimizer_func",
152194
product([0, 5],
153195
[gp_minimize,

skopt/tests/test_space.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ def test_space_api():
252252
assert_array_equal(b1, b2)
253253

254254

255+
def test_space_from_space():
256+
# can you pass a Space instance to the Space constructor?
257+
space = Space([(0.0, 1.0), (-5, 5),
258+
("a", "b", "c"), (1.0, 5.0, "log-uniform"), ("e", "f")])
259+
260+
space2 = Space(space)
261+
262+
assert_equal(space, space2)
263+
264+
255265
def test_normalize():
256266
a = Real(2.0, 30.0, transform="normalize")
257267
for i in range(50):

0 commit comments

Comments
 (0)
0