I am working on an IA (in Python) for small board game, and I need to do a lot of pathfinding per round in order to find the best move I can do (minmax implementation).
My problem is that even if the board is small (9*9), I am able to run it (only) approx. 10000 per second, and I don't know how to speed it. Do you have some advise?
def shortest_path(self, start, goal):
elements = []
heapq.heappush(elements, (0, start))
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
current = None
while len(elements) > 0:
current = heapq.heappop(elements)[1]
if goal.is_on_goal(current):
break
for next in self.edges[current].neighbors:
new_cost = cost_so_far[current] + 1
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
lowest_priority = new_cost + goal.heuristic(next)
heapq.heappush(elements, (lowest_priority, next))
came_from[next] = current
way = []
if goal.is_on_goal(current):
reverse_edge = current
while reverse_edge in came_from:
way.insert(0, reverse_edge)
reverse_edge = came_from.get(reverse_edge, None)
return way