zoukankan      html  css  js  c++  java
  • 蝙蝠算法初探

    蝙蝠算法初探

    function [best,fmin,N_iter]=bat_algorithm()  
    n=20;  % Population size, typically 10 to 40  蝙蝠个体数
    N_gen=1000;  % Number of generations  迭代次数
    % 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=10;           % Number of dimensions 
    
    A=1+rand(n,1);    % Loudness  (constant or decreasing)响度,按照p8要求产生[1,2]的随机数
    r=rand(n,1);      % Pulse rate (constant or decreasing)脉冲率,设置为[0,1]的随机数
    al = 0.85;        
    rr = 0.9;
    r0 = r;
    
    % Lower limit/bounds/ a vector
    Lb=-2*ones(1,d);
    % Upper limit/bounds/ a vector
    Ub=2*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);  %rand(m*n)会生成  m*n的矩阵,矩阵元素是[0,10]随机数
      Fitness(i)=Fun(Sol(i,:));
    end
    % Find the initial best solution
    [fmin,I]=min(Fitness);   %I 记录取得fmin的Fitness的位置,而这位置正是Sol中解的位置;fmin是Fitness中最小的值
    best=Sol(I,:);           %记录最好的解
    
    % Start the iterations -- Bat Algorithm (essential part)  %
    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(i,1)
              % 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(i,1)))
                    Sol(i,:)=S(i,:);
                    Fitness(i)=Fnew;
                    A(i,1) = al*A(i,1);               %对响度进行更新
                    r(i,1) = r0(i,1)*(1-exp(-1*rr*t ));  %对脉冲率进行更新
               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;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Objective function: your own objective function can be written here
    % Note: When you use your own function, please remember to 
    %       change limits/bounds Lb and Ub (see lines 52 to 55) 
    %       and the number of dimension d (see line 51). 
    % 在这里设置你自己函数,并且更新搜索区间上限和下限,以及自变量x的维度
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function y=Fun(x)
    % Griewan函数
    % 输入x,给出相应的y值,在x = ( 0 , 0 ,…, 0 )处有全局极小点0.
        [row,col] = size(x);
        if  row > 1 
            error( ' 输入的参数错误 ' );
        end
        y1 = 1 / 4000 * sum(x.^2 );    
        y2 = 1 ;
        for  h = 1 :col
            y2 = y2 * cos(x(h) / sqrt(h));
        end    
        y = y1 - y2 + 1 ; 
    %%%%% ============ end ====================================
    

      

    参考文献:蝙蝠算法的改进与应用   何子旷  广东工业大学硕士学位论文  2016.5

  • 相关阅读:
    HDU 2433 Travel (最短路,BFS,变形)
    HDU 2544 最短路 (最短路,spfa)
    HDU 2063 过山车 (最大匹配,匈牙利算法)
    HDU 1150 Machine Schedule (最小覆盖,匈牙利算法)
    290 Word Pattern 单词模式
    289 Game of Life 生命的游戏
    287 Find the Duplicate Number 寻找重复数
    283 Move Zeroes 移动零
    282 Expression Add Operators 给表达式添加运算符
    279 Perfect Squares 完美平方数
  • 原文地址:https://www.cnblogs.com/liugl7/p/7788510.html
Copyright © 2011-2022 走看看