Skip to content

Commit

Permalink
Support for suggest_int in neldermead sampler. (#373)
Browse files Browse the repository at this point in the history
* Support for suggest_int.

* Fix for ruff.
  • Loading branch information
KanaiYuma-aist authored Jun 17, 2024
1 parent 20fc863 commit 4ac7e6e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions aiaccel/hpo/samplers/nelder_mead_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def sample_independent(
param_index = list(self._search_space.keys()).index(param_name)
param_value = trial.user_attrs["params"][param_index]

if isinstance(param_distribution, optuna.distributions.IntDistribution):
param_value = int(param_value)

contains = param_distribution._contains(param_distribution.to_internal_repr(param_value))
if not contains:
warnings.warn(
Expand Down
31 changes: 31 additions & 0 deletions tests/hpo/samplers/results_ackley_int.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
x,y,objective
-2,9.014286128198322,14.586422044008508
4,1.973169683940732,9.375537945818003
-6,-6.880109593275947,14.844107008867795
-2,-0.6931908436032099,6.507841622369518
3,-7.7343072878608,14.961217967282048
0,4.827137774183542,10.531632891221612
2,-3.5471589338460197,10.452142477225951
1,-1.4535847568386293,6.1133866373150045
-5,-4.119945284382571,12.343741882384256
1,0.4498909418599062,4.566368541284509
5,-0.3105029713755132,11.5007990848864
0,-0.5975188755462857,3.243659824018007
0,1.3059568231522496,4.7031260630541
0,0.61607142815453,3.251423326258305
-1,-0.4313383892516619,4.526245031477097
-1,-0.21103105647376988,3.5486389761771218
0,0.22958360908201417,1.599587459483838
1,0.4498909418599062,4.566368541284509
0,-0.9840066946188015,2.605064206336659
1,-0.1569042099905017,3.212903780630767
0,-0.26705787637944767,1.8966954751653244
1,0.946532427321368,3.6137337954984194
0,-0.5013719141337591,3.0872498857179833
0,-0.018737133648716736,0.06231823476971021
0,-0.37721154276839375,2.6051598902371538
0,0.5880580182016912,3.2368252189682347
0,-0.13589415252587253,0.8089974198411198
0,-0.38421489525660346,2.6410686067975107
0,0.07613398299735977,0.3625465075510812
0,0.19329100187451556,1.2947108387400936
30 changes: 28 additions & 2 deletions tests/hpo/samplers/test_nelder_mead_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_after_trial_sub_sampler(
mock_iter.method.assert_not_called()


def ackley(x: list[float]) -> float:
def ackley(x: list[int | float]) -> float:
# Ackley function
y = (
-20 * np.exp(-0.2 * np.sqrt(0.5 * (x[0] ** 2 + x[1] ** 2)))
Expand Down Expand Up @@ -234,7 +234,7 @@ def sphere_sleep(x: list[float]) -> float:
class BaseTestNelderMead:
def common_setup(
self,
search_space: dict[str, tuple[float, float]],
search_space: dict[str, tuple[int | float, int | float]],
objective: Callable[[list[float]], float],
result_file_name: str,
study: optuna.Study,
Expand Down Expand Up @@ -435,3 +435,29 @@ def validation(self, results: list[dict[str | Any, str | Any]]) -> None:
assert math.isclose(trial.params["x"], float(result["x"]), rel_tol=0.000001)
assert math.isclose(trial.params["y"], float(result["y"]), rel_tol=0.000001)
assert math.isclose(trial.values[0], float(result["objective"]), rel_tol=0.000001)


class TestNelderMeadAckleyInteger(BaseTestNelderMead):
def setup_method(self) -> None:
search_space = {"x": (-10, 10), "y": (-10.0, 10.0)}
sampler = NelderMeadSampler(search_space=search_space, seed=42)

self.common_setup(
search_space=search_space,
objective=ackley,
result_file_name="results_ackley_int.csv",
study=optuna.create_study(sampler=sampler),
)

def validation(self, results: list[dict[str | Any, str | Any]]) -> None:
for trial, result in zip(self.study.trials, results, strict=False):
assert math.isclose(trial.params["x"], float(result["x"]), rel_tol=0.000001)
assert math.isclose(trial.params["y"], float(result["y"]), rel_tol=0.000001)
assert math.isclose(trial.values[0], float(result["objective"]), rel_tol=0.000001)

def func(self, trial: optuna.trial.Trial) -> float:
params: list[int | float] = []
params.append(trial.suggest_int("x", *[int(item) for item in self.search_space["x"]]))
params.append(trial.suggest_float("y", *self.search_space["y"]))

return self.objective(params)

0 comments on commit 4ac7e6e

Please sign in to comment.