📄 initswarm.m
字号:
function [ParSwarm,OptSwarm]=InitSwarm(SwarmSize,ParticleSize,ParticleScope,AdaptFunc)%function: initialize particles, restrict particle position and volocity%range%[ParSwarm,OptSwarm,BadSwarm]=InitSwarm(SwarmSize,ParticleSize,ParticleScope,AdaptFunc)%%parameter:SwarmSize:size of racial%parameter:ParticleSize:the dimensions of one particle%parameter:ParticleScope:scope of one particle calculated in every dimension;% ParticleScope format:% 3-D particle ParticleScope format:% [x1Min,x1Max% x2Min,x2Max% x3Min,x3Max]%%input parameter:AdaptFunc:fitness function%%output:ParSwarm initialized particles%output:OptSwarm the current best and global best solutions%%use method [ParSwarm,OptSwarm,BadSwarm]=InitSwarm(SwarmSize,ParticleSize,ParticleScope,AdaptFunc);%%%control of faultsif nargin~=4 error('fault numbers of the imput parameters')endif nargout<2 error('number of input is too small, cannot ensure the following run');end[row,colum]=size(ParticleSize);if row>1|colum>1 error('fault of the input parameters dimension, is a 1*1 data.');end[row,colum]=size(ParticleScope);if row~=ParticleSize|colum~=2 error('fault of input paramet dimension scope');end%initialize particle matrix%initialize particle matrix,all set to [0-1]random number%rand('state',0);ParSwarm=2.5*rand(SwarmSize,2*ParticleSize+1);%cordinate of the particle position and velocity scopefor k=1:ParticleSize ParSwarm(:,k)=ParSwarm(:,k)*(ParticleScope(k,2)-ParticleScope(k,1))+ParticleScope(k,1); %corrdinate velocity, corresponding with the position ParSwarm(:,ParticleSize+k)=ParSwarm(:,ParticleSize+k)*(ParticleScope(k,2)-ParticleScope(k,1))+ParticleScope(k,1);end %calculate every fitness function value for every particlefor k=1:SwarmSize ParSwarm(k,2*ParticleSize+1)=AdaptFunc(ParSwarm(k,1:ParticleSize));end%initialize the bst solution matrix of particlesOptSwarm=zeros(SwarmSize+1,ParticleSize);%set the matrix to zero[minValue,row]=min(ParSwarm(:,2*ParticleSize+1));%[maxValue,row]=max(ParSwarm(:,2*ParticleSize+1));%look for the highest solution in the matrix positionOptSwarm=ParSwarm(1:SwarmSize,1:ParticleSize);OptSwarm(SwarmSize+1,:)=ParSwarm(row,1:ParticleSize);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -