Skip to content

Commit

Permalink
Check new neighbors deterministically (#195)
Browse files Browse the repository at this point in the history
* Check new neighbors deterministically

One-exchange neighborhood iterator rigorously checks some of the
generated configurations. The check is performed with the probability 5
%, as decided by `np.random`.

Make the check deterministic (using an explicitly seeded
`np.random.RandomState`) instead of relying on the default numpy random
state, which need not be seeded.

* Make a test consistent with the current behavior

of `get_one_exchange_neighborhood`.

Update the hard-coded expected values.
  • Loading branch information
filipbartek authored Sep 16, 2021
1 parent 9fd85c4 commit 22b979c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ConfigSpace/util.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def get_one_exchange_neighbourhood(
# Only rigorously check every tenth configuration (
# because moving around in the neighborhood should
# just work!)
if np.random.random() > 0.95:
if random.random() > 0.95:
new_configuration.is_valid_configuration()
else:
configuration_space._check_forbidden(new_array)
Expand Down
8 changes: 4 additions & 4 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ def test_random_neighborhood_float(self):
hp = UniformFloatHyperparameter('a', 1, 10)
all_neighbors = self._test_get_one_exchange_neighbourhood(hp)
all_neighbors = [neighbor['a'] for neighbor in all_neighbors]
self.assertAlmostEqual(5.44, np.mean(all_neighbors), places=2)
self.assertAlmostEqual(3.065, np.var(all_neighbors), places=2)
self.assertAlmostEqual(5.49, np.mean(all_neighbors), places=2)
self.assertAlmostEqual(3.192, np.var(all_neighbors), places=2)
hp = UniformFloatHyperparameter('a', 1, 10, log=True)
all_neighbors = self._test_get_one_exchange_neighbourhood(hp)
all_neighbors = [neighbor['a'] for neighbor in all_neighbors]
# Default value is 3.16
self.assertAlmostEqual(3.45, np.mean(all_neighbors), places=2)
self.assertAlmostEqual(2.67, np.var(all_neighbors), places=2)
self.assertAlmostEqual(3.50, np.mean(all_neighbors), places=2)
self.assertAlmostEqual(2.79, np.var(all_neighbors), places=2)

def test_random_neighbor_int(self):
hp = UniformIntegerHyperparameter('a', 1, 10)
Expand Down

0 comments on commit 22b979c

Please sign in to comment.