Python A*
- 作者: 达?矢抾哆拉?
- 来源: 51数据库
- 2022-08-12
#A star algorithm
def euclid_distance(startp, endp):
"""Euclid distance"""
return ((startp[0] - endp[0]) ** 2 + (startp[1] - endp[1]) ** 2) ** 0.500
def manhattan_distance(startp, endp):
"""Manhattan distance"""
return abs(startp[0] - endp[0]) + abs(startp[1] - endp[1])
def surround(point):
"""Return the coordinations of surrounding points"""
return [(point[0] + 1, point[1]), (point[0], point[1] + 1), (point[0] - 1, point[1]), (point[0], point[1] - 1)]
def filtering(points, barrier, closed_list):
"""Filter points not in barrier"""
return filter(lambda x: x not in barrier and x not in closed_list, points)
def astar(mapsize, barrier, startp, endp):
"""param: mapsize: length and height of the map
barrier: coordination of barrier
"""
if endp in barrier or startp in barrier:
print("No optimal path found, startp or endp is in barriers")
return None
g_mat = {}
h_mat = {}
f_mat = {}
path = {}
length, height = mapsize
open_list = []
closed_list = []
optimal_path = []
current = startp
#------------------------step 1------------------------------
closed_list.append(startp)
#------------------------step 2------------------------------
start_sur = list(filtering(surround(startp), barrier, closed_list))
if (start_sur == []):
print ("No optimal path found")
return None
g_mat = g_mat.fromkeys(start_sur, 1)
for i in start_sur:
h_mat[i] = manhattan_distance(i, endp)
f_mat[i] = g_mat.get(i) + h_mat.get(i)
path[i] = startp
open_list.extend(start_sur) #No use
#------------------------step 3------------------------------
while(True):
if (open_list == None):
print("No optimal path found")
break
if endp in closed_list:
print("Optimal path found")
path_point = endp
while(path_point != startp):
optimal_path.append(path_point)
path_point = path.get(path_point)
optimal_path.append(startp)
return list(reversed(optimal_path))
min_f = min(f_mat.values())
target = [k for k in f_mat.keys() if (f_mat[k] == min_f)]
last_target = target[-1]
closed_list.append(last_target)
del f_mat[last_target]
#------------------------step 4------------------------------
current = last_target
cur_sur = filtering(surround(current), barrier, closed_list)
for i in cur_sur:
path[i] = current
g_mat[i] = g_mat.get(last_target) + 1
h_mat[i] = manhattan_distance(i, endp)
f_mat[i] = g_mat.get(i) + h_mat.get(i)
op = astar((5,5), [(3, 2), (3, 4), (4, 3)], (3, 3), (3, 5))
print(op)
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
