From dc06d536511ac7eeb9abae0eb2d2e6feed6f3b3e Mon Sep 17 00:00:00 2001 From: Marie Weiel Date: Wed, 13 Mar 2024 12:03:33 +0100 Subject: [PATCH] harmonize type hints for Individual and Particle in docstrings --- propulate/islands.py | 6 +++--- propulate/propagators/base.py | 32 ++++++++++++++++---------------- propulate/propagators/cmaes.py | 16 ++++++++-------- propulate/propagators/ga.py | 24 ++++++++++++------------ propulate/propagators/pso.py | 30 +++++++++++++++--------------- propulate/propulator.py | 14 +++++++------- propulate/utils.py | 4 ++-- 7 files changed, 63 insertions(+), 63 deletions(-) diff --git a/propulate/islands.py b/propulate/islands.py index 7b5ac087..a4cc042c 100644 --- a/propulate/islands.py +++ b/propulate/islands.py @@ -306,11 +306,11 @@ def _run( logging_interval : int The logging interval. debug : int - The verbosity/debug level. + The debug level. Returns ------- - List[List[Individual] | Individual] + List[List[propulate.Individual] | propulate.Individual] The top-n best individuals on each island. """ self.propulator.propulate(logging_interval, debug) @@ -333,7 +333,7 @@ def evolve( Returns ------- - List[List[Individual] | Individual] + List[List[propulate.Individual] | propulate.Individual] The top-n best individuals on each island. """ return self._run(top_n, logging_interval, debug) diff --git a/propulate/propagators/base.py b/propulate/propagators/base.py index c8df4909..a5402731 100644 --- a/propulate/propagators/base.py +++ b/propulate/propagators/base.py @@ -78,12 +78,12 @@ def __call__(self, inds: List[Individual]) -> Union[List[Individual], Individual Parameters ---------- - inds : List[propulate.individual.Individual] + inds : List[propulate.Individual] The input individuals the propagator is applied to. Returns ------- - List[Individual] | Individual + List[propulate.Individual] | propulate.Individual The individual(s) bred by applying the propagator. While this abstract base class method actually returns ``None``, each concrete child class of ``Propagator`` should return an ``Individual`` instance or a list of them. @@ -182,7 +182,7 @@ def __init__( offspring: int = -1, ) -> None: """ - Initialize the conditional propagator. + Initialize a conditional propagator. Parameters ---------- @@ -208,12 +208,12 @@ def __call__(self, inds: List[Individual]) -> List[Individual]: Parameters ---------- - inds : List[propulate.individual.Individual] + inds : List[propulate.Individual] The input individuals the propagator is applied to. Returns ------- - List[propulate.individual.Individual] + List[propulate.Individual] The output individuals returned by the conditional propagator. """ if ( @@ -283,12 +283,12 @@ def __call__(self, inds: List[Individual]) -> List[Individual]: Parameters ---------- - inds : List[propulate.individual.Individual] + inds : List[propulate.Individual] The input individuals the propagator is applied to. Returns ------- - List[propulate.individual.Individual] + List[propulate.Individual] The output individuals after application of the propagator. """ for p in self.propagators: @@ -329,12 +329,12 @@ def __call__(self, inds: List[Individual]) -> List[Individual]: Parameters ---------- - inds : List[propulate.individual.Individual] + inds : List[propulate.Individual] The input individuals the propagator is applied to. Returns ------- - List[propulate.individual.Individual] + List[propulate.Individual] The selected output individuals after application of the propagator. Raises @@ -385,12 +385,12 @@ def __call__(self, inds: List[Individual]) -> List[Individual]: Parameters ---------- - inds : List[propulate.individual.Individual] + inds : List[propulate.Individual] The individuals the propagator is applied to. Returns ------- - List[propulate.individual.Individual] + List[propulate.Individual] The selected individuals after application of the propagator. Raises @@ -423,7 +423,7 @@ class SelectUniform(Propagator): def __init__(self, offspring: int, rng: Optional[random.Random] = None) -> None: """ - Initialize random-selection propagator. + Initialize a random-selection propagator. Parameters ---------- @@ -440,12 +440,12 @@ def __call__(self, inds: List[Individual]) -> List[Individual]: Parameters ---------- - inds : List[propulate.individual.Individual] + inds : List[propulate.Individual] The individuals the propagator is applied to. Returns ------- - List[propulate.individual.Individual] + List[propulate.Individual] The selected individuals after application of the propagator. Raises @@ -515,12 +515,12 @@ def __call__(self, *inds: Individual) -> Individual: Parameters ---------- - inds : propulate.individual.Individual + inds : propulate.Individual The individuals the propagator is applied to. Returns ------- - propulate.individual.Individual + propulate.Individual The output individual after application of the propagator. Raises diff --git a/propulate/propagators/cmaes.py b/propulate/propagators/cmaes.py index b67e2ca4..c3107202 100644 --- a/propulate/propagators/cmaes.py +++ b/propulate/propagators/cmaes.py @@ -200,16 +200,16 @@ def update_mean(self, new_mean: np.ndarray) -> None: self.old_mean = self.mean self.mean = new_mean - def update_covariance_matrix(self, new_co_matrix: np.ndarray) -> None: + def update_covariance_matrix(self, new_covariance_matrix: np.ndarray) -> None: """ - Setter for the covariance matrix. + Update the covariance matrix. Computes new values for ``b_matrix``, ``d_matrix``, and ``covariance_inv_sqrt``. Decomposition of ``covariance_matrix`` is O(n^3), hence the possibility of lazy updating ``b_matrix`` and ``d_matrix``. Parameters ---------- - new_co_matrix : numpy.ndarray + new_covariance_matrix : numpy.ndarray The new covariance matrix. """ # Update b and d matrix and covariance_inv_sqrt only after certain number of evaluations to ensure 0(n^2). @@ -219,7 +219,7 @@ def update_covariance_matrix(self, new_co_matrix: np.ndarray) -> None: > self.lambd / (self.c_1 + self.c_mu) / self.problem_dimension / 10 ): self.eigen_eval = self.count_eval - self._decompose_co_matrix(new_co_matrix) + self._decompose_co_matrix(new_covariance_matrix) self.covariance_inv_sqrt = ( self.b_matrix @ np.diag(self.d_matrix ** (-1)) @ self.b_matrix.T ) @@ -802,12 +802,12 @@ def __call__(self, inds: List[Individual]) -> Individual: Parameters ---------- - inds: List[Individual] + inds: List[propulate.Individual] Available individuals. Returns ------- - new_ind : Individual + new_ind : propulate.Individual The newly sampled individual. """ num_inds = len(inds) @@ -845,7 +845,7 @@ def _transform_individuals_to_matrix(self, inds: List[Individual]) -> np.ndarray Parameters ---------- - inds : list[Individual] + inds : list[propulate.Individual] The list of individuals. Returns @@ -865,7 +865,7 @@ def _sample_cma(self) -> Individual: Returns ------- - new_ind : Individual + new_ind : propulate.Individual The newly sampled individual. """ # Generate new offspring diff --git a/propulate/propagators/ga.py b/propulate/propagators/ga.py index 420fb8c0..4f136142 100644 --- a/propulate/propagators/ga.py +++ b/propulate/propagators/ga.py @@ -72,12 +72,12 @@ def __call__(self, ind: Individual) -> Individual: Parameters ---------- - ind : propulate.individual.Individual + ind : propulate.Individual The individual the propagator is applied to. Returns ------- - propulate.individual.Individual + propulate.Individual The possibly point-mutated individual after application of the propagator. """ if ( @@ -184,12 +184,12 @@ def __call__(self, ind: Individual) -> Individual: Parameters ---------- - ind : propulate.population.Individual + ind : propulate.Individual The individual the propagator is applied to. Returns ------- - propulate.population.Individual + propulate.Individual The possibly point-mutated individual after application of the propagator. """ if ( @@ -288,12 +288,12 @@ def __call__(self, ind: Individual) -> Individual: Parameters ---------- - ind : propulate.individual.Individual + ind : propulate.Individual The input individual the propagator is applied to. Returns ------- - propulate.individual.Individual + propulate.Individual The possibly interval-mutated output individual after application of the propagator. """ if ( @@ -377,12 +377,12 @@ def __call__(self, inds: List[Individual]) -> Individual: Parameters ---------- - inds : List[propulate.individual.Individual] + inds : List[propulate.Individual] The individuals the propagator is applied to. Returns ------- - propulate.individual.Individual + propulate.Individual The possibly cross-bred individual after application of the propagator. """ ind = copy.deepcopy(inds[0]) # Consider 1st parent. @@ -438,12 +438,12 @@ def __call__(self, inds: List[Individual]) -> Individual: Parameters ---------- - inds : list[propulate.individual.Individual] + inds : list[propulate.Individual] The individuals the propagator is applied to. Returns ------- - propulate.individual.Individual + propulate.Individual The possibly cross-bred individual after application of propagator """ ind = copy.deepcopy(inds[0]) # Consider 1st parent. @@ -507,12 +507,12 @@ def __call__(self, inds: List[Individual]) -> Individual: Parameters ---------- - inds : List[propulate.individual.Individual] + inds : List[propulate.Individual] The individuals the propagator is applied to. Returns ------- - propulate.individual.Individual + propulate.Individual The possibly cross-bred individual after application of the propagator. """ ind = copy.deepcopy(inds[0]) # Consider 1st parent. diff --git a/propulate/propagators/pso.py b/propulate/propagators/pso.py index e08199d5..8fe1999f 100644 --- a/propulate/propagators/pso.py +++ b/propulate/propagators/pso.py @@ -100,7 +100,7 @@ def __call__(self, individuals: List[Individual]) -> Particle: Parameters ---------- - individuals : List[Individual] + individuals : List[propulate.Individual] A list of individuals that must at least contain one individual that belongs to the propagator. This list is used to calculate personal and global best of the particle and the swarm, respectively, and then to update the particle based on the retrieved results. Individuals that @@ -108,7 +108,7 @@ def __call__(self, individuals: List[Individual]) -> Particle: Returns ------- - propulate.population.Particle + propulate.Particle The updated particle. """ old_p, p_best, g_best = self._prepare_data(individuals) @@ -131,12 +131,12 @@ def _prepare_data( Parameters ---------- - individuals : List[Individual] + individuals : List[propulate.Individual] ``Individual`` objects that shall be used as data basis for a PSO update step. Returns ------- - Tuple[propulate.population.Particle, propulate.population.Particle, propulate.population.Particle] + Tuple[propulate.Particle, propulate.Particle, propulate.Particle] The following particles in this very order: 1. old_p: the current particle to be updated now 2. p_best: the personal best value of this particle @@ -195,7 +195,7 @@ def _make_new_particle( Returns ------- - propulate.population.Particle + propulate.Particle The new ``Particle`` object resulting from the PSO update step. """ new_p = Particle(position, velocity, generation, self.rank) @@ -274,7 +274,7 @@ def __call__(self, individuals: List[Individual]) -> Particle: Parameters ---------- - individuals : List[Individual] + individuals : List[propulate.Individual] The list of individuals that must at least contain one individual that belongs to the propagator. This list is used to calculate personal and global best of the particle and the swarm, respectively, and then to update the particle based on the retrieved results. Individuals that @@ -282,7 +282,7 @@ def __call__(self, individuals: List[Individual]) -> Particle: Returns ------- - propulate.population.Particle + propulate.Particle The updated particle. """ old_p, p_best, g_best = self._prepare_data(individuals) @@ -370,7 +370,7 @@ def __call__(self, individuals: List[Individual]) -> Particle: Parameters ---------- - individuals: List[Individual] + individuals: List[propulate.Individual] A list of individuals that must at least contain one individual that belongs to the propagator. This list is used to calculate personal and global best of the particle and the swarm, respectively, and then to update the particle based on the retrieved results. Individuals that @@ -378,7 +378,7 @@ def __call__(self, individuals: List[Individual]) -> Particle: Returns ------- - propulate.population.Particle + propulate.Particle The updated particle. """ old_p, p_best, g_best = self._prepare_data(individuals) @@ -460,7 +460,7 @@ def __call__(self, individuals: List[Individual]) -> Particle: Parameters ---------- - individuals : List[Individual] + individuals : List[propulate.Individual] The list of individuals that must at least contain one individual that belongs to the propagator. This list is used to calculate personal and global best of the particle and the swarm, respectively, and then to update the particle based on the retrieved results. Individuals that @@ -468,7 +468,7 @@ def __call__(self, individuals: List[Individual]) -> Particle: Returns ------- - propulate.population.Particle + propulate.Particle The updated particle. """ # Abuse Constriction's update rule, so I don't have to rewrite it. @@ -549,12 +549,12 @@ def __call__(self, individuals: List[Individual]) -> Particle: Parameters ---------- - individuals : list[Individual] + individuals : List[propulate.Individual] The individuals the propagator is applied to. Returns ------- - propulate.population.Particle + propulate.Particle A single particle object. """ if ( @@ -664,12 +664,12 @@ def __call__(self, individuals: List[Individual]) -> Individual: Parameters ---------- - individuals : List[Individual] + individuals : List[propulate.Individual] The individuals used as data basis for the PSO update. Returns ------- - propulate.population.Individual + propulate.Individual The updated individual. Raises diff --git a/propulate/propulator.py b/propulate/propulator.py index b437a8e6..4cfad97f 100644 --- a/propulate/propulator.py +++ b/propulate/propulator.py @@ -214,7 +214,7 @@ def _get_active_individuals(self) -> Tuple[List[Individual], int]: Returns ------- - List[propulate.individual.Individual] + List[propulate.Individual] All active individuals in the current population. int The number of currently active individuals. @@ -228,7 +228,7 @@ def _breed(self) -> Individual: Returns ------- - propulate.individual.Individual + propulate.Individual The newly bred individual. """ if ( @@ -347,7 +347,7 @@ def _get_unique_individuals(self) -> List[Individual]: Returns ------- - List[propulate.individual.Individual] + List[propulate.Individual] All unique individuals in the current population. """ unique_inds = [] @@ -371,7 +371,7 @@ def _check_intra_island_synchronization( Parameters ---------- - populations : List[List[propulate.individual.Individual]] + populations : List[List[propulate.Individual]] A list of all islands' sorted population lists. Returns @@ -535,9 +535,9 @@ def _check_for_duplicates( Returns ------- - List[List[propulate.individual.Individual | int]] + List[List[propulate.Individual | int]] The individuals and their occurrences. - List[propulate.individual.Individual] + List[propulate.Individual] The unique individuals in the population. """ if active: @@ -577,7 +577,7 @@ def summarize( Returns ------- - List[List[Individual] | Individual] + List[List[propulate.Individual] | propulate.Individual] The top-n best individuals on each island. """ if self.propulate_comm is None: diff --git a/propulate/utils.py b/propulate/utils.py index dd12dc21..6efaa117 100644 --- a/propulate/utils.py +++ b/propulate/utils.py @@ -160,12 +160,12 @@ def make_particle(individual: Individual) -> Particle: Parameters ---------- - individual : Individual + individual : propulate.Individual An individual to be converted to a particle. Returns -------- - Particle + propulate.Particle The converted individual. """ p = Particle(generation=individual.generation)