Skip to content

Commit 0d4197a

Browse files
committed
add get_test_params-method to some optuna sampler classes
1 parent 39835b0 commit 0d4197a

5 files changed

Lines changed: 199 additions & 9 deletions

File tree

src/hyperactive/opt/optuna/_cmaes_sampler.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,52 @@ def _get_sampler(self):
125125
@classmethod
126126
def get_test_params(cls, parameter_set="default"):
127127
"""Return testing parameter settings for the optimizer."""
128+
from hyperactive.experiment.integrations import SklearnCvExperiment
129+
from sklearn.datasets import make_regression
130+
from sklearn.neural_network import MLPRegressor
131+
132+
# Test case 1: Basic continuous parameters (from base)
128133
params = super().get_test_params(parameter_set)
129134
params[0].update({
130135
"sigma0": 0.5,
131136
"n_startup_trials": 1,
132137
})
138+
139+
# Test case 2: Neural network with continuous parameters only
140+
# (CMA-ES specific - only continuous parameters allowed)
141+
X, y = make_regression(n_samples=50, n_features=5, noise=0.1, random_state=42)
142+
mlp_exp = SklearnCvExperiment(
143+
estimator=MLPRegressor(random_state=42, max_iter=100),
144+
X=X, y=y, cv=3
145+
)
146+
147+
continuous_param_space = {
148+
"alpha": (1e-5, 1e-1), # L2 regularization (continuous)
149+
"learning_rate_init": (1e-4, 1e-1), # Learning rate (continuous)
150+
"beta_1": (0.8, 0.99), # Adam beta1 (continuous)
151+
"beta_2": (0.9, 0.999), # Adam beta2 (continuous)
152+
# Note: No categorical parameters - CMA-ES doesn't support them
153+
}
154+
155+
params.append({
156+
"param_space": continuous_param_space,
157+
"n_trials": 8, # Smaller for faster testing
158+
"experiment": mlp_exp,
159+
"sigma0": 0.3, # Different sigma for diversity
160+
"n_startup_trials": 2, # More startup trials
161+
})
162+
163+
# Test case 3: High-dimensional continuous space (CMA-ES strength)
164+
high_dim_continuous = {
165+
f"x{i}": (-1.0, 1.0) for i in range(6) # 6D continuous optimization
166+
}
167+
168+
params.append({
169+
"param_space": high_dim_continuous,
170+
"n_trials": 12,
171+
"experiment": mlp_exp,
172+
"sigma0": 0.7, # Larger initial spread
173+
"n_startup_trials": 3,
174+
})
175+
133176
return params

src/hyperactive/opt/optuna/_grid_sampler.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,47 @@ def get_test_params(cls, parameter_set="default"):
118118
from hyperactive.experiment.integrations import SklearnCvExperiment
119119
from sklearn.datasets import load_iris
120120
from sklearn.svm import SVC
121+
from sklearn.neighbors import KNeighborsClassifier
121122

122123
X, y = load_iris(return_X_y=True)
123-
sklearn_exp = SklearnCvExperiment(estimator=SVC(), X=X, y=y)
124-
125-
param_space = {
124+
125+
# Test case 1: Basic continuous parameters (converted to discrete)
126+
svm_exp = SklearnCvExperiment(estimator=SVC(), X=X, y=y)
127+
param_space_1 = {
126128
"C": [0.01, 0.1, 1, 10],
127129
"gamma": [0.0001, 0.01, 0.1, 1],
128130
}
129-
130-
return [{
131-
"param_space": param_space,
132-
"n_trials": 10,
133-
"experiment": sklearn_exp,
134-
}]
131+
132+
# Test case 2: Mixed categorical and discrete parameters
133+
knn_exp = SklearnCvExperiment(estimator=KNeighborsClassifier(), X=X, y=y)
134+
param_space_2 = {
135+
"n_neighbors": [1, 3, 5, 7], # Discrete integers
136+
"weights": ["uniform", "distance"], # Categorical
137+
"metric": ["euclidean", "manhattan"], # Categorical
138+
"p": [1, 2], # Discrete for minkowski
139+
}
140+
141+
# Test case 3: Small exhaustive grid (tests complete enumeration)
142+
param_space_3 = {
143+
"C": [0.1, 1], # 2 values
144+
"kernel": ["rbf", "linear"], # 2 values
145+
}
146+
# Total: 2 × 2 = 4 combinations, n_trials should cover all
147+
148+
return [
149+
{
150+
"param_space": param_space_1,
151+
"n_trials": 10,
152+
"experiment": svm_exp,
153+
},
154+
{
155+
"param_space": param_space_2,
156+
"n_trials": 15,
157+
"experiment": knn_exp,
158+
},
159+
{
160+
"param_space": param_space_3,
161+
"n_trials": 4, # Exact number for exhaustive search
162+
"experiment": svm_exp,
163+
}
164+
]

src/hyperactive/opt/optuna/_nsga_ii_sampler.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,37 @@ def _get_sampler(self):
115115
@classmethod
116116
def get_test_params(cls, parameter_set="default"):
117117
"""Return testing parameter settings for the optimizer."""
118+
from hyperactive.experiment.integrations import SklearnCvExperiment
119+
from sklearn.datasets import load_iris
120+
from sklearn.ensemble import RandomForestClassifier
121+
122+
# Test case 1: Basic single-objective (inherits from base)
118123
params = super().get_test_params(parameter_set)
119124
params[0].update({
120125
"population_size": 20,
121126
"mutation_prob": 0.2,
122127
"crossover_prob": 0.8,
123128
})
129+
130+
# Test case 2: Multi-objective with mixed parameter types
131+
X, y = load_iris(return_X_y=True)
132+
rf_exp = SklearnCvExperiment(estimator=RandomForestClassifier(random_state=42), X=X, y=y)
133+
134+
mixed_param_space = {
135+
"n_estimators": (10, 50), # Continuous integer
136+
"max_depth": [3, 5, 7, None], # Mixed discrete/None
137+
"criterion": ["gini", "entropy"], # Categorical
138+
"min_samples_split": (2, 10), # Continuous integer
139+
"bootstrap": [True, False], # Boolean categorical
140+
}
141+
142+
params.append({
143+
"param_space": mixed_param_space,
144+
"n_trials": 15, # Smaller for faster testing
145+
"experiment": rf_exp,
146+
"population_size": 8, # Smaller population for testing
147+
"mutation_prob": 0.1,
148+
"crossover_prob": 0.9,
149+
})
150+
124151
return params

src/hyperactive/opt/optuna/_qmc_sampler.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,47 @@ def _get_sampler(self):
110110
@classmethod
111111
def get_test_params(cls, parameter_set="default"):
112112
"""Return testing parameter settings for the optimizer."""
113+
from hyperactive.experiment.integrations import SklearnCvExperiment
114+
from sklearn.datasets import load_iris
115+
from sklearn.linear_model import LogisticRegression
116+
117+
# Test case 1: Halton sequence without scrambling
113118
params = super().get_test_params(parameter_set)
114119
params[0].update({
115120
"qmc_type": "halton",
116121
"scramble": False,
117122
})
123+
124+
# Test case 2: Sobol sequence with scrambling
125+
X, y = load_iris(return_X_y=True)
126+
lr_exp = SklearnCvExperiment(estimator=LogisticRegression(random_state=42, max_iter=1000), X=X, y=y)
127+
128+
mixed_param_space = {
129+
"C": (0.01, 100), # Continuous
130+
"penalty": ["l1", "l2", "elasticnet"], # Categorical
131+
"l1_ratio": (0.0, 1.0), # Continuous ratio
132+
"solver": ["liblinear", "saga"], # Categorical
133+
}
134+
135+
params.append({
136+
"param_space": mixed_param_space,
137+
"n_trials": 16, # Power of 2 for better QMC properties
138+
"experiment": lr_exp,
139+
"qmc_type": "sobol", # Different sequence type
140+
"scramble": True, # With scrambling for randomization
141+
})
142+
143+
# Test case 3: Higher dimensional space (tests QMC scaling)
144+
high_dim_space = {
145+
f"param_{i}": (0.0, 1.0) for i in range(8) # 8-dimensional continuous space
146+
}
147+
148+
params.append({
149+
"param_space": high_dim_space,
150+
"n_trials": 32, # Power of 2, good for QMC
151+
"experiment": lr_exp,
152+
"qmc_type": "sobol",
153+
"scramble": False,
154+
})
155+
118156
return params

src/hyperactive/opt/optuna/_tpe_sampler.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,61 @@ def _get_sampler(self):
117117
@classmethod
118118
def get_test_params(cls, parameter_set="default"):
119119
"""Return testing parameter settings for the optimizer."""
120+
from hyperactive.experiment.integrations import SklearnCvExperiment
121+
from sklearn.datasets import load_wine
122+
from sklearn.ensemble import RandomForestClassifier
123+
from sklearn.svm import SVC
124+
125+
# Test case 1: Basic TPE with standard parameters
120126
params = super().get_test_params(parameter_set)
121127
params[0].update({
122128
"n_startup_trials": 5,
123129
"n_ei_candidates": 12,
124130
})
131+
132+
# Test case 2: Mixed parameter types with warm start
133+
X, y = load_wine(return_X_y=True)
134+
rf_exp = SklearnCvExperiment(estimator=RandomForestClassifier(random_state=42), X=X, y=y)
135+
136+
mixed_param_space = {
137+
"n_estimators": (10, 100), # Continuous integer
138+
"max_depth": [3, 5, 7, 10, None], # Mixed discrete/None
139+
"criterion": ["gini", "entropy"], # Categorical
140+
"min_samples_split": (2, 20), # Continuous integer
141+
"bootstrap": [True, False], # Boolean
142+
}
143+
144+
# Warm start with known good configuration
145+
warm_start_points = [
146+
{"n_estimators": 50, "max_depth": 5, "criterion": "gini",
147+
"min_samples_split": 2, "bootstrap": True}
148+
]
149+
150+
params.append({
151+
"param_space": mixed_param_space,
152+
"n_trials": 20,
153+
"experiment": rf_exp,
154+
"n_startup_trials": 3, # Fewer random trials before TPE
155+
"n_ei_candidates": 24, # More EI candidates for better optimization
156+
"initialize": {"warm_start": warm_start_points},
157+
})
158+
159+
# Test case 3: High-dimensional continuous space (TPE strength)
160+
svm_exp = SklearnCvExperiment(estimator=SVC(), X=X, y=y)
161+
high_dim_space = {
162+
"C": (0.01, 100),
163+
"gamma": (1e-6, 1e2),
164+
"coef0": (0.0, 10.0),
165+
"degree": (2, 5),
166+
"tol": (1e-5, 1e-2),
167+
}
168+
169+
params.append({
170+
"param_space": high_dim_space,
171+
"n_trials": 25,
172+
"experiment": svm_exp,
173+
"n_startup_trials": 8, # More startup for exploration
174+
"n_ei_candidates": 32, # More candidates for complex space
175+
})
176+
125177
return params

0 commit comments

Comments
 (0)