metaheuristic_designer.search_strategy module#

Base class for the Search strategy module.

This module implements the procedure applied in each iteration of the algorithm.

class SearchStrategy(initializer, operator=None, parent_sel=None, survivor_sel=None, name='some strategy', rng=None, **kwargs)[source]#

Bases: ParametrizableMixin, ABC

Orchestrates one iteration of an optimization loop.

A search strategy holds together an Initializer, an Operator, a ParentSelection, and a SurvivorSelection. Together they define how the population is created, perturbed, and pruned each generation. Subclasses can override any step to implement algorithm-specific logic.

Parameters:
initializerInitializer

Creates the starting population.

operatorOperator, optional

The perturbation operator (mutation, crossover, …). Defaults to NullOperator.

parent_selParentSelection, optional

Selects which individuals are used to generate offspring. Defaults to NullParentSelection.

survivor_selSurvivorSelection, optional

Selects which individuals survive to the next generation. Defaults to NullSurvivorSelection.

namestr, optional

Display name used in reports.

rngRNGLike, optional

Random number generator.

**kwargs

Additional keyword arguments stored as schedulable parameters.

Attributes:
params

Access parameter values by attribute-style lookup.

population_size

Gets the amount of individuals in the population.

Parameters:

Methods

extra_report()

Hook called at the end of the optimization (intended for subclasses).

extra_step_info()

Hook called after each generation (intended for subclasses).

gather_parameters()

Collect the current parameters from all sub-components.

get_params()

Return a copy of the current parameter dictionary.

get_state()

Gets the current state of the search strategy as a dictionary.

initialize(objfunc)

Initializes the optimization search strategy.

step(prev_population, objfunc)

Performs a single iteration of the algorithm on a given population.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Advances the state of the search by one iteration.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

reset

gather_parameters()[source]#

Collect the current parameters from all sub-components.

Returns:
dict

A flat dictionary with dotted keys like "operator.F", "parent_sel.amount", etc.

reset(objfunc)[source]#
Parameters:

objfunc (ObjectiveFunc)

initialize(objfunc)[source]#

Initializes the optimization search strategy.

Return type:

Population

Parameters:
objfunc: ObjectiveFunc

Objective function to be optimized.

Returns:
population: Population

The initial population to be used in the algorithm.

Parameters:

objfunc (ObjectiveFunc)

update(progress)[source]#

Advances the state of the search by one iteration.

Parameters:
progressfloat

Current progress of the algorithm (0-1).

Parameters:

progress (float)

abstract step(prev_population, objfunc)[source]#

Performs a single iteration of the algorithm on a given population.

Return type:

Population

Parameters:
populationPopulation

Population of solutions in which to perform the operators.

Returns:
Population

Next population after performing all the steps in the iteration.

Parameters:
get_state()[source]#

Gets the current state of the search strategy as a dictionary.

Return type:

dict

Parameters:
show_population: bool, optional

Save the state of the current population.

Returns:
state: dict

The complete state of the search strategy.

extra_step_info()[source]#

Hook called after each generation (intended for subclasses).

extra_report()[source]#

Hook called at the end of the optimization (intended for subclasses).

class SearchStrategyFromLambda(initializer, iterate_fn, name='Custom strategy', dimension=None, rng=None, **kwargs)[source]#

Bases: SearchStrategy

Strategy whose components can be plain functions.

Accepts each component as either a properly constructed object or a callable; if a callable is provided it is automatically wrapped with the appropriate *FromLambda class. This is the simplest way to build a custom strategy in one go.

Parameters:
initializercallable or Initializer

Function (rng) -> genotype, or an initializer instance.

iterate_fn: callable

Function that advances the state of the algorithm by one full iteration.

namestr, optional

Display name (default "Strategy from lambda").

rngRNGLike, optional

Random number generator.

**kwargs

Forwarded to SearchStrategy.

Attributes:
params

Access parameter values by attribute-style lookup.

population_size

Gets the amount of individuals in the population.

Parameters:
  • initializer (Initializer)

  • iterate_fn (Callable)

  • name (str)

  • dimension (int)

  • rng (Optional[RNGLike])

Methods

extra_report()

Hook called at the end of the optimization (intended for subclasses).

extra_step_info()

Hook called after each generation (intended for subclasses).

gather_parameters()

Collect the current parameters from all sub-components.

get_params()

Return a copy of the current parameter dictionary.

get_state()

Gets the current state of the search strategy as a dictionary.

initialize(objfunc)

Initializes the optimization search strategy.

step(population, objfunc)

Performs a single iteration of the algorithm on a given population.

store_kwargs([progress])

Store keyword arguments and evaluate them at the given progress.

update(progress)

Advances the state of the search by one iteration.

update_kwargs([progress])

Add or replace parameters and immediately evaluate them.

reset

step(population, objfunc)[source]#

Performs a single iteration of the algorithm on a given population.

Parameters:
populationPopulation

Population of solutions in which to perform the operators.

Returns:
Population

Next population after performing all the steps in the iteration.

Parameters: