目录
前言
布谷鸟搜索算法(Cuckoo Search,缩写 CS)是由剑桥大学杨新社教授和S.戴布于2009年提出的一种新兴启发算法。根据昆虫学家的长期观察研究发现,一部分布谷鸟以寄生的方式养育幼鸟,它们不筑巢,而是将自己的卵产在其他鸟的巢中(通常为黄莺、云雀等),由其他鸟(义亲)代为孵化和育雏。然而,如果这些外来鸟蛋被宿主发现,宿主便会抛弃这些鸟蛋或新筑鸟巢。
CS算法通过模拟某些种属布谷鸟(CuckooSpecies)的寄生育雏(BroodParasitism)来有效地求解最优化问题的算法.同时,CS也采用相关的Levy飞行搜索机制。研究表明,布谷鸟搜索比其他群体优化算法更有效.
算法源英文论文参见【智能优化算法】布谷鸟搜索算法-Cuckoo Search源论文
优缺点
算法思想主要基于两个策略:布谷鸟的巢寄生性和莱维飞行机 制。通过随机游走的方式搜索得到一个最优的鸟窝来孵化自己的鸟蛋,这种方式可以达到一种高效的寻优模式。
CS算法主要优点是参数少、操作简单、易实现、随机搜索路径优和寻优能力强等,备受学者关注,相关的科研成果也日益倍增。王凡、贺兴时等已在文献中通过建立CS算法的Markov链模型,理论证明了该算法可收敛于全局最优。CS算法的衍生算法以及应用研究也已得到了快速的发展,但国内外对CS算法的综述性研究比较少,YANG等在文献中对CS算法最初的发展和它的多种改进算法之间进行了比较,没有详细概述CS算法的发展现状。因此,有必要对CS算法的原理、算法改进、其各领域的应用、算法优缺点、使用范围、存在的问题以及下一阶段的研究方向等进行系统、全面的总结和评述,进而呈现CS算法的发展现状,期望该算法能够解决更多更有效的实际问题。
CS算法与遗传算法(GA)、蚁群算法(ACO)、粒子群算法(PSO)、蜂群算法(ABC)等均属于群只能优化算法,它们皆为基于种群,借助迭代来实现优化步骤的概率搜索算法。总结与对比了这5种算法的优点、缺点以及适合求解的问题,结果如表1所示:
表1 CS于GA、ACO、PSO、ABC算法的比较
虽然GA、ACO、PSO和ABC的研究及应用比较成熟,但从表1可知,CS算法在参数数目、全局寻优能力等方面综合优势更大,算法可灵活地跟其他算法进行多种组合,并具有更广泛的适用性。CS算法作为后起之秀,它的优越性使其广泛应用于各个研究领域。
- 优点是 全局寻优能力强,参数少且易于实现,易与其他算法相结合等综合优势,
- 缺点是 存在收敛速度慢、进化后期种群多样性差等不足。CS算法收敛速度偏慢、求解精度较低:CS算法通过莱维飞行机制寻找鸟巢,莱维飞行是一种由小步长的短距离飞行和偶尔大步长的长距离飞行组成的随机游走过程,因此布谷鸟的寻窝路径容易在不同的搜索区域间跳跃,导致布谷鸟算法的局部精细搜索能力较差,在算法迭代后期容易在全局最优解附近的区域出现震荡现象,造成算法效率偏低群多样性差等不足
算法原理
在自然界中,布谷鸟寻找适合自己产卵的鸟窝位置是随机的或是类似随机的方式,为了模拟布谷鸟寻窝的方式,首先,需要设定以下3个理想的状态
(1)布谷鸟一次只产一个卵,并随机选择鸟窝来孵化它;
(2)在随机选择的一组鸟窝中,最好的鸟窝将会被保留到下一代;
(3)可利用的鸟窝数量n是固定的,一个鸟窝的主人能发现一个外来鸟蛋的概率Pa∈[0,1],在这3
个理想状态的基础上,布谷鸟寻窝的路径和位置更新公式如下:
x(t+1)i=x(t)i+α*L(λ),i=1,2,…,n.(1)
其中x(t)i表示第i个鸟窝在第t代的鸟窝位置,*为点对点乘法,α表示步长控制量,L(λ)为Levy随机搜索路径,并且L~u=t-λ,(1pa,则对x(t+1)i进行随机改变,反之不变。最后保留测试值较好的一组鸟窝位置y(t+1)i,此时仍把y(t+1)i记为x(t+1)i。
算法流程图
应用场景
使用布谷鸟算法解决作业车间调度问题(JSP)
下面介绍布谷鸟算法在作业车间调度问题上的实际应用。
问题描述
作业车间调度问题(Job Shop Scheduling, JSP)是最经典的几个NP-hard问题之一。其应用领域极其广泛,涉及航母调度,机场飞机调度,港口码头货船调度,汽车加工流水线等。
作业车间调度在本系统中需要考虑如下约束:
本问题要求算法目标为所有作业加工完成总时间最短
问题的编码与解码
若由n个工件,所有工件最多需要m道工序完成,则JSP的一个可行序列可以被描述成一个n ∗ m 的整数串。本文先通过一定的操作生成一系列初始编码值,后通过对于编码值进行解码来获取满足约束的可行解序列,通过对于可行解序列的好坏适应度情况评估来对编码值进行算法操作。
下面使用一个n = 2 ,m = 3 的示例来对编码和解码进行说明。
首先在定义域内随机生成一个 2 ∗ 3 的实数组如( 0.3 , 0.2 , 0.8 , 0.1 , 0.6 , 0.4 )并对此这些元素按大小排序。规定数值小的顺序靠前且最小的数编号为0,则此时排序结果为:
[2,1,5,0,4,3]
将此数组每个元素分别除以最大工序数 m ,并取下整。则可得到一组可行序列 A :
A =[0,0,1,0,1,1]
显然,可行序列 A 的加工路线为工件0的第1个工序,工件0的第2个工序,工件1的第1个工序,工件0的第3个工序,工件1的第2个工序,工件1的第3个工序。
如此,我就就通过一个可操作的实数序列转换成一个满足约束条件的工序。
但对于所有工件,其的操作序列可能最大工序次数小于m,我们只需将其正常加入,对于缺少的工序数量我们添加相同数量的且持续时间为0的无关工序即可,在进行计算工序时间时忽略这些持续时间为0的工序即可。
程序流程
算法流程基本同上述部分中描述基本一致,现描述如下:
实现代码
-
package jspNew;
-
-
import java.util.
*;
-
-
import org.apache.commons.math
3.special.Gamma;
-
-
/
*
-
* 重点关注test类中的myRead方法中对于数据读入的部分。
-
* 同时注意在Data类中reVec方法对于结果的返回。
-
* 全局最优解 在主流程类Run中的run方法的ansCost中 全局最优机器任务序列,由reVec方法返回
-
*/
-
-
class
test{
-
public static void myRead() {
-
Scanner cin
= new Scanner(System.
in);
-
/
/全局定义
-
Data.n
= cin.nextInt();
/
/TODO 此处需要读取n个工件
-
Data.m
= cin.nextInt();
/
/TODO 此处需要读取最大工序数 每个工件最多有m个工序
-
Pair[][] t
= new Pair[
Data.n][
Data.m];
/
/暂存的任务列表
-
Bugu.n
= cin.nextInt();
/
/TODO 此处需要读取种群数量
-
Bugu.step
= cin.nextInt();
/
/TODO 此处需要读取迭代次数
-
/
/t[
0][
0]
= Pair.make_pair(
1,
3);
-
/
/读取数据
-
for(int i
=
0 ; i
<
Data.n ; i
+
+) {
-
int tm
= cin.nextInt();
/
/TODO 此处需要读取第i个工件的工序数
-
for(int j
=
0 ; j
<
Data.m ; j
+
+) {
-
if(j
>= tm)
-
t[i][j]
= Pair.make_pair(
0,
0);
-
else {
-
int a
= cin.nextInt(),b
= cin.nextInt();
/
/TODO a是第i个工件 在第j道工序 需要在 a机器上执行 b时间
-
t[i][j]
= Pair.make_pair(a, b);
-
}
-
}
-
}
-
/
/创建全局工件,并将数组赋值(从暂存的任务列表中获取)
-
Data.init(
Data.n);
-
for(int i
=
0 ; i
<
Data.n ; i
+
+) {
-
Data.myData[i]
=
Data.
get_
data(t[i]);
-
}
-
/
/ int[] tans
= new int[]{
1,2,2,2,0,0,1,0,1};
-
/
/ System.out.println(
Data.getCost(tans));
-
}
-
}
-
-
/
/实际上是任务类
-
class Pair{
-
public int machine,
time;
-
Pair(int machine,int
time){
-
this.machine
= machine;
-
this.
time
=
time;
-
}
-
/
/工厂模式
-
static Pair make_pair(int machine,int
time) {
-
return new Pair(machine,
time);
-
}
-
}
-
-
/
/
2020年
7月
11日
-
class three extends Pair{
-
int third;
-
three(int a,int b,int c){
-
super(a, b);
-
third
= c;
-
}
-
static three make_three(int a,int b,int c) {
-
return new three(a,b,c);
-
}
-
}
-
-
class
Data{
-
public static
Data[] myData;
/
/全局工件
-
public static int n,m;
/
/全局工件数和最大任务数 任务数量
-
public static double Myrandom() {
-
return Math.
random();
-
}
-
public static void init(int nn) {
-
n
= nn;
-
myData
= new
Data[nn];
-
}
-
public Pair[] task;
/
/每个工件的任务序列
-
public static
Data
get_
data(Pair[] t) {
/
/返回了一个工件
-
Data tt
= new Dat
a();
-
tt.task
= new Pair[m];
-
for(int i
=
0 ; i
< t.
length ; i
+
+) {
-
tt.task[i]
= t[i];
-
}
-
return tt;
-
}
-
-
public static int getCost(int[] t) {
-
Map
<Integer,Integer
> map
= new HashMap
<Integer,Integer
>();
/
/记录每个工件做到了第几个步骤。
-
int[] lastRun
= new int[n];
/
/每个工件上个任务的完成时间。
-
int[] lastMachine
= new int[m];
/
/每个机器上个工件的完成时间。
-
for(int i
=
0; i
< n ;i
+
+) {
-
map.put(i,
0);
/
/一开始都是第
0个步骤。
-
}
-
int ans
= -
1;
-
for(int i
=
0 ; i
< t.
length ; i
+
+) {
-
int now
= t[i];
/
/获取当前的工件编号
-
int pairNum
= map.
get(now);
/
/获取工件编号的步骤。
-
map.remove(now);
/
/移除工件上一个工件编号。
-
map.put(now, pairNum
+
1);
/
/更新工件做到的步骤
-
Pair nowPair
= myData[now].task[pairNum];
/
/获取当前工件的步骤的任务。
-
int nowMachine
= nowPair.machine;
/
/获取任务机器。
-
int nowCost
= nowPair.
time;
/
/获取任务时间。
-
if(nowCost
=
=
0)
-
continue;
-
int newTime
= Math.max(lastMachine[nowMachine], lastRun[now])
+ nowCost;
/
/从当前工件上一个任务结束时间
-
/
/和待放入机器的最后一个工件完成时间中挑个大的。
-
lastMachine[nowMachine]
= lastRun[now]
= newTime;
-
/
/ System.out.println(
"newTime:"
+ newTime
-
/
/
+
" nowMachine:"
+ nowMachine
+
-
/
/
" nowCost:"
+ nowCost);
-
ans
= Math.max(ans, newTime);
/
/获取最长时间
-
}
-
return ans;
-
}
-
/
/
2020年
7月
11日
-
static Vector
<three
>[] reVec(int[]
in){
-
Vector
<three
>[] t
= new Vector[n
*m];
-
int[] vis
= new int[n];
/
/工序数
-
int[] machineLastTime
= new int[m];
-
int[] taskLastTime
= new int[n];
-
for(int i
=
0 ; i
< t.
length ; i
+
+) {
-
t[i]
= new Vector
<three
>();
-
}
-
for(int i
=
0 ; i
<
in.
length ; i
+
+) {
-
int aim
=
in[i];
-
int
index
= vis[aim]
+
+;
-
int machine
= myData[aim].task[
index].machine;
-
/
/System.out.println(machine);
-
int
time
= myData[aim].task[
index].
time;
-
/
/System.out.println(
time);
-
if(
time
=
=
0)
-
continue;
-
int maxStartTime
= taskLastTime[aim]
> machineLastTime[machine] ?
-
taskLastTime[aim] : machineLastTime[machine];
-
/
* System.out.println(
"aim:"
+aim
+
"\n"
+
"index:"
+
index
+
-
"\n"
+
"machine:"
+
-
machine
+
"\n"
+
"time:"
+
time
+
"\n"
+
"taskLastTime:"
+taskLastTime[aim]
-
+
"\n"
+
"machineLastTime"
+machineLastTime[machine]
-
+
"\n-------------------------");
*
/
-
taskLastTime[aim]
= machineLastTime[machine]
= maxStartTime
+
time;
-
t[machine].
add(three.make_three(maxStartTime, taskLastTime[aim],aim));
-
}
-
-
/
/
test
-
/
/System.out.println(m);
-
for(int i
=
0 ; i
< m ; i
+
+) {
-
System.out.print(
"machine "
+i
+
":"
+
'\n');
-
for(int j
=
0 ; j
< t[i].
size() ; j
+
+)
-
System.out.println(
"task :"
+ t[i].
get(j).third
+
" start:"
+ t[i].
get(j).machine
+
" end:"
+ t[i].
get(j).
time);
-
}
-
/
/TODO 返回了一个Vector数组,以 机器编号为 vector数组下标索引
-
/
/对于每个Vector,中存放了若干个三元组,表示一个机器的 任务序列
-
/
/对于每个任务序列中的每个任务 以
<工件名称,开始任务时间,结束任务时间
>储存,
-
/
/三元组的数据结构为three,可见代码中的three部分。
-
return t;
-
}
-
}
-
-
class see{
-
/
/解码,将编码解码成可行任务序列。
-
public static int[] recode(double[] tt) {
-
double[]t
= new double[tt.
length];
/
/临时数组,防止原数组被改。
-
for(int i
=
0 ; i
< t.
length ; i
+
+)
-
t[i]
= tt[i];
-
Map
<Double,Integer
> map
= new HashMap
<Double,Integer
>();
/
/记录以下数组编号。
-
for(int i
=
0 ; i
< t.
length ; i
+
+)
-
map.put(t[i], i);
-
Arrays.
sort(t);
/
/排个序
-
int[] ans
= new int[t.
length];
-
for(int i
=
0 ; i
< t.
length ; i
+
+) {
-
ans[map.
get(t[i])]
= i;
/
/将排序后的顺序填入原来的位置
-
}
-
for(int i
=
0 ; i
< t.
length ; i
+
+) {
-
int t
1
= (int)((double)ans[i]
/ (double)
Data.m);
-
ans[i]
= t
1;
-
}
-
/
/
for(int i
=
0 ; i
< ans.
length ; i
+
+)
-
/
/ System.out.println(ans[i]);
-
/
/ System.out.println();
-
return ans;
-
}
-
/
/得到一个从u到v的正态分布
-
public static double NormalDistribution(double u,double v){
-
java.util.
Random
random
= new java.util.
Random();
-
return Math.sqrt(v)
*
random.nextGaussian()
+u;
-
}
-
/
/得到伽马函数
-
public static double getGamma(double t) {
-
return Gamma.gamma(t);
-
}
-
/
/获取u分布
-
static double u,beta
=
1.5;;
-
private static void getU(){
-
u
= (getGamma(
1.0
+ beta)
*Math.sin((Math.PI
* beta)
/
2.0) )
-
/ (getGamma(((
1.0
+ beta)
/
2.0)
* beta
* Math.pow(
2.0, (beta
-
1.0)
/
2.0)));
-
}
-
/
/获取步长
-
public static double getS() {
-
getU();
-
double newu
= NormalDistribution(
0,u);
-
double newv
= Math.pow(Math.abs(NormalDistribution(
0,1)),
1.0
/beta);
-
return newu
/newv;
-
}
-
public static double randomWalk() {
-
return
1
*
1
* getD();
-
}
-
/
/获取方向 https:
/
/blog.csdn.net
/sj
2050
/article
/details
/
98496868#_
43
-
public static double getD() {
-
double r
= (int)
Data.Myrandom()
* (
100)
+
1;
-
if(r
<
50)
return -
1.0;
-
return
1.0;
-
}
-
static public void seeBest(double[] t,int bestCost) {
-
System.out.println(
"本次布谷鸟找到的最优解是:"
+ bestCost);
-
System.out.println(
"对应的序列是:");
-
int[] ansShow
= see.recode(t);
-
for(int i
=
0 ; i
< ansShow.
length ; i
+
+) {
-
System.out.printf(
"%3d ", ansShow[i]);
-
}
-
System.out.println();
-
}
-
static public void seeIndex(double[] t) {
-
int[] ansShow
= see.recode(t);
-
for(int i
=
0 ; i
< ansShow.
length ; i
+
+) {
-
System.out.printf(
"%3d ", ansShow[i]);
-
}
-
System.out.println();
-
}
-
}
-
-
class Bugu{
-
static public int n;
-
public double []ans;
-
static public int step;
-
Bugu(int n){
-
ans
= new double[n];
-
for(int i
=
0 ; i
< n ; i
+
+) {
-
ans[i]
=
Data.Myrandom();
-
}
-
}
-
static public void seeBugu(Bugu[] a) {
-
for(int i
=
0 ; i
< a.
length ; i
+
+) {
-
System.out.print(
"bugu["
+i
+
"]:");
-
for(int j
=
0 ; j
< a[i].ans.
length ; j
+
+) {
-
System.out.print(a[i].ans[j]
+
" ");
-
}
-
System.out.println();
-
}
-
System.out.println();
-
}
-
-
-
-
}
-
-
class
run {
-
public static void
Run() {
-
-
test.myRead();
-
-
/
/Bugu.n
=
4;
-
int[] cost
= new int[Bugu.n];
-
Bugu[] bugu
= new Bugu[Bugu.n];
-
double[] bestans
= new double[
Data.m
*
Data.n];
-
int ansCost
=
0x
3f
3f
3f;
-
for(int i
=
0 ; i
< Bugu.n ; i
+
+) {
-
bugu[i]
= new Bugu(
Data.n
*
Data.m);
-
/
/System.out.println();
-
cost[i]
=
Data.getCost(see.recode(bugu[i].ans));
-
/
/System.out.println(cost[i]);
-
}
-
-
int bestIndex
= -
1,bestCost
=
0x
3f
3f
3f;
-
for(int i
=
0 ; i
< cost.
length ; i
+
+)
-
if(cost[i]
< bestCost) {
-
bestCost
= cost[i];
-
bestIndex
= i;
-
for(int k
=
0 ; k
< bugu[i].ans.
length; k
+
+)
-
bestans[k]
= bugu[i].ans[k];
-
}
-
ansCost
= bestCost;
-
-
/
/see.seeBest(bestans, ansCost);
-
/
/
if(ansCost
> -
1)
-
/
/
return;
-
while(Bugu.step
>
0) {
-
/
/Bugu.seeBugu(bugu);
-
Bugu.step--;
-
-
/
/找个窝下蛋,更新窝的位置。即莱维飞行。
-
for(int i
=
0 ; i
< Bugu.n ; i
+
+) {
-
/
/System.out.println(updata);
-
for(int j
=
0 ; j
< bugu[i].ans.
length ; j
+
+) {
-
double s
= see.getS(),d
= see.getD();
/
/s是步长,d是方向
-
double updata
= s
* d;
-
bugu[i].ans[j]
+
= Math.abs(bestIndex
- i)
*(updata);
-
/
/
-
}
-
}
-
-
-
-
/
/在差的解当中,
25%的蛋被宿主发现
-
for(int i
=
0 ; i
< Bugu.n ; i
+
+ ) {
-
if(Math.
random()
<
0.25) {
-
for(int j
=
0 ; j
< bugu[i].ans.
length ; j
+
+) {
-
double newUpdata
= see.randomWalk();
-
bugu[i].ans[j]
+
=
0.4
* newUpdata;
-
}
-
}
-
}
-
-
/
/寻找本轮最佳
-
bestCost
=
0x
3f
3f
3f;
-
for(int i
=
0 ; i
< cost.
length ; i
+
+) {
-
cost[i]
=
Data.getCost(see.recode(bugu[i].ans));
-
/
/System.out.println(
"cost[i]:"
+ cost[i]);
-
/
/see.seeIndex(bugu[i].ans);
-
if(cost[i]
< bestCost) {
-
bestCost
= cost[i];
-
bestIndex
= i;
-
}
-
}
-
-
/
/本轮最佳比全局最佳好,则更新。
-
if(bestCost
< ansCost) {
-
ansCost
= bestCost;
-
for(int i
=
0 ; i
< bestans.
length ; i
+
+)
-
bestans[i]
= bugu[bestIndex].ans[i];
-
}
-
/
/see.seeBest(bestans, ansCost);
-
/
/Bugu.seeBugu(bugu);
-
}
-
/
/see.seeBest(bestans, ansCost);
/
/这一行只是用来打印结果的,按需删除
-
/
/ansCost就是全局最优解的值,bestans就是最优解的序列。
-
System.out.println(
"本次布谷鸟找到的最优解是:"
+ ansCost);
-
Data.reVec(see.recode(bestans));
-
}
-
}
-
-
public
class Jsp {
-
public static void main(
String[] argv) {
-
/
/
test.myRead();
-
run.
Run();
-
/
/
test.print();
-
}
-
}
-
/
*
-
3
-
3
-
4
-
1000
-
3
-
0
3
-
1
2
-
2
2
-
3
-
0
2
-
2
1
-
1
4
-
2
-
1
4
-
2
3
-
*
/
算例测试
-
10
-
5
-
1000
-
20000
-
5
-
1
21
-
0
53
-
4
95
-
3
55
-
2
34
-
5
-
0
21
-
3
52
-
4
16
-
2
26
-
1
71
-
5
-
3
39
-
4
98
-
1
42
-
2
31
-
0
12
-
5
-
1
77
-
0
55
-
4
79
-
2
66
-
3
77
-
5
-
0
83
-
3
34
-
2
64
-
1
19
-
4
37
-
5
-
1
54
-
2
43
-
4
79
-
0
92
-
3
62
-
5
-
3
69
-
4
77
-
1
87
-
2
87
-
0
93
-
5
-
2
38
-
0
60
-
1
41
-
3
24
-
4
83
-
5
-
3
17
-
1
49
-
4
25
-
0
44
-
2
98
-
5
-
4
77
-
3
79
-
2
43
-
1
75
-
0
96
计算结果
实际上,在种群数量为5,迭代次数为20000次时,已经可以得到总时间为711的最优解,而在上述例子将迭代次数与种群数量大大提高的情况下,结果如下:
-
本次布谷鸟找到的最优解是:
690
-
machine
0:
-
task :
4
start:
0
end:
83
-
task :
0
start:
83
end:
136
-
task :
7
start:
156
end:
216
-
task :
1
start:
216
end:
237
-
task :
3
start:
237
end:
292
-
task :
5
start:
292
end:
384
-
task :
6
start:
390
end:
483
-
task :
8
start:
483
end:
527
-
task :
9
start:
527
end:
623
-
task :
2
start:
656
end:
668
-
machine
1:
-
task :
0
start:
0
end:
21
-
task :
5
start:
21
end:
75
-
task :
3
start:
75
end:
152
-
task :
6
start:
154
end:
241
-
task :
7
start:
241
end:
282
-
task :
8
start:
282
end:
331
-
task :
9
start:
331
end:
406
-
task :
4
start:
406
end:
425
-
task :
2
start:
546
end:
588
-
task :
1
start:
588
end:
659
-
machine
2:
-
task :
5
start:
75
end:
118
-
task :
7
start:
118
end:
156
-
task :
9
start:
196
end:
239
-
task :
4
start:
239
end:
303
-
task :
6
start:
303
end:
390
-
task :
1
start:
423
end:
449
-
task :
3
start:
449
end:
515
-
task :
8
start:
527
end:
625
-
task :
2
start:
625
end:
656
-
task :
0
start:
656
end:
690
-
machine
3:
-
task :
6
start:
0
end:
69
-
task :
4
start:
83
end:
117
-
task :
9
start:
117
end:
196
-
task :
8
start:
196
end:
213
-
task :
1
start:
237
end:
289
-
task :
2
start:
289
end:
328
-
task :
7
start:
328
end:
352
-
task :
0
start:
352
end:
407
-
task :
3
start:
515
end:
592
-
task :
5
start:
592
end:
654
-
machine
4:
-
task :
9
start:
0
end:
77
-
task :
6
start:
77
end:
154