%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Author: Zhang Youbang
% Time: 2009-06-11
% Version: 1.0
%
% This is a basic PSO algorithm implementation.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
clear output
format compact
% some parameters
particle_number = 500; % total number of particles
iteration_number = 200; % the maximum iteration number
wmax = 20;
wmin = 0.01;
c1 = 2;
c2 = 2;
velocity_max = [15, 15];
velocity_init = [0, 0];
% initliaze particles
gbest_index = 1; % the global best particle's index
for progress = 1:iteration_number
w(progress) = wmax - ((wmax - wmin) / iteration_number) * progress;
end
for progress = 1:iteration_number
for i = 1:particle_number
if progress == 1 % initialize the particle
particles(i).location = [(rand - 0.5) * 10000, (rand - 0.5) * 10000];
particles(i).fitness_value = pso_fittness_function(particles(i).location);
particles(i).pbest_location = particles(i).location;
particles(i).pbest_fitness_value = particles(i).fitness_value;
particles(i).velocity = velocity_init; % just set it to zero
elseif i ~= gbest_index % fly towards the global best fitted particle
particles(i).velocity = w(progress) * particles(i).velocity + c1 * rand() * (particles(i).pbest_location - particles(i).location) + c2 * rand() * (particles(gbest_index).location - particles(i).location);
% constraint velocity
if particles(i).velocity(1) > velocity_max(1);particles(i).velocity(1) = velocity_max(1);end;
if particles(i).velocity(1) < -velocity_max(1);particles(i).velocity(1) = -velocity_max(1);end;
if particles(i).velocity(2) > velocity_max(2);particles(i).velocity(2) = velocity_max(2);end;
if particles(i).velocity(2) < -velocity_max(2);particles(i).velocity(2) = -velocity_max(2);end;
particles(i).location = particles(i).location + particles(i).velocity;
particles(i).fitness_value = pso_fittness_function(particles(i).location);
end
% update local best location
if particles(i).fitness_value < particles(i).pbest_fitness_value
particles(i).pbest_location = particles(i).location;
particles(i).pbest_fitness_value = particles(i).fitness_value;
end
% update global best fitted location
if particles(i).pbest_fitness_value < particles(gbest_index).pbest_fitness_value
gbest_index = i;
end
end % end of i
% show the calculation progress
[progress, particles(gbest_index).pbest_fitness_value]
end % end of n
% print the results
particles(gbest_index).pbest_location
particles(gbest_index).pbest_fitness_value
% end of this file