布谷鸟优化算法 matlab,布谷鸟算法(Cuckoo Search,CS)MATLAB案例详细解析

一、布谷鸟算法理论

模拟退火算法(SA)、遗传算法(GA)、布谷鸟算法(CS)、人工蜂群算法(ABC)学习笔记—附MATLAB注释代码

二、CS算法应用于函数优化

1.流程图

布谷鸟优化算法 matlab,布谷鸟算法(Cuckoo Search,CS)MATLAB案例详细解析_第1张图片

3.代码解析

3.1 主函数 Csmain.m

% Script 布谷鸟算法,求解函数最小值

% @author zhaoyuqiang

%#ok Remove hints of syntax

%#ok

%#ok

clear all ;

close all ;

clc ;

N = 25; % 鸟巢的数量Number of nests(The scale of solution)

D = 10 ; % 问题的维度,一个鸟巢鸟蛋的个数 Dimensionality of solution

T =500 ; %迭代次数的上限 Number of iterations

Xmax = pi ;%%函数上限

Xmin = -pi ;%%函数下限

Pa = 0.25 ; % Probability of building a new nest(After host bird find exotic bird eggs)

nestPop = rand(N,D)*(Xmax-Xmin)+Xmin ; % 初始化寄主的鸟巢Random initial solutions

for t=1:T

levy_nestPop = func_levy(nestPop,Xmax,Xmin) ; % 通过levy飞行产生一个解Generate new solutions by Levy flights

nestPop = func_bestNestPop(nestPop,levy_nestPop); % 与上一代比较,更新适应度较优的鸟巢Choose a best nest among new and old nests

rand_nestPop = func_newBuildNest(nestPop,Pa,Xmax,Xmin); % 根据发现概率舍弃一个鸟巢并建立一个新鸟巢Abandon(Pa) worse nests and build new nests by (Preference random walk )

nestPop = func_bestNestPop(nestPop,rand_nestPop) ; %列出当前最佳的鸟巢 Choose a best nest among new and old nests

[~,index] = max(func_fitness(nestPop)) ; % Best nests更新当代最优鸟巢的位置

trace(t) = func_objValue(nestPop(index,:)) ;

end

[~,index] = max(func_fitness(nestPop)) ; % 查找当前最优鸟巢

%%%输出这个鸟巢里的每个鸟蛋,即是每个解

nestPop(index,:)

figure

plot(trace);

xlabel('迭代次数') ;

ylabel('适应度值') ;

title('适应度进化曲线') ;

3.2 Levy飞行 func_levy.m

说白了就是实现一个随机搜索的公式来更新鸟巢的位置,

Xt+1 = Xt + α \alphaα S

S就是服从Levy分布

Levy~u = t -β \betaβ ,1<= β \betaβ <=3的随机步长。

具体公式解析参考:

通俗易懂的布谷鸟算法与莱维飞行,(附求解函数最小值matlab源码)

function [ result ] = func_levy( nestPop,Xmax,Xmin)

%FUNC_LEVY : Update position of nest by using Levy flights

%@author : zhaoyuqiang

[N,D] = size(nestPop) ;

% Levy flights by Mantegna's algorithm

beta = 1.5 ;

alpha = 1 ;

sigma_u = (gamma(1+beta)*sin(pi*beta/2)/(beta*gamma((1+beta)/2)*2^((beta-1)/2)))^(1/beta) ;

sigma_v = 1 ;

u = normrnd(0,sigma_u,N,D) ;%(第一个参数代表均值,sigma参数代表标准差),生成N×D形式的正态分布的随机数矩阵。

v = normrnd(0,sigma_v,N,D) ;

step = u./(abs(v).^(1/beta)) ;

% alpha = 0.1.*(nestPop(randperm(N),:)-nestPop(randperm(N),:)); % Bad effect

nestPop = nestPop+alpha.*step ;

% Deal with bounds

nestPop(find(nestPop>Xmax)) = Xmax ; %#ok%查找大于Xmax的元素

nestPop(find(nestPop

3.3 与上一代比较,返回较优的鸟巢 func_bestNestPop.m

function [ nestPop ] = func_bestNestPop( nestPop,new_nestPop )

%FUNC_ 此处显示有关此函数的摘要

%@author zhaoyuqiang

index = find(func_fitness(nestPop)

3.4 根据发现概率,舍弃一个鸟巢并建立一个新鸟巢 func_newBuildNest.m

function [ nestPop ] = func_newBuildNest( nestPop ,Pa ,Xmax,Xmin)

%FUNC_NEWBUILDNEST new solutions are generated by using the similarity

% between the existing eggs/solutions and the host eggs/solutions with a discovery rate pa .

%@author zhaoyuqiang

[N,D] = size(nestPop) ;

%%根据发现概率发现鸟蛋,舍弃鸟窝

nestPop = nestPop+rand.*heaviside(rand(N,D)-Pa).*(nestPop(randperm(N),:)-nestPop(randperm(N),:));

% Deal with bounds

nestPop(find(nestPop>Xmax)) = Xmax ; %#ok建立新的鸟窝

nestPop(find(nestPop

3.5 目标函数

function [ result ] = func_objValue( pop )

%FUNC_OBJVALUE 计算目标函数

objValue = sum(pop.^2,2);

result = objValue ;

end

三、输出结果

适应度变化曲线

布谷鸟优化算法 matlab,布谷鸟算法(Cuckoo Search,CS)MATLAB案例详细解析_第2张图片

打印最优解

布谷鸟优化算法 matlab,布谷鸟算法(Cuckoo Search,CS)MATLAB案例详细解析_第3张图片

四、CS案例MATLAB源码下载

布谷鸟算法应用与函数优化详细解析代码以及参考资料.zip

你可能感兴趣的:(布谷鸟优化算法,matlab)