布谷鸟算法(C++实现)

您所在的位置:网站首页 莱维飞行函数 布谷鸟算法(C++实现)

布谷鸟算法(C++实现)

2023-10-31 03:43| 来源: 网络整理| 查看: 265

算法思想

布谷鸟鸟群最终只有最健康的蛋才能孵化出来。 布谷鸟群每只鸟都在拼命寻找好巢穴以达到下最健康的蛋的母的。

算法步骤

在这里插入图片描述

步骤一 初始化

初始化布谷鸟种群数量(鸟窝个数),计算各个鸟窝(解)的函数适应值,并保存最好的鸟窝(当前最优解)。

步骤二 循环体

算法主体的位置更新包含两个,一个是莱维飞行和局部随机行走

莱维飞行

在这里插入图片描述

莱维飞行是由较长时间的短步长和较短时间的长步长组成 Levy分布就是小概率值较大和大概率值较小,和自然界中大多数动物觅食方式方式类似,也就是先找到一片区域再细致的查找猎物,如果没找到,就换一片区域找。

在这里插入图片描述 布谷鸟算法使用 Mantegna 算法来实现对称的 Lévy 稳定分布。 在这里插入图片描述

局部随机行走

在宿主鸟发现布谷鸟蛋时,布谷鸟去寻找新的寄生巢时的位置更新。

在这里插入图片描述 a是步长缩放因子。

循环体 步骤(1)位置更新

在这里插入图片描述 注意: 1.保留上代最优鸟窝位置,其余布谷鸟进行莱维飞行寻找寄宿鸟窝,此处可利用小技巧,如下方让 Lévy 稳定分布乘以系数 (i.x - OldNestPop[bestNum].x),当鸟窝是最优解时,此系数为0,避免丢失最优鸟窝位置。

double stepX = (i.x - OldNestPop[bestNum].x) * R(e) / (pow(abs(R1(e)), 1 / beta)); double stepY = (i.x - OldNestPop[bestNum].x) * R(e) / (pow(abs(R1(e)), 1 / beta));

2.布谷鸟群新寄生一组鸟窝群,与原来的鸟窝群对比,替换原来适应度较差的鸟窝,布谷鸟群只守护适应度高的鸟窝。

循环体 步骤(2)存安去险

在这里插入图片描述 注意: 1.布谷鸟群守护的寄生鸟窝的原宿主会跟布谷鸟打架,布谷鸟如果打不过,这只布谷鸟就会在这个鸟巢附近随机寻找一个新 鸟巢下蛋,并守护。(布谷鸟打不过的概率一般是Pa = 0.25)。 正规说法如下图。 在这里插入图片描述 2.布谷鸟群对更新后的鸟窝群里的布谷鸟蛋进行检测,选出最健康的蛋(找出更新后的当前最优解)。

步骤三 终止条件

在这里插入图片描述

应用举例

以下是对一个二维函数求最小值的C++程序: 此函数为 在这里插入图片描述 此函数图像 在这里插入图片描述 算法结果图

在这里插入图片描述 在这里插入图片描述

C++程序如下

#include #include #include #include #include #include #define pi acos(-1) //5只布谷鸟 constexpr int NestNum = 40; //pi值 //规定X,Y 的取值范围 constexpr double X_max = 5; constexpr double X_min = 0; constexpr double Y_max = 5; constexpr double Y_min = 0; //最大迭代次数 constexpr int MaxIterationTimes = 300; //被宿主发现的概率 constexpr double Pa = 0.25; //自变量结构体 struct Nest { double x; double y; double fitness; }; void fitFunc(Nest& nest); int findBetterNest(std::vector&); std::vector levy(std::vector OldNestPop, std::size_t bestNum); std::vector RandomAbandonPaNestPop(std::vector OldNestPop); //随机数引擎 static std::default_random_engine e(time(0)); static std::uniform_real_distribution u(0, 1); int main(void) { bool flag_output = false; double Xold; double Xnew; double Yold; double Ynew; std::ofstream outfileX("D:\\cuckoo\\cuckooX.txt"); std::ofstream outfileY("D:\\cuckoo\\cuckooY.txt"); std::ofstream outfileZ("D:\\cuckoo\\cuckooZ.txt"); //现在的鸟巢群 std::vector current_nestPop; //迭代次数 int num = 0; //最优鸟巢 int BestNestCurrent; //初始化 for (int i = 0; i // std::cout if (i != BestNestCurrent && NewNestPop[i].fitness if (i != BestNestCurrent && NewNestPop[i].fitness outfileX int BestNum = 0; for (decltype(nestPop.size()) i = 0; i BestNum = i; } } return BestNum; } std::vector levy(std::vector OldNestPop,std::size_t bestNum) { double beta = 1.5; // double alpha = 0.01 * R(e);//有的论文写 double alpha = 0.4; double sigma_u = pow((tgamma(1 + beta) * sin(pi * beta / 2)) / (beta * tgamma((1 + beta) / 2) * pow(2, (beta - 1) / 2)), 1 / beta); double sigma_v = 1; static std::normal_distribution R(0, sigma_u); static std::normal_distribution R1(0, sigma_v); for (auto& i : OldNestPop) { //前面的系数是保证最优鸟巢不会进行levy飞行 double stepX = (i.x - OldNestPop[bestNum].x) * R(e) / (pow(abs(R1(e)), 1 / beta)); double stepY = (i.x - OldNestPop[bestNum].x) * R(e) / (pow(abs(R1(e)), 1 / beta)); //按范围更新X if (i.x + alpha * stepX > X_max) { i.x = X_max; } else if(i.x + alpha * stepX i.x = i.x + alpha * stepX; } //按范围更新Y if (i.y + alpha * stepY > Y_max) { i.y = Y_max; } else if (i.y + alpha * stepY i.y = i.y + alpha * stepY; } fitFunc(i); } return OldNestPop; } std::vector RandomAbandonPaNestPop(std::vector OldNestPop) { double step_sizeX = 0; double step_sizeY = 0; static std::uniform_int_distribution randomInt(0, OldNestPop.size() - 1); for(decltype(OldNestPop.size()) i = 0;i step_sizeX = u(e) * (OldNestPop[randomInt(e)].x - OldNestPop[randomInt(e)].x); step_sizeY = u(e) * (OldNestPop[randomInt(e)].y - OldNestPop[randomInt(e)].y); if (OldNestPop[i].x + step_sizeX > X_max) { OldNestPop[i].x = X_max; } else if(OldNestPop[i].x + step_sizeX OldNestPop[i].x += step_sizeX; } if (OldNestPop[i].y + step_sizeY > Y_max) { OldNestPop[i].y = Y_max; } else if (OldNestPop[i].y + step_sizeY OldNestPop[i].y += step_sizeY; } fitFunc(OldNestPop[i]); } } return OldNestPop; } 算法改进

参考资料: 1.http://blog.sina.com.cn/s/blog_17470b8bb0102xjvg.html 2.https://blog.csdn.net/sj2050/article/details/98496868



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3