MATLAB implementation of particle swarm optimization algorithm (PSO)

Source: Internet
Author: User

Particle swarm optimization (PSO) algorithm is a group search algorithm which simulates the social behavior of avian groups. It is divided into the global best particle optimization and local best particle optimization, for the global best PSO, or called gbest PSO, each particle neighborhood is the entire group, the algorithm pseudo-code is as follows:

Create and initialize an n-dimensional particle swarm

Repeat

For each particle i=1,2,... n Do

Set individual best positions

If f (i) <y then

Y=f (i);

End

Set the global best position

If Y<y Then

Y=y;

End

End

For each particle i=1,2,... n Do

Update speed with velocity equation

Update position with positional equation

End

Until meet termination conditions

The MATLAB implementation code for Gbest PSO is as follows:

Tic% The function indicates that the timing starts

%------Initial formatting--------------------------------------------------

Clear all;

CLC

Format long;

%------given initialization conditions----------------------------------------------

c1=1.4962; % acceleration constant i.e. learning factor 1

c2=1.4962; % acceleration constant i.e. learning factor 2

w=0.7298; % Inertia weight

maxdt=10000; % Maximum number of iterations

d=10; % Search Space dimension (number of unknowns in test function sphere)

n=40; % number of initialized group individuals

eps=10^ (-7); % setting accuracy (with known minimum value)

%------The individual that initializes the population (where the range of positions and velocities can be limited)------------

For I=1:n

For J=1:d

X (i,j) =randn; % produces a random number that obeys a normal distribution as the initial position

V (i,j) =randn; % produces a random number that obeys a normal distribution as the speed of initialization

End

End

%------Calculate the fitness of each particle first, and initialize the individual optimal position y and the global optimal position PG--------

For I=1:n

P (i) =sphere (x (i,:), D);% calculation fitness, test function sphere

Y (i,:) =x (i,:); % initialization of the individual optimal position y is the particle position at time step t=0

End

Pg=x (1,:); %PG for the global optimal position

For I=2:n

If sphere (x (i,:), D) <sphere (pg,d)

Pg=x (i,:);% Update global optimal location

End

End

%------Enter the main loop and iterate according to the formula until the accuracy requirement is met------------

For T=1:MAXDT

For I=1:n

V (i,:) =w*v (i,:) +c1*rand* (Y (i,:)-X (i,:)) +c2*rand* (pg-x (i,:));

X (i,:) =x (i,:) +v (i,:);

If sphere (x (i,:), D) <p (i)

P (i) =sphere (x (i,:), D);% Update Fitness

Y (i,:) =x (i,:);% update individual best position

End

If P (i) <sphere (pg,d)

Pg=y (i,:);% Update group best position

End

End

Pbest (t) =sphere (PG,D);% Save the best position for each generation of groups

End

ToC% This function represents the end of the timing

%------Finally gives the calculation results

Disp (' ************************************************************* ')

DISP (' The global optimal position of the function is: ')

For I=1:d

fprintf (' x (%d) =%s\n ', I,PG (i));

End

fprintf (' The last obtained optimization extremum is:%s\n ', Sphere (pg,d));

Disp (' ************************************************************* ')

The sphere function is as follows:

% Fitness function source program (SPHERE.M)

% parameter x is the variable name, and parameter d is the number of dimensions

function Result=sphere (x,d)

sum=0;

For I=1:d

Sum=sum+x (i) ^2;

End

Result=sum;

Program Run Result:

Elapsed time is 2.943799 seconds.

*************************************************************

The global optimal position of the function is:

X (1) =6.654911e-009

X (2) =5.739281e-009

X (3) =-3.207077e-009

X (4) =-1.107863e-011

X (5) =9.756758e-009

X (6) =6.682152e-009

X (7) =-2.828295e-010

X (8) =2.533800e-009

X (9) =3.868910e-009

X (Ten) =1.740554e-009

The last optimized extremum is: 2.518562e-016

*************************************************************

From the running results can be seen, the program run time of about 3 seconds, found the sphere function of the minimum point near 0, near the origin of the coordinates, the expected results.

This article is from the "Technical Achievement Dream" blog, please be sure to keep this source http://51jishurensheng.blog.51cto.com/10626705/1685063

MATLAB implementation of particle swarm optimization algorithm (PSO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.