EvolutionStrategyOptimizer.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 29
Code Lines 28

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 28
dl 29
loc 29
rs 9.208
c 0
b 0
f 0
cc 1
nop 12

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
    EvolutionStrategyOptimizer as _EvolutionStrategyOptimizer,
10
)
11
12
13 View Code Duplication
class EvolutionStrategyOptimizer(_EvolutionStrategyOptimizer, Search):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
    """
15
    A class implementing the **evolution strategy** for the public API.
16
    Inheriting from the `Search`-class to get the `search`-method and from
17
    the `EvolutionStrategyOptimizer`-backend to get the underlying algorithm.
18
19
    Parameters
20
    ----------
21
    search_space : dict[str, list]
22
        The search space to explore. A dictionary with parameter
23
        names as keys and a numpy array as values.
24
    initialize : dict[str, int]
25
        The method to generate initial positions. A dictionary with
26
        the following key literals and the corresponding value type:
27
        {"grid": int, "vertices": int, "random": int, "warm_start": list[dict]}
28
    constraints : list[callable]
29
        A list of constraints, where each constraint is a callable.
30
        The callable returns `True` or `False` dependend on the input parameters.
31
    random_state : None, int
32
        If None, create a new random state. If int, create a new random state
33
        seeded with the value.
34
    rand_rest_p : float
35
        The probability of a random iteration during the the search process.
36
    population : int
37
        The number of individuals in the population.
38
    offspring : int
39
        The number of offspring to generate in each generation.
40
    replace_parents : bool
41
        If True, the parents are replaced with the offspring in the next
42
        generation. If False, the parents are kept in the next generation and the
43
        offspring are added to the population.
44
    mutation_rate : float
45
        The mutation rate for the mutation operator.
46
    crossover_rate : float
47
        The crossover rate for the crossover operator.
48
    """
49
50
    def __init__(
51
        self,
52
        search_space: Dict[str, list],
53
        initialize: Dict[
54
            Literal["grid", "vertices", "random", "warm_start"],
55
            Union[int, list[dict]],
56
        ] = {"grid": 4, "random": 2, "vertices": 4},
57
        constraints: List[callable] = [],
58
        random_state: int = None,
59
        rand_rest_p: float = 0,
60
        nth_process: int = None,
61
        population=10,
62
        offspring=20,
63
        replace_parents=False,
64
        mutation_rate=0.7,
65
        crossover_rate=0.3,
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
            population=population,
75
            offspring=offspring,
76
            replace_parents=replace_parents,
77
            mutation_rate=mutation_rate,
78
            crossover_rate=crossover_rate,
79
        )
80