A*算法在栅格地图上的路径搜索(python实现)

  作者:邬杨明

1
import numpy 2 from pylab import * 3 4 # 定义一个含有障碍物的20×20的栅格地图 5 # 10表示可通行点 6 # 0表示障碍物 7 # 7表示起点 8 # 5表示终点 9 map_grid = numpy.full((20, 20), int(10), dtype=numpy.int8) 10 # print(map_grid) 11 map_grid[3, 3:8] = 0 12 map_grid[3:10, 7] = 0 13 map_grid[10, 3:8] = 0 14 map_grid[17, 13:17] = 0 15 map_grid[10:17, 13] = 0 16 map_grid[10, 13:17] = 0 17 map_grid[5, 2] = 7 18 map_grid[15, 15] = 5 19 # 画出定义的栅格地图 20 21 # plt.imshow(map_grid, cmap=plt.cm.hot, interpolation='nearest', vmin=0, vmax=10) 22 # plt.colorbar() 23 # xlim(-1, 20) # 设置x轴范围 24 # ylim(-1, 20) # 设置y轴范围 25 # my_x_ticks = numpy.arange(0, 20, 1) 26 # my_y_ticks = numpy.arange(0, 20, 1) 27 # plt.xticks(my_x_ticks) 28 # plt.yticks(my_y_ticks) 29 # plt.grid(True) 30 # plt.show() 31 32 33 class AStar(object): 34 """ 35 创建一个A*算法类 36 """ 37 38 def __init__(self): 39 """ 40 初始化 41 """ 42 self.f = 0
  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对不起,作为一名AI语言模型,我不支持图形界面交互和栅格地图的显示,但我可以提供A*算法Python实现代码。 ```python # 定义节类 class Node: def __init__(self, x, y, cost=0.0, parent=None): self.x = x self.y = y self.cost = cost self.parent = parent def __str__(self): return '(%d %d %.1f)' % (self.x, self.y, self.cost) def __repr__(self): return '(%d %d %.1f)' % (self.x, self.y, self.cost) # 定义A*算法 class AStar: def __init__(self, start, goal, obstacles): self.start = start self.goal = goal self.obstacles = obstacles self.path = list() # 路径 self.openset = [self.start] # 在边缘的节 self.closedset = list() # 已经被探索的节 # 估价函数 def heuristic(self, node): # 使用欧几里得距离计算两个节之间的距离 return ((node.x - self.goal.x) ** 2 + (node.y - self.goal.y) ** 2) ** 0.5 # 进行A*搜索 def search(self): while len(self.openset) > 0: curr = self.openset[0] curr_idx = 0 for idx, item in enumerate(self.openset): if item.cost + self.heuristic(item) < curr.cost + self.heuristic(curr): curr = item curr_idx = idx self.openset.pop(curr_idx) self.closedset.append(curr) # 当前节为目标节搜索结束 if curr.x == self.goal.x and curr.y == self.goal.y: node = curr while node is not None: self.path.append(node) node = node.parent return self.path[::-1] # 生成新的节并进行探索 for i, j in [(0, 1), (0, -1), (1, 0), (-1, 0)]: x = curr.x + i y = curr.y + j cost = curr.cost + 1 child = Node(x, y, cost, curr) # 边缘或已经探索的节不进行重复搜索 if child in self.closedset: continue # 避开障碍物 if child in self.obstacles: continue # 更新边缘节 if child not in self.openset: self.openset.append(child) if __name__ == '__main__': obstacles = [Node(1, 1), Node(2, 2), Node(3, 3)] result = AStar(Node(0, 0), Node(5, 5), obstacles).search() print(result) ``` 在此代码,定义了一个`Node`类用于描述地图的节,其`x`和`y`表示节的坐标,`cost`表示从起到该节路径长度,`parent`表示在路径该节的父节。 在`AStar`类,定义了一个`search`方法用于执行A*算法搜索过程。首先,将起加入到边缘节列表,然后不断从边缘节选择一个代价最小的节进行搜索,将其从边缘节移除并加入到已探索节列表,然后将其周围的节加入到边缘节列表。如果当前节已经是目标节,则搜索结束,追溯路径并返回。如果边缘节列表已经为空,说明没有找到任何路径搜索失败。 `heuristic`方法定义了用于估计当前节到目标节的距离的函数。在这个例子,使用欧几里得距离计算两个节之间的距离。 在`main`函数,定义了起、终和障碍物,并执行搜索算法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值