metaheuristic_designer.parent_selection package#
Submodules#
Module contents#
Parent selection registry and factory.
- class NullParentSelection(name='Nothing', **kwargs)[source]#
Bases:
ParentSelectionNull parent selection, returns the whole population unchanged.
This is the identity element: no individuals are filtered out. Useful when the algorithm does not require a parent selection step (e.g., random search or certain evolution strategies).
- Parameters:
- namestr, optional
Display name. Default
"Nothing".- **kwargs
Keyword arguments forwarded to
ParentSelection.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
- Parameters:
name (Optional[str])
Methods
__call__(population[, amount])Shorthand for
select().gather_params()Return the current parameter dictionary (thin wrapper around
get_params()).get_params()Return a copy of the current parameter dictionary.
get_state()Return a dictionary with the selection method's configuration.
select(population[, amount])Takes a population with its offspring and returns the individuals that survive to produce the next generation.
store_kwargs([progress])Store keyword arguments and evaluate them at the given progress.
update(progress)Re-evaluate all stored parameters at the current progress.
update_kwargs([progress])Add or replace parameters and immediately evaluate them.
- select(population, amount=None)[source]#
Takes a population with its offspring and returns the individuals that survive to produce the next generation.
- Return type:
- Parameters:
- population: Population
Population of individuals that will be selected.
- offspring: Population
Newly generated individuals to be selected.
- Returns:
- selected: Population
List of selected individuals.
- Parameters:
population (Population)
amount (int | None)
- class ParentSelection(name=None, amount=None, rng=None, **kwargs)[source]#
Bases:
ParametrizableMixin,ABCAbstract base for all parent selection methods.
A parent selection chooses which individuals from the current population will be used to generate offspring. Subclasses must implement
select(), which returns a newPopulationcontaining only the selected individuals.- Parameters:
- namestr, optional
Display name for this selection method.
- amountint, optional
Default number of individuals to select. Can be overridden at call time.
- rngRNGLike, optional
Random number generator.
- **kwargs
Additional keyword arguments stored as schedulable parameters.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
- Parameters:
name (Optional[str])
amount (Optional[int])
rng (Optional[RNGLike])
Methods
__call__(population[, amount])Shorthand for
select().Return the current parameter dictionary (thin wrapper around
get_params()).get_params()Return a copy of the current parameter dictionary.
Return a dictionary with the selection method's configuration.
select(population[, amount])Takes a population with its offspring and returns the individuals that survive to produce the next generation.
store_kwargs([progress])Store keyword arguments and evaluate them at the given progress.
update(progress)Re-evaluate all stored parameters at the current progress.
update_kwargs([progress])Add or replace parameters and immediately evaluate them.
- gather_params()[source]#
Return the current parameter dictionary (thin wrapper around
get_params()).- Return type:
dict
- abstract select(population, amount=None)[source]#
Takes a population with its offspring and returns the individuals that survive to produce the next generation.
- Return type:
- Parameters:
- population: Population
Population of individuals that will be selected.
- offspring: Population
Newly generated individuals to be selected.
- Returns:
- selected: Population
List of selected individuals.
- Parameters:
population (Population)
amount (int | None)
- class ParentSelectionDef(selection_fn, params=<factory>, forced_params=<factory>)[source]#
Bases:
objectWrapper that turns a raw parent-selection function into a callable.
- Parameters:
- selection_fncallable
Function
(fitness, amount, rng, **kwargs) -> indices.- paramsdict, optional
Default keyword arguments merged with user-supplied ones.
- forced_paramsdict, optional
Keyword arguments that always override user-supplied ones.
- Parameters:
selection_fn (callable)
params (dict)
forced_params (dict)
Methods
__call__(population[, amount, rng])Call self as a function.
-
selection_fn:
callable#
-
params:
dict#
-
forced_params:
dict#
- class ParentSelectionFromLambda(selection_fn, name=None, amount=None, rng=None, **kwargs)[source]#
Bases:
ParentSelectionParent selection that wraps a user-supplied function.
The function receives the population, the number of individuals to select, a random state, and any stored keyword arguments, and must return an array of selected indices.
- Parameters:
- selection_fncallable
A function
(population, amount, rng, **kwargs) -> indices.- namestr, optional
Display name (defaults to the function’s
__name__).- amountint, optional
Default number of individuals to select.
- rngRNGLike, optional
Random number generator.
- **kwargs
Keyword arguments forwarded to
ParentSelection.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
- Parameters:
selection_fn (Callable)
name (Optional[str])
amount (Optional[int])
rng (Optional[RNGLike])
Methods
__call__(population[, amount])Shorthand for
select().gather_params()Return the current parameter dictionary (thin wrapper around
get_params()).get_params()Return a copy of the current parameter dictionary.
get_state()Return a dictionary with the selection method's configuration.
select(population[, amount])Takes a population with its offspring and returns the individuals that survive to produce the next generation.
store_kwargs([progress])Store keyword arguments and evaluate them at the given progress.
update(progress)Re-evaluate all stored parameters at the current progress.
update_kwargs([progress])Add or replace parameters and immediately evaluate them.
- select(population, amount=None)[source]#
Takes a population with its offspring and returns the individuals that survive to produce the next generation.
- Return type:
- Parameters:
- population: Population
Population of individuals that will be selected.
- offspring: Population
Newly generated individuals to be selected.
- Returns:
- selected: Population
List of selected individuals.
- Parameters:
population (Population)
amount (int | None)
- list_parent_selection_methods()[source]#
Return a list of all registered parent selection method names.
- Return type:
list[str]- Returns:
- list of str
- add_parent_selection_entry(selection_fn, selection_method_name)[source]#
Register a new parent selection method.
- Parameters:
- selection_fncallable
A function with the parent selection signature.
- selection_method_namestr
Name under which to register the method. If it already exists, a warning is logged.
- Parameters:
selection_fn (callable)
selection_method_name (str)
- create_parent_selection(method, name=None, amount=None, rng=None, **kwargs)[source]#
Create a parent selection method by name.
- Return type:
- Parameters:
- methodstr
Key into
parent_sel_map, or a null alias.- namestr, optional
Display name for the selection method.
- amountint, optional
Default number of parents to select.
- rngRNGLike, optional
Random number generator.
- **kwargs
Additional parameters forwarded to the selection function.
- Returns:
- ParentSelectionFromLambda or NullParentSelection
The wrapped selection method.
- Parameters:
method (str)
name (str | None)
amount (int | None)
rng (int | Generator | None)
- create_scaling_fn(method, scaling_factor=2)[source]#
Create a callable that computes normalized selection weights.
- Return type:
Callable- Parameters:
- methodstr
Key into
scaling_map(e.g.,"fitness_proportional").- scaling_factorfloat, optional
Factor forwarded to the underlying scaling function.
- Returns:
- callable
A function
(fitness) -> weightsthat returns a normalized probability vector.
- Parameters:
method (str)
scaling_factor (number | float | int)
- select_best(fitness, amount, rng=None)[source]#
Selects the best parent of the population as parents.
- Return type:
ndarray[tuple[int,...],integer] |ndarray[tuple[int,...],uint8|bool]- Parameters:
- population: ndarray
List of individuals from which the parents will be selected.
- amount: int
Amount of individuals to be chosen as parents.
- Returns:
- parents: ndarray
List of individuals chosen as parents.
- Parameters:
fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])
amount (int)
rng (int | Generator | None)
- shuffle_population(fitness, amount, rng=None)[source]#
Chooses a number of individuals from the population at random without replacement if amount < population_size. If we cannot pick without replacement, we at least make sure we pick every individual at least \(\left\lceil \frac{\text{amount}}{\text{population\_size}} \right\rceil\) times
- Return type:
ndarray[tuple[int,...],integer] |ndarray[tuple[int,...],uint8|bool]- Parameters:
- population: ndarray
List of individuals from which the parents will be selected.
- amount: int
Amount of individuals to be chosen as parents.
- Returns:
- parents: ndarray
List of individuals chosen as parents.
- Parameters:
fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])
amount (int)
rng (int | Generator | None)
- prob_tournament(fitness, amount, rng=None, tournament_size=3, prob=1)[source]#
Selects the parents for the next generation by tournament.
- Return type:
ndarray[tuple[int,...],integer] |ndarray[tuple[int,...],uint8|bool]- Parameters:
- population: ndarray
List of individuals from which the parents will be selected.
- tournament_size: int
Amount of individuals that will be chosen for each tournament.
- prob: float
Probability that a parent with low fitness will win the tournament.
- Returns:
- parents: ndarray
List of individuals chosen as parents.
- Parameters:
fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])
amount (int)
rng (int | Generator | None)
tournament_size (int)
prob (float)
- uniform_selection(fitness, amount, rng=None)[source]#
Chooses a number of individuals from the population at random with replacement.
- Return type:
ndarray[tuple[int,...],integer] |ndarray[tuple[int,...],uint8|bool]- Parameters:
- population: ndarray
List of individuals from which the parents will be selected.
- amount: int
Amount of individuals to be chosen as parents.
- Returns:
- parents: ndarray
List of individuals chosen as parents.
- Parameters:
fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])
amount (int)
rng (int | Generator | None)
- roulette(fitness, amount, rng=None, method='flat_scaling', scaling_factor=None)[source]#
Fitness proportionate parent selection.
- Return type:
ndarray[tuple[int,...],integer] |ndarray[tuple[int,...],uint8|bool]- Parameters:
- population: ndarray
List of individuals from which the parents will be selected.
- amount: int
Amount of individuals to be chosen as parents.
- method: str, optional
Indicates how the roulette will be generated.
- f: float, optional
Parameter passed to some of the roulette generating methods.
- Returns:
- parents: ndarray
List of individuals chosen as parents.
- Parameters:
fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])
amount (int)
rng (int | Generator | None)
method (str)
scaling_factor (float | None)
- sus(fitness, amount, rng=None, method='flat_scaling', scaling_factor=None)[source]#
Stochastic universal sampling parent selection method.
- Return type:
ndarray[tuple[int,...],integer] |ndarray[tuple[int,...],uint8|bool]- Parameters:
- population: ndarray
List of individuals from which the parents will be selected.
- amount: int
Amount of individuals to be chosen as parents.
- method: str, optional
Indicates how the roulette will be generated.
- f: float, optional
Parameter passed to some of the roulette generating methods.
- Returns:
- parents: ndarray
List of individuals chosen as parents.
- Parameters:
fitness (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool])
amount (int)
rng (int | Generator | None)
method (str)
scaling_factor (float | None)