필터 지우기
필터 지우기

this code of BAT algorithm is not working..function i wrote i last three lines..plz guide me

조회 수: 1 (최근 30일)
function [best,fmin,N_iter]=bat_algorithm(para)
% Display help
 help bat_algorithm.m
% Default parameters
if nargin<1,  para=[20 1000 0.5 0.5];  end
n=para(1);      % Population size, typically 10 to 40
N_gen=para(2);  % Number of generations
A=para(3);      % Loudness  (constant or decreasing)
r=para(4);      % Pulse rate (constant or decreasing)
% This frequency range determines the scalings
% You should change these values if necessary
Qmin=0;         % Frequency minimum
Qmax=2;         % Frequency maximum
% Iteration parameters
N_iter=0;       % Total number of function evaluations
% Dimension of the search variables
d=5;           % Number of dimensions 
% Lower limit/bounds/ a vector
Lb=-3*ones(1,d);
% Upper limit/bounds/ a vector
Ub=6*ones(1,d);
% Initializing arrays
Q=zeros(n,1);   % Frequency
v=zeros(n,d);   % Velocities
% Initialize the population/solutions
for i=1:n
  Sol(i,:)=Lb+(Ub-Lb).*rand(1,d);
  Fitness(i)=Fun(Sol(i,:));
end
% Find the initial best solution
[fmin,I]=min(Fitness);
best=Sol(I,:);
for t=1:N_gen, 
% Loop over all bats/solutions
        for i=1:n,
          Q(i)=Qmin+(Qmin-Qmax)*rand;
          v(i,:)=v(i,:)+(Sol(i,:)-best)*Q(i);
          S(i,:)=Sol(i,:)+v(i,:);
          % Apply simple bounds/limits
          Sol(i,:)=simplebounds(Sol(i,:),Lb,Ub);
          % Pulse rate
          if rand>r
          % The factor 0.001 limits the step sizes of random walks 
              S(i,:)=best+0.001*randn(1,d);
          end
       % Evaluate new solutions
             Fnew=Fun(S(i,:));
       % Update if the solution improves, or not too loud
             if (Fnew<=Fitness(i)) & (rand<A) ,
                  Sol(i,:)=S(i,:);
                  Fitness(i)=Fnew;
             end
            % Update the current best solution
            if Fnew<=fmin,
                  best=S(i,:);
                  fmin=Fnew;
            end
          end
          N_iter=N_iter+n;
  end
  % Output/display
  disp(['Number of evaluations: ',num2str(N_iter)]);
  disp(['Best =',num2str(best),' fmin=',num2str(fmin)]);
% Application of simple limits/bounds
function s=simplebounds(s,Lb,Ub)
  % Apply the lower bound vector
  ns_tmp=s;
  I=ns_tmp<Lb;
  ns_tmp(I)=Lb(I);
    % Apply the upper bound vector 
    J=ns_tmp>Ub;
    ns_tmp(J)=Ub(J);
    % Update this new move 
    s=ns_tmp;
  xdata =[ 10.^(-3) 10.^(-2) 10.^(-1) 10.^(0) 10.^(1) 10.^(2)]  ; 
  ydata=(0.5012./(1.9*xdata.^2+1.12*xdata+2));
   function fun=@(x)sum(x(1)./((x(2).*xdata.^1.1+x(3).*xdata.^0.1+1)- ydata).^2);
  댓글 수: 2
Image Analyst
Image Analyst 2018년 5월 2일
I've rescued this from the spam quarantine, put there probably because your code is not formatted. Please read this and fix your post so it won't end up there again. http://www.mathworks.com/matlabcentral/answers/13205#answer_18099
ash
ash 2018년 7월 16일
편집: Walter Roberson 2018년 7월 16일
function [best,fmin,N_iter]=bat_algorithm(para)
% Default parameters
if nargin<1, para=[20 1000 0.5 0.5]; end
n=para(1); % Population size, typically 10 to 40
N_gen=para(2); % Number of generations
A=para(3); % Loudness (constant or decreasing)
r=para(4); % Pulse rate (constant or decreasing)
% This frequency range determines the scalings
Qmin=0; % Frequency minimum
Qmax=2; % Frequency maximum
% Iteration parameters
N_iter=0; % Total number of function evaluations
% Dimension of the search variables
d=5; % Number of dimensions
% Lower limit/bounds/ a vector
Lb=-3*ones(1,d);
% Upper limit/bounds/ a vector
Ub=6*ones(1,d);
% Initializing arrays
Q=zeros(n,1); % Frequency
v=zeros(n,d); % Velocities
% Initialize the population/solutions
for i=1:n
Sol(i,:)=Lb+(Ub-Lb).*rand(1,d);
Fitness(i)=fun(Sol(i,:));
end
% Find the initial best solution
[fmin,I]=min(Fitness);
best=Sol(I,:);
for t=1:N_gen,
% Loop over all bats/solutions
for i=1:n,
Q(i)=Qmin+(Qmin-Qmax)*rand;
v(i,:)=v(i,:)+(Sol(i,:)-best)*Q(i);
S(i,:)=Sol(i,:)+v(i,:);
% Apply simple bounds/limits
Sol(i,:)=simplebounds(Sol(i,:),Lb,Ub);
% Pulse rate
if rand>r
% The factor 0.001 limits the step sizes of random walks
S(i,:)=best+0.001*randn(1,d);
end
% Evaluate new solutions
Fnew=fun(S(i,:));
% Update if the solution improves, or not too loud
if (Fnew<=Fitness(i)) & (rand<A) ,
Sol(i,:)=S(i,:);
Fitness(i)=Fnew;
end
% Update the current best solution
if Fnew<=fmin,
best=S(i,:);
fmin=Fnew;
end
end
N_iter=N_iter+n;
end
% Output/display
disp(['Number of evaluations: ',num2str(N_iter)]);
disp(['Best =',num2str(best),' fmin=',num2str(fmin)]);
% Application of simple limits/bounds
function s=simplebounds(s,Lb,Ub)
% Apply the lower bound vector
ns_tmp=s;
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
% Apply the upper bound vector
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
% Update this new move
s=ns_tmp;
function z=fun(x,xdata,i)
xdata =[10.^(-5) 10.^(-4) 10.^(-3) 10.^(-2) 10.^(-1) 1];
ydata=(0.5012./(xdata.^1.9+2*xdata.^0.9+1 ));
fun = @(x) sum((x(1)./((x(2).*(xdata.^(1+0.9)))+x(3).*(xdata.^(0.9))+1) - ydata).^2);
%%%%%============ end ====================================

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Smoothing and Denoising에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by