Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wenhuizhang/7164309 to your computer and use it in GitHub Desktop.
Save wenhuizhang/7164309 to your computer and use it in GitHub Desktop.
Particle swarm optimization (PSO)_Algorithm_2D
%%http://en.wikipedia.org/wiki/Particle_swarm_optimization
function [pso F] = pso_2D()
% FUNCTION PSO --------USE Particle Swarm Optimization Algorithm
%global present;
% close all;
pop_size = 10; % pop_size
part_size = 2; % part_siz=n-D
gbest = zeros(1,part_size+1);
max_gen = 80;
region=zeros(part_size,2);
region=[-3,3;-3,3];
rand('state',sum(100*clock));
arr_present = ini_pos(pop_size,part_size);
v=ini_v(pop_size,part_size);
pbest = zeros(pop_size,part_size+1);
w_max = 0.9; ֵ
w_min = 0.4;
v_max = 2;
c1 = 2;
c2 = 2;
best_record = zeros(1,max_gen);
arr_present(:,end)=ini_fit(arr_present,pop_size,part_size);
% for k=1:pop_size
% present(k,end) = fitness(present(k,1:part_size));
% end
pbest = arr_present; ֵ
[best_value best_index] = min(arr_present(:,end)); ֵ
gbest = arr_present(best_index,:);
%v = zeros(pop_size,1);
%
% global m;
% m = moviein(1000);
x=[-3:0.01:3];
y=[-3:0.01:3];
z=@(x,y) 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2)- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2)- 1/3*exp(-(x+1).^2 - y.^2);
for i=1:max_gen
grid on;
% plot3(x,y,z);
% subplot(121),ezmesh(z),hold on,grid on,plot3(arr_present(:,1),arr_present(:,2),arr_present(:,3),'*'),hold off;
% subplot(122),ezmesh(z),view([145,90]),hold on,grid on,plot3(arr_present(:,1),arr_present(:,2),arr_present(:,3),'*'),hold off;
ezmesh(z) ,hold on,grid on,plot3(arr_present(:,1),arr_present(:,2),arr_present(:,3),'*'),hold off;
drawnow
F(i)=getframe;
% ezmesh(z)
% % view([-37,90])
% hold on;
% grid on;
% % plot(-0.0898,0.7126,'ro');
% plot3(arr_present(:,1),arr_present(:,2),arr_present(:,3),'*');
% axis([-2*pi,2*pi,-pi,pi,-50,10]);
% hold off;
pause(0.01);
% m(:,i) = getframe;
w = w_max-(w_max-w_min)*i/max_gen;
% fprintf('XXXXXXXXXXXXX\n',i);
% reset = 0;
if reset==1
bit = 1;
for k=1:part_size
bit = bit&(range(arr_present(:,k))<0.1);
end
if bit==1
arr_present = ini_pos(pop_size,part_size);
v = ini_v(pop_size,part_size);
for k=1:pop_size
arr_present(k,end) = fitness(arr_present(k,1:part_size));
end
warning('this is the local max');
display(i);
end
end
for j=1:pop_size
v(j,:) = w.*v(j,:)+c1.*rand.*(pbest(j,1:part_size)-arr_present(j,1:part_size))+c2.*rand.*(gbest(1:part_size)-arr_present(j,1:part_size));
c = find(abs(v)>6);
v(c) = sign(v(c))*6;
arr_present(j,1:part_size) = arr_present(j,1:part_size)+v(j,1:part_size);
arr_present(j,end) = fitness(arr_present(j,1:part_size));
if (arr_present(j,end)>pbest(j,end))&(Region_in(arr_present(j,:),region))
pbest(j,:) = arr_present(j,:);
end
end
[best best_index] = max(arr_present(:,end));
if best>gbest(end)&(Region_in(arr_present(best_index,:),region))
gbest = arr_present(best_index,:);
end
best_record(i) = gbest(end);
display(i);
end
pso = gbest;
display(gbest);
% figure;
% plot(best_record);
% movie2avi(F,'pso_2D1.avi','compression','MSVC');
% ***************************************************************************
% evaluation
% ***************************************************************************
function fit = fitness(present)
fit=3*(1-present(1)).^2.*exp(-(present(1).^2) - (present(2)+1).^2) - 10*(present(1)/5 - present(1).^3 - present(2).^5).*exp(-present(1).^2-present(2).^2)- 1/3*exp(-(present(1)+1).^2 - present(2).^2);
function ini_present=ini_pos(pop_size,part_size)
ini_present = 3*rand(pop_size,part_size+1);
function ini_velocity=ini_v(pop_size,part_size)
ini_velocity =3/2*(rand(pop_size,part_size));
function flag=Region_in(pos_present,region)
[m n]=size(pos_present);
flag=1;
for j=1:n-1
flag=flag&(pos_present(1:j)>=region(j,1))&(pos_present(1:j)<=region(j,2));
end
function arr_fitness=ini_fit(pos_present,pop_size,part_size)
for k=1:pop_size
arr_fitness(k,1) = fitness(pos_present(k,1:part_size));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment