metaheuristic_designer.operators package#

Subpackages#

Submodules#

Module contents#

Operator interfaces and base implementations.

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

class AdaptiveOperator(base_operator, param_operators, encoding, name=None, **kwargs)[source]#

Bases: ExtendedOperator

Operator that dynamically adapts its base operator’s parameters.

At each generation, the parameters encoded in the genotype are decoded and used to update the base operator before applying it to the population. This enables self-adaptive algorithms (e.g., Evolution Strategies with evolving mutation strengths).

See ExtendedOperator for constructor parameters.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:

Methods

__call__(population)

Shorthand for evolve().

evolve(population)

Decode parameters, update the base operator, then apply it.

gather_params()

Collect parameters from the base operator and all parameter operators.

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)

Update schedulable parameters and propagate to sub-operators.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

evolve(population)[source]#

Decode parameters, update the base operator, then apply it.

Return type:

Population

Parameters:
populationPopulation

The current population (whose genotype contains the parameters).

Returns:
Population

The evolved population.

Parameters:

population (Population)

class BOOperator(objfunc, initializer=None, name='Gaussian Regression Surrogate Model', encoding=None, kernel=None, rng=None, batch_size=100, max_samples=100, rbf_scale=1.0, **kwargs)[source]#

Bases: Operator

Bayesian Optimization operator using a GP surrogate.

Fits a Gaussian Process model to the current population, then maximizes the Expected Improvement acquisition function to propose a new candidate solution. The new solution is merged back into the population.

Parameters:
namestr, optional

Display name (default "Gaussian Regression Surrogate Model").

encodingEncoding, optional

Encoding applied to the genotype.

kernelsklearn Kernel, optional

GP kernel. Defaults to RBF(length_scale=1.0) + WhiteKernel(noise_level=1.0).

rngRNGLike, optional

Random number generator.

batch_sizeint, optional

Number of random starting points for acquisition optimization (default 100).

max_samplesint, optional

Maximum number of training points used (default 100). If the population exceeds this, a random subset is selected.

rbf_scalefloat, optional

Multiplicative factor applied to the RBF kernel (default 1.0).

**kwargs

Additional keyword arguments stored as schedulable parameters.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • objfunc (ObjectiveFunc)

  • initializer (Initializer)

  • name (str)

  • encoding (Optional[Encoding])

  • kernel (Optional[Callable])

  • rng (Optional[RNGLike])

  • batch_size (int)

  • max_samples (int)

  • rbf_scale (float)

Methods

__call__(population)

Shorthand for evolve().

evolve(population)

Fit GP, optimize acquisition, and merge the proposed point.

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]#

Fit GP, optimize acquisition, and merge the proposed point.

Return type:

Population

Parameters:
populationPopulation

The current population.

Returns:
Population

The population with the new candidate appended.

Parameters:

population (Population)

class BranchOperator(op_list, random_pick=True, name=None, encoding=None, rng=None, weights=None, p=None, **kwargs)[source]#

Bases: Operator

Operator that stochastically selects among several operators.

For each individual, one operator from op_list is chosen according to the configured method (random with given probability, or manually picked). This allows e.g. applying mutation with a certain probability while leaving the rest untouched.

Parameters:
op_listlist of Operator

The candidate operators.

random_pickbool, optional

Whether to pick an operator at random or by specifying an index (default True).

namestr, optional

Display name; defaults to "method(op_names)".

encodingEncoding, optional

Encoding applied to the genotype.

rngRNGLike, optional

Random number generator.

weights: VectorLike, optional

Weights of each operator when choosing at random.

pfloat, optional

Probability of selecting the first operator (default 0.5). Only applied when op_list has length 2 and no weights are specified.

**kwargs

Additional keyword arguments stored as schedulable parameters.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • op_list (Iterable[Operator])

  • random_pick (bool)

  • name (str)

  • encoding (Optional[Encoding])

  • rng (Optional[RNGLike])

  • weights (Optional[VectorLike])

  • p (float)

Methods

__call__(population)

Shorthand for evolve().

choose_index(idx)

Manually chooses the operator to use next

evolve(population)

Apply a random operator to each individual according to the branch method.

gather_params()

Collect parameters from this operator and all sub-operators.

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)

Update schedulable parameters and propagate to sub-operators.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

gather_params()[source]#

Collect parameters from this operator and all sub-operators.

Return type:

dict

Returns:
dict

Flat dictionary with dotted keys.

evolve(population)[source]#

Apply a random operator to each individual according to the branch method.

Return type:

Population

Parameters:
populationPopulation

The current population.

Returns:
Population

The modified population.

Parameters:

population (Population)

choose_index(idx)[source]#

Manually chooses the operator to use next

Parameters:
idxint

Index of the operator in the list.

Parameters:

idx (ndarray[tuple[int], floating] | ndarray[tuple[int], integer] | ndarray[tuple[int], uint8 | bool] | number | float | int)

update(progress)[source]#

Update schedulable parameters and propagate to sub-operators.

Parameters:
progressfloat

Current progress of the algorithm (0-1).

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 CompositeOperator(op_list, name=None, encoding=None, rng=None)[source]#

Bases: Operator

Operator that sequentially applies a list of operators.

Each operator in op_list receives the population returned by the previous one. This is the canonical way to chain crossover and mutation, or to build more complex pipelines.

Parameters:
op_listlist of Operator

The operators to apply in order.

namestr, optional

Display name; defaults to "Sequence (op_names)".

encodingEncoding, optional

Encoding applied to the genotype.

rngRNGLike, optional

Random number generator (shared with sub-operators).

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • op_list (Iterable[Operator])

  • name (str)

  • encoding (Optional[Encoding])

  • rng (Optional[RNGLike])

Methods

__call__(population)

Shorthand for evolve().

evolve(population)

Apply each operator in sequence.

gather_params()

Collect parameters from this operator and all sub-operators.

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)

Update schedulable parameters and propagate to sub-operators.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

gather_params()[source]#

Collect parameters from this operator and all sub-operators.

Return type:

dict

Returns:
dict

Flat dictionary with dotted keys.

evolve(population)[source]#

Apply each operator in sequence.

Return type:

Population

Parameters:
populationPopulation

The current population.

Returns:
Population

The population after all operators have been applied.

Parameters:

population (Population)

update(progress)[source]#

Update schedulable parameters and propagate to sub-operators.

Parameters:
progressfloat

Current progress of the algorithm (0-1).

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 ExtendedOperator(base_operator, param_operators, encoding, name=None, **kwargs)[source]#

Bases: Operator

Operator that handles a genotype split into solution and extra parameters.

A mask is built from the encoding to separate the solution part from the parameter blocks. The solution is processed by base_operator, while each parameter block can be mutated/adapted by its own operator.

Parameters:
base_operatorOperator

Operator applied to the solution part.

param_operatorsdict

Mapping from parameter names to their mutation operators.

encodingParameterExtendingEncoding

The encoding that defines the genotype layout.

namestr, optional

Display name; defaults to the base operator’s name.

**kwargs

Forwarded to Operator.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:

Methods

__call__(population)

Shorthand for evolve().

evolve(population)

Apply the main masked operator (solution + parameter mutations).

gather_params()

Collect parameters from the base operator and all parameter operators.

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)

Update schedulable parameters and propagate to sub-operators.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

gather_params()[source]#

Collect parameters from the base operator and all parameter operators.

Return type:

dict

Returns:
dict

Flat dictionary with dotted keys.

evolve(population)[source]#

Apply the main masked operator (solution + parameter mutations).

Return type:

Population

Parameters:
populationPopulation

The current population.

Returns:
Population

The evolved population.

Parameters:

population (Population)

update(progress)[source]#

Update schedulable parameters and propagate to sub-operators.

Parameters:
progressfloat

Current progress of the algorithm (0-1).

Parameters:

progress (float)

class MaskedOperator(op_list, mask, name=None, **kwargs)[source]#

Bases: Operator

Operator that partitions the genotype and applies different operators.

A mask (integer array of length vec_size) specifies which operator (index into op_list) handles each gene. This is used internally by ExtendedOperator to separate the solution from auxiliary parameters.

Parameters:
op_listlist of Operator

Operators to apply, one per mask index.

maskarray of int

Array of length vec_size assigning each gene to an operator.

namestr, optional

Display name; defaults to "Split (op_names)".

**kwargs

Forwarded to Operator.

Attributes:
params

Access parameter values by attribute-style lookup.

Parameters:
  • op_list (Iterable[Operator])

  • mask (MaskLike)

  • name (str)

Methods

__call__(population)

Shorthand for evolve().

evolve(population)

Apply the appropriate operator to each slice of the genotype.

gather_params()

Collect parameters from this operator and all sub-operators.

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)

Update schedulable parameters and propagate to sub-operators.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

gather_params()[source]#

Collect parameters from this operator and all sub-operators.

Return type:

dict

Returns:
dict

Flat dictionary with dotted keys.

evolve(population)[source]#

Apply the appropriate operator to each slice of the genotype.

Return type:

Population

Parameters:
populationPopulation

The current population.

Returns:
Population

The modified population.

Parameters:

population (Population)

update(progress)[source]#

Update schedulable parameters and propagate to sub-operators.

Parameters:
progressfloat

Current progress of the algorithm (0-1).

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 ObtainStatisticDef(operator_fn, params=<factory>, forced_params=<factory>)[source]#

Bases: object

Wrap a statistic‑computing function into an Operator.

This adapter is used for functions that compute a single summary vector (e.g., population mean, median, standard deviation) and store it as the new genotype (usually a single-row population).

Parameters:
operator_fncallable

Function with signature (population_matrix, rng, **kwargs) -> np.ndarray.

paramsdict, optional

Default keyword arguments.

forced_paramsdict, optional

Keyword arguments that override user-supplied ones.

Parameters:
  • operator_fn (callable)

  • params (dict)

  • forced_params (dict)

Methods

__call__(population[, rng])

Compute a statistic and replace the population’s genotype.

operator_fn: callable#
params: dict#
forced_params: dict#
class OperatorRandomDef(operator_fn, params=<factory>, forced_params=<factory>)[source]#

Bases: object

Bridge a random-style operator function into an Operator.

This wrapper is intended for operators that replace the genotype with entirely new random values (e.g., uniform sampling, initializer-based reset). It passes the population’s genotype matrix, the initializer, and the random state to the underlying function.

Parameters:
operator_fncallable

Function with signature (population_matrix, initializer, rng, **kwargs) -> np.ndarray.

paramsdict, optional

Default keyword arguments.

forced_paramsdict, optional

Keyword arguments that override any user-supplied ones.

Parameters:
  • operator_fn (callable)

  • params (dict)

  • forced_params (dict)

Methods

__call__(population, initializer[, rng])

Execute the random operator and return a new population.

operator_fn: callable#
params: dict#
forced_params: dict#
class OperatorSwarmDef(operator_fn, params=<factory>, forced_params=<factory>)[source]#

Bases: object

Bridge a swarm operator function into an Operator.

This wrapper is designed for operators that directly receive the whole Population object and the initializer, and are responsible for returning an updated Population themselves (e.g., PSO operators that need access to historical bests).

Parameters:
operator_fncallable

Function with signature (population, initializer, rng, **kwargs) -> Population.

paramsdict, optional

Default keyword arguments.

forced_paramsdict, optional

Keyword arguments that override user-supplied ones.

Parameters:
  • operator_fn (callable)

  • params (dict)

  • forced_params (dict)

Methods

__call__(population, rng, **kwargs)

Execute the swarm operator and return the new population.

operator_fn: callable#
params: dict#
forced_params: dict#
class OperatorFnDef(operator_fn, params=<factory>, forced_params=<factory>)[source]#

Bases: object

Bridge a matrix-to-matrix operator function into an Operator.

This wrapper accepts a callable that operates on a genotype matrix, fitness array, and random state, and turns it into an object that can be used directly on a Population. It merges user-supplied keyword arguments with stored defaults and forced parameters, then invokes the underlying function and updates the population’s genotype.

Parameters:
operator_fncallable

Function with signature (population_matrix, fitness_array, rng, **kwargs) -> np.ndarray.

paramsdict, optional

Default keyword arguments for the operator.

forced_paramsdict, optional

Keyword arguments that always override user-supplied ones.

Parameters:
  • operator_fn (callable)

  • params (dict)

  • forced_params (dict)

Methods

__call__(population[, rng])

Execute the wrapped operator and return a new population.

operator_fn: callable#
params: dict#
forced_params: dict#
list_operators()[source]#

Return a list of all registered operator keys.

Each key is formatted as "registry.operator_name" and can be passed to create_operator().

Return type:

list[str]

Returns:
list of str

Fully qualified operator names.

add_operator_entry(operator_fn, operator_name, operator_registry='custom', preserves_order=False)[source]#

Register a new operator so it can be created by create_operator().

Parameters:
operator_fncallable

A callable that follows the operator signature expected by OperatorFromLambda. Usually wrapped with OperatorFnDef, OperatorRandomDef, etc.

operator_namestr

Key under which the operator is registered.

operator_registrystr, optional

Registry name (default "custom"). If the registry does not exist, it is created.

preserves_orderbool, optional

If True, the operator is marked as order-preserving, meaning individuals retain their position when applying it. Default False.

Parameters:
  • operator_fn (callable)

  • operator_name (str)

  • operator_registry (str)

create_crossover_operator(method, encoding=None, rng=None, name=None, **kwargs)[source]#

Create a crossover operator by name.

Return type:

OperatorFromLambda

Parameters:
methodstr

Key into crossover_ops_map (e.g., "one_point", "uniform").

encodingEncoding, optional

Encoding applied to the genotype after crossover.

rngRNGLike, optional

Random number generator.

namestr, optional

Display name; defaults to method.

**kwargs

Additional parameters forwarded to the operator function (e.g., k, crossover_prob, pairing_method).

Returns:
OperatorFromLambda

The wrapped crossover operator.

Parameters:
  • method (str)

  • encoding (Encoding | None)

  • rng (int | Generator | None)

  • name (str | None)

create_debug_operator(method, encoding=None, name=None, **kwargs)[source]#

Create a debug operator by name.

Return type:

OperatorFromLambda

Parameters:
methodstr

Key into debug_ops_map.

encodingEncoding, optional

Encoding applied to the genotype.

namestr, optional

Display name; defaults to method.

**kwargs

Forwarded to the operator function.

Returns:
OperatorFromLambda

The wrapped debug operator.

Parameters:
  • method (str)

  • encoding (Encoding | None)

  • name (str | None)

create_differential_evolution_operator(method, encoding=None, vectorized=True, name=None, **kwargs)[source]#

Create a DE operator by name.

Return type:

OperatorFromLambda

Parameters:
methodstr

DE variant string, e.g., "de/rand/1".

encodingEncoding, optional

Encoding applied to the genotype.

vectorizedbool, optional

Unused; kept for interface compatibility.

namestr, optional

Display name; defaults to method.

**kwargs

Forwarded to the DE operator function (e.g., F, Cr).

Returns:
OperatorFromLambda

The wrapped DE operator.

Parameters:
  • method (str)

  • encoding (Encoding | None)

  • vectorized (bool)

  • name (str | None)

create_mutation_operator(method, encoding=None, name=None, **kwargs)[source]#

Create a mutation operator by name.

Return type:

OperatorFromLambda

Parameters:
methodstr

Key into mutation_ops_map.

encodingEncoding, optional

Encoding applied to the genotype after mutation.

namestr, optional

Display name; defaults to method.

**kwargs

Parameters forwarded to the mutation function (e.g., N, F, distribution).

Returns:
OperatorFromLambda

The wrapped mutation operator.

Parameters:
  • method (str)

  • encoding (Encoding | None)

  • name (str | None)

create_operator(method, encoding=None, rng=None, name=None, **kwargs)[source]#

Create an operator by name from any registry.

The method string can be a simple key (e.g., "gauss") or dot-separated "registry.key" (e.g., "crossover.one_point").

Return type:

OperatorFromLambda

Parameters:
methodstr

Operator key, possibly with registry prefix.

encodingEncoding, optional

Encoding applied to the genotype after the operator runs.

rngRNGLike, optional

Random number generator.

namestr, optional

Display name; defaults to method.

**kwargs

Parameters forwarded to the underlying operator function.

Returns:
OperatorFromLambda

The wrapped operator.

Raises:
ValueError

If the operator cannot be found.

Parameters:
  • method (str)

  • encoding (Encoding | None)

  • rng (int | Generator | None)

  • name (str | None)

create_permutation_operator(method, encoding=None, name=None, **kwargs)[source]#

Create a permutation operator by name.

Return type:

OperatorFromLambda

Parameters:
methodstr

Key into permutation_ops_map.

encodingEncoding, optional

Encoding applied to the genotype.

namestr, optional

Display name; defaults to method.

**kwargs

Forwarded to the operator function.

Returns:
OperatorFromLambda

The wrapped permutation operator.

Parameters:
  • method (str)

  • encoding (Encoding | None)

  • name (str | None)

create_random_operator(method, initializer, encoding=None, name=None, **kwargs)[source]#

Create a random operator that uses an Initializer for fresh values.

Return type:

OperatorFromLambda

Parameters:
methodstr

Key into random_ops_map.

encodingEncoding, optional

Encoding applied to the genotype.

namestr, optional

Display name; defaults to method.

**kwargs

Forwarded to the operator function.

Returns:
OperatorFromLambda

The wrapped random operator.

Parameters:
create_swarm_operator(method, name=None, **kwargs)[source]#

Create a swarm operator by name.

Return type:

OperatorFromLambda

Parameters:
methodstr

Key into swarm_ops_map (e.g., "pso").

namestr, optional

Display name; defaults to method.

**kwargs

Forwarded to the operator function.

Returns:
OperatorFromLambda

The wrapped swarm operator.

Parameters:
  • method (str)

  • name (str | None)