【啃书】《智能优化算法及其MATLAB实例》例6.2标准粒子群算法进行函数寻优

问题描述

在这里插入图片描述
在这里插入图片描述

仿真过程

标准粒子群算法的进化过程如下
在这里插入图片描述
在这里插入图片描述

matlab源码

%该脚本要命名为func2.m
%%%%%%%%%%%%%%%%%%%%%适应度函数%%%%%%%%%%%%%%%%%%%%%%%
function value=func2(x)

value=sin(x(1)*x(1)+x(2))+cos(5*x(1))+x(1)+x(2);
%20201017lu注:在matlab2019a成功运行
%% 绘制函数图形
% %%%%%f(x,y)=3*cos(x*y)+x+y*y%%%%%%%%%%%%%%%%%
% clear all;
% close all;
% clc;
% x=-4:0.02:4;
% y=-4:0.02:4;
% N=size(x,2);
% for i=11:N
%     for j=1:N
%        z(i,j)=3*cos(x(i)*y(j))+x(i)+y(j)*y(j);
%         %z(i,j)=5*sin(x(i)*y(j))+x(i)*x(i)+y(j)*y(j);
%         %z(i,j)=6*sin(4*x(i))+9*cos(5*y(j))+y(j)*y(j);
%         %z(i,j)=3*cos(6*y(j))+x(i)*x(i)+2*y(j)*y(j);
%         %z(i,j)=sin(x(i)*x(i)+y(j))+cos(5*x(i))+x(i)+y(j);
%     end
% end
% mesh(x,y,z)
% xlabel('x')
% ylabel('y')
%% 标准粒子群算法
%%%%%%%%%%%%%%%%%粒子群算法求函数极值%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%初始化%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%clear all;              %清除所有变量
%close all;              %清图
%clc;                    %清屏
N=100;                  %群体粒子个数
D=2;                    %粒子维数
T=200;                  %最大迭代次数
c1=1.5;                 %学习因子1
c2=1.5;                 %学习因子2
Wmax=0.8;               %惯性权重最大值
Wmin=0.4;               %惯性权重最小值
Xmax=4;                 %位置最大值
Xmin=-4;                %位置最小值
Vmax=1;                 %速度最大值
Vmin=-1;                %速度最小值
%%%%%%%%%%%%%%%%初始化种群个体(限定位置和速度)%%%%%%%%%%%%%%%%
x=rand(N,D) * (Xmax-Xmin)+Xmin;
v=rand(N,D) * (Vmax-Vmin)+Vmin;
%%%%%%%%%%%%%%%%%%初始化个体最优位置和最优值%%%%%%%%%%%%%%%%%%%
p=x;
pbest=ones(N,1);
for i=1:N
    pbest(i)=func2(x(i,:));
end
%%%%%%%%%%%%%%%%%%%初始化全局最优位置和最优值%%%%%%%%%%%%%%%%%%
g=ones(1,D);
gbest=inf;
for i=1:N
    if(pbest(i)<gbest)
        g=p(i,:);
        gbest=pbest(i);
    end
end
gb=ones(1,T);
%%%%%%%%%%%按照公式依次迭代直到满足精度或者迭代次数%%%%%%%%%%%%%
for i=1:T
    for j=1:N
        %%%%%%%%%%%%%%更新个体最优位置和最优值%%%%%%%%%%%%%%%%%
        if (func2(x(j,:))<pbest(j))
            p(j,:)=x(j,:);
            pbest(j)=func2(x(j,:));
        end
        %%%%%%%%%%%%%%%%更新全局最优位置和最优值%%%%%%%%%%%%%%%
        if(pbest(j)<gbest)
            g=p(j,:);
            gbest=pbest(j);
        end
        %%%%%%%%%%%%%%%%计算动态惯性权重值%%%%%%%%%%%%%%%%%%%%
        w=Wmax-(Wmax-Wmin)*i/T;
        %%%%%%%%%%%%%%%%%跟新位置和速度值%%%%%%%%%%%%%%%%%%%%%
        v(j,:)=w*v(j,:)+c1*rand*(p(j,:)-x(j,:))...
            +c2*rand*(g-x(j,:));
        x(j,:)=x(j,:)+v(j,:);
        %%%%%%%%%%%%%%%%%%%%边界条件处理%%%%%%%%%%%%%%%%%%%%%%
        for ii=1:D
            if (v(j,ii)>Vmax)  |  (v(j,ii)< Vmin)
                v(j,ii)=rand * (Vmax-Vmin)+Vmin;
            end
            if (x(j,ii)>Xmax)  |  (x(j,ii)< Xmin)
                x(j,ii)=rand * (Xmax-Xmin)+Xmin;
            end
        end
    end
    %%%%%%%%%%%%%%%%%%%%记录历代全局最优值%%%%%%%%%%%%%%%%%%%%%
    gb(i)=gbest;
end
g                        %最优个体         
gb(end)                   %最优值
figure
plot(gb)
xlabel('迭代次数');
ylabel('适应度值');
title('适应度进化曲线')
g =

    -3.045413624381006e+00    -3.999661730691216e+00


ans =

    -8.777584564419779e+00

在这里插入图片描述

中 智能优化算法及其MATLAB实例(第二版)[包子阳,余继周][电子工业出版社][2018年01月][9787121330308]

相关推荐
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页