📄 qpso_op.m
字号:
%declare the parameters of the optimization
clc
clear
max_iterations = 50;
no_of_particles = 30;
dimensions = 2;
wmax=0.9;
wmin=0.4;
x_min=0.01;
x_max=100;
c1 = 1.3;
c2 = 1.3;
%initialise the particles
particle_position=unifrnd(x_min,x_max,no_of_particles,dimensions);
p_best=particle_position;%初始化pbest
%initialize the p_best_fitness array
for count = 1:no_of_particles
p_best_fitness(count) = 10;%
end
%main particle swrm routine
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
%for count_y=1:dimensions
parameter=[particle_position(count_x,1);particle_position(count_x,2)];
[a1,a2,a3]=mysvm1(parameter);
current_fitness(count_x) =a1 ; %计算粒子的适应度;
end
%decide on p_best etc for each particle
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);
p_best(count_x,1:dimensions) = particle_position(count_x,1:dimensions);
end
end
m_best=sum(p_best)/no_of_particles;
%decide on the global best among all the particles
[g_best_val,g_best_index] = min(current_fitness);%最小适应度值,位置
bestfitness(count)=g_best_val;%每次迭代的最优适应值
%g_best contains the position of teh global best
g_best=particle_position(g_best_index,1:dimensions);
%G_temp(count)=g_best;%每次迭代的GBEST;
%update the position
W(count)=0.5-0.5*(max_iterations-count)/max_iterations;
for count_x = 1:no_of_particles
for count_y = 1:dimensions
% 更新位置
f=rand;
pp=f*p_best(count_x,count_y)+(1-f)*g_best(1,count_y);
u=rand;
v=-log(u);
if(u>0.5)
temp_position = pp+W(count)*abs(m_best(1,count_y)-particle_position(count_x,count_y))*v;
elseif(u<0.5 )
temp_position = pp-W(count)*abs(m_best(1,count_y)-particle_position(count_x,count_y))*v;
end
if(temp_position >x_max)
particle_position(count_x,count_y)=x_max;
elseif(temp_position <x_min)
particle_position(count_x,count_y)=x_min;
else
particle_position(count_x,count_y)=temp_position;
end
end
end
end
current_fitness(g_best_index)
%---------------------------------------------------
% 结果作图
figure(1)
t=1:5:max_iterations ;
plot(t,bestfitness(t)/5)
xlabel('Iterations ');
ylabel('Fitness')
[Error,Yt,Yd]=mysvm1(parameter);
e=Yd-Yt;
e=e.^2;
rsme=sqrt(mean(e))
figure(2)
plot(1:length(Yt),Yt,'rd-',1:length(Yd),Yd,'b.:')
%axis([0,50,0.3,1]);
%title('+为真实值,.为预测值')
legend('实际测量值','软测量估计值');
figure(3)
plot(1:length(Yt),Error,'b.:')
title('误差曲线')
%end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -