1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
from typing import List, Dict, Literal, Literal |
6
|
|
|
|
7
|
|
|
from ..search import Search |
8
|
|
|
from ..optimizers import GridSearchOptimizer as _GridSearchOptimizer |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class GridSearchOptimizer(_GridSearchOptimizer, Search): |
12
|
|
|
""" |
13
|
|
|
A class implementing **grid search** for the public API. |
14
|
|
|
Inheriting from the `Search`-class to get the `search`-method and from |
15
|
|
|
the `GridSearchOptimizer`-backend to get the underlying algorithm. |
16
|
|
|
|
17
|
|
|
Parameters |
18
|
|
|
---------- |
19
|
|
|
search_space : dict[str, list] |
20
|
|
|
The search space to explore. A dictionary with parameter |
21
|
|
|
names as keys and a numpy array as values. |
22
|
|
|
initialize : dict[str, int] |
23
|
|
|
The method to generate initial positions. A dictionary with |
24
|
|
|
the following key literals and the corresponding value type: |
25
|
|
|
{"grid": int, "vertices": int, "random": int, "warm_start": list[dict]} |
26
|
|
|
constraints : list[callable] |
27
|
|
|
A list of constraints, where each constraint is a callable. |
28
|
|
|
The callable returns `True` or `False` dependend on the input parameters. |
29
|
|
|
random_state : None, int |
30
|
|
|
If None, create a new random state. If int, create a new random state |
31
|
|
|
seeded with the value. |
32
|
|
|
rand_rest_p : float |
33
|
|
|
The probability of a random iteration during the the search process. |
34
|
|
|
step_size : int |
35
|
|
|
The step-size for the grid search. |
36
|
|
|
direction : "diagonal" or "orthogonal" |
37
|
|
|
The direction of the grid search. |
38
|
|
|
""" |
39
|
|
|
|
40
|
|
View Code Duplication |
def __init__( |
|
|
|
|
41
|
|
|
self, |
42
|
|
|
search_space: Dict[str, list], |
43
|
|
|
initialize: Dict[ |
44
|
|
|
Literal["grid", "vertices", "random", "warm_start"], int | List |
45
|
|
|
] = {"grid": 4, "random": 2, "vertices": 4}, |
46
|
|
|
constraints: List[callable] = [], |
47
|
|
|
random_state: int = None, |
48
|
|
|
rand_rest_p: float = 0, |
49
|
|
|
nth_process: int = None, |
50
|
|
|
step_size: int = 1, |
51
|
|
|
direction: Literal["diagonal", "orthogonal"] = "diagonal", |
52
|
|
|
): |
53
|
|
|
super().__init__( |
54
|
|
|
search_space=search_space, |
55
|
|
|
initialize=initialize, |
56
|
|
|
constraints=constraints, |
57
|
|
|
random_state=random_state, |
58
|
|
|
rand_rest_p=rand_rest_p, |
59
|
|
|
nth_process=nth_process, |
60
|
|
|
step_size=step_size, |
61
|
|
|
direction=direction, |
62
|
|
|
) |
63
|
|
|
|