# 【智能优化算法】基于全局优化的改进鸡群算法求解单目标优化问题（ECSO）附matlab代码

## 2 部分代码

% -----------------------------------------------------------------------------------------------------------% Chicken Swarm Optimization (CSO) (demo)% Programmed by Xian-bing Meng    % Updated 25 Aug, 2014.                     %% This is a simple demo version only implemented the basic         % idea of the CSO for solving the unconstrained problem, namely Sphere function.    % The details about CSO are illustratred in the following paper.    % (Citation details):                                                % Xian-bing Meng, Xiao-zhi Gao., A new bio-inspired algorithm: Chicken Swarm Optimization%    in: ICSI 2014, Part I, LNCS 8794, pp. 86-94 % Email: x.b.meng12@gmail.com;  xiao-zhi.gao@aalto.fi%% The parameters in CSO are presented as follows.% fitness    % The fitness function% M          % Maxmimal generations (iterations)% pop        % Population size% dim        % Number of dimensions % G          % How often the chicken swamr can be updated.% rPercent   % The population size of roosters accounts for "rPercent" percent of the total population size% hPercent   % The population size of hens accounts for "hPercent" percent of the total population size% mPercent   % The population size of mother hens accounts for "mPercent" percent of the population size of hens%% Using the default value, you can execute this algorithm using the following code.% [ bestX, fMin ] = CSO% ----------------------------------------------------------------------------------------------------------- % Main programs starts herefunction [ bestX, fMin ] = CSO( fitness, M, pop, dim, G, rPercent, hPercent, mPercent )% Display helphelp CSO.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% set the parameter valuesif nargin < 1    Func = @Sphere;    M = 1000;   % Maxmimal generations (iterations)    pop = 100;  % Population size    dim = 20;  % Number of dimensions     G = 10;                            % How often the chicken swamr can be updated. The details of its meaning are illustrated at the following codes.             rPercent = 0.2;    % The population size of roosters accounts for "rPercent" percent of the total population size    hPercent = 0.6;   % The population size of hens accounts for "hPercent" percent of the total population size    mPercent = 0.1;  % The population size of mother hens accounts for "mPercent" percent of the population size of hens                  end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%rNum = round( pop * rPercent );    % The population size of roostershNum = round( pop * hPercent );    % The population size of henscNum = pop - rNum - hNum;          % The population size of chicksmNum = round( hNum * mPercent );   % The population size of mother henslb= -100*ones( 1,dim );    % Lower limit/bounds/     a vectorub= 100*ones( 1,dim );    % Upper limit/bounds/     a vector%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Initializationfor i = 1 : pop    x( i, : ) = lb + (ub - lb) .* rand( 1, dim );   % The position of the i (th) chicken    fit( i ) = Func( x( i, : ) );                          % The fitness value of the i (th) chickenendpFit = fit;                        % The individual's best fitness valuepX = x;                            % The individual's best position corresponding to the pFit[ fMin, bestI ] = min( fit );      % fMin denotes the global optimum fitness valuebestX = x( bestI, : );             % bestX denotes the global optimum position corresponding to fMin %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Start updating the solutions. for t = 1 : M        % This parameter is to describe how the chicks would follow their mother to forage for food.    FL = rand( pop, 1 ) .* 0.4 + 0.5;  % In fact, there exist cNum chicks, thus only cNum values of FL would be used.    %Note that cNum may be dynamically changed!        % The hierarchal order, dominance relationship, mother-child relationship, the roosters, hens and the chicks in a    % group will remain unchanged. These statuses are only updated every several ( G) time steps.    % In fact, this parameter G is used to simulate the situation that the chicken swarm have been changed, including that some    % chickens have died, or the chicks have grown up and became roosters or hens,    % or some mother hens have hatched new offspring (chicks) and so on.    if( mod( t, G ) == 1 )                           sortIndex = ones( pop, 1 ) .* ( pop + 1 );   % Initialize the sortIndex, the values of which would be anything valid.        % Except the ones that are the indexs of the chicken, such as 1,2,3,……pop.                [ ans, sortIndex ] = sort( fit );     % Here ans would be unused. Only sortIndex is useful.        % Note that how the chicken swarm can be divided into several groups and the identity        % of the chickens (roosters, hens and chicks) can be determined all depend on the fitness values of the        % chickens themselves. Hence we use " sortIndex( i ) " to describe the        % chicken, not the index " i " itself.                motherLib = randperm( hNum, mNum ) + rNum;   % Randomly select which mNum hens would be the mother hens.        % We assume that all roosters are stronger than the hens, likewise, hens are stronger than the chicks.        % In CSO, the strong is reflected by the good fitness value. If the        % optimization problems is minimal ones, the more strong ones        % correspond to the ones with lower fitness values.                % Hence 1 : rNum chickens all belong to roosters.        % In turn,  (rNum + 1) : (rNum + 1 + hNum ) belong to hens, .....chicks        % Here motherLib include all the mother hens.         % motherLib is the abbreviation of "mother library".                % Given the fact the 1 : rNum chickens' fitness values maybe not        % the best rNum ones.         % Thus we use sortIndex( 1 : rNum ) to describe the roosters.              mate = randi( rNum, hNum, 1 );     % randomly select each hen's mate, rooster.        % In fact, we can determine which group each hen inhabit using "mate"        % Each rooster stands for a group.For simplicity, we assume that        % there exist only one rooster in each group.                mother = motherLib( randi( mNum, cNum, 1 ) );  % randomly select cNum chicks' mother hens    end      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%       for i = 1 : rNum                                                      % Update the rNum roosters' values.                anotherRooster = randiTabu( 1, rNum, i, 1 );  % randomly select another rooster different from the i (th) chicken.        if( pFit( sortIndex( i ) ) <= pFit( sortIndex( anotherRooster ) ) )            tempSigma = 1;        else            tempSigma = exp( ( pFit( sortIndex( anotherRooster ) ) - pFit( sortIndex( i ) ) ) / abs( pFit( sortIndex( i ) ) + 1e-50 ) );        end                x( sortIndex( i ), : ) = pX( sortIndex( i ), : ) .* ( 1 + tempSigma .* randn( 1, dim ) );        x( sortIndex( i ), : ) = Bounds( x( sortIndex( i ), : ), lb, ub );        fit( sortIndex( i ) ) = Func( x( sortIndex( i ), : ) );    end        for i = ( rNum + 1 ) : ( rNum + hNum )                     % Update the hNum hens' values.                other = randiTabu( 1,  i,  mate( i - rNum ), 1 );  % randomly select another chicken different from the i (th) chicken's mate.        % Note that the "other" chicken's fitness value should be superior        % to that of the i (th) chicken. This means the i (th) chicken may steal        % the better food found by the "other" (th) chicken.                c1 = exp( ( pFit( sortIndex( i ) ) - pFit( sortIndex( mate( i - rNum ) ) ) ) / abs( pFit( sortIndex( i ) ) + 1e-50 ) );        c2 = exp( ( -pFit( sortIndex( i ) ) + pFit( sortIndex( other ) ) ) );        x( sortIndex( i ), : ) = pX( sortIndex( i ), : ) + ( pX( sortIndex( mate( i - rNum ) ), : ) - pX( sortIndex( i ), : ) ) .* c1 .* rand( 1, dim ) +...            ( pX( sortIndex( other ), : ) - pX( sortIndex( i ), : ) ) .* c2 .* rand( 1, dim );         x( sortIndex( i ), : ) = Bounds( x( sortIndex( i ), : ), lb, ub );        fit( sortIndex( i ) ) = Func( x( sortIndex( i ), : ) );    end        for i = ( rNum + hNum + 1 ) : pop                           % Update the cNum chicks' values.        x( sortIndex( i ), : ) = pX( sortIndex( i ), : ) + ( pX( sortIndex( mother( i - rNum - hNum ) ), : ) - pX( sortIndex( i ), : ) ) .* FL( i );        x( sortIndex( i ), : ) = Bounds( x( sortIndex( i ), : ), lb, ub );        fit( sortIndex( i ) ) = Func( x( sortIndex( i ), : ) );    end        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   % Update the individual's best fitness vlaue and the global best fitness value       for i = 1 : pop         if ( fit( i ) < pFit( i ) )            pFit( i ) = fit( i );            pX( i, : ) = x( i, : );        end                if( pFit( i ) < fMin )            fMin = pFit( i );            bestX = pX( i, : );        end    endend% End of the main program%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The following functions are associated with the main program%---------------------------------------------------------------------------------------------------------------------------% Note that this function is the objective functionfunction y = Sphere( x )y = sum( x .^ 2 );% Application of simple limits/boundsfunction s = Bounds( s, Lb, Ub)  % Apply the lower bound vector  temp = s;  I = temp < Lb;  temp(I) = Lb(I);    % Apply the upper bound vector   J = temp > Ub;  temp(J) = Ub(J);  % Update this new move   s = temp;%---------------------------------------------------------------------------------------------------------------------------% Note that this function generate "dim" values, all of which are% different from the value of "tabu"function value = randiTabu( min, max, tabu, dim )value = ones( dim, 1 ) .* max .* 2;num = 1;while ( num <= dim )    temp = randi( [min, max], 1, 1 );    if( length( find( value ~= temp ) ) == dim && temp ~= tabu )        value( num ) = temp;        num = num + 1;    endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

## 4 参考文献

[1]韩斐斐, 赵齐辉, 杜兆宏,等. 全局优化的改进鸡群算法[J]. 计算机应用研究, 2019, 36(8):4.

### 博主简介：擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真，相关matlab代码问题可私信交流。

#### 部分理论引用网络文献，若有侵权联系博主删除。

|
4天前
|

Redis处理大量数据主要依赖于其内存存储结构、高效的数据结构和算法，以及一系列的优化策略
【5月更文挑战第15天】Redis处理大量数据依赖内存存储、高效数据结构和优化策略。选择合适的数据结构、利用批量操作减少网络开销、控制批量大小、使用Redis Cluster进行分布式存储、优化内存使用及监控调优是关键。通过这些方法，Redis能有效处理大量数据并保持高性能。
22 0
|
20小时前
|

m基于BP译码算法的LDPC编译码matlab误码率仿真,对比不同的码长
MATLAB 2022a仿真实现了LDPC码的性能分析，展示了不同码长对纠错能力的影响。短码长LDPC码收敛快但纠错能力有限，长码长则提供更强纠错能力但易陷入局部最优。核心代码通过循环进行误码率仿真，根据EsN0计算误比特率，并保存不同码长（12-768）的结果数据。
18 9
|
2天前
|

MATLAB|【免费】融合正余弦和柯西变异的麻雀优化算法SCSSA-CNN-BiLSTM双向长短期记忆网络预测模型

11 3
|
3天前
|

11 4
|
4天前
|

9 1
|
4天前
|

24 5
|
4天前
|

m基于遗传优化的LDPC码OMS译码算法最优偏移参数计算和误码率matlab仿真
MATLAB2022a仿真实现了遗传优化的LDPC码OSD译码算法，通过自动搜索最佳偏移参数ΔΔ以提升纠错性能。该算法结合了低密度奇偶校验码和有序统计译码理论，利用遗传算法进行全局优化，避免手动调整，提高译码效率。核心程序包括编码、调制、AWGN信道模拟及软输入软输出译码等步骤，通过仿真曲线展示了不同SNR下的误码率性能。
9 1
|
4天前
|

15 0
|
4天前
|

m基于遗传优化的LDPC码NMS译码算法最优归一化参数计算和误码率matlab仿真
MATLAB 2022a仿真实现了遗传优化的归一化最小和(NMS)译码算法，应用于低密度奇偶校验(LDPC)码。结果显示了遗传优化的迭代过程和误码率对比。遗传算法通过选择、交叉和变异操作寻找最佳归一化因子，以提升NMS译码性能。核心程序包括迭代优化、目标函数计算及性能绘图。最终，展示了SNR与误码率的关系，并保存了关键数据。
16 1
|
4天前
|

8 0