function [fbestval,bestparticle] = pso(fname,NDim,MaxIter)
% Run a PSO algorithm
%
%Input Arguments:
% fname - the name of the evaluation .m function
% bounds - a matrix of upper and lower bounds on the variables
% vmax - maximum velocity
% NDim - dimension of the evalation function
% MaxIter - maximum iteration
% Modified Particle Swarm Optimization for Matlab
% Copyright (C) 2002 Shan He, the University of Liverpool
% Intelligence Engineering & Automation Group
flag=0;
iteration = 0;
%MaxIter =1000; % maximum number of iteration
PopSize=50; % population of particles
%NDim=10; % Number of dimension of search space
w=0.73;
c1=2.05;
c2=2.05;
gbest = zeros(NDim,PopSize);
Bound = fname();
% Defined lower bound and upper bound.
LowerBound = zeros(NDim,PopSize);
UpperBound = zeros(NDim,PopSize);
for i=1:PopSize
LowerBound(:,i)=Bound(:,1);
UpperBound(:,i)=Bound(:,2);
end
population = rand(NDim, PopSize).*(UpperBound-LowerBound) + LowerBound; % Initialize swarm population
vmax = ones(NDim,PopSize);
for i=1:NDim
vmax(i,:)=(UpperBound(i,:)-LowerBound(i,:))/10;
end
velocity = vmax.*rand(1); % Initialize velocity
fvalue = zeros(PopSize,1);
% Evaluate initial population
for i = 1:PopSize,
fvalue(i) = fname(population(:,i));
end
pbest = population; % Initializing Best positions�� matrix
fpbest = fvalue; % Initializing the corresponding function values
% Finding best particle in initial population
[fbestval,index] = min(fvalue); % Find the globe best
phis = fbestval;
%population = population + velocity;
while(flag == 0) && (iteration < MaxIter)
iteration = iteration +1;
R1 = rand(NDim, PopSize);
R2 = rand(NDim, PopSize);
% Evaluate the new swarm
for i = 1:PopSize,
fvalue(i) = fname(population(:,i));
end
% Updating the pbest for each particle
% Updating the pbest for each particle
changeColumns = fvalue < fpbest;
pbest(:, find(changeColumns)) = population(:, find(changeColumns)); % find(changeColumns) find the columns which the values are 1
fpbest = fpbest.*( ~changeColumns) + fvalue.*changeColumns;
% Updating index
[fbestval, index] = min(fpbest);
phis(length(phis)+1) = fbestval;
for i=1:PopSize
gbest(:,i) = population(:,index);
end
velocity = w*(velocity + c1*R1.*(pbest-population) + c2*R2.*(gbest-population));
velocity=(velocity<-vmax).*(-vmax)+(velocity>vmax).*(vmax)+(velocity>-vmax&velocity<vmax).*velocity;
% Update the swarm particle
population = population + velocity;
% Prevent particles from flying outside search space
population(population>UpperBound)=UpperBound(population>UpperBound); % crop to upper range
population(population<LowerBound)=LowerBound(population<LowerBound); % crop to lower range
% plot best fitness
% Best(iteration) =fbestval;
% plot(log10(Best),'ro');xlabel('generation'); ylabel('log10(f(x))');
% text(0.5,0.95,['Best = ', num2str(Best(iteration))],'Units','normalized');
% drawnow;
end
bestparticle = gbest(:,1);
pso.rar_Modified_modified PSO
版权申诉
164 浏览量
2022-09-14
16:16:23
上传
评论
收藏 2KB RAR 举报
JaniceLu
- 粉丝: 85
- 资源: 1万+
最新资源
- redis-win-2.8.9,redis-win-2.8.9
- Servlet和JDBC实现三层架构
- Appium-Inspector-2024.6.1-win
- Screenshot_2024-06-14-21-22-39-202_net.csdn.csdnplus.jpg
- Appium-Server-GUI-windows-1.22.3-4
- 基于C语言+python实现的永磁同步电机矢量控制算法仿真+源码(毕业设计&课程设计&项目开发)
- APKPure_v3.20.05_apkpure.com.apk
- HKJC_AOSBS_PROD_L2.7R2Q_Build8172.apk
- jetson官网下载的官方资料
- 小游戏的java程序开发
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈