Particle Swarm Optimizer Class
Definition
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 9.1.0
public class ParticleSwarmOptimizer : PopulationBasedOptimizer<ParticleSwarmOptimizer.ParticleSwarm, ParticleSwarmOptimizer.Particle>
- Inheritance
- Object → ManagedIterativeAlgorithm<Vector<Double>, Double, OptimizationSolutionReport<Vector<Double>>> → MultidimensionalOptimizer<Vector<Double>, OptimizationSolutionReport<Vector<Double>>> → PopulationBasedOptimizer<Vector<Double>, ParticleSwarmOptimizer.ParticleSwarm, ParticleSwarmOptimizer.Particle> → PopulationBasedOptimizer<ParticleSwarmOptimizer.ParticleSwarm, ParticleSwarmOptimizer.Particle> → ParticleSwarmOptimizer
Remarks
Particle Swarm Optimization (PSO) is a population-based stochastic optimization technique inspired by the social behavior of bird flocking or fish schooling. Each particle in the swarm represents a candidate solution and moves through the search space according to its own experience and the experience of neighboring particles.
Before running the algorithm, you must set the LowerBounds and UpperBounds properties to specify the search space constraints. The ExtremumType property specifies whether a minimum or a maximum of the objective function is desired.
The PSO algorithm maintains a population of particles, each representing a potential solution. Particles adjust their positions based on their own best-known positions and the best-known positions of their neighbors, guided by cognitive and social coefficients.
The optimizer supports various topologies, such as global, ring, random, and Von Neumann, which define the neighborhood relationships between particles. Different topologies can significantly affect the performance of the optimization process.
Key parameters include the constriction factor, cognitive coefficient, social coefficient, and maximum velocity, which control the dynamics of particle movement. The population size is determined by the number of dimensions and a population multiplier.
The optimizer also includes mechanisms to handle stagnation and population diversity, ensuring robust convergence to optimal solutions.
Example
The following example demonstrates how to use the ParticleSwarmOptimizer to find a minimum of the Rastrigin function.
// Define the Rastrigin function
Func<Vector<double>, double> rastrigin = (x) =>
{
int n = x.Length;
double A = 10;
double sum = A * n;
for (int i = 0; i < n; i++)
{
sum += x[i] * x[i] - A * Math.Cos(2 * Math.PI * x[i]);
}
return sum;
};
// Define the lower and upper bounds
Vector<double> lowerBounds = Vector.Create<double>(2, -5.12);
Vector<double> upperBounds = Vector.Create<double>(2, 5.12);
// Create the optimizer
var optimizer = new ParticleSwarmOptimizer(
objectiveFunction: rastrigin,
lowerBounds: lowerBounds,
upperBounds: upperBounds
);
// Run the optimization
optimizer.FindMinimum();
// Get the result
Vector<double> solution = optimizer.Extremum;
double value = optimizer.ValueAtExtremum;
Constructors
Particle | Constructs a new ParticleSwarmOptimizer class. |
Particle | Constructs a new Particle Swarm optimizer with specified parameters. |
Properties
Cognitive | Gets or sets the cognitive coefficient that controls the influence of personal best positions. |
Constriction | Gets or sets the constriction factor that controls the impact of previous velocities. |
Convergence |
Gets the collection of convergence tests for the algorithm.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Dimensions |
Gets or sets the number of dimensions of the optimization problem.
(Inherited from PopulationBasedOptimizer<TPopulation, TCandidate>) |
Diversity |
Gets the convergence test that measures population diversity.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Estimated |
Gets a value indicating the size of the absolute
error of the result.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Evaluations |
Gets the number of evaluations needed to execute the algorithm.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Evaluations |
Gets the number of evaluations still available.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Extremum |
Gets or sets the current best approximation to the extremum.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Extremum |
Gets or sets the type of extremum.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Has |
Gets a value indicating whether the optimizer has constraints.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Has |
Indicates whether the degree of parallelism is a property that is shared
across instances.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Initial |
Gets or sets the initial value for the iteration.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Initialization |
Gets or sets the method used to initialize the population.
(Inherited from PopulationBasedOptimizer<TPopulation, TCandidate>) |
Iterations |
Gets the number of iterations needed by the
algorithm to reach the desired accuracy.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Iterations |
Gets the number of iterations remaining.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Lower |
Gets or sets the vector of lower bounds for the solution.
(Inherited from PopulationBasedOptimizer<TPopulation, TCandidate>) |
Max |
Gets or sets the maximum degree of parallelism enabled by this instance.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Max |
Gets or sets the maximum number of evaluations during the calculation.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Max | Gets or sets the maximum number of iterations
to use when approximating the roots of the target
function.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Max | Gets or sets the maximum velocity allowed for particles. |
Min |
Gets or sets the minimum iterations that have to be performed.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Objective |
Gets or sets the objective function.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Parallel |
Gets or sets the configuration for the parallel behavior of the algorithm.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Population |
Gets the current population of solutions.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Population |
Gets the convergence test that uses the population's coefficient of variation.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Population | Gets or sets the population multiplier used to determine the population size. |
Population |
Gets or sets the population size used in the algorithm.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Random |
Gets or sets the random number generator used by the optimizer.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Result |
Gets the result of an algorithm after it has executed.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Social | Gets or sets the social coefficient that controls the influence of the global best position. |
Solution |
Gets the result of an algorithm after it has executed.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Stagnation | Gets or sets the stagnation limit, which is the number of iterations without improvement before declaring stagnation. |
Status |
Gets the AlgorithmStatus following
an execution of the algorithm.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Throw |
Gets or sets whether to throw an
exception when the algorithm fails to converge.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Topology | Gets or sets the topology of the particle swarm. |
Upper |
Gets or sets the upper bounds for the solution.
(Inherited from PopulationBasedOptimizer<TPopulation, TCandidate>) |
Value |
Gets or sets the current value of the objective function.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Values |
Gets the function values of the current population.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Value |
Gets the SimpleConvergenceTest<T> that uses the value of the target functions.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Methods
Add |
Adds a constraint function g(x) that must satisfy g(x) ≤ 0.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Clear |
Clears all constraint functions.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
Evaluate |
Evaluates the objective function.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object) |
Find |
Searches for an extremum.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Find |
Searches for a maximum.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Find |
Searches for a minimum.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Get |
Gets the total constraint violation for a given solution vector.
(Inherited from PopulationBasedOptimizer<TSolution, TPopulation, TCandidate>) |
Get | Serves as the default hash function. (Inherited from Object) |
Get | Gets the Type of the current instance. (Inherited from Object) |
Increment |
Increments the number of evaluations by one.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Increment |
Increments the number of evaluations by the specified amount.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Initialize |
Initializes the population of candidate solutions.
(Overrides PopulationBasedOptimizer<TPopulation, TCandidate>.InitializePopulation()) |
Iterate |
Performs one iteration of the PSO algorithm.
(Overrides ManagedIterativeAlgorithm<T, TError, TReport>.Iterate()) |
Iterated |
Performs tasks after the iteration is completed, but before
the status of the algorithm is finalized.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Memberwise | Creates a shallow copy of the current Object. (Inherited from Object) |
OnConvergence |
Performs any tasks after the main algorithm has converged.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
OnFailure |
Performs any tasks after the main algorithm has failed to converge.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
OnInit |
Initializes the optimizer before solving begins.
(Overrides MultidimensionalOptimizer<T, TReport>.OnInit()) |
Report |
Records the results of an algorithm in case it fails.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Report |
Records the results of an algorithm.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Report |
Records the results of a algorithm that converged successfully.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Reset |
Resets the number of evaluations to zero.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Restart |
Prepares the algorithm to be run again with possibly different inputs.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Run() |
Runs the algorithm.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Run( |
Runs the algorithm using the specified parallelization options.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Set |
Sets the results of an algorithm's execution.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Set |
Sets the value of the objective function at the extremum to the supplied value.
(Inherited from MultidimensionalOptimizer<T, TReport>) |
Test |
Checks whether the algorithm has converged.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Thread |
Increments the number of evaluations by one.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Thread |
Increments the number of evaluations by the specified amount.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
Throw |
Interprets the AlgorithmStatus and
throws the appropriate exception.
(Inherited from ManagedIterativeAlgorithm<T, TError, TReport>) |
ToString | Returns a string that represents the current object. (Inherited from Object) |