diff --git a/examples/optimization_techniques/grid_search_sk.py b/examples/optimization_techniques/grid_search_sk.py new file mode 100644 index 00000000..9a2d74a6 --- /dev/null +++ b/examples/optimization_techniques/grid_search_sk.py @@ -0,0 +1,33 @@ +""" +GridSearchSk Optimizer Example + +This example demonstrates how to use the GridSearchSk optimizer with +loky backend for parallel execution. +""" + +import numpy as np +from hyperactive.opt import GridSearchSk +from hyperactive.experiment.toy import Sphere + +# Define the optimization problem using a toy sphere function +# The sphere function f(x,y) = x² + y² has its minimum at (0,0) +sphere_experiment = Sphere(n_dim=2) + +# Define parameter grid - creates a 9x9 = 81 point grid +param_grid = { + "x0": np.linspace(-2, 2, 9), + "x1": np.linspace(-2, 2, 9), +} + +# Grid search with parallel execution using loky backend +grid_search = GridSearchSk( + param_grid=param_grid, + backend="loky", # Use loky backend for parallelization + backend_params={ + "n_jobs": -1, # Use all available CPU cores + }, + experiment=sphere_experiment, +) + +best_params = grid_search.run() +print(f"Best params: {best_params}, Score: {grid_search.best_score_:.6f}") diff --git a/examples/optimization_techniques/random_search_sk.py b/examples/optimization_techniques/random_search_sk.py new file mode 100644 index 00000000..ffdc91be --- /dev/null +++ b/examples/optimization_techniques/random_search_sk.py @@ -0,0 +1,44 @@ +""" +RandomSearchSk Optimizer Example + +This example demonstrates how to use the RandomSearchSk optimizer with +sklearn integration and threading backend for parallel execution. +""" + +import numpy as np +from scipy.stats import uniform, randint +from sklearn.datasets import load_iris +from sklearn.svm import SVC +from hyperactive.opt import RandomSearchSk +from hyperactive.experiment.integrations import SklearnCvExperiment + +# Load iris dataset and create sklearn experiment +X, y = load_iris(return_X_y=True) +sklearn_experiment = SklearnCvExperiment( + estimator=SVC(), + X=X, + y=y, +) + +# Define parameter distributions for random search +# Mix of discrete and continuous distributions +param_distributions = { + "C": uniform(loc=0.1, scale=10), # Continuous uniform distribution + "gamma": ["scale", "auto"] + list(np.logspace(-4, 1, 20)), # Mixed discrete/continuous + "kernel": ["rbf", "poly", "sigmoid"], # Discrete choices +} + +# Random search with threading backend +random_search = RandomSearchSk( + param_distributions=param_distributions, + n_iter=30, # Number of random samples + random_state=42, # For reproducible results + backend="threading", # Use threading backend for parallelization + backend_params={ + "n_jobs": 2, # Use 2 threads + }, + experiment=sklearn_experiment, +) + +best_params = random_search.run() +print(f"Best params: {best_params}, Score: {random_search.best_score_:.4f}") diff --git a/src/hyperactive/opt/gridsearch/_sk.py b/src/hyperactive/opt/gridsearch/_sk.py index d5060d65..39c6e9b1 100644 --- a/src/hyperactive/opt/gridsearch/_sk.py +++ b/src/hyperactive/opt/gridsearch/_sk.py @@ -1,4 +1,5 @@ """Grid search optimizer.""" + # copyright: hyperactive developers, MIT License (see LICENSE file) from collections.abc import Sequence diff --git a/src/hyperactive/opt/random_search.py b/src/hyperactive/opt/random_search.py index 18a7fb01..4d29c24b 100644 --- a/src/hyperactive/opt/random_search.py +++ b/src/hyperactive/opt/random_search.py @@ -69,6 +69,32 @@ class RandomSearchSk(BaseOptimizer): Callable returning a scalar score when invoked with keyword arguments matching a parameter set. + Example + ------- + Random search with different backend configurations: + + >>> from hyperactive.opt import RandomSearchSk + >>> from scipy.stats import uniform + >>> param_distributions = { + ... "C": uniform(loc=0.1, scale=10), + ... "gamma": ["scale", "auto", 0.001, 0.01, 0.1, 1], + ... } + >>> + >>> # Sequential execution + >>> random_search = RandomSearchSk( + ... param_distributions=param_distributions, + ... n_iter=20, + ... backend="None", + ... ) + >>> + >>> # Parallel execution with threading backend + >>> random_search_parallel = RandomSearchSk( + ... param_distributions=param_distributions, + ... n_iter=20, + ... backend="threading", + ... backend_params={"n_jobs": 2}, + ... ) + Attributes ---------- best_params_ : dict[str, Any] diff --git a/src/hyperactive/utils/parallel.py b/src/hyperactive/utils/parallel.py index 3591c2a9..2678897b 100644 --- a/src/hyperactive/utils/parallel.py +++ b/src/hyperactive/utils/parallel.py @@ -270,6 +270,7 @@ def _get_parallel_test_fixtures(naming="estimator"): fixtures.append({"backend": "dask", "backend_params": {"scheduler": "sync"}}) # test ray backend + """ TODO: faster ray test if _check_soft_dependencies("ray", severity="none"): import os @@ -285,5 +286,5 @@ def _get_parallel_test_fixtures(naming="estimator"): fixtures = [x for x in fixtures if x["backend"] not in SKIP_FIXTURES] # remove backends in SKIP_FIXTURES from fixtures - + """ return fixtures