From 2012e7ea53ed9670f9283f8d6e9328f1c79cb2f6 Mon Sep 17 00:00:00 2001 From: Chinmaya Sahu Date: Thu, 7 Nov 2024 16:16:45 +0530 Subject: [PATCH] Made some improvements --- package/samplers/hill_climb_search/README.md | 8 ++++---- package/samplers/hill_climb_search/__init__.py | 4 ++-- package/samplers/hill_climb_search/example.py | 10 ++++------ .../hill_climb_search/hill_climb_search.py | 14 ++++++++++---- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/package/samplers/hill_climb_search/README.md b/package/samplers/hill_climb_search/README.md index dee21610..4a9ab190 100644 --- a/package/samplers/hill_climb_search/README.md +++ b/package/samplers/hill_climb_search/README.md @@ -13,7 +13,7 @@ The **hill climbing algorithm** is an optimization technique that iteratively im ## Class or Function Names -- HillClimbSearch +- HillClimbingSampler ## Example @@ -22,12 +22,12 @@ import optuna import optunahub def objective(trial): - x = trial.suggest_discrete_uniform("x", -10, 10) - y = trial.suggest_discrete_uniform("y", -10, 10) + x = trial.suggest_int("x", -10, 10) + y = trial.suggest_int("y", -10, 10) return -(x**2 + y**2) mod = optunahub.load_module("samplers/hill_climb_search") -sampler = mod.HillClimbSearch() +sampler = mod.HillClimbingSampler() study = optuna.create_study(sampler=sampler) study.optimize(objective, n_trials=20) ``` diff --git a/package/samplers/hill_climb_search/__init__.py b/package/samplers/hill_climb_search/__init__.py index 6b6c35bb..cfd376ea 100644 --- a/package/samplers/hill_climb_search/__init__.py +++ b/package/samplers/hill_climb_search/__init__.py @@ -1,4 +1,4 @@ -from .hill_climb_search import HillClimbSearch +from .hill_climb_search import HillClimbingSampler -__all__ = ["HillClimbSearch"] +__all__ = ["HillClimbingSampler"] diff --git a/package/samplers/hill_climb_search/example.py b/package/samplers/hill_climb_search/example.py index a2c27703..0d26c2d0 100644 --- a/package/samplers/hill_climb_search/example.py +++ b/package/samplers/hill_climb_search/example.py @@ -5,14 +5,12 @@ if __name__ == "__main__": def objective(trial: optuna.trial.Trial) -> None: - x = trial.suggest_discrete_uniform("x", -10, 10) - y = trial.suggest_discrete_uniform("y", -10, 10) + x = trial.suggest_int("x", -10, 10) + y = trial.suggest_int("y", -10, 10) return -(x**2 + y**2) - module = optunahub.load_module( - package="samplers/hill-climb-search", repo_owner="csking101", ref="hill-climb-algorithm" - ) - sampler = module.HillClimbSearch() + module = optunahub.load_module(package="samplers/hill-climb-search") + sampler = module.HillClimbingSampler() study = optuna.create_study(sampler=sampler) study.optimize(objective, n_trials=20) diff --git a/package/samplers/hill_climb_search/hill_climb_search.py b/package/samplers/hill_climb_search/hill_climb_search.py index 0f79d301..24e946ed 100644 --- a/package/samplers/hill_climb_search/hill_climb_search.py +++ b/package/samplers/hill_climb_search/hill_climb_search.py @@ -7,7 +7,7 @@ import optunahub -class HillClimbSearch(optunahub.samplers.SimpleBaseSampler): +class HillClimbingSampler(optunahub.samplers.SimpleBaseSampler): """A sampler based on the Hill Climb Local Search Algorithm dealing with discrete values.""" def __init__( @@ -32,7 +32,7 @@ def _generate_random_point( """This function generates a random discrete point in the search space""" params = {} for param_name, param_distribution in search_space.items(): - if isinstance(param_distribution, optuna.distributions.FloatDistribution): + if isinstance(param_distribution, optuna.distributions.IntDistribution): total_points = int( (param_distribution.high - param_distribution.low) / param_distribution.step ) @@ -70,7 +70,7 @@ def _generate_neighbors( """This function generates the neighbors of the current point""" neighbors = [] for param_name, param_distribution in search_space.items(): - if isinstance(param_distribution, optuna.distributions.FloatDistribution): + if isinstance(param_distribution, optuna.distributions.IntDistribution): current_value = current_point[param_name] step = param_distribution.step @@ -127,7 +127,13 @@ def sample_relative( # Store the value of the neighbor, if it improves upon the current point neighbor_value = previous_trial.value - if neighbor_value < self._current_point_value: + criteria = ( + neighbor_value < self._current_point_value + if study.direction == "minimize" + else neighbor_value > self._current_point_value + ) + + if criteria: self._best_neighbor = previous_trial.params self._best_neighbor_value = neighbor_value