OptiPy
Loading...
Searching...
No Matches
DifferentialEvolution Class Reference

Implements the Differential Evolution metaheuristic algorithm. More...

#include <DifferentialEvolution.h>

Public Member Functions

 DifferentialEvolution ()=default
 Default constructor (implicitly deleted for static utility class).
 ~DifferentialEvolution ()=default
 Default destructor.

Static Public Member Functions

Factory Methods
static std::unique_ptr< MutationcreateMutation (const std::string &name)
 Creates a mutation strategy object by name.
static std::unique_ptr< CrossovercreateCrossover (const std::string &name)
 Creates a crossover strategy object by name.
Core Optimization
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.

Detailed Description

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.

Member Function Documentation

◆ 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
nameString identifier for the crossover strategy
Returns
std::unique_ptr<Crossover> Heap-allocated crossover strategy object
Exceptions
std::runtime_errorif 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
nameString identifier for the mutation strategy
Returns
std::unique_ptr<Mutation> Heap-allocated mutation strategy object
Exceptions
std::runtime_errorif 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]targetThe target (parent) vector to be modified in-place
[in]mutantThe mutant vector used for crossover
[in]crCrossover rate in range [0, 1]; controls inheritance probability
[in,out]mtMersenne Twister random number generator
Postcondition
target is modified with parameters from mutant according to cr

◆ optimize()

template<Evaluable Problem>
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:

  1. Initializes a population using the problem's initial solution generator
  2. Evaluates initial population fitness
  3. 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
  4. 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
ProblemMust 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]problemThe optimization problem object
[in]popSizePopulation size; must be > 0
[in]fDifferential weight (mutation scaling factor) in range (0, 2]
[in]crCrossover rate in range [0, 1]
[in]maxGenerationsMaximum number of generations to evolve
[in]seedSeed value for random number generation; enables reproducibility
[in,out]mutationStrategyName of mutation strategy; defaults to "rand1" if empty
[in,out]crossoverStrategyName 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: