8000 [MRG] Add 'copy constructor' to Space by betatim · Pull Request #373 · scikit-optimize/scikit-optimize · 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.

[MRG] Add 'copy constructor' to Space #373

Merged
merged 3 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions skopt/optimizer/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ def tell(self, x, y, fit=True):
logits -= np.max(logits)
exp_logits = np.exp(self.eta * logits)
probs = exp_logits / np.sum(exp_logits)
next_x = self.next_xs_[np.argmax(np.random.multinomial(1,
probs))]
next_x = self.next_xs_[np.argmax(self.rng.multinomial(1,
probs))]
else:
next_x = self.next_xs_[0]

Expand Down
3 changes: 3 additions & 0 deletions skopt/space/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ def __repr__(self):
return "Space([{}])".format(
',\n '.join(map(str, dims)))

def __iter__(self):
return iter(self.dimensions)

@property
def is_real(self):
"""
Expand Down
42 changes: 42 additions & 0 deletions skopt/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_array_less
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_raise_message
from sklearn.utils.testing import assert_raises
Expand Down Expand Up @@ -148,6 +149,47 @@ def test_minimizer_api_random_only(minimizer):
check_minimizer_bounds(result, n_calls)


@pytest.mark.parametrize("minimizer", MINIMIZERS)
def test_fixed_random_states(minimizer):
# check that two runs produce exactly same results, if not there is a
# random state somewhere that is not reproducible
n_calls = 7
n_random_starts = 4

space = [(-5.0, 10.0), (0.0, 15.0)]
result1 = minimizer(branin, space, n_calls=n_calls,
n_random_starts=n_random_starts, random_state=1)

dimensions = [(-5.0, 10.0), (0.0, 15.0)]
result2 = minimizer(branin, dimensions, n_calls=n_calls,
n_random_starts=n_random_starts, random_state=1)

assert_array_almost_equal(result1.x_iters, result2.x_iters)
assert_array_almost_equal(result1.func_vals, result2.func_vals)


@pytest.mark.parametrize("minimizer", MINIMIZERS)
def test_minimizer_with_space(minimizer):
# check we can pass a Space instance as dimensions argument and get same
# result
n_calls = 7
n_random_starts = 4

space = Space([(-5.0, 10.0), (0.0, 15.0)])
space_result = minimizer(branin, space, n_calls=n_calls,
n_random_starts=n_random_starts, random_state=1)

check_minimizer_api(space_result, n_calls)
check_minimizer_bounds(space_result, n_calls)

dimensions = [(-5.0, 10.0), (0.0, 15.0)]
result = minimizer(branin, dimensions, n_calls=n_calls,
n_random_starts=n_random_starts, random_state=1)

assert_array_almost_equal(space_result.x_iters, result.x_iters)
assert_array_almost_equal(space_result.func_vals, result.func_vals)


@pytest.mark.parametrize("n_random_starts, optimizer_func",
product([0, 5],
[gp_minimize,
Expand Down
10 changes: 10 additions & 0 deletions skopt/tests/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ def test_space_api():
assert_array_equal(b1, b2)


def test_space_from_space():
# can you pass a Space instance to the Space constructor?
space = Space([(0.0, 1.0), (-5, 5),
("a", "b", "c"), (1.0, 5.0, "log-uniform"), ("e", "f")])

space2 = Space(space)

assert_equal(space, space2)


def test_normalize():
a = Real(2.0, 30.0, transform="normalize")
for i in range(50):
Expand Down
0