Introduction
As we have discussed the difference between the conventional optimization algorithms and the unconventional algorithms are summarized below.
1.The conventional algorithms start from a single initial feasible solution where as the unconventional algorithms start with a set of initial(feasibility not a prerequisite) solutions.
2.The conventional algorithms need the function to be continuously differentiable through out the range of search.
3.The accuracy of the conventional algorithms depending on the selection of initial solution.Always a local solution is assured. The performance of the unconventional algorithms will vary for every run. A solution is assured.It may be local or global.Very rarely diverges.
Particle Swarm Optimization(PSO)
This PSO algorithm also one of the important unconventional optimization algorithms.PSO optimizes a problem by having a population of candidate solutions, here dubbed particles, and moving these particles around in the search-space according to simple mathematical formulae. The movements of the particles are guided by the best found positions in the search-space which are updated as better positions are found by the particles.
PSO is originally attributed to Kennedy, Eberhart and Shi and was first intended for simulating social behaviour. The algorithm was simplified and it was observed to be performing optimization. The book by Kennedy and Eberhart describes many philosophical aspects of PSO and swarm intelligence.
As I am more interested in the implementation of this algorithm interested readers can get more details in the web regrading this algorithm.To implement PSO you neeed not know anything about the algorithm,you should know how to use the code to solve your modelled problem.
PSO Code in MATLAB
There are so many variants of this PSO the code which I found simple and powerful is the vectorised PSO Toolbox by Mr Brian Birge.I thank him for the excellent toolbox.It can be downloaded from the link.
Just download it and unzip it as a folder.You can either add it to MATLAB path or make it as the default folder.
Solving an Optimization problem by PSO
For implementing most of the unconventional algorithms(including the PSO) follow the steps.
1. Model the problem as unconstrained minimization problem( you can refer my earlier post on MATLAB GA toolbox ).
2. Write it as a function file.There are two types of functiones (i) scalar function(ii)vector function
In the scalar function for for every value of X the corresponding function value is returned.
X1-----------F(X1)
In the vector function for for every set of X values the corresponding function values are returned.
[X1 X2 X3 X4...Xn]-------------[F(X1),F(X2),F(X3),F(X4)...F(Xn)]
The code by Mr Brian Birge requires a vector function file .
3.After writing the function file save it in the same PSO folder and change it as default.
The toolbox has lot of details regarding the setting..
4.Run it .You get the solution as well the progressive graphs( iteration vs best solution and movement of particles in two dimension) .
Implementation
The same problem discussed in GA tool box is considered. Same way two files have to be written the first one is the pso setting file(test1.m) .This calls the subroutine vector function file ex2.m.
program 1 test1.m
clear;clc
% lower limir
l=[0 0];
% upper limit
u=[ 10 10];
ran=[l' u'];
% number of variables
n=2;
% settings for pso
%1. population,2. no of iterations,3 refresh on screen 4&5 type of PSO 06&7
%are final and initial swarm velocity 8. final iteration to reach final
%velocity 9. expected function value
Pdef = [20 200 10 2 2 0.9 0.4 200 50 5000 NaN 0 0];
[OUT]=pso_Trelea_vectorized('ex2',n,1,ran,0,Pdef);
out=abs(OUT)
P=out(1:n)
[F x]=ex2(P')
Program 2 vector function file ex2.m
function [F x]=ex2(x);
F=sum((x.*x).').'+1000*((sum(x')-10))^2;
Results
PSO: 1/200 iterations, GBest = 100.
PSO: 20/200 iterations, GBest = 52.210493585432118.
PSO: 40/200 iterations, GBest = 50.166913798388642.
PSO: 60/200 iterations, GBest = 49.997052264769565.
PSO: 80/200 iterations, GBest = 49.978589042793075.
PSO: 100/200 iterations, GBest = 49.978455656591841.
PSO: 120/200 iterations, GBest = 49.975851601760063.
PSO: 140/200 iterations, GBest = 49.975342456412399.
PSO: 160/200 iterations, GBest = 49.975036994685418.
PSO: 180/200 iterations, GBest = 49.97501323855063.
PSO: 200/200 iterations, GBest = 49.975012959884864.
out =
4.99798359369474
4.99701881234046
49.97501295916326
P =
4.99798359369474
4.99701881234046
F =
49.97501295916326
x =
4.99798359369474 4.99701881234046
You are welcome to make some suggestions .All the best.