Total Complexity | 0 |
Total Lines | 34 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | """ |
||
2 | GridSearchSk Optimizer Example |
||
3 | |||
4 | This example demonstrates how to use the GridSearchSk optimizer with |
||
5 | loky backend for parallel execution. |
||
6 | """ |
||
7 | |||
8 | import numpy as np |
||
9 | from hyperactive.opt import GridSearchSk |
||
10 | from hyperactive.experiment.toy import Sphere |
||
11 | |||
12 | # Define the optimization problem using a toy sphere function |
||
13 | # The sphere function f(x,y) = x² + y² has its minimum at (0,0) |
||
14 | sphere_experiment = Sphere(n_dim=2) |
||
15 | |||
16 | # Define parameter grid - creates a 9x9 = 81 point grid |
||
17 | param_grid = { |
||
18 | "x0": np.linspace(-2, 2, 9), |
||
19 | "x1": np.linspace(-2, 2, 9), |
||
20 | } |
||
21 | |||
22 | # Grid search with parallel execution using loky backend |
||
23 | grid_search = GridSearchSk( |
||
24 | param_grid=param_grid, |
||
25 | backend="loky", # Use loky backend for parallelization |
||
26 | backend_params={ |
||
27 | "n_jobs": -1, # Use all available CPU cores |
||
28 | }, |
||
29 | experiment=sphere_experiment, |
||
30 | ) |
||
31 | |||
32 | best_params = grid_search.run() |
||
33 | print(f"Best params: {best_params}, Score: {grid_search.best_score_:.6f}") |
||
34 |