Skip to content

Commit c7bb77f

Browse files
committed
add tpe optimizer
1 parent 49210f2 commit c7bb77f

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

src/hyperactive/opt/gfo/_tree_structured_parzen_estimators.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,76 @@
22

33

44
class TreeStructuredParzenEstimators(_BaseGFOadapter):
5+
"""Tree structured parzen estimators optimizer.
6+
7+
Parameters
8+
----------
9+
search_space : dict[str, list]
10+
The search space to explore. A dictionary with parameter
11+
names as keys and a numpy array as values.
12+
Optional, can be passed later via ``set_params``.
13+
initialize : dict[str, int], default={"grid": 4, "random": 2, "vertices": 4}
14+
The method to generate initial positions. A dictionary with
15+
the following key literals and the corresponding value type:
16+
{"grid": int, "vertices": int, "random": int, "warm_start": list[dict]}
17+
constraints : list[callable], default=[]
18+
A list of constraints, where each constraint is a callable.
19+
The callable returns `True` or `False` dependend on the input parameters.
20+
random_state : None, int, default=None
21+
If None, create a new random state. If int, create a new random state
22+
seeded with the value.
23+
rand_rest_p : float, default=0.1
24+
The probability of a random iteration during the the search process.
25+
warm_start_smbo
26+
The warm start for SMBO.
27+
max_sample_size : int
28+
The maximum number of points to sample.
29+
sampling : dict
30+
The sampling method to use.
31+
replacement : bool
32+
Whether to sample with replacement.
33+
gamma_tpe : float
34+
The parameter for the Tree Structured Parzen Estimators
35+
n_iter : int, default=100
36+
The number of iterations to run the optimizer.
37+
verbose : bool, default=False
38+
If True, print the progress of the optimization process.
39+
experiment : BaseExperiment, optional
40+
The experiment to optimize parameters for.
41+
Optional, can be passed later via ``set_params``.
42+
"""
43+
44+
def __init__(
45+
self,
46+
search_space=None,
47+
initialize=None,
48+
constraints=None,
49+
random_state=None,
50+
rand_rest_p=0.1,
51+
warm_start_smbo=None,
52+
max_sample_size=10000000,
53+
sampling={"random": 1000000},
54+
replacement=True,
55+
gamma_tpe=0.2,
56+
n_iter=100,
57+
verbose=False,
58+
experiment=None,
59+
):
60+
self.random_state = random_state
61+
self.rand_rest_p = rand_rest_p
62+
self.warm_start_smbo = warm_start_smbo
63+
self.max_sample_size = max_sample_size
64+
self.sampling = sampling
65+
self.replacement = replacement
66+
self.gamma_tpe = gamma_tpe
67+
self.search_space = search_space
68+
self.initialize = initialize
69+
self.constraints = constraints
70+
self.n_iter = n_iter
71+
self.experiment = experiment
72+
self.verbose = verbose
73+
74+
super().__init__()
575

676
def _get_gfo_class(self):
777
"""Get the GFO class to use.
@@ -14,3 +84,30 @@ def _get_gfo_class(self):
1484
from gradient_free_optimizers import TreeStructuredParzenEstimators
1585

1686
return TreeStructuredParzenEstimators
87+
88+
@classmethod
89+
def get_test_params(cls, parameter_set="default"):
90+
"""Get the test parameters for the optimizer.
91+
92+
Returns
93+
-------
94+
dict with str keys
95+
The test parameters dictionary.
96+
"""
97+
import numpy as np
98+
99+
params = super().get_test_params()
100+
experiment = params[0]["experiment"]
101+
more_params = {
102+
"experiment": experiment,
103+
"max_sample_size": 100,
104+
"replacement": False,
105+
"gamma_tpe": 0.01,
106+
"search_space": {
107+
"C": np.array([0.01, 0.1, 1, 10]),
108+
"gamma": np.array([0.0001, 0.01, 0.1, 1, 10]),
109+
},
110+
"n_iter": 100,
111+
}
112+
params.append(more_params)
113+
return params

0 commit comments

Comments
 (0)