粒子群优化算法
PSO是粒子群优化算法(——Particle Swarm Optimization)的英文缩写,是一种基于种群的随机优化技术,由Eberhart和Kennedy于1995年提出。粒子群算法模仿昆虫、兽群、鸟群和鱼群等的群集行为,这些群体按照一种合作的方式寻找食物,群体中的每个成员通过学习它自身的经验和其他成员的经验来不断改变其搜索模式。
算法优点
思路简单,编程较容易,对连续问题优化效果相当不错,收敛容易,并且通过参数c1 c2的调节可以使粒子群的学习倾向有所改变从而适应不同的优化问题。
下面我们针对一个二元函数进行粒子群优化,使用语言:MATLAB
初始化
maxgen = 100;
sizepop = 100;
Vmax = 0.6;
Vmin = -0.6;
popmax = 10;
popmin = -10;
c1 = 0.4;
c2 = 1.5;
f = @(x)2*sin(x(1))*sin(x(2))/(x(1)*x(2));
[x,y] = meshgrid(-10:0.05:10,-10:0.05:10);
z = 2.*sin(x).*sin(y)./(x.*y);
figure(1);
mesh(x,y,z);
hold on;
figure(2);
mesh(x,y,z);view(0,90);hold on;
for i = 1:sizepoppop(i,:) = -10+20*rand(1,2);V(i,:) = -0.6+1.2*rand(1,2);fitness(i) = f(pop(i,:));
end[bestfitness,bestindex] = min(fitness);
fitnesszbest = bestfitness;
fitnessgbest = fitness;
zbest = pop(bestindex,:); %种群历史最优解
gbest = pop; %个体历史最优解
for j = 1:sizepop%-----------速度更新-----------------%V(j,:) = V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));V(j,find(V(j,:)>Vmax)) = Vmax; %限制搜索速度V(j,find(V(j,:)<Vmin)) = Vmin;%----------种群更新------------------%pop(j,:) = pop(j,:) + V(j,:);pop(j,find(pop(j,:)>popmax)) = popmax; %限制搜索范围pop(j,find(pop(j,:)<popmin)) = popmin;%----------更新适应值----------------%fitness(j) = f(pop(j,:));end%----------------个体最优更新-------------%for j = 1:sizepopif (fitness(j)<fitnessgbest(j))gbest(j,:) = pop(j,:);fitnessgbest(j,:) = fitness(j);endif (fitness(j)<fitnesszbest)zbest = pop(j,:);fitnesszbest = fitness(j);endendyy(i) = fitnesszbest; %每一代的最优适应值
if (i>1) delete(G); delete(H);endfigure(1);G = scatter3(pop(:,1),pop(:,2),fitness,40,'r.');xlim([-10,10]);ylim([-10,10]);zlim([-1,3]);drawnowhold onfigure(2);H = scatter3(pop(:,1),pop(:,2),fitness,40,'r.');hold on;pause(0.5);
zbest
fitnesszbest
close all;clear;clc;
maxgen = 100;
sizepop = 100;
Vmax = 0.6;
Vmin = -0.6;
popmax = 10;
popmin = -10;
c1 = 0.4;
c2 = 1.5;
%-----------------定义匿名函数---------------------%
f = @(x)2*sin(x(1))*sin(x(2))/(x(1)*x(2));
%-----------------绘制函数--------------------------%
[x,y] = meshgrid(-10:0.05:10,-10:0.05:10);
z = 2.*sin(x).*sin(y)./(x.*y);
figure(1);
mesh(x,y,z);
hold on;
figure(2);
mesh(x,y,z);view(0,90);hold on;
%------------------初始化种群-----------------------%
for i = 1:sizepoppop(i,:) = -10+20*rand(1,2);V(i,:) = -0.6+1.2*rand(1,2);fitness(i) = f(pop(i,:));
end[bestfitness,bestindex] = min(fitness);
fitnesszbest = bestfitness;
fitnessgbest = fitness;
zbest = pop(bestindex,:); %种群历史最优解
gbest = pop; %个体历史最优解%---------------寻优--------------------------%
for i = 1:maxgenfor j = 1:sizepop%-----------速度更新-----------------%V(j,:) = V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));V(j,find(V(j,:)>Vmax)) = Vmax; %限制搜索速度V(j,find(V(j,:)<Vmin)) = Vmin;%----------种群更新------------------%pop(j,:) = pop(j,:) + V(j,:);pop(j,find(pop(j,:)>popmax)) = popmax; %限制搜索范围pop(j,find(pop(j,:)<popmin)) = popmin;%----------更新适应值----------------%fitness(j) = f(pop(j,:));end%----------------个体最优更新-------------%for j = 1:sizepopif (fitness(j)<fitnessgbest(j))gbest(j,:) = pop(j,:);fitnessgbest(j,:) = fitness(j);endif (fitness(j)<fitnesszbest)zbest = pop(j,:);fitnesszbest = fitness(j);endendyy(i) = fitnesszbest; %每一代的最优适应值%---------------绘制搜索动态3D图---------------%if (i>1) delete(G); delete(H);endfigure(1);G = scatter3(pop(:,1),pop(:,2),fitness,40,'r.');xlim([-10,10]);ylim([-10,10]);zlim([-1,3]);drawnowhold onfigure(2);H = scatter3(pop(:,1),pop(:,2),fitness,40,'r.');hold on;pause(0.5);
end
zbest
fitnesszbest

本文发布于:2024-02-01 16:31:07,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170677626837959.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |