我已经找到了QPSO(量子粒子群优化)的Matlab代码。 但QPSO.m没有函数声明。这是否意味着它应该是这样的:
function []= QPSO()
1. popsize=20; % population size
2. MAXITER=3000; % Maximum number of iterations
.......
44. data(run, n)=f_gbest; % Record the fitness value of the
gbest at each iteration at this run
45. end
46. end
end
=================完整源代码=====================
sphere.m
function y=sphere(x)
y=x*x';
end
QPSO.m
%%%%%%%QPSO.m (Source Code of QPSO with Sphere Function as a Benchmark) %%%%%%%%%
popsize=20; % population size
MAXITER=3000; % Maximum number of iterations
dimension=30; % Dimensionality of the problem
irange_l=-100; % Lower bound of initialization scope
irange_r=100; % Upper bound of initialization scope
xmax=100; % Upper bound of the search scope
xmin=-100; % Lower bound of the search scope
M=(xmax-xmin)/2; % The middle point of the search cope on each dimension
alpha=0.75; % A fixed value is used. It can be changed.
runno=50; % Number of times the algorithm runs
data=zeros(runno, MAXITER); % The matrix recording the
fitness value of gbest position at each iteration
%%%%%%%%%%%% Running for runno times%%%%%%%%%%%
for run=1:runno
%%%%%%%%%%%%%%% Initialization of the particle swarm %%%%%%%%%%%
x=(irange_r-irange_l)*rand(popsize,dimension,1) + irange_l; % Initialize the particle position
pbest=x; %Set the pbest position as the current position of the particle
gbest=zeros(1,dimension); % Initialize the gbest poistion vector
for i=1:popsize
f_x(i)=sphere(x(i,:)); %Calculate the fitness value of the current position of the particle
f_pbest(i)=f_x(i);% Set the fitness value of the pbest position to be that of the current position
end
g=min(find(f_pbest==min(f_pbest(1:popsize)))); % Find index of the particle with gbest position
gbest=pbest(g,:); % Determine the gbest position
f_gbest=f_pbest(g); % Determine the fitness value of the gbest position
%%%%%%%%%%%%% The following is the loop of the QPSO’s search process %%%%%%%%%%%
for n=1:MAXITER
% alpha=(1.0-0.5)*(MAXITER-n)/MAXITER+0.5; % Determine the value of alpha
mbest=sum(pbest)/popsize; % Calculate the mbest position
for i=1:popsize %The following is the update of the particle’s position
fi=rand(1,dimension); % Generate a vector of random numbers with distribution U(0,1)
p=fi.*pbest(i,:)+(1-fi).*gbest; % Determine the vector
local focus of the particle
u=rand(1,dimension);
x(i,:)=p+((-1).^ceil(0.5+rand(1,dimension))).*(alpha.*abs(mbest-x(i,:)).*log(1./u));
x(i,:)=x(i,:)-(xmax+xmin)/2;% These tree lines are to restrict the position in search scopes
x(i,:)=sign(x(i,:)).*min(abs(x(i,:)),M);
x(i,:)=x(i,:)+(xmax+xmin)/2;
f_x(i)=sphere(x(i,:)); % Calculate the fitness value of the particle’s current position
if (f_x(i)<f_pbest(i))
pbest(i,:)=x(i,:);% Update the pbest position of the particle
f_pbest(i)=f_x(i);% Update the fitness value of the particle’s pbest position
end
if f_pbest(i)<f_gbest
gbest=pbest(i,:); % Update the gbest position
f_gbest=f_pbest(i); % Update the fitness value of the gbest position
end
end
data(run, n)=f_gbest; % Record the fitness value of the gbest at each iteration at this run
端 端
这里也没有函数声明:
%%%%%The following source codes are used to update the particle’s position in each dimension%%%%%
for d=1: dimension
fi=rand; % Generate the random number ◽∼U(0,1)
p=fi*pbest(i,d)+(1-fi)*gbest(d); % Determine the component of the local focus on each dimension
u=rand; % Generate the random number u.∼U(0,1)
if rand>0.5
x(i,d)=p+alpha*abs(mbest(d)-x(i,d))*log(1/u); % Use “+” operation in the evolution equation
else
x(i,d)=p-alpha*abs(mbest(d)-x(i,d))*log(1/u); % Use “-” operation in the evolution equation
end
if x(i,d)>xmax;
x(i,d)=xmax; % If the component of the position larger than xmax, set it to be xmax
end
if x(i,d)<-xmin;
x(i,d)=-xmin; % If the component of the position smaller than xmin, set it to be xmin
end
end
如何使用Plot显示QPSO的结果?
答案 0 :(得分:3)
是的,因此可以进行没有声明的功能。例如:
function [] = MyFigure()
figure
end
并将其另存为 MyFigure.m 。然后在没有输入或输出的情况下调用MyFigure
将打开一个数字。