?? pso1.m
字號:
% =====================================================================
% PSO algorithm
% Start Date: 2006/9/1
% Last Changed: 2006/11/20
% Specification: MATLAB 7.01 Programing
% This is PSO algorithm and its application in
% solving rosenbrock function
%
% Copyright (c) Ji and Liao in 2006
% All rights Reserved
%==========================================================================
function [gbest_value]=pso1()
close all;
clear all;
itmax=701; %Maximum iteration number
c1=1.8;
c2=1.8;
D=30;
denstination=0;
for iter=1:itmax
W(iter)=1;
end
xmax=2.048; %for rosenbrock
xmin=-2.048;%for rosenbrock
% xmax=5.12; %for rastrigin
% xmin=-5.12;%for rastrigin
num_particle=30; % number of particle, should be more than
%**********************************************************
IndexIteration=1;
tic;
particle=zeros(num_particle,D,itmax);
particle(:,:,1)=xmin+(xmax-xmin)*rand(num_particle,D);
V=round(xmin+(xmax-xmin)*rand(num_particle,D,itmax));
%**********************************************************
fitness=zeros(num_particle,itmax);% each particle's fitness value for all iterations
pbest=zeros(num_particle,D,itmax); % each particle's position for all iteration
calfitness; % get each particle's fitness value in 1st iteration
%**********************************************************
pbest_value(:,1)=fitness(:,1);
[C,I]=min(pbest_value(:,1));
gbest_value(1)=C;
gbest(1,:,1)=particle(I,:,1); % save the best particle 's position
for i=1:num_particle; % for the 1st iteration, each particle's best position is themselves
pbest(i,:,1)=particle(i,:,1);
end
%******************************************************
while IndexIteration<=(itmax-1)
IndexIteration=IndexIteration+1;
V(:,:,IndexIteration)=floor(W(IndexIteration)*V(:,:,IndexIteration-1)+c1*rand*(pbest(:,:,IndexIteration-1)...
-particle(:,:,IndexIteration-1))+c2*rand*(repmat(gbest(:,:,IndexIteration-1),num_particle,1)-particle(:,:,IndexIteration-1)));
particle(:,:,IndexIteration)=particle(:,:,IndexIteration-1)+V(:,:,IndexIteration);
particle(:,:,IndexIteration)=min(particle(:,:,IndexIteration),xmax);% make all position <=255
particle(:,:,IndexIteration)=max(particle(:,:,IndexIteration),xmin);% make all position <=255
calfitness;
%update pbest and pbest_value following
pbest_value(:,IndexIteration)=min(pbest_value(:,IndexIteration-1),fitness(:,IndexIteration));
for (temp=1:num_particle)
if pbest_value(temp,IndexIteration)== fitness(temp,IndexIteration)
pbest(temp,:,IndexIteration)=particle(temp,:,IndexIteration);
else
pbest(temp,:,IndexIteration)=pbest(temp,:,IndexIteration-1);
end
end
% update gbest and gbest_value
[C,I]=min(abs(pbest_value(:,IndexIteration)));
gbest_value(IndexIteration)=min(C,gbest_value(IndexIteration-1));
if gbest_value(IndexIteration)==C
gbest(1,:,IndexIteration)=pbest(I,:,IndexIteration); % save the best particle 's position (in other words, the best codebook)
else
gbest(1,:,IndexIteration)=gbest(1,:,IndexIteration-1);
end
end % end for while, in other words, end iteration
t1=toc;
gbest_value;
last=gbest_value(end)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -