我的代码(我不知道我是否需要显示整个代码或者不显示此问题,对不起,如果我不这样做):
class GridWithWeights(SquareGrid):
def __init__(self, width, height):
self.weights = {}
def cost(self, from_node, to_node):
x = from_node[0]
y = from_node[1]
results = {(x-1, y+1):14, (x, y+1):10, (x+1, y+1):14,
(x-1, y):10, (x+1,y):10,
(x-1, y-1):14, (x, y-1):10, (x+1, y-1):14
}
try:
return results[to_node]
except Exception:
return 0
def in_bounds(self, id):
(x, y) = id
return 0 <= x < self.width and 0 <= y < self.height
def passable(self, id):
return id not in self.walls
def neighbors(self, id):
(x, y) = id
results = [(x-1, y+1), (x, y+1), (x+1, y+1),
(x-1, y), (x+1,y),
(x-1, y-1), (x, y-1), (x+1, y-1)
]
if (x + y) % 2 == 0: results.reverse() # aesthetics
results = filter(self.in_bounds, results)
results = filter(self.passable, results)
return results
def reconstruct_path(came_from, start, goal):
current = goal
path = []
while current != start:
path.append(current)
current = came_from[current]
path.append(start) # optional
path.reverse() # optional
return path
def heuristic(a, b):
(x1, y1) = a
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
def a_star_search(graph, start, goal):
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
current = frontier.get()
if current == goal:
break
# The thing I want to multiprocess
for next in graph.neighbors(current)
new_cost = cost_so_far[current] + graph.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + heuristic(goal, next)
frontier.put(next, priority)
came_from[next] = current
return came_from, cost_so_far
diagram4 = GridWithWeights(360, 240)
diagram4.walls = [(2,5),(1, 7), (1, 8), (2, 7), (2, 8), (3, 7), (3, 8)] # Obstructions
start, goal = (141, 42), (54, 190)
came_from, _ = a_star_search(diagram4, start, goal)
print(reconstruct_path(came_from, start=start, goal=goal))
但是,我试图多线程突出显示的for循环,所以我使用ThreadPoolExecutor并按原样运行它:
from concurrent.futures import ThreadPoolExecutor
def func(next):
new_cost = cost_so_far[current] + graph.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + heuristic(goal, next)
frontier.put(next, priority)
came_from[next] = current
pool = ThreadPoolExecutor()
while not frontier.empty():
current = frontier.get()
if current == goal:
break
pool.map(func, graph.neighbors(current))
return came_from, cost_so_far
问题在于因变量来自&#39;来自&#39;仅包含最后一个流程的数据......
预期结果:
Big dictionary of values like the result I get except start to
finish(run it yourself). The only reason I didn't put it here is
because it keeps freezing my computer
我得到的结果:
{(141, 42): None, (140, 43): (141, 42), (141, 43): (141, 42), (142, 43): (141, 42), (140, 42): (141, 42), (142, 42): (141, 42), (140, 41): (141, 42), (141, 41): (141, 42), (142, 41): (141, 42), (139, 41): (140, 42), (139, 42): (140, 42), (139, 43): (140, 42)}
现在我无法访问其他需要的项目。我正在使用多线程来加速生成字典的过程。对于我现在需要做什么以及我做错了什么,我感到很困惑。我生成所有这些值的原因是网格上的坐标和网格上的路径。