|
Unfit
3.1.1
Data fitting and optimization software
|
A class to implement the Particle Swarm optimization method. More...
#include <ParticleSwarm.hpp>
Public Member Functions | |
| ParticleSwarm () | |
| virtual | ~ParticleSwarm () |
| int | FindMin (GenericCostFunction &CostFunction, std::vector< double > &coordinates) override |
| A method to find the minimum of a model/function using a Particle Swarm approach. 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 Member Functions | |
| int | ProcessFindMin (GenericCostFunction &CostFunction) |
| void | ChaosEnhancement (unsigned enhancement_strategy) |
| std::vector< double > | GenerateTrialParticle (std::size_t member) |
| void | UpdatePopulationMember (GenericCostFunction &CostFunction, std::size_t member) |
Private Attributes | |
| std::vector< double > | best_particle_ |
| std::size_t | dimensions_ |
| std::size_t | cost_ |
Friends | |
| class | TestParticleSwarm |
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 Particle Swarm optimization method.
This class implements the Particle Swarm 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 Particle Swarm comes from Kennedy, Eberhart and Shi. Inspiration for this implementation was derived from the paper entitled "Chaos-enhanced accelerated particle swarm optimization" written by Amir Hossein Gandomi, Gun Jin Yun, Xin-She Yang and Siamak Talatahari.
This class also includes the chaotic maps from the same paper in an effort to further enhance convergence. By default the chaotic maps are not enabled. To use them you need to set ps.options.UseAdaptiveParameters(true). Then you can select which map via ps.options.SetStrategy(x). In total, there are 12 chaotic maps included, selected via indices ranging from 1 to 12.
| Unfit::ParticleSwarm::ParticleSwarm | ( | ) |
The constructor sets all of the parameters to their default values. Here there are three parameters that are set to be different to the default values provided in Options.hpp due to their different usage here. They are:
alpha = 1.0
beta = 0.5
delta = 0.99
|
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.
|
private |
This method updates the beta parameter via one of 12 different chaotic maps, which the user can select via ps.options.SetStrategy(x). Each map generates a random (chaotic) sequence of numbers within the range 0 to 1. The implemented maps and their strategy number are listed below.
1 - Chebyshev Map
2 - Circle Map
3 - Gauss Map
4 - Intermittency Map
5 - Iterative Map
6 - Liebovitch Map
7 - Logistic Map
8 - Piecewise Map
9 - Sine Map
10 - Singer Map
11 - Sinusoidal Map
12 - Tent Map
| enhancement_strategy | The index of the desired chaotic map (1 to 12) |
|
overridevirtual |
A method to find the minimum of a model/function using a Particle Swarm approach.
This is the main method to call to perform a minimization of the provided cost function. Here a Particle Swarm 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 method matches the number of unknowns you expect in your cost function. As the cost function is user supplied there is no way of checking these match. The values you supply are not important as the initial population is randomly generated (unless you invoke the SetAddInitialToPopulation option). The final result (coordinates with best fit) is returned in the coordinates 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. By default the solution can move outside the bounds that are set. To prevent this, invoke the SetUseHardBounds option. Finally, if you want to use an external population generator, you can use that as the initial population instead of generating one here. You can invoke the SetPopulation method (from the GenericOptimizer class) to achieve this.
Intended use: ParticleSwarm ps;
auto rc = ps.FindMin(CostFunction, coordinates);
| CostFunction | The user-supplied function to minimise |
| coordinates | On input must be the same size as the number of parameters required by the cost function. On exit returns the coordinates that provided the minimum cost. |
Implements Unfit::GenericOptimizer.
|
private |
This method generates a trial particle from an existing population member. Given the current particle, P, and the best particle, B, the trial particle is generated via:
trial = (1-beta)*P + beta*B + alpha*P*rand
where rand is a random number that follows a normal distribution with mean 0 and standard deviation 1. You can see that beta scales how much the trial particle moves from P towards the best particle B, and alpha scales the size of the random component.
| member | The population member for which we will create a new trial particle |
|
private |
This method is called by FindMin and contains the main iteration loop. Here is where the actual optimisation takes place. At each iteration a new trial particle is generated for each member of the existing population. If a new particle has a finite cost it replaces the existing particle, otherwise the existing particle is retained. The best particle found so far is also tracked. See the IsConverged documentation from the GenericOptimizer class for convergence information. The iterations will also cease if the maximum number of iterations or cost function evaluations is exceeded. Note that the amplitude of the random component of each trial particle is reduced in each iteration. The alpha parameter scales the random component and at each iteration alpha is updated as alpha*delta (default delta = 0.99).
| CostFunction | Returns the residuals (cost) of the model given a set of parameters |
|
overridevirtual |
Resets Particle Swarm back to its default state. This is equivalent to destroying the object and creating a new one. The values of alpha, beta and delta are set as per the constructor.
Implements Unfit::GenericOptimizer.
|
private |
This method calls GenerateTrialParticle to create a new candidate particle. It then calculates the cost of the trial particle and if the cost is finite then the new particle replaces the existing particle at position 'member' in the population. If the cost of the trial particle is not finite then the existing particle will remain as part of the population.
| CostFunction | The function with which the cost of the new member will be calculated |
| member | The member to be updated |
|
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 |
A variable to store the number of dimensions
1.8.13