基于蚁群算法求解求解TSP问题(JAVA)

原文地址:http://blog.csdn.net/wangqiuyun/article/details/8878298



一、TSP问题

TSP问题(Travelling Salesman Problem)即旅行商问题,又译为旅行推销员问题、货郎担问题,是数学领域中著名问题之一。假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路径的限制是每个城市只能拜访一次,而且最后要回到原来出发的城市。路径的选择目标是要求得的路径路程为所有路径之中的最小值。

TSP问题是一个组合优化问题。该问题可以被证明具有NPC计算复杂性。TSP问题可以分为两类,一类是对称TSP问题(Symmetric TSP),另一类是非对称问题(Asymmetric TSP)。所有的TSP问题都可以用一个图(Graph)来描述:

V={c1, c2, …, ci, …, cn},i = 1,2, …, n,是所有城市的集合.ci表示第i个城市,n为城市的数目;

E={(r, s): r,s∈ V}是所有城市之间连接的集合;

C = {crs: r,s∈ V}是所有城市之间连接的成本度量(一般为城市之间的距离);

如果crs = csr, 那么该TSP问题为对称的,否则为非对称的。


一个TSP问题可以表达为:

求解遍历图G = (V, E, C),所有的节点一次并且回到起始节点,使得连接这些节点的路径成本最低。

二、蚁群算法

蚁群算法(ant colony optimization, ACO),又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型算法。它由Marco Dorigo于1992年在他的博士论文中提出,其灵感来源于蚂蚁在寻找食物过程中发现路径的行为。蚁群算法是一种模拟进化算法,初步的研究表明该算法具有许多优良的性质。针对PID控制器参数优化设计问题,将蚁群算法设计的结果与遗传算法设计的结果进行了比较,数值仿真结果表明,蚁群算法具有一种新的模拟进化优化方法的有效性和应用价值。

蚁群算法原理:假如蚁群中所有蚂蚁的数量为m,所有城市之间的信息素用矩阵pheromone表示,最短路径为bestLength,最佳路径为bestTour。每只蚂蚁都有自己的内存,内存中用一个禁忌表(Tabu)来存储该蚂蚁已经访问过的城市,表示其在以后的搜索中将不能访问这些城市;还有用另外一个允许访问的城市表(Allowed)来存储它还可以访问的城市;另外还用一个矩阵(Delta)来存储它在一个循环(或者迭代)中给所经过的路径释放的信息素;还有另外一些数据,例如一些控制参数(α,β,ρ,Q),该蚂蚁行走玩全程的总成本或距离(tourLength),等等。假定算法总共运行MAX_GEN次,运行时间为t。

蚁群算法计算过程如下:
(1)初始化
(2)为每只蚂蚁选择下一个节点。
(3)更新信息素矩阵
(4)检查终止条件
(5)输出最优值

三、蚁群算法求解TSP问题

在该JAVA实现中我们选择使用tsplib上的数据att48,这是一个对称TSP问题,城市规模为48,其最优值为10628.其距离计算方法下图所示:

具体代码如下:

[java]  view plain  copy
  1. package noah;  
  2.   
  3. import java.util.Random;  
  4. import java.util.Vector;  
  5.   
  6. public class Ant implements Cloneable {  
  7.   
  8.     private Vector<Integer> tabu; // 禁忌表  
  9.     private Vector<Integer> allowedCities; // 允许搜索的城市  
  10.     private float[][] delta; // 信息数变化矩阵  
  11.     private int[][] distance; // 距离矩阵  
  12.     private float alpha;  
  13.     private float beta;  
  14.   
  15.     private int tourLength; // 路径长度  
  16.     private int cityNum; // 城市数量  
  17.     private int firstCity; // 起始城市  
  18.     private int currentCity; // 当前城市  
  19.   
  20.     public Ant() {  
  21.         cityNum = 30;  
  22.         tourLength = 0;  
  23.     }  
  24.   
  25.     /** 
  26.      * Constructor of Ant 
  27.      *  
  28.      * @param num 
  29.      *            蚂蚁数量 
  30.      */  
  31.     public Ant(int num) {  
  32.         cityNum = num;  
  33.         tourLength = 0;  
  34.     }  
  35.   
  36.     /** 
  37.      * 初始化蚂蚁,随机选择起始位置 
  38.      *  
  39.      * @param distance 
  40.      *            距离矩阵 
  41.      * @param a 
  42.      *            alpha 
  43.      * @param b 
  44.      *            beta 
  45.      */  
  46.   
  47.     public void init(int[][] distance, float a, float b) {  
  48.         alpha = a;  
  49.         beta = b;  
  50.         // 初始允许搜索的城市集合  
  51.         allowedCities = new Vector<Integer>();  
  52.         // 初始禁忌表  
  53.         tabu = new Vector<Integer>();  
  54.         // 初始距离矩阵  
  55.         this.distance = distance;  
  56.         // 初始信息数变化矩阵为0  
  57.         delta = new float[cityNum][cityNum];  
  58.         for (int i = 0; i < cityNum; i++) {  
  59.             Integer integer = new Integer(i);  
  60.             allowedCities.add(integer);  
  61.             for (int j = 0; j < cityNum; j++) {  
  62.                 delta[i][j] = 0.f;  
  63.             }  
  64.         }  
  65.         // 随机挑选一个城市作为起始城市  
  66.         Random random = new Random(System.currentTimeMillis());  
  67.         firstCity = random.nextInt(cityNum);  
  68.         // 允许搜索的城市集合中移除起始城市  
  69.         for (Integer i : allowedCities) {  
  70.             if (i.intValue() == firstCity) {  
  71.                 allowedCities.remove(i);  
  72.                 break;  
  73.             }  
  74.         }  
  75.         // 将起始城市添加至禁忌表  
  76.         tabu.add(Integer.valueOf(firstCity));  
  77.         // 当前城市为起始城市  
  78.         currentCity = firstCity;  
  79.     }  
  80.   
  81.     /** 
  82.      *  
  83.      * 选择下一个城市 
  84.      *  
  85.      * @param pheromone 
  86.      *            信息素矩阵 
  87.      */  
  88.   
  89.     public void selectNextCity(float[][] pheromone) {  
  90.         float[] p = new float[cityNum];  
  91.         float sum = 0.0f;  
  92.         // 计算分母部分  
  93.         for (Integer i : allowedCities) {  
  94.             sum += Math.pow(pheromone[currentCity][i.intValue()], alpha)  
  95.                     * Math.pow(1.0 / distance[currentCity][i.intValue()], beta);  
  96.         }  
  97.         // 计算概率矩阵  
  98.         for (int i = 0; i < cityNum; i++) {  
  99.             boolean flag = false;  
  100.             for (Integer j : allowedCities) {  
  101.                 if (i == j.intValue()) {  
  102.                     p[i] = (float) (Math.pow(pheromone[currentCity][i], alpha) * Math  
  103.                             .pow(1.0 / distance[currentCity][i], beta)) / sum;  
  104.                     flag = true;  
  105.                     break;  
  106.                 }  
  107.             }  
  108.             if (flag == false) {  
  109.                 p[i] = 0.f;  
  110.             }  
  111.         }  
  112.         // 轮盘赌选择下一个城市  
  113.         Random random = new Random(System.currentTimeMillis());  
  114.         float sleectP = random.nextFloat();  
  115.         int selectCity = 0;  
  116.         float sum1 = 0.f;  
  117.         for (int i = 0; i < cityNum; i++) {  
  118.             sum1 += p[i];  
  119.             if (sum1 >= sleectP) {  
  120.                 selectCity = i;  
  121.                 break;  
  122.             }  
  123.         }  
  124.         // 从允许选择的城市中去除select city  
  125.         for (Integer i : allowedCities) {  
  126.             if (i.intValue() == selectCity) {  
  127.                 allowedCities.remove(i);  
  128.                 break;  
  129.             }  
  130.         }  
  131.         // 在禁忌表中添加select city  
  132.         tabu.add(Integer.valueOf(selectCity));  
  133.         // 将当前城市改为选择的城市  
  134.         currentCity = selectCity;  
  135.     }  
  136.   
  137.     /** 
  138.      * 计算路径长度 
  139.      *  
  140.      * @return 路径长度 
  141.      */  
  142.     private int calculateTourLength() {  
  143.         int len = 0;  
  144.         //禁忌表tabu最终形式:起始城市,城市1,城市2...城市n,起始城市  
  145.         for (int i = 0; i < cityNum; i++) {  
  146.             len += distance[this.tabu.get(i).intValue()][this.tabu.get(i + 1)  
  147.                     .intValue()];  
  148.         }  
  149.         return len;  
  150.     }  
  151.   
  152.     public Vector<Integer> getAllowedCities() {  
  153.         return allowedCities;  
  154.     }  
  155.   
  156.     public void setAllowedCities(Vector<Integer> allowedCities) {  
  157.         this.allowedCities = allowedCities;  
  158.     }  
  159.   
  160.     public int getTourLength() {  
  161.         tourLength = calculateTourLength();  
  162.         return tourLength;  
  163.     }  
  164.   
  165.     public void setTourLength(int tourLength) {  
  166.         this.tourLength = tourLength;  
  167.     }  
  168.   
  169.     public int getCityNum() {  
  170.         return cityNum;  
  171.     }  
  172.   
  173.     public void setCityNum(int cityNum) {  
  174.         this.cityNum = cityNum;  
  175.     }  
  176.   
  177.     public Vector<Integer> getTabu() {  
  178.         return tabu;  
  179.     }  
  180.   
  181.     public void setTabu(Vector<Integer> tabu) {  
  182.         this.tabu = tabu;  
  183.     }  
  184.   
  185.     public float[][] getDelta() {  
  186.         return delta;  
  187.     }  
  188.   
  189.     public void setDelta(float[][] delta) {  
  190.         this.delta = delta;  
  191.     }  
  192.   
  193.     public int getFirstCity() {  
  194.         return firstCity;  
  195.     }  
  196.   
  197.     public void setFirstCity(int firstCity) {  
  198.         this.firstCity = firstCity;  
  199.     }  
  200.   
  201. }  

[java]  view plain  copy
  1. package noah;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7.   
  8. public class ACO {  
  9.   
  10.     private Ant[] ants; // 蚂蚁  
  11.     private int antNum; // 蚂蚁数量  
  12.     private int cityNum; // 城市数量  
  13.     private int MAX_GEN; // 运行代数  
  14.     private float[][] pheromone; // 信息素矩阵  
  15.     private int[][] distance; // 距离矩阵  
  16.     private int bestLength; // 最佳长度  
  17.     private int[] bestTour; // 最佳路径  
  18.   
  19.     // 三个参数  
  20.     private float alpha;  
  21.     private float beta;  
  22.     private float rho;  
  23.   
  24.     public ACO() {  
  25.   
  26.     }  
  27.   
  28.     /** 
  29.      * constructor of ACO 
  30.      *  
  31.      * @param n 
  32.      *            城市数量 
  33.      * @param m 
  34.      *            蚂蚁数量 
  35.      * @param g 
  36.      *            运行代数 
  37.      * @param a 
  38.      *            alpha 
  39.      * @param b 
  40.      *            beta 
  41.      * @param r 
  42.      *            rho 
  43.      *  
  44.      **/  
  45.     public ACO(int n, int m, int g, float a, float b, float r) {  
  46.         cityNum = n;  
  47.         antNum = m;  
  48.         ants = new Ant[antNum];  
  49.         MAX_GEN = g;  
  50.         alpha = a;  
  51.         beta = b;  
  52.         rho = r;  
  53.     }  
  54.   
  55.     // 给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默  
  56.     @SuppressWarnings("resource")  
  57.     /** 
  58.      * 初始化ACO算法类 
  59.      * @param filename 数据文件名,该文件存储所有城市节点坐标数据 
  60.      * @throws IOException 
  61.      */  
  62.     private void init(String filename) throws IOException {  
  63.         // 读取数据  
  64.         int[] x;  
  65.         int[] y;  
  66.         String strbuff;  
  67.         BufferedReader data = new BufferedReader(new InputStreamReader(  
  68.                 new FileInputStream(filename)));  
  69.         distance = new int[cityNum][cityNum];  
  70.         x = new int[cityNum];  
  71.         y = new int[cityNum];  
  72.         for (int i = 0; i < cityNum; i++) {  
  73.             // 读取一行数据,数据格式1 6734 1453  
  74.             strbuff = data.readLine();  
  75.             // 字符分割  
  76.             String[] strcol = strbuff.split(" ");  
  77.             x[i] = Integer.valueOf(strcol[1]);// x坐标  
  78.             y[i] = Integer.valueOf(strcol[2]);// y坐标  
  79.         }  
  80.         // 计算距离矩阵  
  81.         // 针对具体问题,距离计算方法也不一样,此处用的是att48作为案例,它有48个城市,距离计算方法为伪欧氏距离,最优值为10628  
  82.         for (int i = 0; i < cityNum - 1; i++) {  
  83.             distance[i][i] = 0// 对角线为0  
  84.             for (int j = i + 1; j < cityNum; j++) {  
  85.                 double rij = Math  
  86.                         .sqrt(((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j])  
  87.                                 * (y[i] - y[j])) / 10.0);  
  88.                 // 四舍五入,取整  
  89.                 int tij = (int) Math.round(rij);  
  90.                 if (tij < rij) {  
  91.                     distance[i][j] = tij + 1;  
  92.                     distance[j][i] = distance[i][j];  
  93.                 } else {  
  94.                     distance[i][j] = tij;  
  95.                     distance[j][i] = distance[i][j];  
  96.                 }  
  97.             }  
  98.         }  
  99.         distance[cityNum - 1][cityNum - 1] = 0;  
  100.         // 初始化信息素矩阵  
  101.         pheromone = new float[cityNum][cityNum];  
  102.         for (int i = 0; i < cityNum; i++) {  
  103.             for (int j = 0; j < cityNum; j++) {  
  104.                 pheromone[i][j] = 0.1f; // 初始化为0.1  
  105.             }  
  106.         }  
  107.         bestLength = Integer.MAX_VALUE;  
  108.         bestTour = new int[cityNum + 1];  
  109.         // 随机放置蚂蚁  
  110.         for (int i = 0; i < antNum; i++) {  
  111.             ants[i] = new Ant(cityNum);  
  112.             ants[i].init(distance, alpha, beta);  
  113.         }  
  114.     }  
  115.   
  116.     public void solve() {  
  117.         // 迭代MAX_GEN次  
  118.         for (int g = 0; g < MAX_GEN; g++) {  
  119.             // antNum只蚂蚁  
  120.             for (int i = 0; i < antNum; i++) {  
  121.                 // i这只蚂蚁走cityNum步,完整一个TSP  
  122.                 for (int j = 1; j < cityNum; j++) {  
  123.                     ants[i].selectNextCity(pheromone);  
  124.                 }  
  125.                 // 把这只蚂蚁起始城市加入其禁忌表中  
  126.                 // 禁忌表最终形式:起始城市,城市1,城市2...城市n,起始城市  
  127.                 ants[i].getTabu().add(ants[i].getFirstCity());  
  128.                 // 查看这只蚂蚁行走路径距离是否比当前距离优秀  
  129.                 if (ants[i].getTourLength() < bestLength) {  
  130.                     // 比当前优秀则拷贝优秀TSP路径  
  131.                     bestLength = ants[i].getTourLength();  
  132.                     for (int k = 0; k < cityNum + 1; k++) {  
  133.                         bestTour[k] = ants[i].getTabu().get(k).intValue();  
  134.                     }  
  135.                 }  
  136.                 // 更新这只蚂蚁的信息数变化矩阵,对称矩阵  
  137.                 for (int j = 0; j < cityNum; j++) {  
  138.                     ants[i].getDelta()[ants[i].getTabu().get(j).intValue()][ants[i]  
  139.                             .getTabu().get(j + 1).intValue()] = (float) (1. / ants[i]  
  140.                             .getTourLength());  
  141.                     ants[i].getDelta()[ants[i].getTabu().get(j + 1).intValue()][ants[i]  
  142.                             .getTabu().get(j).intValue()] = (float) (1. / ants[i]  
  143.                             .getTourLength());  
  144.                 }  
  145.             }  
  146.             // 更新信息素  
  147.             updatePheromone();  
  148.             // 重新初始化蚂蚁  
  149.             for (int i = 0; i < antNum; i++) {  
  150.                 ants[i].init(distance, alpha, beta);  
  151.             }  
  152.         }  
  153.   
  154.         // 打印最佳结果  
  155.         printOptimal();  
  156.     }  
  157.   
  158.     // 更新信息素  
  159.     private void updatePheromone() {  
  160.         // 信息素挥发  
  161.         for (int i = 0; i < cityNum; i++)  
  162.             for (int j = 0; j < cityNum; j++)  
  163.                 pheromone[i][j] = pheromone[i][j] * (1 - rho);  
  164.         // 信息素更新  
  165.         for (int i = 0; i < cityNum; i++) {  
  166.             for (int j = 0; j < cityNum; j++) {  
  167.                 for (int k = 0; k < antNum; k++) {  
  168.                     pheromone[i][j] += ants[k].getDelta()[i][j];  
  169.                 }  
  170.             }  
  171.         }  
  172.     }  
  173.   
  174.     private void printOptimal() {  
  175.         System.out.println("The optimal length is: " + bestLength);  
  176.         System.out.println("The optimal tour is: ");  
  177.         for (int i = 0; i < cityNum + 1; i++) {  
  178.             System.out.println(bestTour[i]);  
  179.         }  
  180.     }  
  181.   
  182.   
  183.     /** 
  184.      * @param args 
  185.      * @throws IOException 
  186.      */  
  187.     public static void main(String[] args) throws IOException {  
  188.         System.out.println("Start....");  
  189.         ACO aco = new ACO(48101001.f, 5.f, 0.5f);  
  190.         aco.init("c://data.txt");  
  191.         aco.solve();  
  192.     }  
  193.   
  194. }  

运行结果截图:

四、总结

蚁群算法是一种本质上并行的算法。每只蚂蚁搜索的过程彼此独立,仅通过信息激素进行通信。所以蚁群算法则可以看作是一个分布式的多agent系统,它在问题空间的多点同时开始进行独立的解搜索,不仅增加了算法的可靠性,也使得算法具有较强的全局搜索能力,但是也正是由于其并行性的本质,蚁群算法的搜索时间较长,在求解小规模的NP问题时耗费的计算资源相比其他启发式算法要多,因而显得效率很低下,而当问题趋向于大规模时,蚁群算法还是存在难收敛的问题,个人感觉除非你真想耗费大量计算资源来干一件事情,否则还是慎用蚁群算法。

注:本文部分内容来源于网络,但程序以及分析结果属于本人成果,转载请注明!


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值