metaheuristic_designer.initializer module#

Base class for the Initializer module.

This module implements functions to generate the initial population of the algorithm.

class Initializer(dimension, population_size=1, encoding=None, rng=None)[source]#

Bases: ABC

Abstract 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 via generate_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_individual()

Generate a single individual.

generate_population([n_individuals])

Create a fully formed population of n_individuals individuals.

generate_random()

Generate a single random genotype vector (1-D array).

get_state()

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:

Population

Parameters:
n_individual: int, optional

Number of individuals to generate

Returns:
generated_population: Population

Newly generated population.

Parameters:

n_individuals (int | None)

get_state()[source]#

Return a minimal dictionary identifying this initializer.

Return type:

dict

Returns:
dict

Dictionary with key "class_name".

class InitializerFromLambda(generator, dimension, pop_size=1, encoding=None, rng=None)[source]#

Bases: Initializer

Initializer that uses a user-provided function to generate individuals.

Parameters:
generatorcallable

A function (rng) -> genotype that 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_random()

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).

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).