clc
clear
close all
%初始化粒子群算法参数
popsize = 300;
m = 2;
gen=20; %设置进化代数
xmax = [5.12 5.12];
xmin = [-5.12 -5.12];
vmax = 0.2*xmax;
vmin = -vmax;
fun = @shubert;
pop1 = zeros(popsize,m);
pop2 = zeros(popsize,m);
pop3 = zeros(popsize,m);
pop6 = zeros(gen,m);%存储解码后的每代最优粒子
pop7 = zeros(popsize,m);%存储更新解码后的粒子的位置
for ii1=1:popsize
pop1(ii1,:)=funx(xmin,xmax,m); %初始化种群中的粒子位置,
pop3(ii1,:)=pop1(ii1,:); %初始状态下个体最优值等于初始位置
pop2(ii1,:)=funv(vmax,m); %初始化种群微粒速度,
pop4(ii1,1)=inf;
pop5(ii1,1)=inf;
end
gbest_x=pop1(end,:);
pop0=pop1;
%设置参数
wstart = 1.0;
wend = 0.9;
Tmax = 6000;
Vmax = 6;
Vmin = -6;
c1 = 1.49445;
c2 = 1.49445;
decayscale = 0.7;
temperature = 0.00000001;
markovlength = 30;
k = 0;
b = 0;
%循环开始
for jj = 1:20
for i = 1:markovlength
k = k + 1;
w = wstart - (wstart - wend)*(Tmax - k)/Tmax;
%循环开始
[pop1,pop2,pop3,gbest_x,pop4,pop5,b] = iterateSAPSO(pop1,pop2,pop3,gbest_x,pop4,pop5,w,c1,c2,temperature,popsize,m,b);
yy(k) = pop5;
end
zy(jj) = min(yy);
temperature = temperature*decayscale;
end
i = 1:6000;
figure
plot(zy)
xlabel('迭代次数')
ylabel('适应度')
title('模拟退火算法优化粒子群算法')
x = -2:0.1:2;
y = -2:0.1:2;
x = -10:0.1:10;
y = -10:0.1:10;
[x,y] = meshgrid(x,y);
[m,n] = size(x);
z = zeros(m,n);
for ii = 1:m
for jj = 1:n
xx = [x(ii,jj) y(ii,jj)];
z(ii,jj) = shubertfun(xx);
end
end
figure
surf(x,y,z)
hold on
xlabel('x1')
ylabel('x2')
zlabel('z')
axis([-2 2 -2 2 -200 200])
% axis([-10 10 -10 10 -200 200])
% shading interp
title('Shubert Function')
set(gca,'fontsize',12)
colormap jet
plot(pop0(:,1),pop0(:,2),'ro','MarkerFaceColor','r')
xlabel('X')
ylabel('Y')
title('初始种群')
set(gca,'fontsize',12)
view([-130 40])
figure
surf(x,y,z)
hold on
xlabel('x1')
ylabel('x2')
zlabel('z')
axis([-2 2 -2 2 -200 200])
% axis([-10 10 -10 10 -200 200])
% shading interp
title('Shubert Function')
set(gca,'fontsize',12)
colormap jet
plot(pop1(:,1),pop1(:,2),'ro','MarkerFaceColor','r')
xlabel('X')
ylabel('Y')
title('收敛后的种群')
set(gca,'fontsize',12)
view([-130 40])
%粒子的更新