蚁群算法Python实现

article/2025/9/24 20:55:49

解决的问题

三维地形中,给出起点和重点,找到其最优路径。

这里写图片描述

作图源码:

from mpl_toolkits.mplot3d import proj3d
from mpl_toolkits.mplot3d import Axes3D
import numpy as npheight3d = np.array([[2000,1400,800,650,500,750,1000,950,900,800,700,900,1100,1050,1000,1150,1300,1250,1200,1350,1500],                   [1100,900,700,625,550,825,1100,1150,1200,925,650,750,850,950,1050,1175,1300,1350,1400,1425,1450],                    [200,400,600,600,600,900,1200,1350,1500,1050,600,600,600,850,1100,1200,1300,1450,1600,1500,1400],                   [450,500,550,575,600,725,850,875,900,750,600,600,600,725,850,900,950,1150,1350,1400,1450],                   [700,600,500,550,600,550,500,400,300,450,600,600,600,600,600,600,600,850,1100,1300,1500],                    [500,525,550,575,600,575,550,450,350,475,600,650,700,650,600,600,600,725,850,1150,1450],                    [300,450,600,600,600,600,600,500,400,500,600,700,800,700,600,600,600,600,600,1000,1400],                    [550,525,500,550,600,875,1150,900,650,725,800,700,600,875,1150,1175,1200,975,750,875,1000],                    [800,600,400,500,600,1150,1700,1300,900,950,1000,700,400,1050,1700,1750,1800,1350,900,750,600],                   [650,600,550,625,700,1175,1650,1275,900,1100,1300,1275,1250,1475,1700,1525,1350,1200,1050,950,850],                   [500,600,700,750,800,1200,1600,1250,900,1250,1600,1850,2100,1900,1700,1300,900,1050,1200,1150,1100],                   [400,375,350,600,850,1200,1550,1250,950,1225,1500,1750,2000,1950,1900,1475,1050,975,900,1175,1450],                    [300,150,0,450,900,1200,1500,1250,1000,1200,1400,1650,1900,2000,2100,1650,1200,900,600,1200,1800],                    [600,575,550,750,950,1275,1600,1450,1300,1300,1300,1525,1750,1625,1500,1450,1400,1125,850,1200,1550],                    [900,1000,1100,1050,1000,1350,1700,1650,1600,1400,1200,1400,1600,1250,900,1250,1600,1350,1100,1200,1300],                   [750,850,950,900,850,1000,1150,1175,1200,1300,1400,1325,1250,1125,1000,1150,1300,1075,850,975,1100],                    [600,700,800,750,700,650,600,700,800,1200,1600,1250,900,1000,1100,1050,1000,800,600,750,900],                    [750,775,800,725,650,700,750,775,800,1000,1200,1025,850,975,1100,950,800,900,1000,1050,1100],                    [900,850,800,700,600,750,900,850,800,800,800,800,800,950,1100,850,600,1000,1400,1350,1300],                    [750,800,850,850,850,850,850,825,800,750,700,775,850,1000,1150,875,600,925,1250,1100,950],                   [600,750,900,1000,1100,950,800,800,800,700,600,750,900,1050,1200,900,600,850,1100,850,600]])fig = figure()
ax = Axes3D(fig)
X = np.arange(21)
Y = np.arange(21)
X, Y = np.meshgrid(X, Y)
Z = -20*np.exp(-0.2*np.sqrt(np.sqrt(((X-10)**2+(Y-10)**2)/2)))+20+np.e-np.exp((np.cos(2*np.pi*X)+np.sin(2*np.pi*Y))/2)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='cool')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z')
ax.set_title('3D map')point0 = [0,9,Z[0][9]] 
point1 = [20,7,Z[20][7]]ax.plot([point0[0]],[point0[1]],[point0[2]],'r',marker = u'o',markersize = 15)
ax.plot([point1[0]],[point1[1]],[point1[2]],'r',marker = u'o',markersize = 15)x0,y0,_ = proj3d.proj_transform(point0[0],point0[1],point0[2], ax.get_proj())
x1,y1,_ = proj3d.proj_transform(point1[0],point1[1],point1[2], ax.get_proj())label = pylab.annotate("start", xy = (x0, y0), xytext = (-20, 20),textcoords = 'offset points', ha = 'right', va = 'bottom',bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 1),arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'),fontsize=15)
label2 = pylab.annotate("end", xy = (x1, y1), xytext = (-20, 20),textcoords = 'offset points', ha = 'right', va = 'bottom',bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 1),arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'),fontsize=15)
def update_position(e):x2, y2, _ = proj3d.proj_transform(point0[0],point0[1],point0[2],ax.get_proj())label.xy = x2,y2label.update_positions(fig.canvas.renderer)x1,y1,_ =  proj3d.proj_transform(point1[0],point1[1],point1[2],ax.get_proj())label2.xy = x1,y1label2.update_positions(fig.canvas.renderer)fig.canvas.draw()fig.canvas.mpl_connect('button_release_event', update_position)

基本原理

  • 蚂蚁 k 根据各个城市间链接路径上的信息素浓度决定其下一个访问城市,设Pkij(t)表示 t 时刻蚂蚁k从城市 i 转移到矩阵j的概率,其计算公式为

    Pkij=[τij(t)]α[ηij(t)]βsallowk[τis(t)]α[ηis(t)]β0sallowksallowk

    计算完城市间的转移概率后,采用与遗传算法中一样的轮盘赌方法选择下一个待访问的城市。

  • 当所有的蚂蚁完成一次循环后,各个城市间链接路径上的信息素浓度需进行更新,计算公式为

    {τij(t+1)=(1ρ)τij(t)+ΔτijΔτij=nk=1δτkij

    其中, Δτkij 表示第 k 只蚂蚁在城市i与城市 j 连接路径上释放的信息素浓度;Δτij表示所有蚂蚁在城市 i 与城市j连接路径上释放的信息素浓度之和。

  • 蚂蚁释放信息素的模型
    Δτkij={Q/Lk,0,ki访j

程序代码:

import numpy as np
import matplotlib.pyplot as plt
%pylab
coordinates = np.array([[565.0,575.0],[25.0,185.0],[345.0,750.0],[945.0,685.0],[845.0,655.0],[880.0,660.0],[25.0,230.0],[525.0,1000.0],[580.0,1175.0],[650.0,1130.0],[1605.0,620.0],[1220.0,580.0],[1465.0,200.0],[1530.0,  5.0],[845.0,680.0],[725.0,370.0],[145.0,665.0],[415.0,635.0],[510.0,875.0],[560.0,365.0],[300.0,465.0],[520.0,585.0],[480.0,415.0],[835.0,625.0],[975.0,580.0],[1215.0,245.0],[1320.0,315.0],[1250.0,400.0],[660.0,180.0],[410.0,250.0],[420.0,555.0],[575.0,665.0],[1150.0,1160.0],[700.0,580.0],[685.0,595.0],[685.0,610.0],[770.0,610.0],[795.0,645.0],[720.0,635.0],[760.0,650.0],[475.0,960.0],[95.0,260.0],[875.0,920.0],[700.0,500.0],[555.0,815.0],[830.0,485.0],[1170.0, 65.0],[830.0,610.0],[605.0,625.0],[595.0,360.0],[1340.0,725.0],[1740.0,245.0]])def getdistmat(coordinates):num = coordinates.shape[0]distmat = np.zeros((52,52))for i in range(num):for j in range(i,num):distmat[i][j] = distmat[j][i]=np.linalg.norm(coordinates[i]-coordinates[j])return distmatdistmat = getdistmat(coordinates)numant = 40 #蚂蚁个数
numcity = coordinates.shape[0] #城市个数
alpha = 1   #信息素重要程度因子
beta = 5    #启发函数重要程度因子
rho = 0.1   #信息素的挥发速度
Q = 1iter = 0
itermax = 250etatable = 1.0/(distmat+np.diag([1e10]*numcity)) #启发函数矩阵,表示蚂蚁从城市i转移到矩阵j的期望程度
pheromonetable  = np.ones((numcity,numcity)) # 信息素矩阵
pathtable = np.zeros((numant,numcity)).astype(int) #路径记录表distmat = getdistmat(coordinates) #城市的距离矩阵lengthaver = np.zeros(itermax) #各代路径的平均长度
lengthbest = np.zeros(itermax) #各代及其之前遇到的最佳路径长度
pathbest = np.zeros((itermax,numcity)) # 各代及其之前遇到的最佳路径长度while iter < itermax:# 随机产生各个蚂蚁的起点城市if numant <= numcity:#城市数比蚂蚁数多pathtable[:,0] = np.random.permutation(range(0,numcity))[:numant]else: #蚂蚁数比城市数多,需要补足pathtable[:numcity,0] = np.random.permutation(range(0,numcity))[:]pathtable[numcity:,0] = np.random.permutation(range(0,numcity))[:numant-numcity]length = np.zeros(numant) #计算各个蚂蚁的路径距离for i in range(numant):visiting = pathtable[i,0] # 当前所在的城市#visited = set() #已访问过的城市,防止重复#visited.add(visiting) #增加元素unvisited = set(range(numcity))#未访问的城市unvisited.remove(visiting) #删除元素for j in range(1,numcity):#循环numcity-1次,访问剩余的numcity-1个城市#每次用轮盘法选择下一个要访问的城市listunvisited = list(unvisited)probtrans = np.zeros(len(listunvisited))for k in range(len(listunvisited)):probtrans[k] = np.power(pheromonetable[visiting][listunvisited[k]],alpha)\*np.power(etatable[visiting][listunvisited[k]],alpha)cumsumprobtrans = (probtrans/sum(probtrans)).cumsum()cumsumprobtrans -= np.random.rand()k = listunvisited[find(cumsumprobtrans>0)[0]] #下一个要访问的城市pathtable[i,j] = kunvisited.remove(k)#visited.add(k)length[i] += distmat[visiting][k]visiting = klength[i] += distmat[visiting][pathtable[i,0]] #蚂蚁的路径距离包括最后一个城市和第一个城市的距离#print length# 包含所有蚂蚁的一个迭代结束后,统计本次迭代的若干统计参数lengthaver[iter] = length.mean()if iter == 0:lengthbest[iter] = length.min()pathbest[iter] = pathtable[length.argmin()].copy()      else:if length.min() > lengthbest[iter-1]:lengthbest[iter] = lengthbest[iter-1]pathbest[iter] = pathbest[iter-1].copy()else:lengthbest[iter] = length.min()pathbest[iter] = pathtable[length.argmin()].copy()    # 更新信息素changepheromonetable = np.zeros((numcity,numcity))for i in range(numant):for j in range(numcity-1):changepheromonetable[pathtable[i,j]][pathtable[i,j+1]] += Q/distmat[pathtable[i,j]][pathtable[i,j+1]]changepheromonetable[pathtable[i,j+1]][pathtable[i,0]] += Q/distmat[pathtable[i,j+1]][pathtable[i,0]]pheromonetable = (1-rho)*pheromonetable + changepheromonetableiter += 1 #迭代次数指示器+1#观察程序执行进度,该功能是非必须的if (iter-1)%20==0: print iter-1# 做出平均路径长度和最优路径长度        
fig,axes = plt.subplots(nrows=2,ncols=1,figsize=(12,10))
axes[0].plot(lengthaver,'k',marker = u'')
axes[0].set_title('Average Length')
axes[0].set_xlabel(u'iteration')axes[1].plot(lengthbest,'k',marker = u'')
axes[1].set_title('Best Length')
axes[1].set_xlabel(u'iteration')
fig.savefig('Average_Best.png',dpi=500,bbox_inches='tight')
plt.close()#作出找到的最优路径图
bestpath = pathbest[-1]plt.plot(coordinates[:,0],coordinates[:,1],'r.',marker=u'$\cdot$')
plt.xlim([-100,2000])
plt.ylim([-100,1500])for i in range(numcity-1):#m,n = bestpath[i],bestpath[i+1]print m,nplt.plot([coordinates[m][0],coordinates[n][0]],[coordinates[m][1],coordinates[n][1]],'k')
plt.plot([coordinates[bestpath[0]][0],coordinates[n][0]],[coordinates[bestpath[0]][1],coordinates[n][1]],'b')ax=plt.gca()
ax.set_title("Best Path")
ax.set_xlabel('X axis')
ax.set_ylabel('Y_axis')plt.savefig('Best Path.png',dpi=500,bbox_inches='tight')
plt.close()

这里写图片描述

这里写图片描述


http://chatgpt.dhexx.cn/article/G7ryIyxa.shtml

相关文章

蚁群算法原理与实现(python)

文章目录 蚁群算法的背景蚁群算法的思想蚁群算法的python实现实例总结 蚁群算法的背景 古有牛顿在苹果树下被苹果砸中发现万有引力&#xff0c;三十年前有人观察蚂蚁觅食发明蚁群算法。蚁群算法是意大利学者Dorigo、Maniezzo等人于20世纪90年代看蚂蚁觅食发明的。蹲在地上看蚂…

Python实现HBA混合蝙蝠智能算法优化循环神经网络分类模型(LSTM分类算法)项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后获取。 1.项目背景 蝙蝠算法是2010年杨教授基于群体智能提出的启发式搜索算法&#xff0c;是一种搜索全局最优解的有效方法…

蝙蝠算法(Bat Algorithm,BA)算法

生物学基理 蝙蝠算法&#xff08;Bat Algorithm&#xff0c;BA&#xff09;算法是模拟自然界中蝙蝠利用一种声呐来探测猎物、避免障碍物的随机搜索算法即模拟蝙蝠利用超声波对障碍物或猎物进行最基本的探测、定位能力并将其和优化目标功能相联系。BA算法的仿生原理将种群数量为…

经典蝙蝠算法MATLAB实现

为什么会有这么多基于群智能的算法&#xff0c;蚁群、粒子群、鱼群、烟花、炮竹、猪群、牛群、马群、羊群、猴群、鸡群。。。算法。&#xff1f;&#xff1f;&#xff1f;&#xff1f;&#xff1f;&#xff1f; 黑人问号.jpg 蝙蝠算法( BA) 是 Yang 教授于 2010 年基于群体智能…

【算法学习】蝙蝠算法简介

蝙蝠算法&#xff08;Bat Algorithm&#xff0c;缩写 BA&#xff09;&#xff0c;是一种元启发式优化算法&#xff0c;是杨新社&#xff08;音译自&#xff1a;Xin-She Yang&#xff09;在2010年提出的算法[1]。这个蝙蝠算法以微蝙蝠&#xff08;microbats&#xff09;回声定位…

算法学习:蝙蝠算法简介

蝙蝠算法&#xff08;Bat Algorithm&#xff0c;缩写 BA&#xff09;&#xff0c;是一种元启发式优化算法&#xff0c;是杨新社&#xff08;音译自&#xff1a;Xin-She Yang&#xff09;在2010年提出的算法[1]。这个蝙蝠算法以微蝙蝠&#xff08;microbats&#xff09;回声定位…

蝙蝠算法

蝙蝠算法 蝙蝠算法是根据蝙蝠的回声定位特性开发的优化算法。下面为蝙蝠算法的几个基本规则。 蝙蝠通过回声定位可以感觉到距离&#xff0c;它们在食物/猎物和背景屏障之间也有差异。蝙蝠的速度由(Vi)和蝙蝠的位置表示为(Xi)&#xff0c;具有频率Fmin、变化波长λ和寻找食物的响…

元启发式算法之一:蝙蝠算法BA

目录 一、算法 1. 定义 2. 步骤 3. 特点 二、蝙蝠 1.描述 2.先进技能-声纳 1&#xff09; 回声定位-Acoustics of Echolocation 2&#xff09; 行为参数化分析 3. 技能属性 三、 蝙蝠算法 1.算法模型建立之规则理想化简化 2.算法模型建立之近似值应用 3.算法模型建立…

算法优化笔记|蝙蝠算法的理解及实现

蝙蝠算法&#xff08;Bat Algorithm,BA &#xff09;理解及实现 一、蝙蝠算法背景介绍二、蝙蝠算法原理三、蝙蝠模型构建四、蝙蝠算法的Python实现五、总结 一、蝙蝠算法背景介绍 蝙蝠算法是2010年杨教授基于群体智能提出的启发式搜索算法&#xff0c;是一种搜索全局最优解的有…

降采样-升采样

降采样 深度卷积神经网络中降采样的方法&#xff1a; stride大于1的poolingstride大于1的convstride大于1的reorg&#xff08;reorganization改组整顿&#xff09;&#xff0c;由Joseph Redmond在YOLOv2中首次提出。 conv 用stride2的conv降采样的卷积神经网络效果与使用po…

Simulink用计数器降采样

需要将一些高频信号转成低频信号&#xff0c;但是不想降低数据发送的频率&#xff0c;用ZOH会降低数据频率&#xff0c;所以自己搭了一个计数器。 fcn中的程序就是求余数的程序&#xff1a; function y fcn(u)y rem(u,50);结果&#xff1a;

深度学习: pooling (池化 / 降采样)

在知乎上面看到一个关于池化的神解释&#xff0c;摘来&#xff1a; 池化涨水 卷积的目的是为了得到物体的边缘形状可以想象水要了解山立体的形状 水位低时得出山脚的形状 水位中等时得出山腰的形状 水位高时得出山顶的形状三点就可以大致描出山的简笔画池化的过程升高水位(扩大…

echarts-sampling降采样

今天修改一个echarts的渲染效率问题。 首先修改请求数据的问题&#xff0c;原先的逻辑是点击的时候请求数据&#xff0c;我看了下数据大概有七千多条&#xff0c;请求耗时是1.6s-1.7s&#xff0c;这是在我的电脑上。同事反馈在他的电脑上需要5s以上。 于是修改逻辑为&#xf…

可观测|时序数据降采样在Prometheus实践复盘

作者&#xff1a;智真 基于 Prometheus 的监控实践中&#xff0c;尤其是在规模较大时&#xff0c;时序数据的存储与查询是其中非常关键&#xff0c;而且问题点较多的一环。如何应对大数据量下的长周期查询&#xff0c;原生的 Prometheus 体系并未能给出一个令人满意的答案。对此…

opencv——上采样与降采样

目录 图像金字塔为什么要使用图像金字塔 上采样与降采样高斯金字塔高斯不同拉普拉斯金字塔相关API上采样降采样 上采样与降采样代码实现代码效果 先降采样再上采样代码实现高斯不同代码实现 图像金字塔 这个也可以看做金字塔&#xff0c;如果我们把一个小方格当成一个二值图像的…

降采样数字滤波器-MATLAB建模

用于SD-ADC后级的降采样数字滤波器 说明 第六届集创赛芯海杯赛题对数字滤波器的要求不是很高&#xff0c;记录一下从Matlab搭建模型到Verilog HDL代码实现&#xff0c;到一部分后仿的过程。 Matlab-Simulink搭建模型 降采样数字滤波器由三级组成&#xff0c;第一级CIC抽取滤…

Matlab | 滤波降采样操作

文章目录 仿真代码仿真结果仿真结论 仿真代码 close all;clear;clc; fs16000;fs_j2000;f0100;t0:1/fs:10; s_k sin(2*pi*f0*t); s_k_d lowpass(s_k,1000,fs); s_k_j resample(s_k_d,1,8); windowLen 2^ceil(log2(fs)); windowLen1 2^ceil(log2(fs_j)); s1 s_k(1,1:windo…

python图像降采样,【图像处理】——改变图像的大小(降采样重采样)

目录 我们常常需要将图片变成一样大小的图片&#xff0c;这样利于整理和批量操作&#xff0c;这时候就需要使用到调整图像的大小了 这里需要使用到的是重采样和降采样&#xff0c;重采样用在拉伸图像上&#xff0c;增加的像素点的像素值是通过插值法来得到的&#xff0c;最常用…

Verilog功能模块——降采样

一. 模块功能与应用场景 模块功能&#xff1a;对输入信号进行降采样。 应用场景&#xff1a; 输入数据量太大&#xff0c;后级难以处理&#xff0c;需要减小信号量而不丢失关键信息 二. 模块框图与使用说明 参数DOWN_SAMPLING_TIME控制降采样倍数&#xff0c;参数DIN_WIDTH控…

点云降采样

点云处理有时因为数据量太大&#xff0c;我们需要对其进行下采样。 这里的方法是先将点云填入固定大小的三维网格中&#xff0c;然后每个网格中选一个点生成新的点云。 新点云即为下采样后的点云。 这里使用斯坦福兔子作为测试点云。 小兔子pcd下载地址。 原始点云&#xff1a;…