| @@ 11-77 (lines=67) @@ | ||
| 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 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"], int | List |
|
| 53 | ] = {"grid": 4, "random": 2, "vertices": 4}, |
|
| 54 | constraints: List[callable] = [], |
|
| 55 | random_state: int = None, |
|
| 56 | rand_rest_p: float = 0, |
|
| 57 | nth_process: int = None, |
|
| 58 | population=10, |
|
| 59 | offspring=10, |
|
| 60 | crossover="discrete-recombination", |
|
| 61 | n_parents=2, |
|
| 62 | mutation_rate=0.5, |
|
| 63 | crossover_rate=0.5, |
|
| 64 | ): |
|
| 65 | super().__init__( |
|
| 66 | search_space=search_space, |
|
| 67 | initialize=initialize, |
|
| 68 | constraints=constraints, |
|
| 69 | random_state=random_state, |
|
| 70 | rand_rest_p=rand_rest_p, |
|
| 71 | nth_process=nth_process, |
|
| 72 | population=population, |
|
| 73 | offspring=offspring, |
|
| 74 | crossover=crossover, |
|
| 75 | n_parents=n_parents, |
|
| 76 | mutation_rate=mutation_rate, |
|
| 77 | crossover_rate=crossover_rate, |
|
| 78 | ) |
|
| 79 | ||
| @@ 13-77 (lines=65) @@ | ||
| 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"], int | List |
|
| 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 | population=10, |
|
| 61 | offspring=20, |
|
| 62 | replace_parents=False, |
|
| 63 | mutation_rate=0.7, |
|
| 64 | crossover_rate=0.3, |
|
| 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 | replace_parents=replace_parents, |
|
| 76 | mutation_rate=mutation_rate, |
|
| 77 | crossover_rate=crossover_rate, |
|
| 78 | ) |
|
| 79 | ||
| @@ 13-67 (lines=55) @@ | ||
| 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"], int | List |
|
| 49 | ] = {"grid": 4, "random": 2, "vertices": 4}, |
|
| 50 | constraints: List[callable] = [], |
|
| 51 | random_state: int = None, |
|
| 52 | rand_rest_p: float = 0, |
|
| 53 | nth_process: int = None, |
|
| 54 | population=10, |
|
| 55 | mutation_rate=0.9, |
|
| 56 | crossover_rate=0.9, |
|
| 57 | ): |
|
| 58 | super().__init__( |
|
| 59 | search_space=search_space, |
|
| 60 | initialize=initialize, |
|
| 61 | constraints=constraints, |
|
| 62 | random_state=random_state, |
|
| 63 | rand_rest_p=rand_rest_p, |
|
| 64 | nth_process=nth_process, |
|
| 65 | population=population, |
|
| 66 | mutation_rate=mutation_rate, |
|
| 67 | crossover_rate=crossover_rate, |
|
| 68 | ) |
|
| 69 | ||