实验目的

(1)理解蚁群算法的原理以及优缺点
(2)能够用蚁群算法解决实际问题

实验内容

旅行商问题(TSP,traveling salesman problem):一商人去n个城市销货,所有城市走—遍再回到起点,使所走路程最短。
要求使用Python语言用蚁群算法解决TSP问题。

参考代码:

'''
蚁群算法在TSP问题中的实现
'''
import random
import copy
import sys# 参数
'''
ALPHA:信息启发因子,值越大,则蚂蚁选择之前走过的路径可能性就越大,值越小,则蚁群搜索范围就会减少,容易陷入局部最优
BETA:Beta值越大,蚁群越就容易选择局部较短路径,这时算法收敛速度会加快,但是随机性不高,容易得到局部的相对最优
'''
(ALPHA, BETA, RHO, Q) = (15.0, 2.0, 0.5, 100.0)
# 城市数,蚁群
(city_num, ant_num) = (50, 50)
distance_x = [178, 272, 176, 171, 650, 499, 267, 703, 408, 437, 491, 74, 532,416, 626, 42, 271, 359, 163, 508, 229, 576, 147, 560, 35, 714,757, 517, 64, 314, 675, 690, 391, 628, 87, 240, 705, 699, 258,428, 614, 36, 360, 482, 666, 597, 209, 201, 492, 294]
distance_y = [170, 395, 198, 151, 242, 556, 57, 401, 305, 421, 267, 105, 525,381, 244, 330, 395, 169, 141, 380, 153, 442, 528, 329, 232, 48,498, 265, 343, 120, 165, 50, 433, 63, 491, 275, 348, 222, 288,490, 213, 524, 244, 114, 104, 552, 70, 425, 227, 331]
# 城市距离和信息素
distance_graph = [[0.0 for col in range(city_num)] for raw in range(city_num)]  # 50*50 0.0   #距离
pheromone_graph = [[1.0 for col in range(city_num)] for raw in range(city_num)]  # 50*50  1.0  ##信息素浓度# ----------- 蚂蚁 -----------
class Ant(object):# 初始化def __init__(self, ID):self.ID = ID  # IDself.__clean_data()  # 随机初始化出生点# 初始数据def __clean_data(self):self.path = []  # 当前蚂蚁的路径self.total_distance = 0.0  # 当前路径的总距离self.move_count = 0  # 移动次数self.current_city = -1  # 当前停留的城市self.open_table_city = [True for i in range(city_num)]  # 探索城市的状态city_index = random.randint(0, city_num - 1)  # 随机初始出生点self.current_city = city_indexself.path.append(city_index)self.open_table_city[city_index] = Falseself.move_count = 1# 选择下一个城市def __choice_next_city(self):next_city = -1select_citys_prob = [0.0 for i in range(city_num)]  # 存储去下个城市的概率total_prob = 0.0# 获取去下一个城市的概率for i in range(city_num):if self.open_table_city[i]:  # 还没有走过try:# 计算概率:与信息素浓度成正比,与距离成反比select_citys_prob[i] = pow(pheromone_graph[self.current_city][i], ALPHA) * pow((1.0 / distance_graph[self.current_city][i]), BETA)total_prob += select_citys_prob[i]except ZeroDivisionError as e:print('Ant ID: {ID}, current city: {current}, target city: {target}'.format(ID=self.ID,current=self.current_city,target=i))sys.exit(1)# 轮盘选择城市if total_prob > 0.0:  # 选择城市i# 产生一个随机概率,0.0-total_probtemp_prob = random.uniform(0.0, total_prob)for i in range(city_num):if self.open_table_city[i]:# 轮次相减temp_prob -= select_citys_prob[i]if temp_prob < 0.0:next_city = ibreak# 未从概率产生,顺序选择一个未访问城市# if next_city == -1:#     for i in range(city_num):#         if self.open_table_city[i]:#             next_city = i#             breakif (next_city == -1):  #next_city = random.randint(0, city_num - 1)while ((self.open_table_city[next_city]) == False):  # if==False,说明已经遍历过了next_city = random.randint(0, city_num - 1)# 返回下一个城市序号return next_city# 计算路径总距离def __cal_total_distance(self):temp_distance = 0.0for i in range(1, city_num):start, end = self.path[i], self.path[i - 1]temp_distance += distance_graph[start][end]# 回路end = self.path[0]temp_distance += distance_graph[start][end]self.total_distance = temp_distance# 移动操作def __move(self, next_city):self.path.append(next_city)self.open_table_city[next_city] = Falseself.total_distance += distance_graph[self.current_city][next_city]self.current_city = next_cityself.move_count += 1# 搜索路径def search_path(self):# 初始化数据self.__clean_data()# 搜素路径,遍历完所有城市为止while self.move_count < city_num:# 移动到下一个城市next_city = self.__choice_next_city()self.__move(next_city)# 计算路径总长度self.__cal_total_distance()# ----------- TSP问题 -----------class TSP(object):def __init__(self):self.new()# 计算城市之间的距离for i in range(city_num):for j in range(city_num):temp_distance = pow((distance_x[i] - distance_x[j]), 2) + pow((distance_y[i] - distance_y[j]),2)  # pow() 方法返回 xy(x的y次方) 的值。temp_distance = pow(temp_distance, 0.5)distance_graph[i][j] = float(int(temp_distance + 0.5))# 初始化def new(self):# 初始城市之间的距离和信息素for i in range(city_num):for j in range(city_num):pheromone_graph[i][j] = 1.0self.ants = [Ant(ID) for ID in range(ant_num)]  # 初始蚁群self.best_ant = Ant(-1)  # 初始最优解self.best_ant.total_distance = 1 << 31  # 初始最大距离 <<位运算符,左移31位self.iter = 1  # 初始化迭代次数# 开始搜索def search_path(self):while (1):# 遍历每一只蚂蚁for ant in self.ants:# 搜索一条路径ant.search_path()# 与当前最优蚂蚁比较if ant.total_distance < self.best_ant.total_distance:# 更新最优解self.best_ant = copy.deepcopy(ant)# 更新信息素self.__update_pheromone_gragh()#print(u"迭代次数:", self.iter, u"最佳路径总距离:", int(self.best_ant.total_distance))#可以查看迭代的过程if int(self.best_ant.total_distance) <= 4000:print('迭代完成')breakself.iter += 1# 更新信息素def __update_pheromone_gragh(self):import sys# 获取每只蚂蚁在其路径上留下的信息素temp_pheromone = [[0.0 for col in range(city_num)] for raw in range(city_num)]  # 初始化新迭代信息素for ant in self.ants:length = 0for i in range(1, city_num):start, end = ant.path[i - 1], ant.path[i]# 在路径上的每两个相邻城市间留下信息素,与路径总距离反比###########开始1#############length += distance_graph[start][end]  # 获取总长度for i in range(1, city_num):start, end = ant.path[i - 1], ant.path[i]temp_pheromone[start][end] = max(temp_pheromone[start][end], sys.maxsize / length)###########结束1############## 更新所有城市之间的信息素,旧信息素衰减加上新迭代信息素###########开始2#############for i in range(0, city_num):for s in range(0, city_num):pheromone_graph[i][s] += temp_pheromone[i][s]###########结束2############## ----------- 程序的入口处 -----------
if __name__ == '__main__':TSP().search_path()

系列文章:

实验1:猴子摘香蕉问题的Python编程实现
实验2:编程实现简单恐龙识别系统的知识表示
实验3:搜索算法求解8数码问题
实验4:字句集消解实验
实验5:简单恐龙识别系统的产生式推理
实验6:蚁群算法在TSP问题中的实现
实验7:粒子群优化算法实验
实验8:遗传算法在TSP问题中的实现
实验9:BP神经网络实验

实验6:蚁群算法在TSP问题中的实现相关推荐

  1. 人工智能实验:蚁群算法求解TSP问题(Python代码实现,附有详细实验报告地址)

    项目简介 这是人工智能实验课的一次作业,项目文件中包含两个py文件,其中Main.py是算法的主体,而其他一些实现则放在AidFunctions.py文件中.代码注释比较详细,可以对照实验报告进行阅览 ...

  2. 蚁群算法解决tsp问题python_蚁群算法在解决TSP问题中的应用

    陈灵佳 文章首先对蚁群算法与TSP问题进行简要介绍,在此基础上对蚁群算法在解决TSP问题中的应用进行论述.期望通过本文的研究能够对TSP问题的解决有所帮助. [关键词]蚁群算法 TSP问题 最优解 1 ...

  3. 用蚁群算法求解TSP问题

    TSP是什么?TSP全称Travelling salesman problem.中文名:旅行商问题.就是模拟退火中讲到的14个城市之间巡回旅行,求路径最短的问题. 为什么偏偏找他呢?因为这是一个衡量算 ...

  4. 机器学习(MACHINE LEARNING)MATLAB蚁群算法解决TSP问题

    文章目录 1 蚁群算法 2 蚁群算法与TSP问题的关系 3 代码实现 1 蚁群算法 基本原理: (1)蚂蚁在携带等量的信息素一路释放 (2)信息素浓度会和路径的长度成反比 (3)下次蚂蚁来到该路口会选 ...

  5. 蚁群算法解决tsp问题c语言,蚁群算法解决TSP问题程序.doc

    蚁群算法解决TSP问题程序 蚁群算法用于求解TSP问题,经过仿真测试,发现此程序的优化效率和鲁棒性都非常好. 这与在无线多媒体传感器网络路由算法应用到的寻找最佳路径的蚁群算法非常相似. functio ...

  6. 蚁群算法求解TSP问题的源代码

    蚁群算法求解TSP问题的源代码 分类: 智能算法2014-05-07 17:25 524人阅读 评论(1) 收藏 举报 蚁群算法 TSP win32程序设计 旅行商问题大都是用遗传算法求解,不过蚁群算 ...

  7. 蚁群算法解决 TSP 问题

    蚁群算法解决 TSP 问题 数据集 Tools.py Ant.py ACO_G.py 运行效果 数据集 json 形式(c.json)的中国各省市经纬度数据集,一共 2241 个市的数据,为后来的 T ...

  8. 蚁群算法求解TSP问题

    蚁群算法求解TSP问题 蚁群算法求解TSP问题 蚁群算法求解TSP问题 旅行商问题,即TSP问题(Traveling Salesman Problem)又译为旅行推销员问题.货郎担问题,是数学领域中著 ...

  9. 【建模算法】基于蚁群算法求解TSP问题(Python实现)

    [建模算法]基于蚁群算法(ACA)求解TSP问题(Python实现) TSP (traveling salesman problem,旅行商问题)是典型的NP完全问题,即其最坏情况下的时间复杂度随着问 ...

最新文章

  1. php 相加 机组数字,PHP-80型等压比例混合器
  2. [luoguP2760] 科技庄园(背包DP)
  3. (44)FPGA条件编译(选择语句)
  4. POJ 2115C Looooops[一元线性同余方程]
  5. 深度学习(二十一)基于FCN的图像语义分割-CVPR 2015
  6. Spotlight监控Oracle--Spotlight On Oracle安装和使用
  7. 16进制颜色转换RGB原理
  8. 记仇表情包在线生成源码
  9. linux系统构建学习笔记
  10. js文档加载事件---
  11. vue基于vant实现上拉加载下拉刷新
  12. 标准柯西分布_对柯西分布性质的进一步讨论
  13. Word2Vec实战
  14. 孩子小学总喜欢用计算机做数学,数学到底该怎样学?真实用!
  15. js两只手指控制div图片放大缩小功能
  16. 新型网络病毒“风暴”防护解决方案
  17. 使用httpclient必须知道的参数设置及代码写法、存在的风险
  18. 苹果ppt_如何选择一款趁手的PPT软件 | 一千零一夜PPT系列
  19. 20155305乔磊2016-2017-2《Java程序设计》第五周学习总结
  20. 360兼容模式,搜狗等奇葩浏览器下无法正常渲染的问题

热门文章

  1. 最小生成树计数模板及原理
  2. lumia 更新到 windows 10 之后的一些问题及解决办法
  3. 十进制转十六进制方法
  4. JAVA中0xFF代表什么_详解 0xff 的作用
  5. 航天一院(运载火箭研究院)待遇情况
  6. 量化投资之自动化实盘交易
  7. Android6.0的phone应用源码分析(3)——phone 拨号UI分析
  8. 网络连接图标不见了还能上网的解决办法
  9. do...while(0)的妙用[转自:http://www.yuanma.org/data/2007/0209/article_2271.htm]
  10. C#Assembly的使用