请选择 进入手机版 | 继续访问电脑版

Hello Mat

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

鸡群算法

[复制链接]

1278

主题

1504

帖子

90

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22549
发表于 2018-3-26 22:26:08 | 显示全部楼层 |阅读模式
鸡群算法。。。。。
  1. % -------------------------------------------------------------------------
  2. % Chicken Swarm Optimization (CSO) (demo)
  3. % Programmed by Xian-Bing Meng
  4. % Updated at Jun 21, 2015.   
  5. % Email: x.b.meng12@gmail.com
  6. %
  7. % This is a simple demo version only implemented the basic idea of CSO for        
  8. % solving the unconstrained problem, namely Sphere function.   
  9. % The details about CSO are illustratred in the following paper.                                                
  10. % Xian-Bing Meng, et al. A new bio-inspired algorithm: Chicken Swarm
  11. %    Optimization. The Fifth International Conference on Swarm Intelligence
  12. %
  13. % The parameters in CSO are presented as follows.
  14. % FitFunc    % The objective function
  15. % M          % Maxmimal generations (iterations)
  16. % pop        % Population size
  17. % dim        % Dimension
  18. % G          % How often the chicken swarm can be updated.
  19. % rPercent   % The population size of roosters accounts for "rPercent"
  20. %   percent of the total population size
  21. % hPercent   % The population size of hens accounts for "hPercent" percent
  22. %  of the total population size
  23. % mPercent   % The population size of mother hens accounts for "mPercent"
  24. %  percent of the population size of hens
  25. %
  26. % Using the default value,CSO can be executed using the following code.
  27. % [ bestX, fMin ] = CSO
  28. % -------------------------------------------------------------------------

  29. %*************************************************************************
  30. % Revision 1
  31. % Revised at May 23, 2015
  32. % 1.Note that the previous version of CSO doen't consider the situation
  33. %   that there maybe not exist hens in a group.
  34. %   We assume there exist at least one hen in each group.

  35. % Revision 2
  36. % Revised at Jun 24, 2015
  37. % 1.Correct an error at line "100".
  38. %*************************************************************************

  39. % Main programs

  40. function [ bestX, fMin ] = CSO( FitFunc, M, pop, dim, G, rPercent, hPercent, mPercent )
  41. % Display help
  42. help CSO.m
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. % set the default parameters
  45. if nargin < 1
  46.     FitFunc = @Sphere;
  47.     M = 1000;  
  48.     pop = 100;  
  49.     dim = 20;  
  50.     G = 10;      
  51.     rPercent = 0.15;
  52.     hPercent = 0.7;  
  53.     mPercent = 0.5;                  
  54. end

  55. rNum = round( pop * rPercent );    % The population size of roosters
  56. hNum = round( pop * hPercent );    % The population size of hens
  57. cNum = pop - rNum - hNum;          % The population size of chicks
  58. mNum = round( hNum * mPercent );   % The population size of mother hens

  59. lb= -100*ones( 1,dim );   % Lower bounds
  60. ub= 100*ones( 1,dim );    % Upper bounds

  61. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  62. %Initialization
  63. for i = 1 : pop
  64.     x( i, : ) = lb + (ub - lb) .* rand( 1, dim );
  65.     fit( i ) = FitFunc( x( i, : ) );
  66. end
  67. pFit = fit; % The individual's best fitness value
  68. pX = x;     % The individual's best position corresponding to the pFit

  69. [ fMin, bestIndex ] = min( fit );        % fMin denotes the global optimum
  70. % bestX denotes the position corresponding to fMin
  71. bestX = x( bestIndex, : );   

  72. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  73. % Start the iteration.

  74. for t = 1 : M
  75.    
  76.     % This parameter is to describe how closely the chicks would follow
  77.     % their mother to forage for food. In fact, there exist cNum chicks,
  78.     % thus only cNum values of FL would be used.
  79.     FL = rand( pop, 1 ) .* 0.4 + 0.5;  
  80.    
  81.     % The chicken swarm'status about hierarchal order, dominance  
  82.     % relationship, mother-child relationship, the roosters, hens and the
  83.     % chicks in a group will remain stable during a period of time. These  
  84.     % statuses can be updated every several (G) time steps.The parameter G
  85.     % is used to simulate the situation that the chicken swarm have been  
  86.     % changed, including some chickens have died, or the chicks have grown
  87.     % up and became roosters or hens, some mother hens have hatched new
  88.     % offspring (chicks) and so on.
  89.    
  90.    if mod( t, G ) == 1 || t == 1   
  91.                
  92.         [ ans, sortIndex ] = sort( pFit );   
  93.         % How the chicken swarm can be divided into groups and the identity
  94.         % of the chickens (roosters, hens and chicks) can be determined all
  95.         % depend on the fitness values of the chickens themselves. Hence we
  96.         % use sortIndex(i) to describe the chicken, not the index i itself.
  97.         
  98.         motherLib = randperm( hNum, mNum ) + rNum;   
  99.         % Randomly select mNum hens which would be the mother hens.
  100.         % We assume that all roosters are stronger than the hens, likewise,
  101.         % hens are stronger than the chicks.In CSO, the strong is reflected
  102.         % by the good fitness value. Here, the optimization problems is
  103.         % minimal ones, thus the more strong ones correspond to the ones  
  104.         % with lower fitness values.
  105.   
  106.         % Given the fact the 1 : rNum chickens' fitness values maybe not
  107.         % the best rNum ones. Thus we use sortIndex( 1 : rNum ) to describe
  108.         % the roosters. In turn, sortIndex( (rNum + 1) :(rNum + 1 + hNum ))
  109.         % to describle the mother hens, .....chicks.

  110.         % Here motherLib include all the mother hens.
  111.       
  112.        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  113.         % Randomly select each hen's mate, rooster. Hence we can determine
  114.         % which group each hen inhabits using "mate".Each rooster stands
  115.         % for a group.For simplicity, we assume that there exist only one
  116.         % rooster and at least one hen in each group.
  117.         mate = randpermF( rNum, hNum );
  118.         
  119.         % Randomly select cNum chicks' mother hens
  120.         mother = motherLib( randi( mNum, cNum, 1 ) );  
  121.    end
  122.    
  123.    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
  124.    for i = 1 : rNum                % Update the rNum roosters' values.
  125.         
  126.         % randomly select another rooster different from the i (th) one.
  127.         anotherRooster = randiTabu( 1, rNum, i, 1 );  
  128.         if( pFit( sortIndex( i ) ) <= pFit( sortIndex( anotherRooster ) ) )
  129.             tempSigma = 1;
  130.         else
  131.             tempSigma = exp( ( pFit( sortIndex( anotherRooster ) ) - ...
  132.                 pFit( sortIndex( i ) ) ) / ( abs( pFit( sortIndex(i) ) )...
  133.                 + realmin ) );
  134.         end
  135.         
  136.         x( sortIndex( i ), : ) = pX( sortIndex( i ), : ) .* ( 1 + ...
  137.             tempSigma .* randn( 1, dim ) );
  138.         x( sortIndex( i ), : ) = Bounds( x( sortIndex( i ), : ), lb, ub );
  139.         fit( sortIndex( i ) ) = FitFunc( x( sortIndex( i ), : ) );
  140.     end
  141.    
  142.     for i = ( rNum + 1 ) : ( rNum + hNum )  % Update the hNum hens' values.
  143.         
  144.         other = randiTabu( 1,  i,  mate( i - rNum ), 1 );  
  145.         % randomly select another chicken different from the i (th)  
  146.         % chicken's mate. Note that the "other" chicken's fitness value  
  147.         % should be superior to that of the i (th) chicken. This means the   
  148.         % i (th) chicken may steal the better food found by the "other"
  149.         % (th) chicken.
  150.         
  151.         c1 = exp( ( pFit( sortIndex( i ) ) - pFit( sortIndex( mate( i - ...
  152.             rNum ) ) ) )/ ( abs( pFit( sortIndex(i) ) ) + realmin ) );
  153.             
  154.         c2 = exp( ( -pFit( sortIndex( i ) ) + pFit( sortIndex( other ) )));

  155.         x( sortIndex( i ), : ) = pX( sortIndex( i ), : ) + ( pX(...
  156.             sortIndex( mate( i - rNum ) ), : )- pX( sortIndex( i ), : ) )...
  157.              .* c1 .* rand( 1, dim ) + ( pX( sortIndex( other ), : ) - ...
  158.              pX( sortIndex( i ), : ) ) .* c2 .* rand( 1, dim );
  159.         x( sortIndex( i ), : ) = Bounds( x( sortIndex( i ), : ), lb, ub );
  160.         fit( sortIndex( i ) ) = FitFunc( x( sortIndex( i ), : ) );
  161.     end
  162.    
  163.     for i = ( rNum + hNum + 1 ) : pop    % Update the cNum chicks' values.
  164.         x( sortIndex( i ), : ) = pX( sortIndex( i ), : ) + ( pX( ...
  165.             sortIndex( mother( i - rNum - hNum ) ), : ) - ...
  166.             pX( sortIndex( i ), : ) ) .* FL( i );
  167.         x( sortIndex( i ), : ) = Bounds( x( sortIndex( i ), : ), lb, ub );
  168.         fit( sortIndex( i ) ) = FitFunc( x( sortIndex( i ), : ) );
  169.     end
  170.    
  171.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  172.    % Update the individual's best fitness vlaue and the global best one
  173.    
  174.     for i = 1 : pop
  175.         if ( fit( i ) < pFit( i ) )
  176.             pFit( i ) = fit( i );
  177.             pX( i, : ) = x( i, : );
  178.         end
  179.         
  180.         if( pFit( i ) < fMin )
  181.             fMin = pFit( i );
  182.             bestX = pX( i, : );
  183.         end
  184.     end
  185. end
  186. % End of the main program

  187. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  188. % The following functions are associated with the main program
  189. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  190. % This function is the objective function
  191. function y = Sphere( x )
  192. y = sum( x .^ 2 );

  193. % Application of simple limits/bounds
  194. function s = Bounds( s, Lb, Ub)
  195.   % Apply the lower bound vector
  196.   temp = s;
  197.   I = temp < Lb;
  198.   temp(I) = Lb(I);
  199.   
  200.   % Apply the upper bound vector
  201.   J = temp > Ub;
  202.   temp(J) = Ub(J);
  203.   % Update this new move
  204.   s = temp;

  205. %--------------------------------------------------------------------------
  206. % This function generate "dim" values, all of which are different from
  207. %  the value of "tabu"
  208. function value = randiTabu( min, max, tabu, dim )
  209. value = ones( dim, 1 ) .* max .* 2;
  210. num = 1;
  211. while ( num <= dim )
  212.     temp = randi( [min, max], 1, 1 );
  213.     if( length( find( value ~= temp ) ) == dim && temp ~= tabu )
  214.         value( num ) = temp;
  215.         num = num + 1;
  216.     end
  217. end

  218. %--------------------------------------------------------------------------
  219. function result = randpermF( range, dim )
  220. % The original function "randperm" in Matlab is only confined to the
  221. % situation that dimension is no bigger than dim. This function is
  222. % applied to solve that situation.

  223. temp = randperm( range, range );
  224. temp2 = randi( range, dim, 1 );
  225. index = randperm( dim, ( dim - range ) );
  226. result = [ temp, temp2( index )' ];
复制代码
代码下载:https://pan.baidu.com/s/18So1iW4q2ifcMruSuksqEg




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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 00:28 , Processed in 0.198237 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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