metaheuristic_designer.operator module#

Base class for the Operator module.

This module implements procedures to modify the current solutions so that we explore the search space.

class Operator(name=None, encoding=None, preserves_order=False, rng=None, **kwargs)[source]#

Bases: ParametrizableMixin, ABC

Abstract base for all perturbation operators.

An Operator modifies a population (typically by applying mutation, crossover, or a composite of several steps). Subclasses must implement evolve().

Parameters:
namestr, optional

Display name for this operator.

encodingEncoding, optional

Post-processing applied to the genotype matrix after the operator runs. Defaults to DefaultEncoding.

preserves_orderbool, optional

If True, the operator keeps individuals in the same order (useful for one-to-one survivor selection). Default False.

rngRNGLike, optional

Random number generator.

**kwargs

Additional keyword arguments stored as schedulable parameters.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • name (Optional[str])

  • encoding (Optional[Encoding])

  • preserves_order (bool)

  • rng (Optional[RNGLike])

Methods

__call__(population)

Shorthand for evolve().

evolve(population)

Evolves an population using a given strategy.

gather_params()

Return the current parameter dictionary (thin wrapper around get_params()).

get_params()

Return a copy of the current parameter dictionary.

get_state()

Gets the current state of the algorithm as a dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update([progress])

Updates the internal parameters.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

gather_params()[source]#

Return the current parameter dictionary (thin wrapper around get_params()).

abstract evolve(population)[source]#

Evolves an population using a given strategy.

Return type:

Population

Parameters:
population: Population

The population that will be used.

Returns:
new_population: Population

The modified population.

Parameters:

population (Population)

update(progress=0)[source]#

Updates the internal parameters.

Parameters:

progress (float)

get_state()[source]#

Gets the current state of the algorithm as a dictionary.

Return type:

dict

Returns:
state: dict

The complete state of the operator.

class NullOperator(name=None)[source]#

Bases: Operator

Operator class that returns the individual without changes. Surprisingly useful.

Since it’s a no-op, it has the preserves_order flag set to True.

Parameters:
name: str, optional

Name that is associated with the operator.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:

name (Optional[str])

Methods

__call__(population)

Shorthand for evolve().

evolve(population)

Evolves an population using a given strategy.

gather_params()

Return the current parameter dictionary (thin wrapper around get_params()).

get_params()

Return a copy of the current parameter dictionary.

get_state()

Gets the current state of the algorithm as a dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update([progress])

Updates the internal parameters.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

evolve(population)[source]#

Evolves an population using a given strategy.

Return type:

Population

Parameters:
population: Population

The population that will be used.

Returns:
new_population: Population

The modified population.

Parameters:

population (Population)

class OperatorFromLambda(operator_fn, name=None, encoding=None, preserves_order=False, rng=None, **kwargs)[source]#

Bases: Operator

Operator that wraps a user‑supplied function.

The function receives a Population, an Initializer, a random state, and any stored keyword arguments, and must return a modified Population.

Parameters:
operator_fncallable

A function (population, initializer, rng, **kwargs) -> Population.

namestr, optional

Display name (defaults to the function’s __name__).

encodingEncoding, optional

See Operator.

preserves_orderbool, optional

See Operator.

rngRNGLike, optional

See Operator.

**kwargs

Keyword arguments forwarded to Operator and also passed to operator_fn on each call.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • operator_fn (Callable)

  • name (Optional[str])

  • encoding (Optional[Encoding])

  • preserves_order (bool)

  • rng (Optional[RNGLike])

Methods

__call__(population)

Shorthand for evolve().

evolve(population)

Evolves an population using a given strategy.

gather_params()

Return the current parameter dictionary (thin wrapper around get_params()).

get_params()

Return a copy of the current parameter dictionary.

get_state()

Gets the current state of the algorithm as a dictionary.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update([progress])

Updates the internal parameters.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

evolve(population)[source]#

Evolves an population using a given strategy.

Return type:

Population

Parameters:
population: Population

The population that will be used.

Returns:
new_population: Population

The modified population.

Parameters:

population (Population)