metaheuristic_designer.population module#
Base class for the Population module.
This module implements a data structure to hold the collection of solutions we are considering.
- class Population(genotype_matrix, encoding=None)[source]#
Bases:
objectContainer for a set of candidate solutions and their fitness.
A
Populationholds the genotype matrix, fitness and objective values, historical bests, and the current best individual. It is the central data structure passed between components of the optimization loop.- Parameters:
- objfuncObjectiveFunc
The objective function that will evaluate the population.
- genotype_matrixndarray
2-D array of shape
(N, M)containing the genotypes.- encodingEncoding, optional
The encoding used to translate between genotype and phenotype. Defaults to
DefaultEncoding.
- Parameters:
genotype_matrix (MatrixLike)
encoding (Optional[Encoding])
Methods
apply_encoding([encoding])Return the population passed through the decoding function defined in the encoding.
apply_selection(selected_pop, selection_idx)Replaces the chosen individuals from the input population to the current population.
apply_slice(sliced_pop, mask)Apply the values of the population to a subset of the components of the population vectors.
Return the best genotype and its maximized fitness value.
Return the best decoded solution and its raw objective value.
debug_repr([max_solutions, max_vars])Return a compact string representation for debugging.
decode([encoding])Return the population matrix passed through the decoding function defined in the encoding.
decode_params([encoding])Decode the auxiliary parameters stored in the genotype.
encode([encoding])Encode the current population matrix using the given encoding.
Return a dictionary with the current population state.
join(other_population)Adds to the current population the individuals of the input population.
join_populations(population1, population2)Concatenate two populations into a new one.
repeat([amount])Duplicates the individuals of the population.
Sorts the individuals by fitness.
step([_progress])Updates the best solution in the population.
take_selection(selection_idx)Takes a subset of the population given a mask.
take_slice(mask)Takes a subset of the components in the population vectors.
update_best_from_parents(parents)Update the best solution if a better one exists in parents.
update_genotype(genotype_source)Replace the genotype matrix.
- best_individual()[source]#
Return the best genotype and its maximized fitness value.
- Return type:
Tuple[ndarray[tuple[int,int],floating] |ndarray[tuple[int,int],integer] |ndarray[tuple[int,int],uint8|bool],float]- Returns:
- best_genotypeMatrixLike
The genotype vector of the best individual.
- best_fitnessfloat
The internal fitness (always maximized).
- best_solution()[source]#
Return the best decoded solution and its raw objective value.
- Return type:
Tuple[Any,float]- Returns:
- solutionAny
The decoded phenotype of the best individual.
- objectivefloat
The raw objective value.
- update_genotype(genotype_source)[source]#
Replace the genotype matrix.
- Return type:
- Parameters:
- genotype_sourcendarray or Population
New genotypes. If a
Populationis given, its genotype matrix is used.
- Returns:
- Population
self, with updated genotypes and, if the size changed, re-initialized fitness and historical bests.
- Parameters:
genotype_source (ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool] | Population)
- take_selection(selection_idx)[source]#
Takes a subset of the population given a mask.
- Return type:
- Parameters:
- selection_idx: ndarray
An array of indices or a mask that indicate which individuals to take from the population.
- Returns:
- selected_population: Population
A copy of the population containing only the chosen individuals.
- Parameters:
selection_idx (ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool])
- apply_selection(selected_pop, selection_idx)[source]#
Replaces the chosen individuals from the input population to the current population.
- Return type:
- Parameters:
- selected_pop: Population
Population where to take the individuals that will replace the ones in the population.
- selection_idx: ndarray
An array of indices or a mask that indicate which individuals to take from the population.
- Returns:
- self: Population
- Parameters:
selected_pop (Population)
selection_idx (ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool])
- take_slice(mask)[source]#
Takes a subset of the components in the population vectors.
- Return type:
- Parameters:
- mask: ndarray
An array of indices or a mask that indicate which components to take from each vector in the population.
- Returns:
- sliced_population: Population
A copy of the population containing the masked individuals.
- Parameters:
mask (ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool])
- apply_slice(sliced_pop, mask)[source]#
Apply the values of the population to a subset of the components of the population vectors.
- Return type:
- Parameters:
- sliced_pop: Population
Population where to take the individuals from which we will take the components that will replace the ones in the current population.
- mask: ndarray
An array of indices or a mask that indicate which components to take from each vector in the population.
- Returns:
- self: Population
- Parameters:
sliced_pop (Population)
mask (ndarray[tuple[int, ...], integer] | ndarray[tuple[int, ...], uint8 | bool])
- static join_populations(population1, population2)[source]#
Concatenate two populations into a new one.
- Return type:
- Parameters:
- population1Population
First population.
- population2Population
Second population.
- Returns:
- Population
A new population containing all individuals from both inputs.
- Parameters:
population1 (Population)
population2 (Population)
- join(other_population)[source]#
Adds to the current population the individuals of the input population.
- Return type:
- Parameters:
- other_population: Population
Population that will be concatenated with the current one.
- Returns:
- joined_populations: Population
A population containing both the individuals from the current population and the ones from the input population.
- Parameters:
other_population (Population)
- update_best_from_parents(parents)[source]#
Update the best solution if a better one exists in parents.
- Return type:
- Parameters:
- parentsPopulation
Population whose best individual may improve the current one.
- Returns:
- Population
self, with possibly updatedbest,best_fitness, andbest_objective.
- Parameters:
parents (Population)
- step(_progress=0)[source]#
Updates the best solution in the population.
- Return type:
- Returns:
- self: Population
- Parameters:
_progress (float)
- repeat(amount=2)[source]#
Duplicates the individuals of the population.
- Return type:
- Parameters:
- amount: int, optional
The amount of times to repeat the individuals in the population.
- Returns:
- repeated_population: Population
- Parameters:
amount (int)
- apply_encoding(encoding=None)[source]#
Return the population passed through the decoding function defined in the encoding.
- Return type:
- Returns:
- decoded_population: Any
- Parameters:
encoding (Encoding | None)
- decode(encoding=None)[source]#
Return the population matrix passed through the decoding function defined in the encoding.
- Return type:
Iterable- Returns:
- MatrixLike
The decoded genotype matrix.
- Parameters:
encoding (Encoding | None)
- decode_params(encoding=None)[source]#
Decode the auxiliary parameters stored in the genotype.
Only works with
ParameterExtendingEncoding.- Return type:
Iterable- Parameters:
- encodingEncoding, optional
Encoding to use; defaults to
self.encoding.
- Returns:
- dict or None
Dictionary of parameter arrays, or
Noneif the encoding does not support extended parameters.
- Parameters:
encoding (Encoding | None)
- encode(encoding=None)[source]#
Encode the current population matrix using the given encoding.
- Return type:
ndarray[tuple[int,int],floating] |ndarray[tuple[int,int],integer] |ndarray[tuple[int,int],uint8|bool]- Parameters:
- encodingEncoding, optional
Encoding to use; defaults to
self.encoding.
- Returns:
- MatrixLike
The encoded genotype matrix.
- Parameters:
encoding (Encoding | None)
- get_state()[source]#
Return a dictionary with the current population state.
- Return type:
dict- Returns:
- dict
Keys include
genotype_matrix,fitness,objective, historical bests, and the best individual.
- debug_repr(max_solutions=5, max_vars=5)[source]#
Return a compact string representation for debugging.
- Return type:
str- Parameters:
- max_solutionsint, optional
Maximum number of rows to include in the preview.
- max_varsint, optional
Maximum number of columns to include in the preview.
- Returns:
- str
- Parameters:
max_solutions (int)
max_vars (int)