蚁群算法
基本思想
蚂蚁靠什么找出最短路径?
• 信息素:信息素是一种由蚂蚁自身释放的易挥发的物质,能够实现蚁群内的间接通信。蚂蚁在寻找食物时,在其经过的路径上会释放信息素,信息素可以被其他的蚂蚁感知,并且信息素的浓度越高,对应的路径越短
• 正反馈:蚂蚁会以较大概率选择信息素浓度较高的路径,并释放一定量的信息素,从而使距离较短的信息素浓度被加强形成正反馈*
蚁群算法解决TSP问题步骤以及预备知识
预备知识
①
②
③
④
⑤
算法步骤
①初始化城市距离矩阵,贪心算法获得第一个路径,第一次更新信息素矩阵
②为每一只蚂蚁随机选择初始城市
③计算前往下一个城市的概率,更新禁忌表
④轮盘赌选择下一个城市
⑤更新信息素矩阵
循环执行,找出解空间的最优解
部分代码展示(用到了四个java文件)
蚂蚁类
package antGroupAlgrithm;
//蚂蚁类
public class Ants {
// 禁忌表, 记录走过的城市
public boolean table[];
// 路径
public int path[];
//每一个的概率以及累积概率用于轮盘赌
public double pro[];
public double accumulatePro[];
//记录当前路径花费的距离
public double distance;
Ants(int number) {
this.table = new boolean[number];
this.path = new int[number+1];
for (int i = 0; i < number; i++) {
this.table[i] = false;
this.path[i]=-1;
}
this.path[number]=-1;
this.distance=0;
}
public boolean notOver() {
for(int i=0;i<this.table.length;i++)
if(this.table[i]==false)
return true;
return false;
}
}
城市类
package antGroupAlgrithm;
//城市类
public class City {
//记录城市的坐标
public int x,y;
//记录城市的标志
public int ID;
City(int lx,int ly,int sign){
this.x=lx;
this.y=ly;
this.ID=sign;
}
}
解空间类
package antGroupAlgrithm;
//解空间的每一个元素,路径和距离的一个结构体
public class DistancePath {
public int path[];
public double distance;
DistancePath(int num){
this.path=new int[num+1];
this.distance=0;
}
}
主算法类
变量部分
public class AntAlgrithm {
public int cityNum;
public City city[];
public Ants ants[];
// 记录已经被选择的出发点
public boolean start[];
// 距离矩阵与信息素矩阵
public double distanceMatrix[][];
public double pheromones[][];
//记录全局解结果
public DistancePath dp[][];
public static final double alpha = 1;
public static final double beta = 2;
public static final double p = 0.5;
public static final int antnumber = 18;
public static final int MAX = 100000;
public static final int times = 500;
//构造函数
AntAlgrithm(int x[], int y[]) {
this.cityNum = x.length;
this.city = new City[cityNum];
this.ants = new Ants[antnumber];
this.start = new boolean[cityNum];
for (int i = 0; i < this.cityNum; i++) {
this.city[i] = new City(x[i], y[i], i);
this.start[i] = false;
}
// 初始化每一只蚂蚁
for (int i = 0; i < antnumber; i++) {
this.ants[i] = new Ants(this.cityNum);
this.ants[i].pro = new double[this.cityNum - 1];
this.ants[i].accumulatePro = new double[this.cityNum - 1];
}
//记录解空间的初始化
this.dp=new DistancePath[times][antnumber];
for(int i=0;i<this.dp.length;i++) {
for(int j=0;j<this.dp[i].length;j++)
this.dp[i][j]=new DistancePath(this.cityNum);
}
}
//初始化函数
public void Initial() {
this.distanceMatrix = new double[this.cityNum][this.cityNum];
this.pheromones = new double[this.cityNum][this.cityNum];
for (int i = 0; i < this.cityNum; i++) {
for (int j = i; j < this.cityNum; j++) {
if (j == i)
this.distanceMatrix[i][j] = MAX;
else {
this.distanceMatrix[i][j] = Math.hypot(this.city[i].x - this.city[j].x,
this.city[i].y - this.city[j].y);
this.distanceMatrix[j][i] = this.distanceMatrix[i][j];
}
}
}
// 利用贪心算法更新信息素矩阵
double cn = this.greedy();
double tao;
tao = antnumber / cn;
for (int i = 0; i < this.cityNum; i++) {
for (int j = i + 1; j < this.cityNum; j++) {
this.pheromones[i][j] = tao;
this.pheromones[j][i] = tao;
}
}
}
主函数部分
public static void main(String[] args) {
//城市坐标
int xCoordinate[] = { 1304, 3639, 4177, 3712, 3488, 3326, 3238, 4196, 4312, 4386, 3007, 2562, 2788, 2381, 1332,
3715, 3918, 4061, 3780, 3676, 4029, 4263, 3429, 3507, 3394, 3439, 2935, 3140, 2545, 2778, 2370 };
int yCoordinate[] = { 2312, 1315, 2244, 1399, 1535, 1556, 1229, 1004, 790, 570, 1970, 1756, 1491, 1676, 695,
1678, 2179, 2370, 2212, 2578, 2838, 2931, 1908, 2367, 2643, 3201, 3240, 3550, 2357, 2826, 2975 };
int flag = 0;
//初始化
AntAlgrithm ata = new AntAlgrithm(xCoordinate, yCoordinate);
ata.Initial();
//演变
while (flag < times) {
ata.tsp();
ata.record(flag);
ata.remake();
flag += 1;
}
//选择最优解
ata.chooseBest();
}
需要注意的是,蚁群算法得到的解不是固定的,但是收敛于一个较小的值
代码运行截图
如果觉得有帮助的话就点个赞吧!