Unfit  3.1.1
Data fitting and optimization software
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
Unfit::DifferentialEvolution Class Reference

A class to implement the Differential Evolution optimization method. More...

#include <DifferentialEvolution.hpp>

Inheritance diagram for Unfit::DifferentialEvolution:
Unfit::GenericOptimizer

Public Member Functions

 DifferentialEvolution ()
 
virtual ~DifferentialEvolution ()
 
int FindMin (GenericCostFunction &CostFunction, std::vector< double > &coordinates)
 A method to find the minimum of a model/function using a Differential Evolution approach. More...
 
void Reset ()
 
- Public Member Functions inherited from Unfit::GenericOptimizer
 GenericOptimizer ()
 
virtual ~GenericOptimizer ()
 
void ResetGenericOptimizer ()
 
virtual double GetCost (std::size_t index=0) const noexcept
 
virtual bool GetIsPopulationBased () const noexcept
 
virtual std::size_t GetNumberOfIterations () const noexcept
 
virtual std::size_t GetNumberOfFunctionEvaluations () const noexcept
 
virtual std::vector< std::vector< double > > GetPopulation () const
 
virtual std::vector< double > GetSolution (std::size_t index=0) const
 
virtual void SetPopulation (const std::vector< std::vector< double >> &population)
 

Private Member Functions

std::vector< double > NewPopulationMember (GenericCostFunction &CostFunction, unsigned member)
 
std::vector< double > GenerateTrialMember (unsigned i)
 
int ProcessFindMin (GenericCostFunction &CostFunction)
 

Private Attributes

std::vector< std::vector< double > > new_population_
 
std::vector< double > best_member_
 
unsigned dimensions_
 
unsigned cost_
 

Friends

class TestDifferentialEvolution
 

Additional Inherited Members

- Public Attributes inherited from Unfit::GenericOptimizer
Unfit::Bounds bounds
 
Unfit::Options options
 
- Protected Member Functions inherited from Unfit::GenericOptimizer
virtual bool CalculateCost (GenericCostFunction &CostFunction, std::vector< double > &x)
 
void GeneratePopulation (GenericCostFunction &CostFunction, std::size_t dimensions)
 
void GenerateRandomEngines ()
 
virtual bool IsConverged (const std::vector< double > &best_member, std::size_t truncated_index=0) const
 Checks to see if the population has converged. More...
 
virtual void PrintInitialOutput (double best_cost) const
 
virtual void PrintIterationOutput (double best_cost) const
 
virtual void PrintFinalOutput () const
 
virtual void SortPopulation () noexcept
 
- Protected Attributes inherited from Unfit::GenericOptimizer
std::vector< std::vector< double > > population_
 
std::vector< std::mt19937 > random_engines_
 
std::atomic< std::size_t > function_evaluations_
 
std::atomic< std::size_t > iterations_
 
bool is_population_based_
 

Detailed Description

A class to implement the Differential Evolution optimization method.

This class implements the Differential Evolution optimization method. It requires a cost function (written by the user) and an initial guess of the unknown parameters. The initial guess is only needed to obtain the number of unknowns, so make sure it is of the correct length. The algorithm will proceed to try to find a set of parameters that minimize the cost function.

The idea behind Differential Evolution comes from Kenneth Price and Rainer Storn. Inspiration for this implementation was derived from their work, in particular see: http://www1.icsi.berkeley.edu/~storn/code.html

Constructor & Destructor Documentation

◆ DifferentialEvolution()

Unfit::DifferentialEvolution::DifferentialEvolution ( )

The constructor sets all of the parameters to their default values.

◆ ~DifferentialEvolution()

Unfit::DifferentialEvolution::~DifferentialEvolution ( )
virtualdefault

As we could derive from this class, the destructor should be virtual. In this case the default destructor is just fine as we are putting everything into std::vectors which take care of themselves.

Member Function Documentation

◆ FindMin()

int Unfit::DifferentialEvolution::FindMin ( GenericCostFunction CostFunction,
std::vector< double > &  coordinates 
)
virtual

A method to find the minimum of a model/function using a Differential Evolution approach.

This is the main method to call to perform a minimization of the provided cost function. Here a Differential Evolution optimization technique is adopted. There are a few points worth noting before starting the optimization. First, make sure that the length of the coordinates vector that is passed in to this function matches the number of unknowns you have in your cost function. As the cost function is user supplied there is no way of checking these match up. The values you supply are not important as the initial population is randomly generated. The final result (coordinates with best fit) is returned in the vector. Second, it is advisable to add bounds to each of the variables in the fit. The algorithm generates possible answers between the provided bounds, and if no bounds are provided the bounds are +/- the maximum double the computer can represent. Chances are you know enough about the problem to make more sensible choices. Convergence with +/- 1e8, for example, is still much faster than the default bounds.

Intended use: int rc = object.FindMin(CostFunction, coordinates)

Parameters
CostFunctionThe user-supplied function to minimise
coordinatesOn input must be the same size as the number of parameters in the cost function. On exit returns the unknowns that provided the minimum cost.
Returns
Zero upon success, non-zero upon failure.
-1 = Empty coordinate vector
1 = Maximum number of function evaluations was reached
2 = Maximum number of iterations was reached

Implements Unfit::GenericOptimizer.

◆ GenerateTrialMember()

std::vector< double > Unfit::DifferentialEvolution::GenerateTrialMember ( unsigned  i)
private

This method generates a trial member. Once a trial member has been obtained the trial member is compared to the relevant member of the current population to decide if it is a worthy replacement (done in ProcessFindMin). There are a number of strategies that can be employed to generate a trial member. Following Price & Storn, those implemented here are (in order, see SetStrategy):

  • best/1/exp
  • rand/1/exp
  • rand-to-best/1/exp
  • best/2/exp
  • rand/2/exp
  • best/1/bin
  • rand/1/bin
  • rand-to-best/1/bin
  • best/2/bin
  • rand/2/bin

The leading best or rand or rand-to-best is the base member to which a weighted average of other members is added. The number refers to how many pairs (1 or 2) of population members are used to calculate the increment fo the base member. Finally, the trailing exp or bin refers to an exponential or binary cross over method.

Parameters
ithe index of the population member for which a trial member will be generated
Returns
a vector containing the trial member

◆ NewPopulationMember()

std::vector< double > Unfit::DifferentialEvolution::NewPopulationMember ( GenericCostFunction CostFunction,
unsigned  member 
)
private

This method calls GenerateTrialMember to get a proposed addition to the population. If the proposed member has a better cost it is returned to be added to the population, otherwise the existing member is returned such that this member remains unchanged at this iteration.

Parameters
CostFunctionThe cost function to be optimised
memberThe index of the population member to be chosen
Returns
A vector containing the new population member

◆ ProcessFindMin()

int Unfit::DifferentialEvolution::ProcessFindMin ( GenericCostFunction CostFunction)
private

This method is called by FindMin and contains the main iteration loop. Here is where the actual optimisation takes place, the population is mutated by vector combinations and convergence is determined.

Parameters
CostFunctionReturns the residuals of the model
Returns
Zero upon success, non-zero upon failure
1 = Maximum number of function evaluations was reached
2 = Maximum number of iterations was reached

◆ Reset()

void Unfit::DifferentialEvolution::Reset ( )
virtual

Resets DifferentialEvolution back to its default state. This is equivalent to destroying the object and creating a new one.

Implements Unfit::GenericOptimizer.

Friends And Related Function Documentation

◆ TestDifferentialEvolution

friend class TestDifferentialEvolution
friend

For unit testing purposes only

Member Data Documentation

◆ best_member_

std::vector<double> Unfit::DifferentialEvolution::best_member_
private

A vector that contains the best solution found so far

◆ cost_

unsigned Unfit::DifferentialEvolution::cost_
private

the index of the cost of each vertex

◆ dimensions_

unsigned Unfit::DifferentialEvolution::dimensions_
private

variable to store the number of dimensions

◆ new_population_

std::vector<std::vector<double> > Unfit::DifferentialEvolution::new_population_
private

The next population, constructed then swapped with population_


The documentation for this class was generated from the following files: