metaheuristic_designer.initializers package#
Submodules#
- metaheuristic_designer.initializers.composite_initializer module
- metaheuristic_designer.initializers.direct_initializer module
- metaheuristic_designer.initializers.exponential_initializer module
- metaheuristic_designer.initializers.extended_initializer module
- metaheuristic_designer.initializers.gaussian_initializer module
- metaheuristic_designer.initializers.halton_initializer module
- metaheuristic_designer.initializers.latin_hypercube_initializer module
- metaheuristic_designer.initializers.perm_initializer module
- metaheuristic_designer.initializers.seed_initializer module
- metaheuristic_designer.initializers.sobol_initializer module
- metaheuristic_designer.initializers.uniform_initializer module
Module contents#
Concrete initializer implementations provided by the library.
- class DirectInitializer(default_init, solutions, encoding=None, rng=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_initInitializer
Fallback initializer for
generate_random().- solutionsPopulation, list or ndarray
The set of solutions to draw from.
- encodingEncoding, optional
Encoding attached to the population (used when solutions is a
Population).- rngRNGLike, optional
Random number generator.
- Parameters:
default_init (Initializer)
solutions (Population | List | np.ndarray)
encoding (Encoding)
rng (Optional[RNGLike])
Methods
Return a chosen individual from the stored solution set in cyclic order.
generate_population([n_individuals])Create a population by drawing from the stored solutions.
Return a completely random individual generated from a fallback initializer strategy
get_state()Return a minimal dictionary identifying this initializer.
- generate_random()[source]#
Return a completely random individual generated from a fallback initializer strategy
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool]- Returns:
- VectorLike
A 1-D array sampled from a fallback distribution.
- generate_individual()[source]#
Return a chosen individual from the stored solution set in cyclic order.
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool]- Returns:
- VectorLike
A 1-D array taken from the predefined solutions.
- generate_population(n_individuals=None)[source]#
Create a population by drawing from the stored solutions.
- Return type:
- Parameters:
- objfuncObjectiveFunc
The objective function.
- n_individualsint, optional
Number of individuals to generate. Defaults to
population_size.
- Returns:
- Population
A population built from the predefined solutions.
- Parameters:
n_individuals (int | None)
- class ExponentialInitializer(dimension, beta, population_size=1, encoding=None, dtype=<class 'float'>, rng=None)[source]#
Bases:
InitializerInitializer that generates individuals with values drawn from an exponential distribution.
- Parameters:
- dimensionint
Length of the genotype vector.
- betafloat or array
Scale parameter of the exponential distribution (1 / rate).
- pop_sizeint, optional
Number of individuals to generate (default 1).
- encodingEncoding, optional
Encoding that will be passed to each individual.
- dtypetype, optional
Desired NumPy dtype of the generated vectors (default
float).- rngRNGLike, optional
Random number generator.
Methods
generate_individual()Generate a single individual.
generate_population([n_individuals])Create a fully formed population of n_individuals individuals.
Generate a single random genotype vector (1-D array).
get_state()Return a minimal dictionary identifying this initializer.
- class ExtendedInitializer(solution_init, param_init_dict, encoding, rng=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_initInitializer
Initializer for the solution part of the genotype.
- param_init_dictdict
Mapping of parameter names to their corresponding initializers.
- encodingParameterExtendingEncoding
The extended encoding that defines the parameter layout.
- rngRNGLike, optional
Random number generator.
- Parameters:
solution_init (Initializer)
param_init_dict (dict)
encoding (ParameterExtendingEncoding)
Methods
Generate an individual (by default identical to
generate_random()).generate_population([n_individuals])Create a new random population that included adaptive parameters.
Generate a random genotype vector with solution and parameter parts.
get_state()Return a minimal dictionary identifying this initializer.
- generate_random()[source]#
Generate a random genotype vector with solution and parameter parts.
- Returns:
- ndarray
A 1-D array with the solution followed by the extra parameters.
- generate_individual()[source]#
Generate an individual (by default identical to
generate_random()).- Returns:
- ndarray
A 1-D array with the solution and parameter parts.
- generate_population(n_individuals=None)[source]#
Create a new random population that included adaptive parameters.
- Parameters:
- objfunc: ObjectiveFunc
Objective function that will be propagated to each individual.
- n_individual: int, optional
Number of individuals to generate
- Returns:
- generated_population: Population
Newly generated population.
- class GaussianInitializer(dimension, g_mean, g_std, population_size=1, encoding=None, dtype=<class 'float'>, rng=None)[source]#
Bases:
InitializerInitializer that generates individuals with values drawn from a Gaussian (normal) distribution.
- Parameters:
- dimensionint
Length of the genotype vector.
- g_meanfloat or array
Mean of the distribution. If an array is given, it must have length dimension.
- g_stdfloat or array
Standard deviation of the distribution. If an array is given, it must have length dimension.
- pop_sizeint, optional
Number of individuals to generate (default 1).
- encodingEncoding, optional
Encoding that will be passed to each individual.
- dtypetype, optional
Desired NumPy dtype of the generated vectors (default
float).- rngRNGLike, optional
Random number generator.
Methods
generate_individual()Generate a single individual.
generate_population([n_individuals])Create a fully formed population of n_individuals individuals.
Generate a single random genotype vector (1-D array).
get_state()Return a minimal dictionary identifying this initializer.
- class Initializer(dimension, population_size=1, encoding=None, rng=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:
- dimensionint
Length of the genotype vector.
- population_sizeint, optional
Number of individuals to generate (default 1).
- encodingEncoding, optional
Encoding that will be attached to every individual. Defaults to
DefaultEncoding.- rngRNGLike, optional
Random number generator.
- Parameters:
dimension (int)
population_size (int)
encoding (Optional[Encoding])
rng (Optional[RNGLike])
Methods
Generate a single individual.
generate_population([n_individuals])Create a fully formed population of n_individuals individuals.
Generate a single random genotype vector (1-D array).
Return a minimal dictionary identifying this initializer.
- abstract generate_random()[source]#
Generate a single random genotype vector (1-D array).
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool]- Returns:
- VectorLike
A newly generated genotype vector (1-D array).
- 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).
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool]- Returns:
- Any
A newly generated individual.
- generate_population(n_individuals=None)[source]#
Create a fully formed population of n_individuals individuals.
- Return type:
- Parameters:
- n_individual: int, optional
Number of individuals to generate
- Returns:
- generated_population: Population
Newly generated population.
- Parameters:
n_individuals (int | None)
- class InitializerFromLambda(generator, dimension, pop_size=1, encoding=None, rng=None)[source]#
Bases:
InitializerInitializer that uses a user-provided function to generate individuals.
- Parameters:
- generatorcallable
A function
(rng) -> genotypethat returns a single genotype vector.- dimensionint
Length of the genotype vector.
- pop_sizeint, optional
Number of individuals to generate (default 1).
- encodingEncoding, optional
Encoding attached to every individual.
- rngRNGLike, optional
Random number generator.
- Parameters:
generator (Callable)
dimension (int)
pop_size (int)
encoding (Optional[Encoding])
rng (Optional[RNGLike])
Methods
generate_individual()Generate a single individual.
generate_population([n_individuals])Create a fully formed population of n_individuals individuals.
Generate a single random genotype vector (1-D array).
get_state()Return a minimal dictionary identifying this initializer.
- class PermInitializer(dimension, population_size=1, encoding=None, rng=None)[source]#
Bases:
InitializerInitializer that generates individuals as random permutations of integers
0, 1, …, dimension-1.- Parameters:
- dimensionint
Length of the permutation (number of elements).
- pop_sizeint, optional
Number of individuals to generate (default 1).
- encodingEncoding, optional
Encoding that will be passed to each individual.
- rngRNGLike, optional
Random number generator.
- Parameters:
dimension (int)
population_size (int)
encoding (Optional[Encoding])
rng (Optional[RNGLike])
Methods
generate_individual()Generate a single individual.
generate_population([n_individuals])Create a fully formed population of n_individuals individuals.
Generate a single random genotype vector (1-D array).
get_state()Return a minimal dictionary identifying this initializer.
- class CompositeInitializer(dimension, initializers, weights=None, population_size=None, encoding=None, rng=None)[source]#
Bases:
InitializerMethods
Generate an individual from one of the initializers chosen at random.
generate_population([n_individuals])Generate a population from individuals chosen at random from the initializers.
Generate a random individual from one initializers chosen at random.
get_state()Return a minimal dictionary identifying this initializer.
- Parameters:
dimension (int)
initializers (Iterable[Initializer])
weights (ndarray[tuple[int], floating] | None)
population_size (int | None)
encoding (Encoding)
rng (int | Generator | None)
- generate_random()[source]#
Generate a random individual from one initializers chosen at random.
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool]- Returns:
- VectorLike
A 1-D array generated by the fallback initializer.
- generate_individual()[source]#
Generate an individual from one of the initializers chosen at random.
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool]- Returns:
- ndarray
A 1-D array representing the individual.
- generate_population(n_individuals=None)[source]#
Generate a population from individuals chosen at random from the initializers.
- Parameters:
- objfunc: ObjectiveFunc
Objective function that will be propagated to each individual.
- n_individual: int, optional
Number of individuals to generate
- Returns:
- generated_population: Population
Newly generated population.
- class FixedCompositeInitializer(dimension, initializers, amounts=None, population_size=None, encoding=None, rng=None)[source]#
Bases:
InitializerMethods
Generate an individual from one of the initializers chosen deterministically.
generate_population([n_individuals])Generate a population from individuals chosen at random from the initializers.
Generate a random individual from one of the initializers chosen deterministically.
get_state()Return a minimal dictionary identifying this initializer.
- Parameters:
dimension (int)
initializers (Iterable[Initializer])
amounts (ndarray[tuple[int], integer] | None)
population_size (int | None)
encoding (Encoding)
rng (int | Generator | None)
- generate_random()[source]#
Generate a random individual from one of the initializers chosen deterministically.
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool]- Returns:
- VectorLike
A 1-D array generated by the fallback initializer.
- generate_individual()[source]#
Generate an individual from one of the initializers chosen deterministically.
- Return type:
ndarray[tuple[int],floating] |ndarray[tuple[int],integer] |ndarray[tuple[int],uint8|bool]- Returns:
- ndarray
A 1-D array representing the individual.
- generate_population(n_individuals=None)[source]#
Generate a population from individuals chosen at random from the initializers.
- Parameters:
- objfunc: ObjectiveFunc
Objective function that will be propagated to each individual.
- n_individual: int, optional
Number of individuals to generate
- Returns:
- generated_population: Population
Newly generated population.
- class SeededInitializer(default_init, solutions, insert_prob=0.1, population_size=None, rng=None)[source]#
Bases:
CompositeInitializerInitializer 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_initInitializer
Fallback initializer for random individuals.
- solutionsPopulation, Iterable[VectorLike] or MatrixLike
Set of predefined solutions to draw from.
- insert_probfloat, optional
Probability of using a predefined solution (default 0.1).
- rngRNGLike, optional
Random number generator.
- Parameters:
default_init (Initializer)
solutions (Population | Iterable[VectorLike] | MatrixLike)
insert_prob (float)
population_size (int)
rng (Optional[RNGLike])
Methods
generate_individual()Generate an individual from one of the initializers chosen at random.
generate_population([n_individuals])Generate a population from individuals chosen at random from the initializers.
generate_random()Generate a random individual from one initializers chosen at random.
get_state()Return a minimal dictionary identifying this initializer.
- class FixedSeededInitializer(default_init, solutions, n_to_insert=None, population_size=None, rng=None)[source]#
Bases:
FixedCompositeInitializerInitializer 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_initInitializer
Fallback initializer for random individuals.
- solutionsPopulation, Iterable[VectorLike] or MatrixLike
Set of predefined solutions to draw from.
- n_to_insertint, optional
Exact number of predefined solutions to insert. Defaults to the size of the solution set.
- rngRNGLike, optional
Random number generator.
- Parameters:
default_init (Initializer)
solutions (Population | Iterable[VectorLike] | MatrixLike)
n_to_insert (int)
population_size (int)
rng (Optional[RNGLike])
Methods
generate_individual()Generate an individual from one of the initializers chosen deterministically.
generate_population([n_individuals])Generate a population from individuals chosen at random from the initializers.
generate_random()Generate a random individual from one of the initializers chosen deterministically.
get_state()Return a minimal dictionary identifying this initializer.
- class UniformInitializer(dimension, lower_bound, upper_bound, population_size=1, encoding=None, dtype=<class 'float'>, rng=None)[source]#
Bases:
InitializerInitializer that generates individuals with values drawn from a uniform distribution.
- Parameters:
- dimensionint
Length of the genotype vector.
- lower_boundfloat or array
Lower bound(s) of the distribution. If an array is given, it must have length dimension.
- upper_boundfloat or array
Upper bound(s) of the distribution. Must match the shape of lower_bound.
- population_sizeint, optional
Number of individuals to generate (default 1).
- encodingEncoding, optional
Encoding that will be passed to each individual.
- dtypetype, optional
Desired NumPy dtype of the generated vectors (default
float).- rngRNGLike, optional
Random number generator.
Methods
Generate a single individual.
generate_population([n_individuals])Create a fully formed population of n_individuals individuals.
Generate a single random genotype vector (1-D array).
get_state()Return a minimal dictionary identifying this initializer.
- generate_random()[source]#
Generate a single random genotype vector (1-D array).
- Returns:
- VectorLike
A newly generated genotype vector (1-D array).
- 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:
- Any
A newly generated individual.
- class LatinHypercubeInitializer(dimension, lower_bound, upper_bound, population_size=1, encoding=None, dtype=<class 'float'>, rng=None)[source]#
Bases:
InitializerInitializer that generates individuals using the Latin Hypercube Sampling (LHS) technique, in which values are drawn from a stratified uniform distribution that more efficiently samples the search space than naive uniform sampling.
- Parameters:
- dimensionint
Length of the genotype vector.
- lower_boundfloat or array
Lower bound(s) of the distribution. If an array is given, it must have length dimension.
- upper_boundfloat or array
Upper bound(s) of the distribution. Must match the shape of lower_bound.
- population_sizeint, optional
Number of individuals to generate (default 1).
- encodingEncoding, optional
Encoding that will be passed to each individual.
- dtypetype, optional
Desired NumPy dtype of the generated vectors (default
float).- rngRNGLike, optional
Random number generator.
Methods
generate_individual()Generate a single individual.
generate_population([n_individuals])Create a fully formed population of n_individuals individuals.
Generate a single random genotype vector (1-D array).
get_state()Return a minimal dictionary identifying this initializer.
- generate_random()[source]#
Generate a single random genotype vector (1-D array).
- Returns:
- VectorLike
A newly generated genotype vector (1-D array).
- generate_population(n_individuals=None)[source]#
Create a fully formed population of n_individuals individuals.
- Return type:
- Parameters:
- objfunc: ObjectiveFunc
Objective function that will be propagated to each individual.
- n_individual: int, optional
Number of individuals to generate
- Returns:
- generated_population: Population
Newly generated population.
- Parameters:
n_individuals (int | None)
- class SobolInitializer(dimension, lower_bound, upper_bound, population_size=1, scramble=True, fallback=None, encoding=None, dtype=<class 'float'>, rng=None)[source]#
Bases:
InitializerInitializer that generates individuals using the Sobol sequences, this is a quasi-random method designed for covering the space with low-discrepancy samples.
- Parameters:
- dimensionint
Length of the genotype vector.
- lower_boundfloat or array
Lower bound(s) of the distribution. If an array is given, it must have length dimension.
- upper_boundfloat or array
Upper bound(s) of the distribution. Must match the shape of lower_bound.
- population_sizeint, optional
Number of individuals to generate (default 1).
- encodingEncoding, optional
Encoding that will be passed to each individual.
- dtypetype, optional
Desired NumPy dtype of the generated vectors (default
float).- rngRNGLike, optional
Random number generator.
- Parameters:
fallback (Optional[Initializer])
Methods
generate_individual()Generate a single individual.
generate_population([n_individuals])Create a fully formed population of n_individuals individuals.
Generate a single random genotype vector (1-D array).
get_state()Return a minimal dictionary identifying this initializer.
- generate_random()[source]#
Generate a single random genotype vector (1-D array).
- Returns:
- VectorLike
A newly generated genotype vector (1-D array).
- generate_population(n_individuals=None)[source]#
Create a fully formed population of n_individuals individuals.
- Return type:
- Parameters:
- objfunc: ObjectiveFunc
Objective function that will be propagated to each individual.
- n_individual: int, optional
Number of individuals to generate
- Returns:
- generated_population: Population
Newly generated population.
- Parameters:
n_individuals (int | None)
- class HaltonInitializer(dimension, lower_bound, upper_bound, population_size=1, scramble=True, fallback=None, encoding=None, dtype=<class 'float'>, rng=None)[source]#
Bases:
InitializerInitializer that generates individuals using the Halton sequences, this is a pseudo-random method designed for covering the space efficiently.
- Parameters:
- dimensionint
Length of the genotype vector.
- lower_boundfloat or array
Lower bound(s) of the distribution. If an array is given, it must have length dimension.
- upper_boundfloat or array
Upper bound(s) of the distribution. Must match the shape of lower_bound.
- population_sizeint, optional
Number of individuals to generate (default 1).
- encodingEncoding, optional
Encoding that will be passed to each individual.
- dtypetype, optional
Desired NumPy dtype of the generated vectors (default
float).- rngRNGLike, optional
Random number generator.
- Parameters:
fallback (Optional[Initializer])
Methods
generate_individual()Generate a single individual.
generate_population([n_individuals])Create a fully formed population of n_individuals individuals.
Generate a single random genotype vector (1-D array).
get_state()Return a minimal dictionary identifying this initializer.
- generate_random()[source]#
Generate a single random genotype vector (1-D array).
- Returns:
- VectorLike
A newly generated genotype vector (1-D array).
- generate_population(n_individuals=None)[source]#
Create a fully formed population of n_individuals individuals.
- Return type:
- Parameters:
- objfunc: ObjectiveFunc
Objective function that will be propagated to each individual.
- n_individual: int, optional
Number of individuals to generate
- Returns:
- generated_population: Population
Newly generated population.
- Parameters:
n_individuals (int | None)