Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中A*规划中的移动_Python_A Star - Fatal编程技术网

python中A*规划中的移动

python中A*规划中的移动,python,a-star,Python,A Star,在python中,我很难让代码中的明星从开始位置移动到结束位置。我不是在寻找他们确切的答案,只是想得到一些帮助,让我朝着正确的方向前进。我还想知道如何设置代码,从五个设置网格中选择一个,并允许您选择开始和结束路径。如果我让它随机生成一个数字,我是否能够对网格使用if语句。然后使用与if语句对应的网格 import numpy as np import heapq import numpy as np import matplotlib.pyplot as plt from matplotlib.

在python中,我很难让代码中的明星从开始位置移动到结束位置。我不是在寻找他们确切的答案,只是想得到一些帮助,让我朝着正确的方向前进。我还想知道如何设置代码,从五个设置网格中选择一个,并允许您选择开始和结束路径。如果我让它随机生成一个数字,我是否能够对网格使用if语句。然后使用与if语句对应的网格

import numpy as np
import heapq
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

# creating a square grid
grid = np.array([
    [0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0],
    [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 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, 1, 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]])

# start point and goal
start = (0, 0)
goal = (10, 15)

# plot map and path
fig, ax = plt.subplots(figsize=(12, 12))
ax.imshow(grid, cmap=plt.cm.Dark2)
ax.scatter(start[1], start[0], marker="*", color="yellow", s=200)
ax.scatter(goal[1], goal[0], marker="*", color="red", s=200)
plt.show()

# define Heuristic function

def heuristic(a, b):
    return np.sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)

# define A-star 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))

# creating route
route = astar(grid, start, goal)
# add the start position
route = route + [start]
# reverse the backward sequence
route = route[::-1]
# print route
print(route)

# 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)
#plot map and path

fig, ax = plt.subplots(figsize=(20,20))
ax.imshow(grid, cmap=plt.cm.Dark2)
ax.scatter(start[1],start[0], marker ="*", color ="yellow", s = 200)
ax.scatter(goal[1],goal[0], marker ="*", color ="red", s = 200)
ax.plot(y_coords,x_coords, color ="black")

plt.show()
将numpy导入为np
进口heapq
将numpy作为np导入
将matplotlib.pyplot作为plt导入
从matplotlib.pyplot导入图
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, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 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, 1, 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)
目标=(10,15)
#绘图和路径
图,ax=plt.子批次(图尺寸=(12,12))
ax.imshow(网格,cmap=plt.cm.Dark2)
最大散布(开始[1],开始[0],标记=“*”,颜色=“黄色”,s=200)
最大散布(目标[1],目标[0],标记=“*”,颜色=“红色”,s=200)
plt.show()
#定义启发式函数
def启发式(a,b):
返回np.sqrt((b[0]-a[0])**2+(b[1]-a[1])**2)
#定义A星函数
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