Code Duplication    Length = 56-68 lines in 3 locations

src/gradient_free_optimizers/optimizer_search/genetic_algorithm.py 1 location

@@ 11-78 (lines=68) @@
8
from ..optimizers import GeneticAlgorithmOptimizer as _GeneticAlgorithmOptimizer
9
10
11
class GeneticAlgorithmOptimizer(_GeneticAlgorithmOptimizer, Search):
12
    """
13
    A class implementing the **genetic algorithm** for the public API.
14
    Inheriting from the `Search`-class to get the `search`-method and from
15
    the `GeneticAlgorithmOptimizer`-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 search process.
34
    population : int
35
        The number of individuals in the population.
36
    offspring : int
37
        The number of offspring to generate in each generation.
38
    crossover : str
39
        The crossover operator to use.
40
    n_parents : int
41
        The number of parents to select for crossover.
42
    mutation_rate : float
43
        The mutation rate.
44
    crossover_rate : float
45
        The crossover rate.
46
    """
47
48
    def __init__(
49
        self,
50
        search_space: Dict[str, list],
51
        initialize: Dict[
52
            Literal["grid", "vertices", "random", "warm_start"],
53
            Union[int, list[dict]],
54
        ] = {"grid": 4, "random": 2, "vertices": 4},
55
        constraints: List[callable] = [],
56
        random_state: int = None,
57
        rand_rest_p: float = 0,
58
        nth_process: int = None,
59
        population=10,
60
        offspring=10,
61
        crossover="discrete-recombination",
62
        n_parents=2,
63
        mutation_rate=0.5,
64
        crossover_rate=0.5,
65
    ):
66
        super().__init__(
67
            search_space=search_space,
68
            initialize=initialize,
69
            constraints=constraints,
70
            random_state=random_state,
71
            rand_rest_p=rand_rest_p,
72
            nth_process=nth_process,
73
            population=population,
74
            offspring=offspring,
75
            crossover=crossover,
76
            n_parents=n_parents,
77
            mutation_rate=mutation_rate,
78
            crossover_rate=crossover_rate,
79
        )
80

src/gradient_free_optimizers/optimizer_search/evolution_strategy.py 1 location

@@ 13-78 (lines=66) @@
10
)
11
12
13
class EvolutionStrategyOptimizer(_EvolutionStrategyOptimizer, Search):
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

src/gradient_free_optimizers/optimizer_search/differential_evolution.py 1 location

@@ 13-68 (lines=56) @@
10
)
11
12
13
class DifferentialEvolutionOptimizer(_DifferentialEvolutionOptimizer, Search):
14
    """
15
    A class implementing the **differential evolution** for the public API.
16
    Inheriting from the `Search`-class to get the `search`-method and from
17
    the `DifferentialEvolutionOptimizer`-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
    mutation_rate : float
39
        The mutation rate.
40
    crossover_rate : float
41
        The crossover rate.
42
    """
43
44
    def __init__(
45
        self,
46
        search_space: Dict[str, list],
47
        initialize: Dict[
48
            Literal["grid", "vertices", "random", "warm_start"],
49
            Union[int, list[dict]],
50
        ] = {"grid": 4, "random": 2, "vertices": 4},
51
        constraints: List[callable] = [],
52
        random_state: int = None,
53
        rand_rest_p: float = 0,
54
        nth_process: int = None,
55
        population=10,
56
        mutation_rate=0.9,
57
        crossover_rate=0.9,
58
    ):
59
        super().__init__(
60
            search_space=search_space,
61
            initialize=initialize,
62
            constraints=constraints,
63
            random_state=random_state,
64
            rand_rest_p=rand_rest_p,
65
            nth_process=nth_process,
66
            population=population,
67
            mutation_rate=mutation_rate,
68
            crossover_rate=crossover_rate,
69
        )
70