|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
from typing import List, Dict, Literal, Union |
|
6
|
|
|
|
|
7
|
|
|
from ..search import Search |
|
8
|
|
|
from ..optimizers import BayesianOptimizer as _BayesianOptimizer |
|
9
|
|
|
from ..optimizers.smb_opt.bayesian_optimization import gaussian_process |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
View Code Duplication |
class BayesianOptimizer(_BayesianOptimizer, Search): |
|
|
|
|
|
|
13
|
|
|
""" |
|
14
|
|
|
A class implementing the **bayesian optimizer** for the public API. |
|
15
|
|
|
Inheriting from the `Search`-class to get the `search`-method and from |
|
16
|
|
|
the `BayesianOptimizer`-backend to get the underlying algorithm. |
|
17
|
|
|
|
|
18
|
|
|
Parameters |
|
19
|
|
|
---------- |
|
20
|
|
|
search_space : dict[str, list] |
|
21
|
|
|
The search space to explore. A dictionary with parameter |
|
22
|
|
|
names as keys and a numpy array as values. |
|
23
|
|
|
initialize : dict[str, int] |
|
24
|
|
|
The method to generate initial positions. A dictionary with |
|
25
|
|
|
the following key literals and the corresponding value type: |
|
26
|
|
|
{"grid": int, "vertices": int, "random": int, "warm_start": list[dict]} |
|
27
|
|
|
constraints : list[callable] |
|
28
|
|
|
A list of constraints, where each constraint is a callable. |
|
29
|
|
|
The callable returns `True` or `False` dependend on the input parameters. |
|
30
|
|
|
random_state : None, int |
|
31
|
|
|
If None, create a new random state. If int, create a new random state |
|
32
|
|
|
seeded with the value. |
|
33
|
|
|
rand_rest_p : float |
|
34
|
|
|
The probability of a random iteration during the search process. |
|
35
|
|
|
warm_start_smbo |
|
36
|
|
|
The warm start for SMBO. |
|
37
|
|
|
max_sample_size : int |
|
38
|
|
|
The maximum number of points to sample. |
|
39
|
|
|
sampling : dict |
|
40
|
|
|
The sampling method to use. |
|
41
|
|
|
replacement : bool |
|
42
|
|
|
Whether to sample with replacement. |
|
43
|
|
|
gpr : dict |
|
44
|
|
|
The Gaussian Process Regressor to use. |
|
45
|
|
|
xi : float |
|
46
|
|
|
The exploration-exploitation trade-off parameter. |
|
47
|
|
|
""" |
|
48
|
|
|
|
|
49
|
|
|
def __init__( |
|
50
|
|
|
self, |
|
51
|
|
|
search_space: Dict[str, list], |
|
52
|
|
|
initialize: Dict[ |
|
53
|
|
|
Literal["grid", "vertices", "random", "warm_start"], |
|
54
|
|
|
Union[int, list[dict]], |
|
55
|
|
|
] = {"grid": 4, "random": 2, "vertices": 4}, |
|
56
|
|
|
constraints: List[callable] = [], |
|
57
|
|
|
random_state: int = None, |
|
58
|
|
|
rand_rest_p: float = 0, |
|
59
|
|
|
nth_process: int = None, |
|
60
|
|
|
warm_start_smbo=None, |
|
61
|
|
|
max_sample_size: int = 10000000, |
|
62
|
|
|
sampling: Dict[Literal["random"], int] = {"random": 1000000}, |
|
63
|
|
|
replacement: bool = True, |
|
64
|
|
|
gpr=gaussian_process["gp_nonlinear"], |
|
65
|
|
|
xi: float = 0.03, |
|
66
|
|
|
): |
|
67
|
|
|
super().__init__( |
|
68
|
|
|
search_space=search_space, |
|
69
|
|
|
initialize=initialize, |
|
70
|
|
|
constraints=constraints, |
|
71
|
|
|
random_state=random_state, |
|
72
|
|
|
rand_rest_p=rand_rest_p, |
|
73
|
|
|
nth_process=nth_process, |
|
74
|
|
|
warm_start_smbo=warm_start_smbo, |
|
75
|
|
|
max_sample_size=max_sample_size, |
|
76
|
|
|
sampling=sampling, |
|
77
|
|
|
replacement=replacement, |
|
78
|
|
|
gpr=gpr, |
|
79
|
|
|
xi=xi, |
|
80
|
|
|
) |
|
81
|
|
|
|