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 SpiralOptimization as _SpiralOptimization |
9
|
|
|
|
10
|
|
|
|
11
|
|
View Code Duplication |
class SpiralOptimization(_SpiralOptimization, Search): |
|
|
|
|
12
|
|
|
""" |
13
|
|
|
A class implementing the **spiral optimizer** for the public API. |
14
|
|
|
Inheriting from the `Search`-class to get the `search`-method and from |
15
|
|
|
the `SpiralOptimization`-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
|
|
|
population : int |
35
|
|
|
The number of particles in the swarm. |
36
|
|
|
decay_rate : float |
37
|
|
|
This parameter is a factor, that influences the radius of the particles during their spiral movement. |
38
|
|
|
Lower values accelerates the convergence of the particles to the best known position, while values above 1 eventually lead to a movement where the particles spiral away from each other. |
39
|
|
|
""" |
40
|
|
|
|
41
|
|
|
def __init__( |
42
|
|
|
self, |
43
|
|
|
search_space: Dict[str, list], |
44
|
|
|
initialize: Dict[ |
45
|
|
|
Literal["grid", "vertices", "random", "warm_start"], |
46
|
|
|
Union[int, list[dict]], |
47
|
|
|
] = {"grid": 4, "random": 2, "vertices": 4}, |
48
|
|
|
constraints: List[callable] = [], |
49
|
|
|
random_state: int = None, |
50
|
|
|
rand_rest_p: float = 0, |
51
|
|
|
nth_process: int = None, |
52
|
|
|
population: int = 10, |
53
|
|
|
decay_rate: float = 0.99, |
54
|
|
|
): |
55
|
|
|
super().__init__( |
56
|
|
|
search_space=search_space, |
57
|
|
|
initialize=initialize, |
58
|
|
|
constraints=constraints, |
59
|
|
|
random_state=random_state, |
60
|
|
|
rand_rest_p=rand_rest_p, |
61
|
|
|
nth_process=nth_process, |
62
|
|
|
population=population, |
63
|
|
|
decay_rate=decay_rate, |
64
|
|
|
) |
65
|
|
|
|