Skip to content

Commit

Permalink
optim: UniformFloat get neighbors
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiebergman committed Dec 19, 2023
1 parent 79decbd commit add84b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
13 changes: 9 additions & 4 deletions ConfigSpace/hyperparameters/uniform_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,18 @@ def __init__(
self.normalized_default_value = self._inverse_transform(self.default_value)
super().__init__(name=name, default_value=default_value, meta=meta)

def is_legal(self, value: float) -> bool:
def is_legal(self, value: float, *, normalized: bool = False) -> bool:
if not isinstance(value, (float, int)):
return False
return self.upper >= value >= self.lower

if normalized:
return 0.0 <= value <= 1.0

return self.lower <= value <= self.upper

def is_legal_vector(self, value: float) -> bool:
# NOTE: This really needs a better name as it doesn't operate on vectors,
# rather individual values.
# it means that it operates on the normalzied space, i.e. what is gotten
# by inverse_transform
return 1.0 >= value >= 0.0
Expand Down Expand Up @@ -216,8 +221,8 @@ def get_neighbors(
# Generate batches of number * 2 candidates, filling the above
# buffer until we have enough valid candidates.
# We should not overflow as the buffer
while offset <= number:
candidates = rs.normal(value, std, size=SAMPLE_SIZE)
while offset < number:
candidates = rs.normal(value, std, size=(SAMPLE_SIZE,))
valid_candidates = candidates[(candidates >= 0) & (candidates <= 1)]

n_candidates = len(valid_candidates)
Expand Down
18 changes: 10 additions & 8 deletions test/test_hyperparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ def test_uniformfloat_is_legal():
assert not f1.is_legal_vector(-0.1)
assert not f1.is_legal_vector(1.1)


with pytest.raises(TypeError):
assert not f1.is_legal_vector("Hahaha")

Expand Down Expand Up @@ -742,9 +741,9 @@ def test_betafloat():
b1 = BetaFloatHyperparameter("param", lower=0.0, upper=1.0, alpha=3.0, beta=1.0)

# with identical domains, beta and uniform should sample the same points
assert u1.get_neighbors(0.5, rs=np.random.RandomState(42)) == b1.get_neighbors(
0.5,
rs=np.random.RandomState(42),
np.testing.assert_equal(
u1.get_neighbors(0.5, rs=np.random.RandomState(42)),
b1.get_neighbors(0.5, rs=np.random.RandomState(42)),
)
# Test copy
copy_f1 = copy.copy(f1)
Expand Down Expand Up @@ -1186,9 +1185,7 @@ def test_uniforminteger():
assert f1.log is False
assert f1.normalized_default_value == pytest.approx((2.0 + 0.49999) / (5.49999 + 0.49999))

quantization_warning = (
"Setting quantization < 1 for Integer Hyperparameter param has no effect"
)
quantization_warning = "Setting quantization < 1 for Integer Hyperparameter param has no effect"
with pytest.raises(ValueError, match=quantization_warning):
_ = UniformIntegerHyperparameter("param", 0, 10, q=0.1) # type: ignore
with pytest.raises(ValueError, match=quantization_warning):
Expand Down Expand Up @@ -1881,7 +1878,12 @@ def test_betaint_legal_float_values():
match="Illegal default value 0.5",
):
_ = BetaIntegerHyperparameter(
"param", lower=-2.0, upper=2.0, alpha=3.0, beta=1.1, default_value=0.5,
"param",
lower=-2.0,
upper=2.0,
alpha=3.0,
beta=1.1,
default_value=0.5,
)


Expand Down
11 changes: 6 additions & 5 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def _test_get_one_exchange_neighbourhood(hp):
num_neighbors = 0
if not isinstance(hp, list):
hp = [hp]

for hp_ in hp:
cs.add_hyperparameter(hp_)
if np.isinf(hp_.get_num_neighbors()):
Expand All @@ -90,7 +91,7 @@ def _test_get_one_exchange_neighbourhood(hp):
config = cs.get_default_configuration()
all_neighbors = []
for i in range(100):
neighborhood = get_one_exchange_neighbourhood(config, i)
neighborhood = get_one_exchange_neighbourhood(config, i, num_neighbors=num_neighbors)
for new_config in neighborhood:
assert config != new_config
assert dict(config) != dict(new_config)
Expand Down Expand Up @@ -129,14 +130,14 @@ def test_random_neighborhood_float():
hp = UniformFloatHyperparameter("a", 1, 10)
all_neighbors = _test_get_one_exchange_neighbourhood(hp)
all_neighbors = [neighbor["a"] for neighbor in all_neighbors]
assert pytest.approx(np.mean(all_neighbors), abs=1e-2) == 5.49
assert pytest.approx(np.var(all_neighbors), abs=1e-2) == 3.192
assert pytest.approx(np.mean(all_neighbors), abs=1e-2) == 5.47
assert pytest.approx(np.var(all_neighbors), abs=1e-2) == 2.78
hp = UniformFloatHyperparameter("a", 1, 10, log=True)
all_neighbors = _test_get_one_exchange_neighbourhood(hp)
all_neighbors = [neighbor["a"] for neighbor in all_neighbors]
# Default value is 3.16
assert pytest.approx(np.mean(all_neighbors), abs=1e-2) == 3.50
assert pytest.approx(np.var(all_neighbors), abs=1e-2) == 2.79
assert pytest.approx(np.mean(all_neighbors), abs=1e-2) == 3.43
assert pytest.approx(np.var(all_neighbors), abs=1e-2) == 2.17


def test_random_neighbor_int():
Expand Down

0 comments on commit add84b5

Please sign in to comment.