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 ( |
9
|
|
|
RandomRestartHillClimbingOptimizer as _RandomRestartHillClimbingOptimizer, |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
|
13
|
|
View Code Duplication |
class RandomRestartHillClimbingOptimizer( |
|
|
|
|
14
|
|
|
_RandomRestartHillClimbingOptimizer, Search |
15
|
|
|
): |
16
|
|
|
""" |
17
|
|
|
A class implementing the **random restart hill climbing optimizer** for the public API. |
18
|
|
|
Inheriting from the `Search`-class to get the `search`-method and from |
19
|
|
|
the `RandomRestartHillClimbingOptimizer`-backend to get the underlying algorithm. |
20
|
|
|
|
21
|
|
|
Parameters |
22
|
|
|
---------- |
23
|
|
|
search_space : dict[str, list] |
24
|
|
|
The search space to explore. A dictionary with parameter |
25
|
|
|
names as keys and a numpy array as values. |
26
|
|
|
initialize : dict[str, int] |
27
|
|
|
The method to generate initial positions. A dictionary with |
28
|
|
|
the following key literals and the corresponding value type: |
29
|
|
|
{"grid": int, "vertices": int, "random": int, "warm_start": list[dict]} |
30
|
|
|
constraints : list[callable] |
31
|
|
|
A list of constraints, where each constraint is a callable. |
32
|
|
|
The callable returns `True` or `False` dependend on the input parameters. |
33
|
|
|
random_state : None, int |
34
|
|
|
If None, create a new random state. If int, create a new random state |
35
|
|
|
seeded with the value. |
36
|
|
|
rand_rest_p : float |
37
|
|
|
The probability of a random iteration during the the search process. |
38
|
|
|
epsilon : float |
39
|
|
|
The step-size for the climbing. |
40
|
|
|
distribution : str |
41
|
|
|
The type of distribution to sample from. |
42
|
|
|
n_neighbours : int |
43
|
|
|
The number of neighbours to sample and evaluate before moving to the best |
44
|
|
|
of those neighbours. |
45
|
|
|
n_iter_restart : int |
46
|
|
|
The number of iterations after which to restart at a random position. |
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
|
|
|
epsilon: float = 0.03, |
61
|
|
|
distribution: Literal[ |
62
|
|
|
"normal", "laplace", "gumbel", "logistic" |
63
|
|
|
] = "normal", |
64
|
|
|
n_neighbours: int = 3, |
65
|
|
|
n_iter_restart: int = 10, |
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
|
|
|
epsilon=epsilon, |
75
|
|
|
distribution=distribution, |
76
|
|
|
n_neighbours=n_neighbours, |
77
|
|
|
n_iter_restart=n_iter_restart, |
78
|
|
|
) |
79
|
|
|
|