metaheuristic_designer.strategies.classic package#
Submodules#
- metaheuristic_designer.strategies.classic.CMA_ES module
- metaheuristic_designer.strategies.classic.DE module
- metaheuristic_designer.strategies.classic.ES module
- metaheuristic_designer.strategies.classic.GA module
- metaheuristic_designer.strategies.classic.SA module
- metaheuristic_designer.strategies.classic.hill_climb module
- metaheuristic_designer.strategies.classic.local_search module
- metaheuristic_designer.strategies.classic.random_search module
Module contents#
Classic population-based strategies (GA, DE, ES, CMA-ES, SA, RandomSearch).
- class CMA_ES(initializer, survivor_sel=None, name='CMA-ES', offspring_size=None, rng=None, mean=None, sigma=None, cond_tol=100000000.0, sigma_tol=1e-10, **kwargs)[source]#
Bases:
EDAStrategyCovariance Matrix Adaptation Evolution Strategy (CMA-ES).
This is a population-based algorithm that samples new solutions from a multivariate normal distribution whose mean and covariance are adapted each generation based on the best individuals.
Note
The architecture of this class is provisional. It currently overrides
initialize()andperturb()directly. Once the distribution-based (EDA) abstraction is in place, CMA-ES will be rewritten to use that common interface.- Parameters:
- initializerInitializer
Provides population size and genotype shape, but does not generate the initial solutions.
- survivor_selSurvivorSelection, optional
How survivors are selected. Defaults to the strategy’s default (generational).
- namestr, optional
Display name (default
"CMA-ES").- offspring_sizeint or SchedulableParameter, optional
Number of offspring per generation. If
None, the initializer’s population size is used.- rngRNGLike, optional
Random number generator.
- meanVectorLike, optional
Initial mean vector. If not given, it is computed from the objective’s bounds (or randomly if no bounds exist).
- sigmaVectorLike, optional
Initial step size. If not given, a default is computed.
- **kwargs
Forwarded to
VariablePopulation.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
population_sizeGets the amount of individuals in the population.
- Parameters:
initializer (Initializer)
survivor_sel (SurvivorSelection)
name (str)
offspring_size (Optional[int | SchedulableParameter])
mean (Optional[VectorLike])
sigma (Optional[VectorLike])
cond_tol (float)
sigma_tol (float)
Methods
reset
- initialize(objfunc)[source]#
Create the initial population by sampling from the current distribution.
- Return type:
- Parameters:
- objfuncObjectiveFunc
The objective function, used to infer bounds if mean or sigma are not provided.
- Returns:
- Population
A freshly sampled population with unevaluated fitness.
- Parameters:
objfunc (ObjectiveFunc)
- estimate_parameters(population)[source]#
Update the distribution parameters
The parents (the best μ individuals from the previous generation) are used to update mean, sigma, covariance, and the evolution paths.
- Parameters:
- populationPopulation
The selected parents (must be already evaluated).
- Returns:
- Population
Offspring population of size offspring_size.
- class HillClimb(initializer, operator=None, survivor_sel=None, params=None, name='HillClimb', rng=None, **kwargs)[source]#
Bases:
SingleSolutionStrategyHill Climbing algorithm.
A single solution is perturbed each iteration. If the new solution is better, it replaces the current one. By default, the survivor selection is set to one-to-one competition (
"hill_climb"in the survivor registry).- Parameters:
- initializerInitializer
Population initializer (typically creates a single individual).
- operatorOperator, optional
Perturbation operator. Defaults to
NullOperator.- survivor_selSurvivorSelection, optional
Survivor selection method; defaults to
"hill_climb".- paramsdict, optional
Additional parameters stored as schedulable values.
- namestr, optional
Display name (default
"HillClimb").- rngRNGLike, optional
Random number generator.
- **kwargs
Forwarded to
SearchStrategy.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
population_sizeGets the amount of individuals in the population.
- Parameters:
initializer (Initializer)
operator (Optional[Operator])
survivor_sel (Optional[SurvivorSelection])
params (Optional[dict])
name (str)
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(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
- class LocalSearch(initializer, operator=None, survivor_sel=None, name='LocalSearch', iterations=100, rng=None, **kwargs)[source]#
Bases:
PopulationBasedStrategyLocal Search algorithm.
At each iteration the current solution is duplicated iterations times, and every copy is perturbed independently. The best among the original and the perturbed copies survives. By default, the survivor selection is set to
"local_search"(one parent vs. many offspring).- Parameters:
- initializerInitializer
Population initializer.
- operatorOperator, optional
Perturbation operator.
- survivor_selSurvivorSelection, optional
Survivor selection; defaults to
"local_search".- namestr, optional
Display name (default
"LocalSearch").- iterationsint, optional
Number of perturbed copies per iteration (default 100).
- rngRNGLike, optional
Random number generator.
- **kwargs
Forwarded to
SearchStrategy.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
population_sizeGets the amount of individuals in the population.
- Parameters:
initializer (Initializer)
operator (Optional[Operator])
survivor_sel (Optional[SurvivorSelection])
name (str)
iterations (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(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
- class DE(initializer, de_operator_name='DE/best/1', survivor_sel=None, name='DE', rng=None, F=0.8, Cr=0.9, p=0.1, **kwargs)[source]#
Bases:
PopulationBasedStrategyDifferential Evolution algorithm.
Uses a DE mutation operator (e.g.,
"DE/best/1") and one-to-one survivor selection by default. The population size stays constant, and every individual is perturbed each generation.- Parameters:
- initializerInitializer
Population initializer.
- de_operator_namestr, optional
DE variant (default
"DE/best/1").- survivor_selSurvivorSelection, optional
Survivor selection; defaults to one-to-one competition.
- namestr, optional
Display name (default
"DE").- rngRNGLike, optional
Random number generator.
- Ffloat or SchedulableParameter, optional
Scale factor (default 0.8).
- Crfloat or SchedulableParameter, optional
Crossover probability (default 0.9).
- pfloat or SchedulableParameter, optional
Elite fraction for
/pbest/variants (default 0.1).- **kwargs
Forwarded to
StaticPopulation.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
population_sizeGets the amount of individuals in the population.
- Parameters:
initializer (Initializer)
de_operator_name (str)
survivor_sel (Optional[SurvivorSelection])
name (str)
rng (Optional[RNGLike])
F (float | SchedulableParameter)
Cr (float | SchedulableParameter)
p (float | SchedulableParameter)
Methods
reset
- class ES(initializer, mutation_op, crossover_op=None, parent_sel=None, survivor_sel=None, offspring_size=None, name='ES', rng=None, **kwargs)[source]#
Bases:
ShuffledPopulationStrategyEvolution Strategy (μ+λ or μ,λ).
Applies mutation (and optionally crossover) to the selected parents, then selects survivors. By default, no parent selection is performed (all individuals are used).
- Parameters:
- initializerInitializer
Population initializer.
- mutation_opOperator
Mutation operator.
- crossover_opOperator, optional
Crossover operator. If
None, only mutation is applied.- parent_selParentSelection, optional
Parent selection (default: use the whole population).
- survivor_selSurvivorSelection, optional
Survivor selection (default: generational).
- offspring_sizeint, optional
Number of offspring per generation.
- namestr, optional
Display name (default
"ES").- **kwargs
Forwarded to
VariablePopulation.
- Attributes:
- initializer
paramsAccess parameter values by attribute-style lookup.
population_sizeGets the amount of individuals in the population.
- Parameters:
initializer (Initializer)
mutation_op (Operator)
crossover_op (Optional[Operator])
parent_sel (Optional[ParentSelection])
survivor_sel (Optional[SurvivorSelection])
offspring_size (Optional[int])
name (str)
rng (Optional[RNGLike])
Methods
reset
- class GA(initializer, mutation_op, crossover_op, parent_sel, survivor_sel, name='GA', mutation_prob=0.1, crossover_prob=0.9, rng=None, **kwargs)[source]#
Bases:
PopulationBasedStrategyGenetic Algorithm.
Combines crossover (applied with probability crossover_prob) and mutation (applied per individual with probability mutation_prob) via a
BranchOperator. The population size is constant.- Parameters:
- initializerInitializer
Population initializer.
- mutation_opOperator
Mutation operator (will be applied probabilistically).
- crossover_opOperator
Crossover operator (applied pairwise).
- parent_selParentSelection
Parent selection method.
- survivor_selSurvivorSelection
Survivor selection method.
- namestr, optional
Display name (default
"GA").- mutation_probfloat or SchedulableParameter, optional
Individual-level probability of mutation (default 0.1).
- crossover_probfloat or SchedulableParameter, optional
Pair-level probability of crossover (default 0.9). If the crossover operator supports it, this value is injected via
update_kwargs.- rngRNGLike, optional
Random number generator.
- **kwargs
Forwarded to
StaticPopulation.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
population_sizeGets the amount of individuals in the population.
- Parameters:
initializer (Initializer)
mutation_op (Operator)
crossover_op (Operator)
parent_sel (ParentSelection)
survivor_sel (SurvivorSelection)
name (str)
mutation_prob (float | SchedulableParameter)
crossover_prob (float | SchedulableParameter)
rng (Optional[RNGLike])
Methods
reset
- class RandomSearch(initializer, name='RandomSearch', rng=None, **kwargs)[source]#
Bases:
PopulationBasedStrategyRandom search algorithm.
Each iteration replaces the current population with completely new random individuals (via a
random.randomoperator). No perturbation of existing solutions occurs.- Parameters:
- initializerInitializer
Population initializer.
- namestr, optional
Display name (default
"RandomSearch").- **kwargs
Forwarded to
HillClimb.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
population_sizeGets the amount of individuals in the population.
- Parameters:
initializer (Initializer)
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
- class SA(initializer, operator, name='SA', iterations=100, temperature_init=100, alpha=0.99, rng=None, **kwargs)[source]#
Bases:
SingleSolutionStrategySimulated Annealing algorithm.
A single solution is perturbed each iteration. The new solution is accepted if it is better, or probabilistically if it is worse, according to an exponentially decaying temperature schedule.
- Parameters:
- initializerInitializer
Population initializer (usually creates a single individual).
- operatorOperator
Perturbation operator.
- namestr, optional
Display name (default
"SA").- iterationsint or SchedulableParameter, optional
Number of iterations at constant temperature (default 100).
- temperature_initfloat or SchedulableParameter, optional
Starting temperature (default 100).
- alphafloat or SchedulableParameter, optional
Cooling factor (default 0.99).
- rngRNGLike, optional
Random number generator.
- **kwargs
Forwarded to
HillClimb.
- Attributes:
paramsAccess parameter values by attribute-style lookup.
population_sizeGets the amount of individuals in the population.
- temperature
- Parameters:
initializer (Initializer)
operator (Operator)
name (str)
iterations (int | SchedulableParameter)
temperature_init (float | SchedulableParameter)
alpha (float | SchedulableParameter)
rng (Optional[RNGLike])
Methods
reset