这是我在参与AGV调度系统开发工作中形成的一些认识,是我的个人观点,想到什么写到什么。我自己也在学习,有不同观点可以一起讨论。由于涉及企业知识产权,文中代码为另外单独实现的DEMO,文章内容仅供参考。
A*算法是路径规划中使用得比较多的算法,其实现起来比较简单,实践效果也挺好且便于在规划中引入一些定制化规则。故在AGV调度的应用场景需求下,其相比D*之类的算法要更加适合。
在AGV调度场景下,D*之类算法重规划上的优势用处不大,因为AGV调度系统的重规划往往是由交管系统发起的,要么交管模块用其他算法直接搜索出策略,要么交管模块更新状态代价之后重规划。而这两者都使得上次规划得到的状态失去意义。
故本章来讨论一下A*和双向A*算法在AGV调度系统里的应用。在调度系统中,双向A*算法相比于A*算法其实有好有坏。好的是部分情况下搜索量变小,坏的是会引入一些A*算法没有的问题。而且双向A*代价其实并不一定比A*低。
A*算法不管是在栅格地图还是拓扑地图的场景都是可以使用的,实际调度系统项目中,用的比较多的还是拓扑地图。因为栅格地图通常只用在控制点不偏心的AGV小车上,而拓扑地图则通用所有场景并且也可以给地图附加更多的属性信息。但是拓扑地图比较抽象也不好呈现效果,为了方便演示,这里使用的是栅格地图。
A*算法:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import heapq as hq
# 搜索节点
class Node(object):
def __init__(self, nowx=0, nowy=0, cost=0, dist=0, lastx=0, lasty=0) -> None:
self.nowx = nowx
self.nowy = nowy
self.cost = cost
self.dist = dist
self.lastx = lastx
self.lasty = lasty
def __lt__(self, other):
return self.dist < other.dist
def __le__(self, other):
return self.dist <= other.dist
def __gt__(self, other):
return self.dist > other.dist
def __ge__(self, other):
return self.dist >= other.dist
def __eqeq__(self, other):
return self.dist == other.dist
def __ne__(self, other):
return self.dist != other.dist
# 求距离
def GetDist(start, end):
# return abs(end[0] - start[0]) + abs(end[1] - start[1])
return (abs(end[0] - start[0])**2 + abs(end[1] - start[1])**2)**0.5 * 2
def Contain(list, point):
for i in range(len(list)):
if list[i].nowx == point[0] and list[i].nowy == point[1]:
return True
map_x = 20
map_y = 20
obsindex = 3
# 产生地图矩阵
# Map = np.zeros(map_x * map_y)
# obs_num = int(Map.size * 0.1)
# obs_a = np.random.randint(low=0, high=Map.size, size=obs_num)
# Map[obs_a] = obsindex
# map = Map.reshape([map_x, map_y])
# start = [9, 4]
# end = [9, 14]
# map = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
cmap = colors.ListedColormap(
['none', 'black', 'white', 'magenta', 'yellow', 'cyan', 'green', 'red', 'blue'])
# 初始化open与close数组
open = []
close = []
hq.heapify(open)
hq.heappush(open, (GetDist(start, end), Node(nowx=start[0], nowy=start[1], dist=GetDist(start, end))))
# 搜索路径
while len(open) > 0:
item = hq.heappop(open)
nowx = item[1].nowx
nowy = item[1].nowy
# 避免超出地图界限
if nowx < 0 or nowx >= map_x or nowy < 0 or nowy >= map_y:
continue
# 避开障碍
if map[nowx][nowy] == obsindex:
continue
# 标记地图
map[nowx][nowy] = 1
# 搜索到终点
if nowx == end[0] and nowy == end[1]:
break
# 取出的节点已经在close列表里面则不在使用
if Contain(close, [nowx, nowy]):
continue
# 当前节点加入close列表
close.append(item[1])
# 生成当前节点的下一步策略
newcost = item[1].cost + 1
hq.heappush(open, (GetDist([nowx + 1, nowy], end) + newcost, Node(nowx=nowx + 1, nowy=nowy, dist=GetDist([nowx + 1, nowy], end), lastx=nowx, lasty = nowy, cost = newcost)))
hq.heappush(open, (GetDist([nowx, nowy + 1], end) + newcost, Node(nowx=nowx, nowy=nowy + 1, dist=GetDist([nowx, nowy + 1], end), lastx=nowx, lasty = nowy, cost = newcost)))
hq.heappush(open, (GetDist([nowx - 1, nowy], end) + newcost, Node(nowx=nowx - 1, nowy=nowy, dist=GetDist([nowx - 1, nowy], end), lastx=nowx, lasty = nowy, cost = newcost)))
hq.heappush(open, (GetDist([nowx, nowy - 1], end) + newcost, Node(nowx=nowx, nowy=nowy - 1, dist=GetDist([nowx, nowy - 1], end), lastx=nowx, lasty = nowy, cost = newcost)))
map[start[0]][start[1]] = 5
map[end[0]][end[1]] = 6
# 打印最终结果
print(map)
plt.imshow(map, cmap=cmap, interpolation='nearest', vmin=0, vmax=7)
plt.show()

第一种障碍物场景的效果 
第二种障碍物场景的效果
双向A*:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import heapq as hq
# 搜索节点
class Node(object):
def __init__(self, nowx=0, nowy=0, cost=0, dist=0, lastx=0, lasty=0) -> None:
self.nowx = nowx
self.nowy = nowy
self.cost = cost
self.dist = dist
self.lastx = lastx
self.lasty = lasty
def __lt__(self, other):
return self.dist < other.dist
def __le__(self, other):
return self.dist <= other.dist
def __gt__(self, other):
return self.dist > other.dist
def __ge__(self, other):
return self.dist >= other.dist
def __eqeq__(self, other):
return self.dist == other.dist
def __ne__(self, other):
return self.dist != other.dist
# 求距离
def GetDist(start, end):
# return abs(end[0] - start[0]) + abs(end[1] - start[1])
return (abs(end[0] - start[0])**2 + abs(end[1] - start[1])**2)**0.5 * 2
def Contain(list, point):
for i in range(len(list)):
if list[i].nowx == point[0] and list[i].nowy == point[1]:
return True
map_x = 20
map_y = 20
obsindex = 3
# 产生地图矩阵
# Map = np.zeros(map_x * map_y)
# obs_num = int(Map.size * 0.1)
# obs_a = np.random.randint(low=0, high=Map.size, size=obs_num)
# Map[obs_a] = obsindex
# map = Map.reshape([map_x, map_y])
# start = [9, 4]
# end = [9, 14]
# map = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0],
# [0, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 0],
# [0, 0,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
cmap = colors.ListedColormap(
['none', 'black', 'white', 'magenta', 'yellow', 'cyan', 'green', 'red', 'blue'])
# 初始化open与close数组
openA = []
closeA = []
hq.heapify(openA)
hq.heappush(openA, (GetDist(start, end), Node(nowx=start[0], nowy=start[1], dist=GetDist(start, end))))
openB = []
closeB = []
hq.heapify(openB)
hq.heappush(openB, (GetDist(end, start), Node(nowx=end[0], nowy=end[1], dist=GetDist(end, start))))
# 搜索路径
while len(openA) > 0 and len(openB) > 0:
item = hq.heappop(openA)
nowx = item[1].nowx
nowy = item[1].nowy
# 避免超出地图界限
if nowx < 0 or nowx >= map_x or nowy < 0 or nowy >= map_y:
continue
# 避开障碍
if map[nowx][nowy] == obsindex:
continue
# 搜索到终点
if Contain(closeB, [nowx, nowy]):
break
# 标记地图
map[nowx][nowy] = 1
# 取出的节点已经在close列表里面则不在使用
if Contain(closeA, [nowx, nowy]):
continue
# 当前节点加入close列表
closeA.append(item[1])
# 生成当前节点的下一步策略
newcost = item[1].cost + 1
hq.heappush(openA, (GetDist([nowx + 1, nowy], end) + newcost, Node(nowx=nowx + 1, nowy=nowy, dist=GetDist([nowx + 1, nowy], end), lastx=nowx, lasty = nowy, cost = newcost)))
hq.heappush(openA, (GetDist([nowx, nowy + 1], end) + newcost, Node(nowx=nowx, nowy=nowy + 1, dist=GetDist([nowx, nowy + 1], end), lastx=nowx, lasty = nowy, cost = newcost)))
hq.heappush(openA, (GetDist([nowx - 1, nowy], end) + newcost, Node(nowx=nowx - 1, nowy=nowy, dist=GetDist([nowx - 1, nowy], end), lastx=nowx, lasty = nowy, cost = newcost)))
hq.heappush(openA, (GetDist([nowx, nowy - 1], end) + newcost, Node(nowx=nowx, nowy=nowy - 1, dist=GetDist([nowx, nowy - 1], end), lastx=nowx, lasty = nowy, cost = newcost)))
item = hq.heappop(openB)
nowx = item[1].nowx
nowy = item[1].nowy
# 避免超出地图界限
if nowx < 0 or nowx >= map_x or nowy < 0 or nowy >= map_y:
continue
# 避开障碍
if map[nowx][nowy] == obsindex:
continue
# 搜索到终点
if Contain(closeA, [nowx, nowy]):
break
# 标记地图
map[nowx][nowy] = 4
# 取出的节点已经在close列表里面则不在使用
if Contain(closeB, [nowx, nowy]):
continue
# 当前节点加入close列表
closeB.append(item[1])
# 生成当前节点的下一步策略
newcost = item[1].cost + 1
hq.heappush(openB, (GetDist([nowx, nowy - 1], start) + newcost, Node(nowx=nowx, nowy=nowy - 1, dist=GetDist([nowx, nowy - 1], start), lastx=nowx, lasty = nowy, cost = newcost)))
hq.heappush(openB, (GetDist([nowx - 1, nowy], start) + newcost, Node(nowx=nowx - 1, nowy=nowy, dist=GetDist([nowx - 1, nowy], start), lastx=nowx, lasty = nowy, cost = newcost)))
hq.heappush(openB, (GetDist([nowx, nowy + 1], start) + newcost, Node(nowx=nowx, nowy=nowy + 1, dist=GetDist([nowx, nowy + 1], start), lastx=nowx, lasty = nowy, cost = newcost)))
hq.heappush(openB, (GetDist([nowx + 1, nowy], start) + newcost, Node(nowx=nowx + 1, nowy=nowy, dist=GetDist([nowx + 1, nowy], start), lastx=nowx, lasty = nowy, cost = newcost)))
map[start[0]][start[1]] = 5
map[end[0]][end[1]] = 6
# 打印最终结果
print(map)
plt.imshow(map, cmap=cmap, interpolation='nearest', vmin=0, vmax=7)
plt.show()

第一种障碍物场景的效果

第二种障碍物场景的效果
从上面对比效果来看由于启发值的存在,双向搜索对A*的改进并没有体现出优势来。可能双向算法对Dijkstar算法比A*算法要有效果。这还是写这篇文章的时候我才认识到的,之前在公司实现的时候没有去对比两者,想当然以为双向A*会比A*好,现在看来怕不是改了个寂寞。
而在问题上面,我们参考下面的图片:

在调度系统中,很明显搜索结果是1这条路线,但是实际1和2的行驶代价是一样的,但是转向代价不一样。我们更希望车辆走2的路线而不是1的路线,所以通常我们会加上转向代价并调整启发值的计算方式。如果是双向A*,由于交汇的是中间点,即使加上代价,我们求得的仍然不是2这条路径。
目录一.加解密算法数字签名对称加密DES(DataEncryptionStandard)3DES(TripleDES)AES(AdvancedEncryptionStandard)RSA加密法DSA(DigitalSignatureAlgorithm)ECC(EllipticCurvesCryptography)非对称加密签名与加密过程非对称加密的应用对称加密与非对称加密的结合二.数字证书图解一.加解密算法加密简单而言就是通过一种算法将明文信息转换成密文信息,信息的的接收方能够通过密钥对密文信息进行解密获得明文信息的过程。根据加解密的密钥是否相同,算法可以分为对称加密、非对称加密、对称加密和非
1.问题描述使用Python的turtle(海龟绘图)模块提供的函数绘制直线。2.问题分析一幅复杂的图形通常都可以由点、直线、三角形、矩形、平行四边形、圆、椭圆和圆弧等基本图形组成。其中的三角形、矩形、平行四边形又可以由直线组成,而直线又是由两个点确定的。我们使用Python的turtle模块所提供的函数来绘制直线。在使用之前我们先介绍一下turtle模块的相关知识点。turtle模块提供面向对象和面向过程两种形式的海龟绘图基本组件。面向对象的接口类如下:1)TurtleScreen类:定义图形窗口作为绘图海龟的运动场。它的构造器需要一个tkinter.Canvas或ScrolledCanva
我一直在尝试用Ruby实现Luhn算法。我一直在执行以下步骤:该公式根据其包含的校验位验证数字,该校验位通常附加到部分帐号以生成完整帐号。此帐号必须通过以下测试:从最右边的校验位开始向左移动,每第二个数字的值加倍。将乘积的数字(例如,10=1+0=1、14=1+4=5)与原始数字的未加倍数字相加。如果总模10等于0(如果总和以零结尾),则根据Luhn公式该数字有效;否则无效。http://en.wikipedia.org/wiki/Luhn_algorithm这是我想出的:defvalidCreditCard(cardNumber)sum=0nums=cardNumber.to_s.s
下面是我写的一个计算斐波那契数列中的值的方法:deffib(n)ifn==0return0endifn==1return1endifn>=2returnfib(n-1)+(fib(n-2))endend它工作到n=14,但在那之后我收到一条消息说程序响应时间太长(我正在使用repl.it)。有人知道为什么会这样吗? 最佳答案 Naivefibonacci进行了大量的重复计算-在fib(14)fib(4)中计算了很多次。您可以将内存添加到您的算法中以使其更快:deffib(n,memo={})ifn==0||n==1returnnen
为了防止在迁移到生产站点期间出现数据库事务错误,我们遵循了https://github.com/LendingHome/zero_downtime_migrations中列出的建议。(具体由https://robots.thoughtbot.com/how-to-create-postgres-indexes-concurrently-in概述),但在特别大的表上创建索引期间,即使是索引创建的“并发”方法也会锁定表并导致该表上的任何ActiveRecord创建或更新导致各自的事务失败有PG::InFailedSqlTransaction异常。下面是我们运行Rails4.2(使用Acti
我正在构建一个连接到服务器并等待数据的客户端Ruby库,但也允许用户通过调用方法发送数据。我使用的机制是有一个初始化套接字对的类,如下所示:definitialize@pipe_r,@pipe_w=Socket.pair(:UNIX,:STREAM,0)end我允许开发人员调用以将数据发送到服务器的方法如下所示:defsend(data)@pipe_w.write(data)@pipe_w.flushend然后我在一个单独的线程中有一个循环,我从连接到服务器的socket和@pipe_r中选择:defsocket_loopThread.newdosocket=TCPSocket.new
我需要一个用于大型动态任务集合的调度程序。目前我正在查看resque-scheduler,rufus-scheduler,和clockwork.如果您提供有关选择使用哪一个(或其他替代方案)的建议,我将不胜感激。一些细节:有大量要定期执行的任务(最多100K)。最短执行周期为1h。新任务可能会不时出现。现有任务可能会更改或删除。调度延迟最小化在这里不是关键任务(可扩展性和可持续性最重要)。任务执行不是繁重的操作,可以轻松并行。总结,我需要类似cron的Ruby项目,它可以处理大量动态变化的任务集合。更新:我花了一天时间尝试调度库,现在我想简单总结一下新获得的经验。我已经不再关注Cloc
我正在开发一个类似微论坛的项目,其中一个特殊用户发布一条快速(接近推文大小)的主题消息,订阅者可以用他们自己的类似大小的消息来响应。直截了当,没有任何形式的“挖掘”或投票,只是每个主题消息的响应按时间顺序排列。但预计会有很高的流量。我们想根据它们引起的响应嗡嗡声来标记主题消息,使用0到10的等级。在谷歌上搜索了一段时间的趋势算法和开源社区应用示例,到目前为止已经收集到两个有趣的引用资料,但我还没有完全理解它们:Understandingalgorithmsformeasuringtrends,关于使用基线趋势算法比较维基百科页面浏览量的讨论,在SO上。TheBritneySpearsP
我收到错误:unsupportedcipheralgorithm(AES-256-GCM)(RuntimeError)但我似乎具备所有要求:ruby版本:$ruby--versionruby2.1.2p95OpenSSL会列出gcm:$opensslenc-help2>&1|grepgcm-aes-128-ecb-aes-128-gcm-aes-128-ofb-aes-192-ecb-aes-192-gcm-aes-192-ofb-aes-256-ecb-aes-256-gcm-aes-256-ofbRuby解释器:$irb2.1.2:001>require'openssl';puts
文章目录一.Dijkstra算法想解决的问题二.Dijkstra算法理论三.java代码实现一.Dijkstra算法想解决的问题解决的问题:求解单源最短路径,即各个节点到达源点的最短路径或权值考察其他所有节点到源点的最短路径和长度局限性:无法解决权值为负数的情况二.Dijkstra算法理论参数:S记录当前已经处理过的源点到最短节点U记录还未处理的节点dist[]记录各个节点到起始节点的最短权值path[]记录各个节点的上一级节点(用来联系该节点到起始节点的路径)Dijkstra算法步骤:(1)初始化:顶点集S:节点A到自已的最短路径长度为0。只包含源点,即S={A}顶点集U:包含除A外的其他顶