Hello Mat

 找回密码
 立即注册
查看: 5571|回复: 1

鸟群算法

[复制链接]

1323

主题

1551

帖子

0

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22647
发表于 2018-3-26 22:31:45 | 显示全部楼层 |阅读模式
鸟群算法。。。。。
  1. % ------------------------------------------------------------------------
  2. % Bird Swarm Algorithm (BSA) (demo)
  3. % Programmed by Xian-Bing Meng
  4. % Updated at Jun 19, 2015.   
  5. % Email: x.b.meng12@gmail.com
  6. %
  7. % This is a simple demo version only implemented the basic idea of BSA for        
  8. % solving the unconstrained problem, namely Sphere function.  
  9. %
  10. % The details about BSA are illustratred in the following paper.   
  11. % Xian-Bing Meng, et al (2015): A new bio-inspXred optimisation algorithm:
  12. % Bird Swarm Algorithm, Journal of Experimental & Theoretical
  13. % Artificial Intelligence, DOI: 10.1080/0952813X.2015.1042530
  14. %
  15. % The parameters in BSA are presented as follows.
  16. % FitFunc    % The objective function
  17. % M          % Maxmimal generations (iterations)
  18. % pop        % Population size
  19. % dim        % Dimension
  20. % FQ         % The frequency of birds' flight behaviours
  21. % c1         % Cognitive accelerated coefficient
  22. % c2         % Social accelerated coefficient
  23. % a1, a2     % Two paramters which are related to the indirect and direct
  24. %              effect on the birds' vigilance bahaviors.
  25. %
  26. % Using the default value, BSA can be executed using the following code.
  27. % [ bestX, fMin ] = BSA
  28. % ------------------------------------------------------------------------

  29. % Main programs

  30. function [ bestX, fMin ] = BSA( FitFunc, M, pop, dim, FQ, c1, c2, a1, a2 )
  31. % Display help
  32. help BSA.m
  33. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  34. % set the default parameters
  35. if nargin < 1
  36.     FitFunc = @Sphere;
  37.     M = 1000;   
  38.     pop = 30;  
  39.     dim = 20;   
  40.     FQ = 10;   
  41.     c1 = 1.5;
  42.     c2 = 1.5;
  43.     a1 = 1;
  44.     a2 = 1;
  45. end

  46. % set the parameters
  47. lb= -100*ones( 1,dim );   % Lower bounds
  48. ub= 100*ones( 1,dim );    % Upper bounds
  49. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  50. %Initialization

  51. for i = 1 : pop
  52.     x( i, : ) = lb + (ub - lb) .* rand( 1, dim );
  53.     fit( i ) = FitFunc( x( i, : ) );
  54. end
  55. pFit = fit; % The individual's best fitness value
  56. pX = x;     % The individual's best position corresponding to the pFit

  57. [ fMin, bestIndex ] = min( fit );  % fMin denotes the global optimum
  58. % bestX denotes the position corresponding to fMin
  59. bestX = x( bestIndex, : );   

  60. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  61. % Start the iteration.

  62. for iteration = 1 : M
  63.      
  64.     prob = rand( pop, 1 ) .* 0.2 + 0.8;%The probability of foraging for food
  65.    
  66.     if( mod( iteration, FQ ) ~= 0 )         
  67.         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  68.         % Birds forage for food or keep vigilance
  69.         sumPfit = sum( pFit );
  70.         meanP = mean( pX );
  71.         for i = 1 : pop
  72.             if rand < prob(i)
  73.                 x( i, : ) = x( i, : ) + c1 * rand.*(bestX - x( i, : ))+ ...
  74.                     c2 * rand.*( pX(i,:) - x( i, : ) );
  75.             else
  76.                 person = randiTabu( 1, pop, i, 1 );
  77.                
  78.                 x( i, : ) = x( i, : ) + rand.*(meanP - x( i, : )) * a1 * ...
  79.                     exp( -pFit(i)/( sumPfit + realmin) * pop ) + a2 * ...
  80.                     ( rand*2 - 1) .* ( pX(person,:) - x( i, : ) ) * exp( ...
  81.                     -(pFit(person) - pFit(i))/(abs( pFit(person)-pFit(i) )...
  82.                     + realmin) * pFit(person)/(sumPfit + realmin) * pop );
  83.             end
  84.             
  85.             x( i, : ) = Bounds( x( i, : ), lb, ub );  
  86.             fit( i ) = FitFunc( x( i, : ) );
  87.         end
  88.         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  89.    
  90.     else
  91.         FL = rand( pop, 1 ) .* 0.4 + 0.5;    %The followed coefficient
  92.         
  93.         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  94.         % Divide the bird swarm into two parts: producers and scroungers.
  95.         [ans, minIndex ] = min( pFit );
  96.         [ans, maxIndex ] = max( pFit );
  97.         choose = 0;
  98.         if ( minIndex < 0.5*pop && maxIndex < 0.5*pop )
  99.             choose = 1;
  100.         end
  101.         if ( minIndex > 0.5*pop && maxIndex < 0.5*pop )
  102.             choose = 2;
  103.         end
  104.         if ( minIndex < 0.5*pop && maxIndex > 0.5*pop )
  105.             choose = 3;
  106.         end
  107.         if ( minIndex > 0.5*pop && maxIndex > 0.5*pop )
  108.             choose = 4;
  109.         end
  110.         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  111.         if choose < 3
  112.             for i = (pop/2+1) : pop
  113.                 x( i, : ) = x( i, : ) * ( 1 + randn );
  114.                 x( i, : ) = Bounds( x( i, : ), lb, ub );
  115.                 fit( i ) = FitFunc( x( i, : ) );
  116.             end
  117.             if choose == 1
  118.                 x( minIndex,: ) = x( minIndex,: ) * ( 1 + randn );
  119.                 x( minIndex, : ) = Bounds( x( minIndex, : ), lb, ub );
  120.                 fit( minIndex ) = FitFunc( x( minIndex, : ) );
  121.             end
  122.             for i = 1 : 0.5*pop
  123.                 if choose == 2 || minIndex ~= i
  124.                     person = randi( [(0.5*pop+1), pop ], 1 );
  125.                     x( i, : ) = x( i, : ) + (pX(person, :) - x( i, : )) * FL( i );
  126.                     x( i, : ) = Bounds( x( i, : ), lb, ub );
  127.                     fit( i ) = FitFunc( x( i, : ) );
  128.                 end
  129.             end
  130.         else
  131.             for i = 1 : 0.5*pop
  132.                 x( i, : ) = x( i, : ) * ( 1 + randn );
  133.                 x( i, : ) = Bounds( x( i, : ), lb, ub );
  134.                 fit( i ) = FitFunc( x( i, : ) );
  135.             end
  136.             if choose == 4
  137.                 x( minIndex,: ) = x( minIndex,: ) * ( 1 + randn );
  138.                 x( minIndex, : ) = Bounds( x( minIndex, : ), lb, ub );
  139.                 fit( minIndex ) = FitFunc( x( minIndex, : ) );
  140.             end
  141.             for i = (0.5*pop+1) : pop
  142.                 if choose == 3 || minIndex ~= i
  143.                     person = randi( [1, 0.5*pop], 1 );
  144.                     x( i, : ) = x( i, : ) + (pX(person, :) - x( i, : )) * FL( i );
  145.                     x( i, : ) = Bounds( x( i, : ), lb, ub );
  146.                     fit( i ) = FitFunc( x( i, : ) );
  147.                 end
  148.             end   
  149.         end
  150.         
  151.     end
  152.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  153.     % Update the individual's best fitness vlaue and the global best one
  154.    
  155.     for i = 1 : pop
  156.         if ( fit( i ) < pFit( i ) )
  157.             pFit( i ) = fit( i );
  158.             pX( i, : ) = x( i, : );
  159.         end
  160.         
  161.         if( pFit( i ) < fMin )
  162.             fMin = pFit( i );
  163.             bestX = pX( i, : );
  164.         end
  165.     end
  166.    
  167. end

  168. % End of the main program

  169. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  170. % The following functions are associated with the main program
  171. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  172. % This function is the objective function
  173. function y = Sphere( x )
  174. y = sum( x .^ 2 );

  175. % Application of simple limits/bounds
  176. function s = Bounds( s, Lb, Ub)
  177.   % Apply the lower bound vector
  178.   temp = s;
  179.   I = temp < Lb;
  180.   temp(I) = Lb(I);
  181.   
  182.   % Apply the upper bound vector
  183.   J = temp > Ub;
  184.   temp(J) = Ub(J);
  185.   % Update this new move
  186.   s = temp;

  187. %--------------------------------------------------------------------------
  188. % This function generate "dim" values, all of which are different from
  189. %  the value of "tabu"
  190. function value = randiTabu( min, max, tabu, dim )
  191. value = ones( dim, 1 ) .* max .* 2;
  192. num = 1;
  193. while ( num <= dim )
  194.     temp = randi( [min, max], 1, 1 );
  195.     if( length( find( value ~= temp ) ) == dim && temp ~= tabu )
  196.         value( num ) = temp;
  197.         num = num + 1;
  198.     end
  199. end
复制代码
代码+pdf下载:https://pan.baidu.com/s/1yp-WWOM4gcKjWWTzmnvK9Q







算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-11-22 19:13 , Processed in 0.214464 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表