Numpy 一条不应该穿过墙壁的小路
我画了一张图表,以0和1为起点,以2为起点,以1为终点。我的1就是我的墙。 Astar寻路应该避开墙壁,但它会穿过墙壁,有人知道为什么吗Numpy 一条不应该穿过墙壁的小路,numpy,path,a-star,numpy-ndarray,Numpy,Path,A Star,Numpy Ndarray,我画了一张图表,以0和1为起点,以2为起点,以1为终点。我的1就是我的墙。 Astar寻路应该避开墙壁,但它会穿过墙壁,有人知道为什么吗 grid = pd.read_csv(r'C:\Users\605760\Desktop\path rec\pat.csv',header=None) # start point and goal start, = zip(*np.where(grid==2)) goal, = zip(*np.where(grid==-1)) heuristic fu
grid = pd.read_csv(r'C:\Users\605760\Desktop\path rec\pat.csv',header=None)
# start point and goal
start, = zip(*np.where(grid==2))
goal, = zip(*np.where(grid==-1))
heuristic function for path scoring
##############################################################################
def heuristic(a, b):
return np.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)
def heuristic(a, b):
return np.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)
##############################################################################
# path finding function
##############################################################################
def astar(array, start, goal):
neighbors = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]
close_set = set()
came_from = {}
gscore = {start:0}
fscore = {start:heuristic(start, goal)}
oheap = []
heapq.heappush(oheap, (fscore[start], start))
while oheap:
current = heapq.heappop(oheap)[1]
if current == goal:
data = []
while current in came_from:
data.append(current)
current = came_from[current]
return data
close_set.add(current)
for i, j in neighbors:
neighbor = current[0] + i, current[1] + j
tentative_g_score = gscore[current] + heuristic(current, neighbor)
if 0 <= neighbor[0] < array.shape[0]:
if 0 <= neighbor[1] < array.shape[1]:
if array[neighbor[0]][neighbor[1]] == 1:
continue
else:
# array bound y walls
continue
else:
# array bound x walls
continue
if neighbor in close_set and tentative_g_score >= gscore.get(neighbor, 0):
continue
if tentative_g_score < gscore.get(neighbor, 0) or neighbor not in [i[1]for i in oheap]:
came_from[neighbor] = current
gscore[neighbor] = tentative_g_score
fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal)
heapq.heappush(oheap, (fscore[neighbor], neighbor))
return False
route = astar(grid, start, goal)
route = route + [start]
route = route[::-1]
print(route)
##############################################################################
# plot the path
##############################################################################
#extract x and y coordinates from route list
x_coords = []
y_coords = []
for i in (range(0,len(route))):
x = route[i][0]
y = route[i][1]
x_coords.append(x)
y_coords.append(y)
grid=pd.read\u csv(r'C:\Users\605760\Desktop\path rec\pat.csv',header=None)
#起点和目标
开始,=zip(*np.where(grid==2))
目标=zip(*np.where(grid=-1))
路径评分的启发式函数
##############################################################################
def启发式(a,b):
返回np.sqrt((b[0]-a[0])**2+(b[1]-a[1])**2)
def启发式(a,b):
返回np.sqrt((b[0]-a[0])**2+(b[1]-a[1])**2)
##############################################################################
#寻径函数
##############################################################################
def astar(阵列、开始、目标):
邻域=[(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]
关闭集合=集合()
来自={}
gscore={start:0}
fscore={开始:启发式(开始,目标)}
oheap=[]
heapq.heappush(oheap,(fscore[start],start))
而oheap:
电流=heapq.heappop(oheap)[1]
如果当前==目标:
数据=[]
当电流从以下位置进入时:
data.append(当前)
当前=来自[当前]
返回数据
关闭集合。添加(当前)
对于邻居中的i,j:
邻居=电流[0]+i,电流[1]+j
暂定分数=gscore[当前]+启发式(当前,邻居)
如果0