Implements the Differential Evolution metaheuristic algorithm.
More...
#include <DifferentialEvolution.h>
|
|
| DifferentialEvolution ()=default |
| | Default constructor (implicitly deleted for static utility class).
|
|
| ~DifferentialEvolution ()=default |
| | Default destructor.
|
|
| static std::unique_ptr< Mutation > | createMutation (const std::string &name) |
| | Creates a mutation strategy object by name.
|
| static std::unique_ptr< Crossover > | createCrossover (const std::string &name) |
| | Creates a crossover strategy object by name.
|
| static void | crossover (std::vector< double > &target, const std::vector< double > &mutant, double cr, MersenneTwister &mt) |
| | Performs binomial crossover between target and mutant vectors.
|
| template<Evaluable Problem> |
| static OptResult | optimize (Problem &problem, size_t popSize, double f, double cr, size_t maxGenerations, unsigned long seed, std::string &mutationStrategy, std::string &crossoverStrategy) |
| | Runs the Differential Evolution optimization algorithm.
|
Implements the Differential Evolution metaheuristic algorithm.
DifferentialEvolution provides a genetic algorithm-inspired approach to continuous optimization. The algorithm maintains a population of candidate solutions and iteratively evolves them through mutation and crossover operations guided by fitness-based selection.
Key features:
- Support for multiple mutation strategies (rand1, rand2, best1, best2, randToBest1)
- Support for multiple crossover strategies (binomial, exponential)
- OpenMP parallelization for population-level operations
- Template-based design supporting arbitrary Evaluable problem types
- Deterministic seeding for reproducible results
- Note
- All methods are static; this class serves as a namespace for DE-related functionality.
◆ createCrossover()
| std::unique_ptr< Crossover > DifferentialEvolution::createCrossover |
( |
const std::string & | name | ) |
|
|
inlinestatic |
Creates a crossover strategy object by name.
Supported crossover strategies:
- "bin": Binomial (uniform) crossover - each parameter inherited independently
- "exp": Exponential crossover - contiguous parameter blocks inherited
- Parameters
-
| name | String identifier for the crossover strategy |
- Returns
- std::unique_ptr<Crossover> Heap-allocated crossover strategy object
- Exceptions
-
| std::runtime_error | if the strategy name is not recognized |
◆ createMutation()
| std::unique_ptr< Mutation > DifferentialEvolution::createMutation |
( |
const std::string & | name | ) |
|
|
inlinestatic |
Creates a mutation strategy object by name.
Supported mutation strategies:
- "rand1": DE/rand/1 - Random base with one difference vector
- "rand2": DE/rand/2 - Random base with two difference vectors
- "best1": DE/best/1 - Best base with one difference vector
- "best2": DE/best/2 - Best base with two difference vectors
- "randToBest1": DE/rand-to-best/1 - Interpolation between rand and best
- Parameters
-
| name | String identifier for the mutation strategy |
- Returns
- std::unique_ptr<Mutation> Heap-allocated mutation strategy object
- Exceptions
-
| std::runtime_error | if the strategy name is not recognized |
◆ crossover()
| void DifferentialEvolution::crossover |
( |
std::vector< double > & | target, |
|
|
const std::vector< double > & | mutant, |
|
|
double | cr, |
|
|
MersenneTwister & | mt ) |
|
inlinestatic |
Performs binomial crossover between target and mutant vectors.
In binomial crossover, each parameter of the trial vector is inherited from the mutant vector with probability cr, or from the target vector with probability (1 - cr). At least one parameter is always inherited from the mutant to ensure diversity.
- Parameters
-
| [in,out] | target | The target (parent) vector to be modified in-place |
| [in] | mutant | The mutant vector used for crossover |
| [in] | cr | Crossover rate in range [0, 1]; controls inheritance probability |
| [in,out] | mt | Mersenne Twister random number generator |
- Postcondition
target is modified with parameters from mutant according to cr
◆ optimize()
| OptResult DifferentialEvolution::optimize |
( |
Problem & | problem, |
|
|
size_t | popSize, |
|
|
double | f, |
|
|
double | cr, |
|
|
size_t | maxGenerations, |
|
|
unsigned long | seed, |
|
|
std::string & | mutationStrategy, |
|
|
std::string & | crossoverStrategy ) |
|
static |
Runs the Differential Evolution optimization algorithm.
Executes the complete DE optimization loop:
- Initializes a population using the problem's initial solution generator
- Evaluates initial population fitness
- For each generation:
- Applies mutation strategy to generate mutant vectors
- Applies crossover strategy to generate trial vectors
- Evaluates trial vector fitness
- Selects survivors (greedy selection based on fitness)
- Tracks global best solution
- Returns best solution found and convergence history
Thread Safety:
- Population evaluation is parallelized with OpenMP
- Global best tracking uses critical sections to prevent data races
- Each thread maintains thread-local best fitness to minimize synchronization
- Template Parameters
-
| Problem | Must satisfy the Evaluable concept with methods:
- getInitialSolutions(std::vector<std::vector<double>>&)
- evaluateSolution(const std::vector<double>&) -> double
- getLowerBounds() -> double
- getUpperBounds() -> double
|
- Parameters
-
| [in,out] | problem | The optimization problem object |
| [in] | popSize | Population size; must be > 0 |
| [in] | f | Differential weight (mutation scaling factor) in range (0, 2] |
| [in] | cr | Crossover rate in range [0, 1] |
| [in] | maxGenerations | Maximum number of generations to evolve |
| [in] | seed | Seed value for random number generation; enables reproducibility |
| [in,out] | mutationStrategy | Name of mutation strategy; defaults to "rand1" if empty |
| [in,out] | crossoverStrategy | Name of crossover strategy; defaults to "bin" if empty |
- Returns
- OptResult struct containing:
- bestSolution: best vector found
- bestFitness: fitness value of best solution
- bestFitnesses: convergence history (best fitness per generation)
- Note
- RNG seeding ensures deterministic results: distinct seeds are used for mutation and crossover at each (generation, individual) pair to prevent state contamination while maintaining reproducibility.
-
Returns empty OptResult if popSize == 0.
The documentation for this class was generated from the following file: