📄 gppso126c.m
字号:
%PSO >> function for the PSO ALGORITHM
%
% USAGES: 1.) [fxmin, xmin, Swarm, history] = PSO(psoOptions);
% 2.) [fxmin, xmin, Swarm, history] = PSO;
% 3.) fxmin = PSO(psoOptions);
% 3.) PSO
% etc.
%
% Arguments : psoOptions--> A Matlab stucture containing all PSO related options. (see also: get_psoOptions)
% Return Values : [fxmin, xmin, Swarm, history]
% | | | |_The history of the algorithm. Depicts how the function value of GBest changes over the run.
% | | |_The final Swarm. (A matrix containing co-ordinates of all particles)
% | |_The co-ordinates of Best (ever) particle found during the PSO's run.
% |__The objective value of Best (^xmin) particle.
%
% History : Author : NB (Niu Ben)
% Created on : 02122004 (Friday. 2nd DEC, 2004)
% Comments : The basic PSO algorithm.
% Modified on : 1012004 (Thursday. 10th DEC, 2004)
% Comments : It uses psoOptions structure now. More organized.
%
% see also: get_psoOptions
%Globals
%ave=0;
%for i=1:2
%fidSPSO=fopen('SF1Rosenbrock.txt','w');
SwarmSize = 20;
Dim=6;
AA= [1,0,0,0,1,0
0,1,1,1,1,1
0,1,1,1,1,1
0,1,1,1,0,0
1,1,1,0,1,0
0,1,1,0,0,1];
S = [3,4,3,7,5,2];
for pppp=1:2
Vmax=4;
GM = 0; % Global minimum (used in the stopping criterion)
ErrGoal = 1e-10; % Desired accuracy
Iterations=9;
% initializing variables
success=0;
iter = 0; % Iterations' counter
%maxstep=10;
%cross=0;
fevals = 0; % Function evaluations' counter
%y=zeros(2,(Iterations+1));
% Using params---
% Determine the value of weight change
w = 0.729; %Initial inertia weight's value
c1=1.494;
c2=1.494;
% Initialize graph
%A = [0,0,0,0,1,0
% 0,0,1,1,1,1
% 0,1,0,1,1,1
% 0,1,1,0,0,0
% 1,1,1,0,0,0
%0,1,1,0,0,0];
%while (Dim>1)% Sensor still left for scheduling
% Initialize parometers in the cost function
% Initialize Swarm and Velocity
Swarm = round(rand(SwarmSize, Dim));
VStep = rand(SwarmSize, Dim)*Vmax*2 - Vmax;
fSwarm= fit(Swarm,AA,S,Dim,SwarmSize);
fevals = fevals + SwarmSize;
% Initializing the Best positions matrix and
% the corresponding function values
PBest = Swarm;
fPBest = fSwarm;
% Finding best particle in initial population
[fGBest, g] = max(fSwarm);
lastbpf = fGBest;
Best = Swarm(g,:); %Used to keep track of the Best particle ever
fBest = fGBest;
% Output
disp(sprintf('Clique\t\tfGBest\t\t\tfSwarm'));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% THE PSO LOOP %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
iiter=0;
while( iter <= Iterations)
iter = iter+1;
%%%%%%%%%%%%%%%%%
% The PLAIN PSO %
%Set GBest
A= repmat(Swarm(g,:), SwarmSize, 1); %A = GBest. repmat(X, m, n) repeats the matrix X in m rows by n columns.
% Generate Random Numbers
R1 = rand(SwarmSize, Dim);
R2 = rand(SwarmSize, Dim);
% Calculate Velocity
VStep = w*VStep + c1*R1.*(PBest-Swarm) + c2*R2.*(A-Swarm);
% Apply Vmax Operator for v > Vmax
changeRows = VStep > Vmax;
VStep(find(changeRows)) =Vmax;
% Apply Vmax Operator for v < -Vmax
changeRows = VStep < -Vmax;
VStep(find(changeRows)) = -Vmax;
% ::UPDATE POSITIONS OF PARTICLES::
PP=rand(SwarmSize,Dim);
for i= 1:SwarmSize%evaluate new Swarm
for j = 1:Dim
if PP(i,j)<(1/(1+exp(-VStep(i,j))))
Swarm(i,j)=1;
else
Swarm(i,j)=0;
end
end
end
% Find initial function values
fSwarm= fit(Swarm,AA,S,Dim,SwarmSize);
fevals = fevals + SwarmSize;
% Updating the best position for each particle
changeRows = fSwarm > fPBest;
fPBest(find(changeRows)) = fSwarm(find(changeRows));%适应值跟换
PBest(find(changeRows), :) = Swarm(find(changeRows), :);%粒子位置跟换
lastbpart = PBest(g, :);%保存最好位置
% Updating index g
[fGBest, g] = max(fPBest); % 保存最好适应值
%Update Best. Only if fitness has improved.
if fGBest > lastbpf %如果本次适应值好于上次则保存
[fBest, b] = max(fPBest);%最好适应值为fBest
Best = PBest(b,:);%最好位置为Best
end
lastbpf = fGBest; %To be used to find Best保留本次适应值准备与下一次比较
x(iter)=iter;
% y(pppp,iter)=fGBest
%%OUTPUT%%
%disp(sprintf('%4d\t\t\t%.5g\t\t\t%5d', iter, fGBest, fevals));
end
%fclose all;
%thebest(i)=fGBest;
%ave=ave+thebest(i);
%axis([0 1000 0 200]);
%xlabel('iterations');
%ylabel( 'Best-fitness=fGBest(i)')
%title('Multi-pattern Evolutionary process')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% END OF PSO LOOP %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%[fxmax, b] = max(fPBest)
%xmax = PBest(b, :)
%disp(sprintf('%4d\t\t\t%.5g\t\t\t%5d', Best,iter,fSwarm ));
[AA,S,Dim]=NewAAS(Dim,Best,AA,S);
end
%plot(x,log10(y(2,:)))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -