|
Unfit
3.1.1
Data fitting and optimization software
|
A class to implement the NelderMead optimization method. More...
#include <NelderMead.hpp>
Public Member Functions | |
| NelderMead () | |
| virtual | ~NelderMead () |
| int | FindMin (GenericCostFunction &CostFunction, std::vector< double > &coordinates) override |
| Implements the Nelder-Mead optimization method. More... | |
| void | Reset () override |
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 Types | |
| enum | Operation { Reflected, Expanded, ContractedIn, ContractedOut, Shrunk, Restarted } |
Private Member Functions | |
| int | GeneratePopulation (GenericCostFunction &CostFunction, const std::vector< double > &initial_point) |
| Generate the initial simplex by accepting a vector which contains the initial guess and the CostFunction to compute costs. More... | |
| void | ComputeCentroid () |
| bool | ContractInside (GenericCostFunction &CostFunction) |
| bool | ContractOutside (GenericCostFunction &CostFunction) |
| bool | Reflect (GenericCostFunction &CostFunction) |
| bool | Expand (GenericCostFunction &CostFunction) |
| bool | Shrink (GenericCostFunction &CostFunction) |
| bool | IsDegenerate () |
| void | InitialiseVectors () |
| void | UseAdaptiveParameters () |
| void | PrintInitialOutput (double best_cost) const override |
| void | PrintIterationOutput (double best_cost) const override |
| int | ProcessFindMin (GenericCostFunction &CostFunction) |
| int | RegeneratePopulation (GenericCostFunction &CostFunction) |
Private Attributes | |
| std::vector< double > | contract_ |
| std::vector< double > | centroid_ |
| std::vector< double > | reflect_ |
| std::vector< double > | expand_ |
| std::size_t | dimensions_ |
| std::size_t | best_vertex_ |
| std::size_t | worst_vertex_ |
| std::size_t | next_worst_vertex_ |
| std::size_t | cost_ |
| double | restart_best_cost_ |
| enum Operation | process_ |
Friends | |
| class | TestNelderMead |
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 | 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 NelderMead optimization method.
This class implements the NelderMead optimization method. It requires a cost function (written by the user) and an initial guess of the unknown parameters. It then proceeds to try to find a set of parameters that minimize the cost function.
The inspiration for this implementation was derived from the paper: Implementing the Nelder-Mead simplex algorithm with adaptive parameters, Journal of Computational Optimization and Applications, Fuchang Gao & Lixing Han, 51(1):259-277, 2012
|
private |
Operation contains the various operations that are possible at each iteration of the NelderMead algorithm.
| Unfit::NelderMead::NelderMead | ( | ) |
The constructor sets all of the parameters to their default values.
|
inlinevirtual |
As we are deriving from this class, the destructor should be virtual. In this case an empty destructor is just fine as we are putting everything into std::vectors which take care of themselves.
|
private |
Compute the centroid of n good vertices without computing the cost function by averaging the positions of all of the vertices except the vertex with the worst cost.
|
private |
ContractInside is called by ProcessFindMin. As the name suggests, it performs an inside contraction, which creates a new candidate point for the simplex between the worst point and the centroid. The distance is controlled by the gamma parameter.
contractin = centroid - gamma*(reflect - centroid)
| CostFunction | (input) the function to be implemented |
|
private |
ContractOutside is called by ProcessFindMin. As the name suggests, it performs an outside contraction, which creates a new candidate point for the simplex between the reflect point and the centroid. The distance is controlled by the gamma parameter.
contractout = centroid + gamma*(reflect - centroid_[i])
| CostFunction | (input) the function to be implemented |
|
private |
Computes the expanded coordinates and their cost function based on beta
expand = centroid + beta*(reflect - centroid)
| CostFunction | (input) the function specified by the user |
|
overridevirtual |
Implements the Nelder-Mead optimization method.
This method takes in a cost function (user supplied) and an initial guess for the unknown parameters and uses the Nelder-Mead simplex algorithm to try to find the set of parameters that provides a minimum cost. This is implemented as a nonlinear least squares problem, so the cost will always be >= 0. This is known as a local optimizer, so the results you get will depend on how good the initial guess is.
Intended use: auto rc = object.FindMin(CostFunction, coordinates)
| CostFunction | A user supplied cost function that returns a vector of residuals. |
| coordinates | A vector that contains the initial guess (coordinates) of the unknown parameters. |
Implements Unfit::GenericOptimizer.
|
private |
Generate the initial simplex by accepting a vector which contains the initial guess and the CostFunction to compute costs.
The initial simplex is generated following L.Pfeffer at Stanford. The initial simplex is scaled with respect to the characteristic lengths of the problem. The scaling is defined with respect to the initial point. The method proceeds by defining the scale. Scale : non-zero scale : 0.05 zero scale : 0.00025
The first vertex is always the initial guess.
For the subsequent vertices, we examine ith coordinate and if nth coordinate is zero, scale it up by zero scale while keeping the other coordinates constant. The new set of coordinates will be the nth vertex.
If nth coordinate is non-zero, scale the value up by non-zero scale while keeping the other coordinates constant. The new set of coordinates will be the nth vertex.
If the coordinates cross a bound, the algorithm tries to find a valid simplex if it can. If the cost of any of the vertices in a geometrically valid simplex are NAN or INF then the algorithm is considered to have failed.
NOTES: Vertices of the initial simplex are not sorted under this function. Generated simplex is stored in the 2D vector vertices_
| CostFunction | (input) the function to be implemented |
| initial_point | (input) the initial guess point for the FindMin function |
|
private |
Initialise the working vectors vertices_, contract_, centroid_, reflect_, expand_ to size depending on the dimension of the problem.
|
private |
Check if the vertices in the simplex have become colinear. If they have the simplex is considered degenerate.
|
overrideprivatevirtual |
Prints the column headers relating to iteration by iteration output to the screen if the output level has been set to be >= 1.
| best_cost | The best cost after the initial setup |
Reimplemented from Unfit::GenericOptimizer.
|
overrideprivatevirtual |
At each iteration, prints nothing if the output level is zero, the iteration number if the output level is one, and the iteration number plus the number of function evaluations and the best cost if the output level is greater than one, along with the operation from the last iteration.
| best_cost | The best cost after the latest iteration |
Reimplemented from Unfit::GenericOptimizer.
|
private |
Here is the main implementation of the Nelder Mead simplex algorithm to which the FindMin methods are a front-end.
| CostFunction | (input) a user function |
|
private |
Compute the centroid and the reflection point and evaluate the value of R based on alpha (alpha>0). This computes the centroid of best and good vertices, and get the reflected point with respect to the worst vertex and alpha.
reflect = centroid_ + alpha*(centroid_ - worst)
| CostFunction | (input) the function to be implemented |
|
private |
Creates a new simplex around the best vertex by making a call to GeneratePopulation internally.
| CostFunction | (input) the function to be implemented |
|
overridevirtual |
Resets the variables to default values of NelderMead. Equivalent to destroying the object and creating a new one.
Implements Unfit::GenericOptimizer.
|
private |
Shrink shifts Good and Worst vertices towards Best vertex It shifts all the vertices except the Best vertex, based on delta
| CostFunction | (input) the function to be implemented |
|
private |
Set the parameters to be used based on the number of dimensions
|
friend |
For unit testing purposes only
|
private |
The index of the best vertex
|
private |
Vector to store the centroid
|
private |
Vector to store the contracted point
|
private |
The index of the cost of each vertex
|
private |
Variable to store the number of dimensions
|
private |
Vector to store the expanded point
|
private |
The index of the next-to-worst vertex
|
private |
Enum data type to store the operation executed
|
private |
Vector to store the reflected point
|
private |
Variable to store the best cost before a restart
|
private |
The index of the worst vertex
1.8.13