【问题标题】:Matlab function without declaration没有声明的matlab函数
【发布时间】:2016-02-07 14:47:01
【问题描述】:

我找到了这个用于 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 的结果?

【问题讨论】:

  • 请删除行号,因为这样运行代码是不可能的。同时保留 MATLAB 的原始缩进。
  • @Adriaan 我已经编辑了代码,如果你想了解更多细节可以回到这本书:《粒子群优化,经典和量子视角》

标签: matlab function


【解决方案1】:

是的,可以这样创建没有声明的函数。例如:

function [] = MyFigure()
    figure
end

并将其保存为 MyFigure.m。然后在没有输入或输出的情况下调用MyFigure 将打开一个图形。

【讨论】:

  • 在我看来,将其称为MyFigure() 通常更清楚,以表明它是一个函数。
  • @horchler 如果这让我敦促他们使用它的任何人都更清楚。然而,自从我开始使用 MATLAB 以来,我非常习惯于在没有额外括号的情况下调用函数(例如 figurecolorbar 等)。
  • 不同之处在于它们是内置函数,其行为是众所周知的。
  • @Adriaan,我如何查看 QPSO 算法的结果,当我运行代码时它没有返回任何结果。
  • @shdotcom 那么你需要为你的函数设置输出。输出是您在方括号之间设置的所需知识的变量,即function [a,b] = QPSO()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 2018-05-15
  • 1970-01-01
相关资源
最近更新 更多