インテリジェント最適化アルゴリズム-粒子群アルゴリズム(Matlab実装)

コンテンツ

1ナレッジリターン

2Matlabコード

3結果表示 


1ナレッジリターン

私はすでにそれを前に詳細に要約しました:

粒子群アルゴリズム(制約付き)-PythonとMatlabの実装

2Matlabコード

%% 粒子群算法
%%========欢迎关注公众号:电力系统与算法之美=======
clear
close all
clc
%% 初始化种群
SearchAgents_no = 30 ; % 种群规模
dim = 10 ; % 粒子维度
Max_iter = 1000 ; % 迭代次数
ub = 5 ;
lb = -5 ;
c1 = 1.5 ; % 学习因子1
c2 = 1.5 ; % 学习因子2
w = 0.8 ; % 惯性权重
vmax = 3 ; % 最大飞行速度
pos = lb + rand(SearchAgents_no,dim).*(ub-lb) ; % 初始化粒子群的位置
v = - vmax +2*vmax* rand(SearchAgents_no,dim) ; % 初始化粒子群的速度
%% 初始化每个历史最优粒子
pBest = pos ; 
pbestfit = zeros(SearchAgents_no,1);
for i = 1:SearchAgents_no
pbestfit(i) = sum(pos(i,:).^2) ; 
end
%% 初始化全局历史最优粒子
[gBestfit,index] = min(pbestfit) ;
gBest = pos(index,:) ;
Convergence_curve = zeros(Max_iter,1);
 
for t=1:Max_iter
    for i=1:SearchAgents_no
        %% 更新个体的位置和速度
        v(i,:) = w*v(i,:)+c1*rand*(pBest(i,:)-pos(i,:))+c2*rand*(gBest-pos(i,:)) ;
        pos(i,:) = pos(i,:)+v(i,:) ;
        %% 边界处理
        v(i,:) = min(v(i,:), vmax);
        v(i,:) = max(v(i,:), -vmax);
        pos(i,:) =min(pos(i,:), ub);
        pos(i,:) =max(pos(i,:), lb);
        %% 更新个体最优
        f1 = sum(pos(i,:).^2);
        if f1<pbestfit(i)    
           pBest(i,:) = pos(i,:) ;
           pbestfit(i) = f1;
        end
        %% 更新全局最优
       if pbestfit(i) < gBestfit
            gBest = pBest(i,:) ;
            gBestfit = pbestfit(i) ;
       end
    end
    %% 每代最优解对应的目标函数值
    Convergence_curve(t) = gBestfit; 
    disp(['Iteration = ' num2str(t)  ', Evaluations = ' num2str(gBestfit)]);
end
 
figure('unit','normalize','Position',[0.3,0.35,0.4,0.35],'color',[1 1 1],'toolbar','none')
subplot(1,2,1);
x = -5:0.1:5;y=x;
L=length(x);
f=zeros(L,L);
for i=1:L
    for j=1:L
       f(i,j) = x(i)^2+y(j)^2;
    end
end
surfc(x,y,f,'LineStyle','none');
xlabel('x_1');
ylabel('x_2');
zlabel('F')
title('Objective space')
 
subplot(1,2,2);
semilogy(Convergence_curve,'Color','r','linewidth',1.5)
title('Convergence_curve')
xlabel('Iteration');
ylabel('Best score obtained so far');
 
axis tight
grid on
box on
legend('PSO')
display(['The best solution obtained by PSO is : ', num2str(gBest)]);
display(['The best optimal value of the objective funciton found by PSO is : ', num2str(gBestfit)]);
 

3結果表示 

 

 

 

おすすめ

転載: blog.csdn.net/weixin_46039719/article/details/123722838