Passed
Push — master ( 7ebd63...8bd9a3 )
by Simon
06:11
created

gradient_free_optimizers.optimizer_search.particle_swarm_optimization   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 32
dl 0
loc 74
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ParticleSwarmOptimizer.__init__() 0 28 1
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 ParticleSwarmOptimizer as _ParticleSwarmOptimizer
9
10
11
class ParticleSwarmOptimizer(_ParticleSwarmOptimizer, Search):
12
    """
13
    A class implementing **particle swarm optimization** for the public API.
14
    Inheriting from the `Search`-class to get the `search`-method and from
15
    the `ParticleSwarmOptimizer`-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
    inertia : float
37
        The inertia of the swarm.
38
    cognitive_weight : float
39
        A factor of the movement towards the personal best position of the individual optimizers in the population.
40
    social_weight : float
41
        A factor of the movement towards the personal best position of the individual optimizers in the population.
42
    temp_weight : float
43
        The temperature weight of the swarm.
44
    """
45
46
    def __init__(
47
        self,
48
        search_space: Dict[str, list],
49
        initialize: Dict[
50
            Literal["grid", "vertices", "random", "warm_start"], int | List
51
        ] = {"grid": 4, "random": 2, "vertices": 4},
52
        constraints: List[callable] = [],
53
        random_state: int = None,
54
        rand_rest_p: float = 0,
55
        nth_process: int = None,
56
        population: int = 10,
57
        inertia: float = 0.5,
58
        cognitive_weight: float = 0.5,
59
        social_weight: float = 0.5,
60
        temp_weight: float = 0.2,
61
    ):
62
        super().__init__(
63
            search_space=search_space,
64
            initialize=initialize,
65
            constraints=constraints,
66
            random_state=random_state,
67
            rand_rest_p=rand_rest_p,
68
            nth_process=nth_process,
69
            population=population,
70
            inertia=inertia,
71
            cognitive_weight=cognitive_weight,
72
            social_weight=social_weight,
73
            temp_weight=temp_weight,
74
        )
75