📄 pso1.m
字号:
%%####################################################################
%%#### Particle swarm optimization
%%#### With linkage operator
%%#### Duan Dengxin 2006-5-16
%%####################################################################
%初始化各项参数
max_iterations = 1000;
no_of_particles = 50;
dimensions = 1;
%delta_min = -0.003;
%delta_max = 0.003;
c1 = 2;
c2 = 2;
%u = [0.01:0.0001:1];
%v = 5 + sin(pi*u) + 0.3*sign(u-0.3)... % 采用"HeaviSine信号"
% + 0.8*sign(0.5-u) + 0.1*sign(u-0.2)...
% + 0.2*sign(u-0.1) + 0.1*sign(-u+0.5)...
% + 0.7*sign(-u+0.6) + 0.1*sign(-u+0.7);
%ddx = awgn(v,30); %在信号中加入信噪比为80dB的白高斯噪声
%plot(x,y);
%plot(x,z);
%[CA,CD] = DWT(ddx,'db4');
%gcv(CA,0.01)
%plot(ddb);title('test');
%plot(x,ddx);
%[CA,CH,CV,CD] = dwt2(ddx,'db1');
%加载信号
load signal;
%初始化微粒位置和速度参数
for count_x = 1:no_of_particles
for count_y = 1:dimensions
particle_position(count_x,count_y) = rand;
particle_velocity(count_x,count_y) = rand/20;
p_best(count_x,count_y) = particle_position(count_x,count_y);
end
end
%初始化pbest
for count = 1:no_of_particles
p_best_fitness(count) = -1000;
end
%主要程序,进行PSO运算
for count = 1:max_iterations
%find the fitness of each particle
%change fitness function as per equation requiresd and dimensions
for count_x = 1:no_of_particles
%x = particle_position(count_x,1);
%y = particle_position(count_x,2);
%z = particle_position(count_x,3);
%soln = x^2 - 3*y*x + z;
%x = particle_position(count_x);
%soln = x^2-2*x+1;
x = particle_position(count_x);
%====================
%plot(u,ddx);
current_fitness(count_x) = gcv(CD,x); % fitness值即为gcv值
%current_fitness(count_x) = 5-(x-1)^2;
%====================
% soln = x-7;
% if soln~=0
% current_fitness(count_x) = 1/abs(soln);
% else
% current_fitness =1000;
% end
end
%确定pbest
for count_x = 1:no_of_particles
if current_fitness(count_x) < p_best_fitness(count_x)
p_best_fitness(count_x) = current_fitness(count_x);
for count_y = 1:dimensions
p_best(count_x,count_y) = particle_position(count_x,count_y);
end
end
end
%确定gbest
[g_best_val,g_best_index] = max(current_fitness);
for count_y = 1:dimensions
g_best(count_y) = particle_position(g_best_index,count_y);
end
%微粒位置和速度改变
for count_x = 1:no_of_particles
for count_y = 1:dimensions
p_current(count_y) = particle_position(count_x,count_y);
end
for count_y = 1:dimensions
particle_velocity(count_y) = particle_velocity(count_y) + c1*rand*(p_best(count_y)-p_current(count_y)) + c2*rand*(g_best(count_y)-p_current(count_y));
particle_positon(count_x,count_y) = p_current(count_y) +particle_velocity(count_y);
end
end
end
%给出结果
g_best
current_fitness(g_best_index)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -