metaheuristic_designer.initializers package¶
Submodules¶
metaheuristic_designer.initializers.direct_initializer module¶
Initializer that uses a set of predefined solutions as the first generation.
- class DirectInitializer(default_init: Initializer, solutions: Population | List | ndarray, encoding: Encoding | None = None, random_state: int | Generator | None = None)[source]¶
Bases:
InitializerInitializer that seeds the population with a given set of solutions.
If the number of individuals requested exceeds the size of the stored set, individuals are cycled through. Random individuals from a fallback initializer are used when
generate_random()is called directly.- Parameters:
default_init (Initializer) – Fallback initializer for
generate_random().solutions (Population, list or ndarray) – The set of solutions to draw from.
encoding (Encoding, optional) – Encoding attached to the population (used when solutions is a
Population).random_state (RNGLike, optional) – Random number generator.
- generate_random() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate a single random genotype vector (1-D array).
- Returns:
A newly generated genotype vector (1-D array).
- Return type:
VectorLike
- generate_individual() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Return a randomly chosen individual from the stored solution set.
- Returns:
A 1-D array taken from the predefined solutions.
- Return type:
VectorLike
- generate_population(objfunc: ObjectiveFunc, n_individuals: int | None = None) Population[source]¶
Create a population by drawing from the stored solutions.
- Parameters:
objfunc (ObjectiveFunc) – The objective function.
n_individuals (int, optional) – Number of individuals to generate. Defaults to
population_size.
- Returns:
A population built from the predefined solutions.
- Return type:
metaheuristic_designer.initializers.exponential_initializer module¶
Initializer that samples from an exponential distribution.
- class ExponentialInitializer(dimension, beta, pop_size=1, encoding=None, dtype=<class 'float'>, random_state=None)[source]¶
Bases:
InitializerInitializer that generates individuals with values drawn from an exponential distribution.
- Parameters:
dimension (int) – Length of the genotype vector.
beta (float or array) – Scale parameter of the exponential distribution (1 / rate).
pop_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding that will be passed to each individual.
dtype (type, optional) – Desired NumPy dtype of the generated vectors (default
float).random_state (RNGLike, optional) – Random number generator.
- generate_random()[source]¶
Generate a single random genotype vector (1-D array).
- Returns:
A newly generated genotype vector (1-D array).
- Return type:
VectorLike
- generate_individual()[source]¶
Generate a single individual.
By default simply delegates to
generate_random(). Returns a newly generated individual (a 1-D array).Override this method if your initializer needs to distinguish between a randomly initialize individual and a solution generated with another strategy (See SeedProbInitializer).
- Returns:
A newly generated individual.
- Return type:
Any
metaheuristic_designer.initializers.extended_initializer module¶
Initializer for genotypes that contain both a solution and extra parameters.
- class ExtendedInitializer(solution_init: Initializer, param_init_dict: dict, encoding: ParameterExtendingEncoding, random_state=None)[source]¶
Bases:
InitializerInitializer that combines a solution initializer with one or more parameter initializers.
This is used with
ParameterExtendingEncodingto produce genotypes that store extra information (e.g., velocity for PSO, mutation strengths for self-adaptation).- Parameters:
solution_init (Initializer) – Initializer for the solution part of the genotype.
param_init_dict (dict) – Mapping of parameter names to their corresponding initializers.
encoding (ParameterExtendingEncoding) – The extended encoding that defines the parameter layout.
random_state (RNGLike, optional) – Random number generator.
- generate_random()[source]¶
Generate a random genotype vector with solution and parameter parts.
- Returns:
A 1-D array with the solution followed by the extra parameters.
- Return type:
ndarray
- generate_individual()[source]¶
Generate an individual (by default identical to
generate_random()).- Returns:
A 1-D array with the solution and parameter parts.
- Return type:
ndarray
metaheuristic_designer.initializers.gaussian_initializer module¶
Initializer that samples from a Gaussian (normal) distribution.
- class GaussianInitializer(dimension, g_mean, g_std, pop_size=1, encoding=None, dtype=<class 'float'>, random_state=None)[source]¶
Bases:
InitializerInitializer that generates individuals with values drawn from a Gaussian (normal) distribution.
- Parameters:
dimension (int) – Length of the genotype vector.
g_mean (float or array) – Mean of the distribution. If an array is given, it must have length dimension.
g_std (float or array) – Standard deviation of the distribution. If an array is given, it must have length dimension.
pop_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding that will be passed to each individual.
dtype (type, optional) – Desired NumPy dtype of the generated vectors (default
float).random_state (RNGLike, optional) – Random number generator.
- generate_random()[source]¶
Generate a single random genotype vector (1-D array).
- Returns:
A newly generated genotype vector (1-D array).
- Return type:
VectorLike
- generate_individual()[source]¶
Generate a single individual.
By default simply delegates to
generate_random(). Returns a newly generated individual (a 1-D array).Override this method if your initializer needs to distinguish between a randomly initialize individual and a solution generated with another strategy (See SeedProbInitializer).
- Returns:
A newly generated individual.
- Return type:
Any
metaheuristic_designer.initializers.perm_initializer module¶
Initializer that generates random permutations.
- class PermInitializer(dimension: int, population_size: int = 1, encoding: Encoding | None = None, random_state: int | Generator | None = None)[source]¶
Bases:
InitializerInitializer that generates individuals as random permutations of integers
0, 1, …, dimension-1.- Parameters:
dimension (int) – Length of the permutation (number of elements).
pop_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding that will be passed to each individual.
random_state (RNGLike, optional) – Random number generator.
metaheuristic_designer.initializers.seed_initializer module¶
Initializers that insert predefined solutions into the population.
- class SeedProbInitializer(default_init: Initializer, solutions: Population | Iterable[ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]] | ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], insert_prob: float = 0.1, random_state=None)[source]¶
Bases:
InitializerInitializer that inserts a predefined solution with a given probability.
With probability insert_prob, a randomly chosen solution from the provided set is used; otherwise a random individual is generated by the fallback initializer.
- Parameters:
default_init (Initializer) – Fallback initializer for random individuals.
solutions (Population, Iterable[VectorLike] or MatrixLike) – Set of predefined solutions to draw from.
insert_prob (float, optional) – Probability of using a predefined solution (default 0.1).
random_state (RNGLike, optional) – Random number generator.
- generate_random() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate a random individual using the fallback initializer.
- Returns:
A 1-D array generated by the fallback initializer.
- Return type:
VectorLike
- generate_individual() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate an individual, possibly replacing it with a seeded solution.
With probability insert_prob a randomly chosen predefined solution is returned; otherwise a new random individual is created by the fallback initializer.
- Returns:
A 1-D array representing the individual.
- Return type:
ndarray
- class SeedDetermInitializer(default_init: Initializer, solutions: Population | Iterable[ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]] | ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], n_to_insert: int | None = None, random_state=None)[source]¶
Bases:
InitializerInitializer that inserts a fixed number of predefined solutions.
The first n_to_insert individuals generated are taken from the solution set (cycled if necessary); the remaining are created by the fallback initializer.
- Parameters:
default_init (Initializer) – Fallback initializer for random individuals.
solutions (Population, Iterable[VectorLike] or MatrixLike) – Set of predefined solutions to draw from.
n_to_insert (int, optional) – Exact number of predefined solutions to insert. Defaults to the size of the solution set.
random_state (RNGLike, optional) – Random number generator.
- generate_random() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate a random individual using the fallback initializer.
- Returns:
A 1-D array generated by the fallback initializer.
- Return type:
VectorLike
- generate_individual() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Return a predefined solution until the insertion quota is met.
The first n_to_insert individuals are taken from the solution set (cycling if necessary). After that, random individuals are generated by the fallback initializer.
- Returns:
A 1-D array representing the individual.
- Return type:
VectorLike
- generate_population(objfunc, n_individuals: int | None = None) Population[source]¶
Create the population, resetting the insertion counter first.
- Parameters:
objfunc (ObjectiveFunc) – The objective function.
n_individuals (int, optional) – Number of individuals to generate. Defaults to
population_size.
- Returns:
A population with the predefined solutions inserted at the beginning.
- Return type:
metaheuristic_designer.initializers.uniform_initializer module¶
Initializer that samples from a uniform distribution.
- class UniformInitializer(dimension, lower_bound, upper_bound, population_size=1, encoding=None, dtype=<class 'float'>, random_state=None)[source]¶
Bases:
InitializerInitializer that generates individuals with values drawn from a uniform distribution.
- Parameters:
dimension (int) – Length of the genotype vector.
lower_bound (float or array) – Lower bound(s) of the distribution. If an array is given, it must have length dimension.
upper_bound (float or array) – Upper bound(s) of the distribution. Must match the shape of lower_bound.
population_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding that will be passed to each individual.
dtype (type, optional) – Desired NumPy dtype of the generated vectors (default
float).random_state (RNGLike, optional) – Random number generator.
- generate_random()[source]¶
Generate a single random genotype vector (1-D array).
- Returns:
A newly generated genotype vector (1-D array).
- Return type:
VectorLike
- generate_individual()[source]¶
Generate a single individual.
By default simply delegates to
generate_random(). Returns a newly generated individual (a 1-D array).Override this method if your initializer needs to distinguish between a randomly initialize individual and a solution generated with another strategy (See SeedProbInitializer).
- Returns:
A newly generated individual.
- Return type:
Any
Module contents¶
Concrete encoding implementations provided by the library.
- class DirectInitializer(default_init: Initializer, solutions: Population | List | ndarray, encoding: Encoding | None = None, random_state: int | Generator | None = None)[source]¶
Bases:
InitializerInitializer that seeds the population with a given set of solutions.
If the number of individuals requested exceeds the size of the stored set, individuals are cycled through. Random individuals from a fallback initializer are used when
generate_random()is called directly.- Parameters:
default_init (Initializer) – Fallback initializer for
generate_random().solutions (Population, list or ndarray) – The set of solutions to draw from.
encoding (Encoding, optional) – Encoding attached to the population (used when solutions is a
Population).random_state (RNGLike, optional) – Random number generator.
- generate_random() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate a single random genotype vector (1-D array).
- Returns:
A newly generated genotype vector (1-D array).
- Return type:
VectorLike
- generate_individual() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Return a randomly chosen individual from the stored solution set.
- Returns:
A 1-D array taken from the predefined solutions.
- Return type:
VectorLike
- generate_population(objfunc: ObjectiveFunc, n_individuals: int | None = None) Population[source]¶
Create a population by drawing from the stored solutions.
- Parameters:
objfunc (ObjectiveFunc) – The objective function.
n_individuals (int, optional) – Number of individuals to generate. Defaults to
population_size.
- Returns:
A population built from the predefined solutions.
- Return type:
- class ExponentialInitializer(dimension, beta, pop_size=1, encoding=None, dtype=<class 'float'>, random_state=None)[source]¶
Bases:
InitializerInitializer that generates individuals with values drawn from an exponential distribution.
- Parameters:
dimension (int) – Length of the genotype vector.
beta (float or array) – Scale parameter of the exponential distribution (1 / rate).
pop_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding that will be passed to each individual.
dtype (type, optional) – Desired NumPy dtype of the generated vectors (default
float).random_state (RNGLike, optional) – Random number generator.
- generate_random()[source]¶
Generate a single random genotype vector (1-D array).
- Returns:
A newly generated genotype vector (1-D array).
- Return type:
VectorLike
- generate_individual()[source]¶
Generate a single individual.
By default simply delegates to
generate_random(). Returns a newly generated individual (a 1-D array).Override this method if your initializer needs to distinguish between a randomly initialize individual and a solution generated with another strategy (See SeedProbInitializer).
- Returns:
A newly generated individual.
- Return type:
Any
- class ExtendedInitializer(solution_init: Initializer, param_init_dict: dict, encoding: ParameterExtendingEncoding, random_state=None)[source]¶
Bases:
InitializerInitializer that combines a solution initializer with one or more parameter initializers.
This is used with
ParameterExtendingEncodingto produce genotypes that store extra information (e.g., velocity for PSO, mutation strengths for self-adaptation).- Parameters:
solution_init (Initializer) – Initializer for the solution part of the genotype.
param_init_dict (dict) – Mapping of parameter names to their corresponding initializers.
encoding (ParameterExtendingEncoding) – The extended encoding that defines the parameter layout.
random_state (RNGLike, optional) – Random number generator.
- generate_random()[source]¶
Generate a random genotype vector with solution and parameter parts.
- Returns:
A 1-D array with the solution followed by the extra parameters.
- Return type:
ndarray
- generate_individual()[source]¶
Generate an individual (by default identical to
generate_random()).- Returns:
A 1-D array with the solution and parameter parts.
- Return type:
ndarray
- class GaussianInitializer(dimension, g_mean, g_std, pop_size=1, encoding=None, dtype=<class 'float'>, random_state=None)[source]¶
Bases:
InitializerInitializer that generates individuals with values drawn from a Gaussian (normal) distribution.
- Parameters:
dimension (int) – Length of the genotype vector.
g_mean (float or array) – Mean of the distribution. If an array is given, it must have length dimension.
g_std (float or array) – Standard deviation of the distribution. If an array is given, it must have length dimension.
pop_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding that will be passed to each individual.
dtype (type, optional) – Desired NumPy dtype of the generated vectors (default
float).random_state (RNGLike, optional) – Random number generator.
- generate_random()[source]¶
Generate a single random genotype vector (1-D array).
- Returns:
A newly generated genotype vector (1-D array).
- Return type:
VectorLike
- generate_individual()[source]¶
Generate a single individual.
By default simply delegates to
generate_random(). Returns a newly generated individual (a 1-D array).Override this method if your initializer needs to distinguish between a randomly initialize individual and a solution generated with another strategy (See SeedProbInitializer).
- Returns:
A newly generated individual.
- Return type:
Any
- class Initializer(dimension: int, population_size: int = 1, encoding: Encoding | None = None, random_state: int | Generator | None = None)[source]¶
Bases:
ABCAbstract base for all population initializers.
An initializer creates the first generation of individuals. It must provide a way to generate a single random genotype vector (a 1-D NumPy array) via
generate_random()and can optionally wrap it with a different definition of an individual viagenerate_individual().- Parameters:
dimension (int) – Length of the genotype vector.
population_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding that will be attached to every individual. Defaults to
DefaultEncoding.random_state (RNGLike, optional) – Random number generator.
- abstract generate_random() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate a single random genotype vector (1-D array).
- Returns:
A newly generated genotype vector (1-D array).
- Return type:
VectorLike
- generate_individual() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate a single individual.
By default simply delegates to
generate_random(). Returns a newly generated individual (a 1-D array).Override this method if your initializer needs to distinguish between a randomly initialize individual and a solution generated with another strategy (See SeedProbInitializer).
- Returns:
A newly generated individual.
- Return type:
Any
- generate_population(objfunc: ObjectiveFunc, n_individuals: int | None = None) Population[source]¶
Create a fully formed population of n_individuals individuals.
- Parameters:
objfunc (ObjectiveFunc) – Objective function that will be propagated to each individual.
n_individual (int, optional) – Number of individuals to generate
- Returns:
generated_population – Newly generated population.
- Return type:
- class InitializerFromLambda(generator: Callable, dimension: int, pop_size: int = 1, encoding: Encoding | None = None, random_state: int | Generator | None = None)[source]¶
Bases:
InitializerInitializer that uses a user-provided function to generate individuals.
- Parameters:
generator (callable) – A function
(random_state) -> genotypethat returns a single genotype vector.dimension (int) – Length of the genotype vector.
pop_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding attached to every individual.
random_state (RNGLike, optional) – Random number generator.
- class PermInitializer(dimension: int, population_size: int = 1, encoding: Encoding | None = None, random_state: int | Generator | None = None)[source]¶
Bases:
InitializerInitializer that generates individuals as random permutations of integers
0, 1, …, dimension-1.- Parameters:
dimension (int) – Length of the permutation (number of elements).
pop_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding that will be passed to each individual.
random_state (RNGLike, optional) – Random number generator.
- class SeedDetermInitializer(default_init: Initializer, solutions: Population | Iterable[ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]] | ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], n_to_insert: int | None = None, random_state=None)[source]¶
Bases:
InitializerInitializer that inserts a fixed number of predefined solutions.
The first n_to_insert individuals generated are taken from the solution set (cycled if necessary); the remaining are created by the fallback initializer.
- Parameters:
default_init (Initializer) – Fallback initializer for random individuals.
solutions (Population, Iterable[VectorLike] or MatrixLike) – Set of predefined solutions to draw from.
n_to_insert (int, optional) – Exact number of predefined solutions to insert. Defaults to the size of the solution set.
random_state (RNGLike, optional) – Random number generator.
- generate_random() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate a random individual using the fallback initializer.
- Returns:
A 1-D array generated by the fallback initializer.
- Return type:
VectorLike
- generate_individual() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Return a predefined solution until the insertion quota is met.
The first n_to_insert individuals are taken from the solution set (cycling if necessary). After that, random individuals are generated by the fallback initializer.
- Returns:
A 1-D array representing the individual.
- Return type:
VectorLike
- generate_population(objfunc, n_individuals: int | None = None) Population[source]¶
Create the population, resetting the insertion counter first.
- Parameters:
objfunc (ObjectiveFunc) – The objective function.
n_individuals (int, optional) – Number of individuals to generate. Defaults to
population_size.
- Returns:
A population with the predefined solutions inserted at the beginning.
- Return type:
- class SeedProbInitializer(default_init: Initializer, solutions: Population | Iterable[ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool]] | ndarray[tuple[int, int], floating] | ndarray[tuple[int, int], integer] | ndarray[tuple[int, int], uint8 | bool], insert_prob: float = 0.1, random_state=None)[source]¶
Bases:
InitializerInitializer that inserts a predefined solution with a given probability.
With probability insert_prob, a randomly chosen solution from the provided set is used; otherwise a random individual is generated by the fallback initializer.
- Parameters:
default_init (Initializer) – Fallback initializer for random individuals.
solutions (Population, Iterable[VectorLike] or MatrixLike) – Set of predefined solutions to draw from.
insert_prob (float, optional) – Probability of using a predefined solution (default 0.1).
random_state (RNGLike, optional) – Random number generator.
- generate_random() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate a random individual using the fallback initializer.
- Returns:
A 1-D array generated by the fallback initializer.
- Return type:
VectorLike
- generate_individual() ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool][source]¶
Generate an individual, possibly replacing it with a seeded solution.
With probability insert_prob a randomly chosen predefined solution is returned; otherwise a new random individual is created by the fallback initializer.
- Returns:
A 1-D array representing the individual.
- Return type:
ndarray
- class UniformInitializer(dimension, lower_bound, upper_bound, population_size=1, encoding=None, dtype=<class 'float'>, random_state=None)[source]¶
Bases:
InitializerInitializer that generates individuals with values drawn from a uniform distribution.
- Parameters:
dimension (int) – Length of the genotype vector.
lower_bound (float or array) – Lower bound(s) of the distribution. If an array is given, it must have length dimension.
upper_bound (float or array) – Upper bound(s) of the distribution. Must match the shape of lower_bound.
population_size (int, optional) – Number of individuals to generate (default 1).
encoding (Encoding, optional) – Encoding that will be passed to each individual.
dtype (type, optional) – Desired NumPy dtype of the generated vectors (default
float).random_state (RNGLike, optional) – Random number generator.
- generate_random()[source]¶
Generate a single random genotype vector (1-D array).
- Returns:
A newly generated genotype vector (1-D array).
- Return type:
VectorLike
- generate_individual()[source]¶
Generate a single individual.
By default simply delegates to
generate_random(). Returns a newly generated individual (a 1-D array).Override this method if your initializer needs to distinguish between a randomly initialize individual and a solution generated with another strategy (See SeedProbInitializer).
- Returns:
A newly generated individual.
- Return type:
Any