蚁群算法

基本思想

蚂蚁靠什么找出最短路径?
• 信息素:信息素是一种由蚂蚁自身释放的易挥发的
物质,能够实现蚁群内的间接通信。蚂蚁在寻找食
物时,在其经过的路径上会释放信息素,信息素可
以被其他的蚂蚁感知,并且信息素的浓度越高,对
应的路径越短
• 正反馈:蚂蚁会以较大概率选择信息素浓度较高的
路径,并释放一定量的信息素,从而使距离较短的
信息素浓度被加强形成正反馈*

蚁群算法解决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();}

需要注意的是,蚁群算法得到的解不是固定的,但是收敛于一个较小的值

代码运行截图

如果觉得有帮助的话就点个赞吧!

蚁群算法-JAVA实现相关推荐

  1. 蚁群算法画图java_[转载]简单蚁群算法 + JAVA实现蚁群算法

    一 引言 蚁群算法(ant colony optimization,ACO),又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型技术.它由Marco Dorigo于1992年在他的博士论文中引入,其灵 ...

  2. 蚁群算法java实现_简单蚁群算法 + JAVA实现蚁群算法

    一 引言 蚁群算法(ant colony optimization,ACO),又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型技术.它由Marco Dorigo于1992年在他的博士论文中引入,其灵 ...

  3. tsp java_蚁群算法java实现以及TSP问题蚁群算法求解

    1. 蚁群算法简介 蚁群算法(Ant Clony Optimization, ACO)是一种群智能算法,它是由一群无智能或有轻微智能的个体(Agent)通过相互协作而表现出智能行为,从而为求解复杂问题 ...

  4. 蚁群算法java实现_蚁群算法java实现以及TSP问题蚁群算法求解

    1. 蚁群算法简介 蚁群算法(Ant Clony Optimization, ACO)是一种群智能算法,它是由一群无智能或有轻微智能的个体(Agent)通过相互协作而表现出智能行为,从而为求解复杂问题 ...

  5. java蚁群算法_蚁群算法JAVA版

    展开全部 说明:信息素权重,路径权重和信息素蒸发率对最后的结果影响很大,需要微调. 目前发现2 / 5 / 0.5 能达到稍e68a8432313133353236313431303231363533 ...

  6. tsp问题用蚁群算法java实现_TSP解决之道——蚁群算法

    参考 蚁群算法原理与应用1-自然计算与群体智能 简介 1.蚁群算法(Ant Clony Optimization,ACO)是一种群智能算法,它是由一群无智能或有轻微智能的个体(Agent)通过相互协作 ...

  7. python蚁群算法路径规划_使用python实现蚁群算法

    此次使用python实现蚁群算法是仿照蚁群优化算法的JAVA实现中的蚁群算法实现方法,使用的也是其中的数据(此处为上传数据),如需更深一步了解蚁群算法原理和具体实现过程,请参考蚁群优化算法的JAVA实 ...

  8. 关于精英蚁群算法matlab,蚁群算法MATLAB解VRP问题

    Excel  exp12_3_2.xls内容: ANT_VRP函数: function [R_best,L_best,L_ave,Shortest_Route,Shortest_Length]=ANT ...

  9. 【运筹优化】结合天际线启发式的蚁群算法求解二维矩形装箱问题 + Java代码实现

    文章目录 一.天际线启发式 二.蚁群算法结合天际线启发式 2.1 构建序列 2.1.1 思路一 2.1.2 思路二 2.1.3 思路N 三.Java代码实现 3.1 项目结构 3.2 Ant 3.3 ...

最新文章

  1. ios集成firebase_如何将Firebase与您的应用程序集成
  2. 值得深思的问题——分库分表就能无限扩容吗?
  3. 人工智能写出第一篇文章:我真的没想要消灭你们,人类!
  4. C语言如何使用其他文件定义的结构体?(C++报错:无法转换到不完整的类【需在头文件中定义结构体??】)
  5. 【Linux】Linux-路径切换-相对路径和绝对路径快捷键记录linux 知识点记录
  6. 有机化学芳烃 芳香性
  7. dnf如何快速拾取物品_DNF手游泰拉该如何获取,游戏蜂窝辅助托管快速获取泰拉...
  8. scala调用java库_从scala调用java时的java.lang.IllegalAccessError – solutions / workarounds?...
  9. 我的失败与伟大 —— 产品原型的打造
  10. Python argparse模块、argparse.ArgumentParser()用法解析
  11. https工作原理及CA证书及验证证书
  12. 那些年京东出现的BUG损失惨重,你还敢瞧不起测试?
  13. 【vue】vue中设置路径别名
  14. 《猎罪图鉴》中暴露出人脸识别安全隐患,你的脸或许正被仿冒
  15. 批处理中的for详解
  16. 如何登录锐捷设备(路由篇)
  17. 计算机配置64位,win10 64位系统最低配置要求_win10系统 64位最低电脑配置要求多少...
  18. 英飞凌SP370方案胎压监测器-专业级视频课程-朱有鹏-专题视频课程
  19. 支付宝正式发布黑莓版客户端
  20. Windows10安装NVM

热门文章

  1. 今天开始做战斗,回合制战斗代码实现第三篇,特殊的回合制游戏Slg(策略战棋)
  2. 巴斯勒相机的ip掩码_26:IPMaskCheck识别有效的ip地址和掩码并分类统计
  3. sql语句-删除表数据drop、truncate和delete的用法
  4. 黄聪:wordpress调用函数大全
  5. awstats分析nginx日志
  6. minijson使用_JSON教程介绍
  7. Form 表单 邮箱、手机号、中文名、身份证号码格式验证
  8. Excel无需取消隐藏直接就可以删除隐藏的工作表
  9. 思科DHCP配置命令
  10. webview过滤广告