蚁群算法(AG)是一种模拟蚂蚁觅食行为的模拟优化算法,它是由意大利学者Dorigo M等人于1991年首先提出,并首先使用在解决TSP(旅行商问题)上。
之后,又系统研究了蚁群算法的基本原理和数学模型.
蚁群算法的基本思想:
蚁群算法的基本原理:

1、蚂蚁在路径上释放信息素。

2、碰到还没走过的路口,就随机挑选一条路走。同时,释放与路径长度有关的信息素。

3、信息素浓度与路径长度成反比。后来的蚂蚁再次碰到该路口时,就选择信息素浓度较高路径。

4、最优路径上的信息素浓度越来越大。

5、最终蚁群找到最优寻食路径。

人工蚁群与真实蚁群对比:

基于TSP问题的基本蚁群算法:

TSP求解中,假设蚁群算法中的每只蚂蚁是具有以下特征的简单智能体:

每次周游,每只蚂蚁在其经过的支路(i,j)上都留下信息素。

‚蚂蚁选择城市的概率与城市之间的距离和当前连接支路上所包含的信息素余量有关。

ƒ为了强制蚂蚁进行合法的周游,直到一次周游完成后,才允许蚂蚁游走已访问过的城市(这可由禁忌表来控制)。

基本蚁群的两个过程:

(1)状态转移

(2)信息素更新

(1)状态转移

为了避免残留信息素过多而淹没启发信息,在每只蚂蚁走完一步或者完成对所有n个城市的遍历(也即一个循环结束)后,要对残留信息进行更新处理。

由此,t+n时刻在路径(i,j)上的信息量可按如下规则进行调整:

(2)信息素更新模型

蚁周模型(Ant-Cycle模型)

蚁量模型(Ant-Quantity模型)

蚁密模型(Ant-Density模型)

区别:

1.蚁周模型利用的是全局信息,即蚂蚁完成一个循环后更新所有路径上的信息素;

2.蚁量和蚁密模型利用的是局部信息,即蚂蚁完成一步后更新路径上的信息素。

蚁群算法基本流程:

蚁群算法中主要参数的选择:

蚁群算法中主要参数的理想选择如下:

国内外,对于离散域蚁群算法的改进研究成果很多,例如自适应蚁群算法、基于信息素扩散的蚁群算法等,这里仅介绍离散域优化问题的自适应蚁群算法。

自适应蚁群算法:对蚁群算法的状态转移概率、信息素挥发因子、信息量等因素采用自适应调节策略为一种基本改进思路的蚁群算法。

自适应蚁群算法中两个最经典的方法:蚁群系统(AntColony System, ACS)和最大-最小蚁群系统(MAX-MINAnt System, MMAS)。

蚁群系统对基本蚁群算法改进:

①蚂蚁的状态转移规则不同;

②全局更新规则不同;

③新增了对各条路径信息量调整的局部更新规则

下面是Python实现求50个城市之间最短距离的代码

# -*- coding: utf-8 -*-
import random
import copy
import time
import sys
import math
import tkinter #//GUI模块
import threading
from functools import reduce# 参数
'''
ALPHA:信息启发因子,值越大,则蚂蚁选择之前走过的路径可能性就越大,值越小,则蚁群搜索范围就会减少,容易陷入局部最优
BETA:Beta值越大,蚁群越就容易选择局部较短路径,这时算法收敛速度会加快,但是随机性不高,容易得到局部的相对最优
'''
(ALPHA, BETA, RHO, Q) = (1.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)]
pheromone_graph = [ [1.0 for col in range(city_num)] for raw in range(city_num)]#----------- 蚂蚁 -----------
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:# 产生一个随机概率,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, root, width = 800, height = 600, n = city_num):# 创建画布self.root = root                               self.width = width      self.height = height# 城市数目初始化为city_numself.n = n# tkinter.Canvasself.canvas = tkinter.Canvas(root,width = self.width,height = self.height,bg = "#EBEBEB",             # 背景白色 xscrollincrement = 1,yscrollincrement = 1)self.canvas.pack(expand = tkinter.YES, fill = tkinter.BOTH)self.title("TSP蚁群算法(n:初始化 e:开始搜索 s:停止搜索 q:退出程序)")self.__r = 5self.__lock = threading.RLock()     # 线程锁self.__bindEvents()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)temp_distance = pow(temp_distance, 0.5)distance_graph[i][j] =float(int(temp_distance + 0.5))# 按键响应程序def __bindEvents(self):self.root.bind("q", self.quite)        # 退出程序self.root.bind("n", self.new)          # 初始化self.root.bind("e", self.search_path)  # 开始搜索self.root.bind("s", self.stop)         # 停止搜索# 更改标题def title(self, s):self.root.title(s)# 初始化def new(self, evt = None):# 停止线程self.__lock.acquire()self.__running = Falseself.__lock.release()self.clear()     # 清除信息 self.nodes = []  # 节点坐标self.nodes2 = [] # 节点对象# 初始化城市节点for i in range(len(distance_x)):# 在画布上随机初始坐标x = distance_x[i]y = distance_y[i]self.nodes.append((x, y))# 生成节点椭圆,半径为self.__rnode = self.canvas.create_oval(x - self.__r,y - self.__r, x + self.__r, y + self.__r,fill = "#ff0000",      # 填充红色outline = "#000000",   # 轮廓白色tags = "node",)self.nodes2.append(node)# 显示坐标self.canvas.create_text(x,y-10,              # 使用create_text方法在坐标(302,77)处绘制文字text = '('+str(x)+','+str(y)+')',    # 所绘制文字的内容fill = 'black'                       # 所绘制文字的颜色为灰色)# 顺序连接城市#self.line(range(city_num))# 初始城市之间的距离和信息素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           # 初始最大距离self.iter = 1                                    # 初始化迭代次数 # 将节点按order顺序连线def line(self, order):# 删除原线self.canvas.delete("line")def line2(i1, i2):p1, p2 = self.nodes[i1], self.nodes[i2]self.canvas.create_line(p1, p2, fill = "#000000", tags = "line")return i2# order[-1]为初始值reduce(line2, order, order[-1])# 清除画布def clear(self):for item in self.canvas.find_all():self.canvas.delete(item)# 退出程序def quite(self, evt):self.__lock.acquire()self.__running = Falseself.__lock.release()self.root.destroy()print (u"\n程序已退出...")sys.exit()# 停止搜索def stop(self, evt):self.__lock.acquire()self.__running = Falseself.__lock.release()# 开始搜索def search_path(self, evt = None):# 开启线程self.__lock.acquire()self.__running = Trueself.__lock.release()while self.__running:# 遍历每一只蚂蚁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))# 连线self.line(self.best_ant.path)# 设置标题self.title("TSP蚁群算法(n:随机初始 e:开始搜索 s:停止搜索 q:退出程序) 迭代次数: %d" % self.iter)# 更新画布self.canvas.update()self.iter += 1# 更新信息素def __update_pheromone_gragh(self):# 获取每只蚂蚁在其路径上留下的信息素temp_pheromone = [[0.0 for col in range(city_num)] for raw in range(city_num)]for ant in self.ants:for i in range(1,city_num):start, end = ant.path[i-1], ant.path[i]# 在路径上的每两个相邻城市间留下信息素,与路径总距离反比temp_pheromone[start][end] += Q / ant.total_distancetemp_pheromone[end][start] = temp_pheromone[start][end]# 更新所有城市之间的信息素,旧信息素衰减加上新迭代信息素for i in range(city_num):for j in range(city_num):pheromone_graph[i][j] = pheromone_graph[i][j] * RHO + temp_pheromone[i][j]# 主循环def mainloop(self):self.root.mainloop()#----------- 程序的入口处 -----------if __name__ == '__main__':print (u"""
--------------------------------------------------------程序:蚁群算法解决TPS问题程序 作者:许彬 日期:2015-12-10语言:Python 2.7
-------------------------------------------------------- """)TSP(tkinter.Tk()).mainloop()

蚁群算法原理及其实现(python)相关推荐

  1. 蚁群算法原理详解和matlab代码

    1原理: 蚂蚁在寻找食物源的时候,能在其走过的路径上释放一种叫信息素的激素,使一定范围内的其他蚂蚁能够察觉到.当一些路径上通过的蚂蚁越来越多时,信息素也就越来越多,蚂蚁们选择这条路径的概率也就越高,结 ...

  2. 道路匹配MapMatching:GPS轨迹点常用聚类算法介绍(K-Means聚类、蚁群算法等)

    道路匹配MapMatching:GPS轨迹点常用聚类算法介绍(K-Means聚类.蚁群算法等) 前言 一.聚类算法是什么? 二.道路匹配中常见聚类算法介绍 1.K-Means算法 2.基于时间和距离的 ...

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

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

  4. MATLAB机器学习系列-12:蚁群算法优化原理及其matlab实现

    蚁群算法原理 概述 蚁群算法(Ant Colony Algorithm, ACA)由Marco Dorigo于1992年在他的博士论文中首次提出,该算法模拟了自然界中蚂蚁的觅食行为. 蚂蚁在寻找食物源 ...

  5. 离散蚁群算法实例(求解旅行商问题)

    蚁群算法 蚁群算法原理 万字长文带你了解蚁群算法及求解复杂约束问题[源码实现] 上面这篇博文的蚁群算法是实数编码.今天讲解下离散编码的蚁群算法.      算法原理不再解释,直接上算例. 旅行商问题 ...

  6. 数学建模算法模型--蚁群算法

    有关蚁群算法学习资料分享: 链接:https://pan.baidu.com/s/10rY9OYN0ADfhKDXOK0R4fA?pwd=v09z  提取码:v09z 蚁群算法(Ant Colony ...

  7. 群智能算法 第4关:蚁群算法 - 商队旅行最短路径计算

    任务描述 本关任务:使用 python 实现蚁群算法,并寻找商队旅行最短路径. 相关知识 为了完成本关任务,你需要掌握:1.蚁群算法原理,2.蚁群算法流程,3.使用蚁群算法解决商队旅行问题. 蚁群算法 ...

  8. 智能优化算法之蚁群算法(1)

    蚁群算法(ant colony algorithm) : 一种模拟进化算法 蚂蚁在觅食过程中能够在其经过的路径留下一种称为信息素的物质,并在觅食的过程中能感知这种物质的强度,并指导自己的行动方向,他们 ...

  9. 蚁群算法和简要matlab来源

    1 蚁群算法原理 从1991由意大利学者 M. Dorigo,V. Maniezzo 和 A. Colorni 通过模拟蚁群觅食行为提出了一种基于群体的模拟进化算法--蚁群优化.极大关注,蚁群算法的特 ...

  10. 蚁群算法(Ant Colony Optimization)

    蚁群算法(Ant Colony Optimization) 蚁群算法简介 蚁群算法(Ant Clony Optimization, ACO)是一种群智能算法,它是由一群无智能或有轻微智能的个体(Age ...

最新文章

  1. 【C++】C/C++ 中default/delete特性
  2. 如何读取csv文件中第n行数据python-python数据处理之如何选取csv文件中某几行的数据...
  3. PHP + Redis 实现消息队列
  4. python中object转str_python的id()函数介绍 python怎么将objectid转为str
  5. 带参数的过滤器|| 过滤器案例:格式化日期|| time.js  ||
  6. 搭建一个自己的SVN服务器
  7. IT运维管理与ITIL
  8. 前台获取json未定义问题之两种常用解决办法
  9. 平面/UI设计师社区交流网站集设|给你的作品多一个展示机会
  10. C8051汇编语言递归,基于C8051F310单片机的LED灯控制器汇编语言程序调试
  11. 动态添加GRIDVIEW内容 和数据绑定
  12. Amos实操教程|调节效应检验
  13. 友华改设备标识命令_PT632 G_2装备指令
  14. python中keyword_python的keyword模块用法实例分析
  15. Python——飞机大战源码(含飞机爆炸效果动图)
  16. csdn博客更换皮肤
  17. 基于Kinect 2.0深度图像的快速体积测量
  18. 学会原谅自己,我们会更加强大
  19. UML---序列图/时序图/顺序图
  20. 七牛云的存储对象的地区对应表

热门文章

  1. python 合并txt文件
  2. 穿越火线枪战王者服务器维护,CF手游体验服7.3维护公告 战争风云模式登场
  3. couldn't connect to the device trackpad
  4. 浅谈unicode字符集及编码方式
  5. oracle数据库sql语句修改表某列字段长度
  6. 免费智能AI文章生成器-只需要输入关键词自动生成文章的软件
  7. 使用uiautomation 实现微信自动发送消息
  8. 台湾骑行环岛攻略(转)
  9. Firefox选择哪个IE TAB
  10. java 实现微信搜索附近人功能