class randomMAP: def __init__(self,size,start,goal,p): self.size=size self.start=start self.goal=goal self.p=p def creatMAP(self): self.map=np.zeros(self.size,dtype='int') for i in range(self.map.shape[0]): for j in range(self.map.shape[1]): if((i!=self.start[0] or i!=self.start[1]) and (j!=self.goal[0] or j!=self.goal[1])) and random.random() <self.p: self.map[i][j] = 5 map1=randomMAP((20,20),(0,0),(19,19),0.3) map1.creatMAP() plt.matshow(map1.map) plt.show()在这个随机地图的基础上创建Point和Astar类,用于A*算法解决迷宫问题
时间: 2024-01-02 08:04:46 浏览: 198
好的,下面是代码实现:
```python
import numpy as np
import random
import heapq
import matplotlib.pyplot as plt
class randomMAP:
def __init__(self, size, start, goal, p):
self.size = size
self.start = start
self.goal = goal
self.p = p
def creatMAP(self):
self.map = np.zeros(self.size, dtype='int')
for i in range(self.map.shape[0]):
for j in range(self.map.shape[1]):
if((i != self.start[0] or i != self.start[1]) and (j != self.goal[0] or j != self.goal[1])) and random.random() < self.p:
self.map[i][j] = 5
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
self.g = 0
self.h = 0
self.f = 0
self.parent = None
def __lt__(self, other):
return self.f < other.f
class Astar:
def __init__(self, map, start, goal):
self.map = map
self.start = start
self.goal = goal
def heuristic(self, p1, p2):
return abs(p1.x - p2.x) + abs(p1.y - p2.y)
def get_neighbors(self, point):
neighbors = []
if point.x > 0 and self.map[point.x - 1][point.y] != 5:
neighbors.append(Point(point.x - 1, point.y))
if point.x < self.map.shape[0] - 1 and self.map[point.x + 1][point.y] != 5:
neighbors.append(Point(point.x + 1, point.y))
if point.y > 0 and self.map[point.x][point.y - 1] != 5:
neighbors.append(Point(point.x, point.y - 1))
if point.y < self.map.shape[1] - 1 and self.map[point.x][point.y + 1] != 5:
neighbors.append(Point(point.x, point.y + 1))
return neighbors
def find_path(self):
open_list = []
closed_list = []
start_point = Point(self.start[0], self.start[1])
goal_point = Point(self.goal[0], self.goal[1])
heapq.heappush(open_list, start_point)
while len(open_list) > 0:
current_point = heapq.heappop(open_list)
if current_point.x == goal_point.x and current_point.y == goal_point.y:
path = []
while current_point.parent is not None:
path.append((current_point.x, current_point.y))
current_point = current_point.parent
path.append((start_point.x, start_point.y))
path.reverse()
return path
closed_list.append(current_point)
neighbors = self.get_neighbors(current_point)
for neighbor in neighbors:
if neighbor in closed_list:
continue
g = current_point.g + 1
if neighbor not in open_list:
neighbor.g = g
neighbor.h = self.heuristic(neighbor, goal_point)
neighbor.f = neighbor.g + neighbor.h
neighbor.parent = current_point
heapq.heappush(open_list, neighbor)
else:
if g < neighbor.g:
neighbor.g = g
neighbor.f = neighbor.g + neighbor.h
neighbor.parent = current_point
return None
map1 = randomMAP((20, 20), (0, 0), (19, 19), 0.3)
map1.creatMAP()
plt.matshow(map1.map)
plt.show()
astar = Astar(map1.map, (0, 0), (19, 19))
path = astar.find_path()
if path is not None:
print(path)
else:
print("No path found.")
```
这里我们定义了一个`Point`类来表示地图上的一个点,其中包含了`x`和`y`坐标,以及`g`、`h`、`f`和`parent`属性。`g`表示起点到该点的距离,`h`表示该点到终点的估算距离,`f`表示`g`和`h`的和,`parent`表示该点的父节点。
然后我们定义了`Astar`类来实现A*算法。这里的`heuristic`函数采用了曼哈顿距离作为估算函数,`get_neighbors`函数用来获取当前点的邻居节点。在`find_path`函数中,我们使用了一个优先队列来维护开放列表,每次从开放列表中弹出f值最小的节点进行拓展。如果找到了终点,就从终点一直回溯到起点,构造出一条路径。如果开放列表为空,说明没有找到路径。
最后,我们生成了一个随机地图并在matplotlib中显示出来,然后使用A*算法找到起点到终点的路径,如果找到了路径就输出路径上的所有点,否则输出"No path found."。
阅读全文