• 大小: 7KB
    文件类型: .m
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: Matlab
  • 标签: matlab  

资源简介

这是自适应粒子群算法的matlab源代码,十分经典好用

资源截图

代码片段和文件信息

function [pso t i] = apso(max_genpop_sizepart_size)
%   FUNCTION PSO  --------USE Particle Swarm Optimization Algorithm

%clear all
t=cputime;
D_max = 0.25;
D_min = 0.001;
E_max = 2;
E_min = 0.25;
Pm = 0.005;
%pop_size = 30;          %   pop_size 种群大小
%part_size = 30;          %   part_size 粒子大小 
Q  =  10*part_size;                   %   划分的块数
gbest = zeros(1part_size+1);            %   gbest 当前搜索到的最小的值
%max_gen = 30;          %   max_gen 最大迭代次数
region=zeros(part_size2);  %   设定搜索空间范围
%region=[-300300;-300300;-300300;-300300;-300300;-300300;-300300;-300300;-300300;-300300];          %区域                                                                                      **每一维设定不同范围
% region=[-3030;-3030;-3030;-3030;-3030;-3030;-3030;-3030;-3030;-3030];  
 %region=[-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33];
 region=[-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33;-33];
mode=‘exploit‘;
rand(‘state‘sum(100*clock));   %   重置随机数发生器状态
arr_present = ini_pos(pop_sizepart_sizeregion);   %   present 当前位置随机初始化rand()的范围为0~1
v_max = 1;   
v=ini_v(pop_sizepart_sizev_max);             %   初始化当前速度


pbest = zeros(pop_sizepart_size+1);      %   pbest 粒子以前搜索到的最优值,最后一列包括这些值的适应度
w_max = 0.7;                            %   w_max 权系数最大值
w_min = 0.05;
          %                                                                                          **最大速度为粒子的范围宽度
c1 = 2;                   %   学习因子
c2 = 2;                   %   学习因子
best_record = zeros(1max_gen);     %   best_record记录最好的粒子的适应度。
%  ————————————————————————
%   计算原始种群的适应度及初始化
%  ————————————————————————
arr_present(:end)=ini_fit(arr_presentpop_sizepart_size);
pbest = arr_present;                                        %初始化各个粒子最优值
[best_value best_index] = min(arr_present(:end));         %初始化全局最优,即适应度为全局最小的值,根据需要也可以选取为最大值
gbest = arr_present(best_index:);
% i=1;
% while (i<=max_gen&abs(gbest(end))>0.01)
%   i=i+1;
  for i=1:max_gen  
    D = D_caculation(arr_presentregion);
    E = E_caculation(arr_presentQpop_sizeregion);
    if D < D_min || E        mode=‘explore‘;
    end
    if D > D_max && E>E_max
        mode=‘exploit‘;
    end
    

   
   if mode == ‘exploit‘
       w = D*(w_max-w_min)/(D_max-D_min)+(D_max*w_min-D_min*w_max)/(D_max-D_min);
      for j=1:pop_size
       
             v(j:) = w.*v(j:)+c1.*rand.*(pbest(j1:part_size)-arr_present(j1:part_size))...
            +c2.*rand.*(gbest(1:part_size)-arr_present(j1:part_size));                        %  粒子速度更新 (a)

        %   判断v的大小,限制v的绝对值小于v_max————————————————————————————
        c = find(abs(v)>v_max);                                                                                              %**最大速度设置,粒子的范围宽度
        v(c) = sign(v(c))*v_max;   

        arr_present(j1:part_size) = arr_p

评论

共有 条评论