|
Unfit
3.1.1
Data fitting and optimization software
|
A class to implement the Differential Evolution optimization method. More...
#include <DifferentialEvolution.hpp>
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_ |
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
| Unfit::DifferentialEvolution::DifferentialEvolution | ( | ) |
The constructor sets all of the parameters to their default values.
|
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.
|
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)
| CostFunction | The user-supplied function to minimise |
| coordinates | On 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. |
Implements Unfit::GenericOptimizer.
|
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):
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.
| i | the index of the population member for which a trial member will be generated |
|
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.
| CostFunction | The cost function to be optimised |
| member | The index of the population member to be chosen |
|
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.
| CostFunction | Returns the residuals of the model |
|
virtual |
Resets DifferentialEvolution back to its default state. This is equivalent to destroying the object and creating a new one.
Implements Unfit::GenericOptimizer.
|
friend |
For unit testing purposes only
|
private |
A vector that contains the best solution found so far
|
private |
the index of the cost of each vertex
|
private |
variable to store the number of dimensions
|
private |
The next population, constructed then swapped with population_
1.8.13