Particle swarm optimization: Difference between revisions

From Cornell University Computational Optimization Open Textbook - Optimization Wiki
Jump to navigation Jump to search
Line 127: Line 127:


===Step 4: Setup Particle Data===
===Step 4: Setup Particle Data===
Fourth
Each particle has three main properties
*Position of particle <math>i \,</math>, at iteration <math>t\,</math>, denoted as <math>X^i (t)\,</math> where <math>X^i (t) = (x^i (t), y^i (t)) \,</math> or <math>\begin{bmatrix} x^i(t) \\ y^i(t) \end{bmatrix}</math>
*Velocity Vector denoted as <math>V^i (t) = (v_x^i(t), v_y^i(t))\,</math> or <math>\begin{bmatrix} V_x^i(t) \\ V_y^i(t) \end{bmatrix}</math>
*Objective Function Value of <math>f(X^i (t))=f(x^i (t),y^i (t))\,</math>
Where <math>P\,</math> equals total number of particles, cumulatively these would be represented as:
<math>X^i (t) = [X^0 (t), \cdots, X^{P-1} (t)] = \begin{bmatrix} x^0(t) & \cdots & x^{P-1}(t) \\ y^0(t) & \cdots & y^{P-1}(t) \end{bmatrix}</math>
<math>V^i (t) = [V^0 (t), \cdots, V^{P-1} (t)] = \begin{bmatrix} V_x^0(t) & \cdots & V_x^{P-1}(t) \\ V_y^0(t) & \cdots & V_y^{P-1}(t) \end{bmatrix}</math>
<math>f(X^i (t))=[f(X^0 (t)),  \cdots , f(X{P-1}] (t))]=[f(x^0 (t),y^0 (t)), \cdots,f(x^{P-1} (t),y^{P-1} (t))\,</math>
Important Note <math>X^i (t)\,</math> only tracks the current iteration of particle <math>i\,</math>, not all values of a particle over <math>m\,</math> iterations: <math>X^7(t) \neq \begin{bmatrix} x^7(0) & \cdots & x^7(m) \\ y^7(0) & \cdots & y^7(m) \end{bmatrix}</math> and <math> V^7(t) \neq \begin{bmatrix} V_x^7(0) & \cdots & V_x^7(m) \\ V_y^7(0) & \cdots & V_y^7(m) \end{bmatrix}</math> and <math> f(X^7(t)) \neq [f(x^7(0),y^7(0), \cdots, f(x^7(m),y^7(m)] </math>
This wiki guide will use randomly generated initialization for  particle position and velocity. There are alternative initialization methods available that are not covered in this guide such as using a set of points in a grid pattern.
For <math> X^i(0) \,</math> and <math> V^i(0) \, </math> , define initialization data for all particles given and define a range to search within:
<math> X^i(t)\,</math>
<math> -3 \le x \le 3 </math>
<math> -3 \le y \le 3 </math>
To ensure an even distribution and less initial clustering of the particles, we will use a uniform distribution to create surrogate data to initialize the position. Depending on how the code is set up, scaling and offset factors may be required.
We will use a normal distribution to create surrogate data to initialize the Velocity vector. Depending how the code is set up, a scaling factor may be required.
Example Particle Data, Particle # 7:
*Particle Position Coordinates at time (<math> t </math>) :
*Particle Velocity Vector at time (<math> t </math>) :
*Particle Objective Value at time (<math> t </math>) :


===Step 5: Evaluate at Initialization Points===
===Step 5: Evaluate at Initialization Points===

Revision as of 01:15, 12 December 2024

Author: David Schluneker (dms565), Thomas Ploetz (tep52), Michael Sorensen (mds385), Amrith Kumaar (ak836), Andrew Duffy (ajd296) (ChemE 6800 Fall 2024)

Stewards: Nathan Preuss, Wei-Han Chen, Tianqi Xiao, Guoqing Hu


Introduction

Particle Swarm Optimization (PSO) is inspired by nature and groups or swarms of natural creatures. It uses multiple “particles” distributed across a solution space to slowly converge upon a global, optimized solution. This algorithm does not require calculation of the function’s gradient or slope, instead using simple weights and the contribution of the rest of the swarm to provide a “fitness” value [1] to leading to a technique dependent only on simple calculations and a number of starting points.

The PSO algorithm was formulated and initially presented by Kennedy and Eberhart specifically to find solutions to discontinuous, but non-differentiable functions. The concept was presented at the IEEE conference on neural networks in 1995 [2]. The pair later published a book Swarm Intelligence in 2001 that further expanded on the concept [3]. Others have summarized the algorithm [4] [5] [6]. This algorithm does not leverage formal methodology or mathematical rigor to identify an ideal point but instead uses a heuristic approach to explore a wide range of starting locations and conditions. These features allow the PSO algorithm to efficiently optimize complex functions.

The biological analogy for a PSO algorithm is a flock of birds, a school of fish, or a colony of ants trying to find a food resource. Each animal, or particle, is an independent entity that is conducting its own search routine for an optimal result. However, the particles will also be influenced and guided by motions of the larger swarm. The results any particle has already seen, as well as the influence of the larger swarm helps all the particles converge to an optimal solution.

In a more technical dissection, a PSO algorithm must be initially set-up with how many particles and iterations the user wants to use. More particles improve the convergence likelihood and help ensure the global optimum can be found due to a more complete search at the cost of computing resources required. The PSO algorithm allows a user to set different “weights”, step sizes, and speeds for the particles to control how influenced each particle is by its past points (cognitive) and by the other nearby points (social) in the swarm and how large of steps will be taken. These weights allow algorithm adjustment to tailor the performance and computational requirements.

The goals of studying this topic are to learn more about an algorithm capable of solving nondifferentiable functions that are relevant to modern problems. Many optimization algorithms use the gradient function liberally, so it is interesting to see an algorithm that does not require gradient computations. Additionally, the algorithm is still in active use in many fields [15], [24]. PSO is regularly used to solve optimization problems in the gas and oil industry, antenna design [25], and electric power distribution [10]. As functions get very complex in the real-world, the PSO algorithm is still capable of performing an effective search. Relevance to modern optimization problems make PSO an interesting research area.

Algorithm Discussion

The structure of the algorithm is simple. The algorithm seeks to find an optimum of an objective function, f(x). A set of particles, X with members x X , forming a swarm, is initialized randomly. The algorithm runs for a number of iterations. At each iteration, for each particle in the swarm, each particle’s position is updated to be x i+1, p = xi, p + vi, p. Subscript i indicates the iteration of the algorithm. Subscript p indicates the index of the particle in the swarm. Both the optimal solution for a particular particle, xbest, p, and a global optimum, xbest, g are tracked. f(xbest, p) is the best solution to the objective function that a particular particle, p, has experienced over all iterations. f(xbest, g ) is the best solution to the objective function for all particles, at all iterations. The algorithm is halted when either a fixed number of iterations is reached or a stopping criteria is reached. A typical stopping criteria is when the change between iterations is below a user-set threshold, demonstrating that the particles are not moving from the identified solution point. The algorithm’s control flow is shown in the following flow chart.

Figure 1: Algorithm Flow Chart A velocity, vi, p is independently computed for each particle xi,p. The velocity is the result of the sum of the preceding velocity, a cognitive component and a social component. The cognitive component is rand() *wc* (xbest, p - xi, d) and pulls the solution towards the best point observed by this particle. The social component is rand() *ws* (xbest, g - xi, d) and pulls the solution towards the best point observed across the entire swarm. Each of the social component and cognitive component are weighed by a factor. The state that must be tracked across loop iterations is; xi,p, xbest, p, vi, p for each particle and xbest, g.

The convergence of the algorithm can be checked a few different ways. The original papers were based on training neural networks, so the optimization was halted when the neural network was sufficiently able to make predictions [18, 19]. This approach is flawed because the designer of the algorithm used a priori knowledge of how good the neural network will perform. More generally, this approach incorporates application specific logic to stop when the optimal solution is good enough. Alternatively, the algorithm can be terminated following a predetermined number of iterations. Another approach is to stop when the solution's improvement at a given iteration stops [20].

The pseudocode for the PSO algorithm attempting to minimize a solution follows. The algorithm can be modified to search for a maximum by changing the initialization of xbest, p and xbest, galong with their updates. SWARM is initialized randomly vparticle initialized to 0 xbest, p initialized xbest, g initialized to For i in MAX_ITERATIONS: For particle in SWARM: cognitive := rand() *wc * (xbest, p- Xid) social := rand() *ws* (xbest, g - Xid) vparticle = vparticle + cognitive + social xi+1 = xi + vi if f(xi+1) < f(xbest, p) xbest, p= xi+1 if f(xi+1) < f(Pig ) xbest, g= xi+1


PRINT “Local optimal at xbest, g”


Figure 2: Algorithm Pseudocode

The algorithm has a few hyper-parameters including; number of particles in the swarm, weights of the cognitive update on the velocity, weights on the social update on the velocity. The Pymoo Python implementation suggests a swarm size of 25, velocity weight of 0.8, a social update weight of 2.0 and a cognitive update of 2.0 [33]. Practitioners will need to adjust parameters and determine how their problem reacts to modifications of the hyper-parameters. Heuristics may be used to adjust the various weights between iterations of the algorithm. Algorithms that adjust the hyper parameters based on heuristics are called Adaptive Particle Swarm Optimization (APSO) [36].

PSO makes very few assumptions. The algorithm can be trivially adopted to work with real, integer or mixed functions. The algorithm does not rely on gradients or hessians, so there are no assumptions or requirements on the function being continuous or differentiable. However, the lack of assumptions comes at a cost, there is no proof that the algorithm will find an optimal solution. One of the original papers by Kennedy in 1997 [19] indicated a tendency for the optimization to get stuck in local minimum. Adjustment of the algorithm’s hyper-parameters is a performance vs robustness trade that may have avoided sticking in a local optimum.


Numerical Example

This section will walk through a numerical example of solving a complex objective function using Particle Swarm Optimization.

Given a non-convex function with multiple local optima:

Step 1: Define the Objective Function

Objective Function:

Step 2: Define Key Variables

Symbol Description Value Additional Notes
P Total Number of Particles used to search the function 25 Larger number means greater chance of finding the global optimum, but larger computational load
random number between 0 and 1 (0,1) to be randomly generated at each evaluation
random number between 0 and 1 (0,1) to be randomly generated at each evaluation
Inertia Weight Constant (0,1) 0.7
Cognitive Coefficient

(Independence, prioritize the self)

0.1* recommend & sufficiently low value for slower smoother operation
Social Coefficient

(Codependence, prioritize the group)

0.15* recommend & sufficiently low value for slower smoother operation


  • The algorithm is highly sensitive to c1 and c2. Recommend referencing the search space and using 1/30 to 1/60 of the total range. Ex: target search range is -3 to 3 by -3 to 3, use a value 0.1 to 0.2 to prevent “overshooting” and oscillations of the individual particles.

Step 3: Define Stopping Criteria

For this example we are choosing a set number of iterations (t):

It is important to note that there are alternative stopping criteria such as checking for convergence of the particles - such as a minimum delta between points or using a Confidence Interval on the X and Y coordinates as your threshold. This could lead to more, or less, iterations depending upon what threshold is used. Care should be taken to ensure the stopping criteria can actually be met. This wiki does not provide an example of this methodology at this point in time.

Step 4: Setup Particle Data

Each particle has three main properties

  • Position of particle , at iteration , denoted as where or
  • Velocity Vector denoted as or
  • Objective Function Value of

Where equals total number of particles, cumulatively these would be represented as:

Important Note only tracks the current iteration of particle , not all values of a particle over iterations: and and

This wiki guide will use randomly generated initialization for particle position and velocity. There are alternative initialization methods available that are not covered in this guide such as using a set of points in a grid pattern.

For and , define initialization data for all particles given and define a range to search within:

To ensure an even distribution and less initial clustering of the particles, we will use a uniform distribution to create surrogate data to initialize the position. Depending on how the code is set up, scaling and offset factors may be required.

We will use a normal distribution to create surrogate data to initialize the Velocity vector. Depending how the code is set up, a scaling factor may be required.

Example Particle Data, Particle # 7:

  • Particle Position Coordinates at time () :
  • Particle Velocity Vector at time () :
  • Particle Objective Value at time () :

Step 5: Evaluate at Initialization Points

Fifth

Step 6: Calculate Next Iteration Position, Velocity, Value

Sixth

Step 7: Iteratively Solve

Calculate next optima, position, velocity, and function values for each particle. Repeat until the stopping criteria has been met.


Applications

The strength of PSO is in its simplicity and ability to handle arbitrary functions. The algorithm does not use gradient information, making it useful in contexts where computation of the gradient is expensive or the function is otherwise unreliable.

PSO was originally used to train neural networks (NN). Neural networks frequently have highly non-linear loss functions and it is therefore difficult to find a global optimum. [21]. Several papers have noted an improvement in NN accuracy using PSO optimization when compared to back-propagation due to the ability to avoid local optimum [26]. However, the current state of the current practice for training deep neural networks remains the back-propagation algorithm. Back-propagation is more efficient in the context of neural networks because for each particle at each iteration, there will need to be an error function computation. For example, a swarm size of 25 requires 25 times more error function evaluations [31].

In addition to identifying weights in the Neural Networks (training), PSO has been used in selecting hyper-parameters for neural networks. In these applications PSO is used to maximize the neural network’s accuracy by varying parameters including number of nodes and structure of the networks [28]. PSO fits this application well because this function is nonlinear and the gradient is non-continuous. Hyper parameters for NNs may be real, integer or binary values.

PSO is most often compared to other heuristic based algorithms such as; genetic algorithms differential evolution simulated annealing. One study for a bus scheduling application did find the PSO was better than genetic algorithms with respect to the optimality of solution and run-time [31]. The results in the paper are difficult to generalize, as results will be sensitive to the application as well as the algorithm’s hyper-parameters. The recommended choice of heuristic optimization algorithms remains up to the practitioner’s judgement.

A number of different implementations are available as libraries. Pyswarms is a Python API [32], Pymoo is an alternative Python implementation [33]. Pyswarms is a compact implementation specifically focused on PSO, whereas Pymoo is a more complete library with multiple objective function implementations. Matlab does have PSO implementation as part of the optimization toolbox [34] There is a 3rd party Matlab implementation available for free [35]. However, the algorithm is simple so many prefer to implement the algorithm directly.


Conclusions

The PSO algorithm is a heuristic based optimization algorithm. The heuristics are loosely based on the behavior of a flock of seagulls. The algorithm’s strengths are in its simplicity, lack of reliance on a gradient, and robustness against getting stuck in local optimum. The drawback is that the “swarm” requires a large number of function evaluations, making the algorithm slow. Additionally, the algorithm has no guarantee of convergence. The ability to find an optimum is sensitive to starting conditions and hyper-parameters.

The algorithm is easy to get up and running with several public implementations available in the Python and Matlab programming languages. Alternatively, the algorithm is so simple that some will implement the algorithm themselves.

When using the algorithm, practitioners need to select several weights that control the algorithms. A cognitive weight determines to what extent a particle’s previous optimum affects future searches. A social weight determines to what extent all particle’s global optimum affects future searches. These parameters and the swarm size need to be adjusted carefully to balance robustness against computational efficiency.

The PSO algorithm continues to be developed. There is research into how to adjust the algorithm’s hyper parameters at various iterations of the algorithm. These algorithms are called adaptive particle swarm optimization (APSO) and take some guesswork the algorithm. The algorithm has also been combined with genetic optimization algorithms, though the algorithms remain heuristic based and do not guarantee optimum results. \cite{ElStat2}

PSO continues to be used in applications that are non-linear or are non-differentiable. Recent (2024) studies have used PSO for RF in optimizing the placement of terminals [29] and for predicting landslides [30].


References

  1. Vyas, Ajay Kumar (2022) Artificial Intelligence for Renewable Energy Systems, John Wiley & Sons
  2. Kennedy, J. and Eberhart, R. (1995) Particle Swarm Optimization, Proceedings of the IEEE International Conference on Neural Networks
  3. Kennedy, J. and Eberhart, R. (2001) Swarm Intelligence, Academic Press
  4. Rao, Singiresu (2020) Engineering Optimization - Theory and Practice (5th Edition), John Wiley and Sons
  5. Belyadi, Hoss and Haghighat, Alizera (2021) Machine Learning Guides for Oil and Gas Using Python - A Step-by-Step Breakdown with Data, Algorithms, Codes, and Applications, Elsevier
  6. Hassan, Rania (2004) Particle Swarm Optimization: Methods and Applications, https://dspace.mit.edu/bitstream/handle/1721.1/68163/16-888-spring-2004/contents/lecture-notes/l13_msdo_pso.pdf