首页 > 编程知识 正文

布谷鸟算法Cuckoo SearchCSMATLAB案例详细解析,扫描算法scan详细解析

时间:2023-05-03 16:57:44 阅读:242682 作者:1962

目录 一、布谷鸟算法理论二、CS算法应用于函数优化1.流程图3.代码解析3.1 主函数 Csmain.m3.2 Levy飞行 func_levy.m3.3 与上一代比较,返回较优的鸟巢 func_bestNestPop.m3.4 根据发现概率,舍弃一个鸟巢并建立一个新鸟巢 func_newBuildNest.m3.5 目标函数3.6 计算适应度函数 三、输出结果四、CS案例MATLAB源码下载

一、布谷鸟算法理论

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

二、CS算法应用于函数优化 1.流程图

3.代码解析 3.1 主函数 Csmain.m % Script 布谷鸟算法,求解函数最小值% @author zhaoyuqiang %#ok<*SAGROW> Remove hints of syntax%#ok<*CLALL>%#ok<*FNDSB>clear all ; close all ;clc ;N = 25; % 鸟巢的数量Number of nests(The scale of solution)D = 10 ; % 问题的维度,一个鸟巢鸟蛋的个数 Dimensionality of solutionT =500 ; %迭代次数的上限 Number of iterationsXmax = 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 solutionsfor 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 algorithmbeta = 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 effectnestPop = nestPop+alpha.*step ;% Deal with boundsnestPop(find(nestPop>Xmax)) = Xmax ; %#ok<*FNDSB>%查找大于Xmax的元素nestPop(find(nestPop<Xmin)) = Xmin ;result = nestPop ; end 3.3 与上一代比较,返回较优的鸟巢 func_bestNestPop.m function [ nestPop ] = func_bestNestPop( nestPop,new_nestPop )%FUNC_ 此处显示有关此函数的摘要%@author zhaoyuqiangindex = find(func_fitness(nestPop)<func_fitness(new_nestPop)) ;%与上一代比较适应度,选择一个适应度更大的更新鸟窝nestPop(index,:) = new_nestPop(index,:) ;%nestPop(index) = new_nestPop(index) ;end 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 boundsnestPop(find(nestPop>Xmax)) = Xmax ; %#ok<*FNDSB>建立新的鸟窝nestPop(find(nestPop<Xmin)) = Xmin ;end 3.5 目标函数 function [ result ] = func_objValue( pop )%FUNC_OBJVALUE 计算目标函数objValue = sum(pop.^2,2);result = objValue ;end 3.6 计算适应度函数 function [ result ] = func_fitness( pop )%OBJFUNCTION 求适应度,最小值% 待优化目标函数% x: 种群或者个体% result : 种群适应度objValue = func_objValue(pop);result = 4001 - objValue ;end 三、输出结果 适应度变化曲线
打印最优解
四、CS案例MATLAB源码下载

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。