Guest User

Untitled

a guest
Feb 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. class Node(object):
  2.     def __init__(self, row, col):
  3.         self.row = row
  4.         self.col = col
  5.         self.passable = passable
  6.         self.parent = None
  7.         self.g = 0
  8.         self.h = 0
  9.         self.f = 0
  10.  
  11. class AStar(object):
  12.     def __init__(self):
  13.         self.open = []
  14.         heapq.heapify(self.open)
  15.         self.closed = set()
  16.         self.nodes = []
  17.  
  18.     # initialize node list
  19.     def init_grid(self, ant_loc, target):
  20.         for row in range(ants.rows):
  21.             for col in range(ants.cols):
  22.                 if ants.passable((row, col)):
  23.                     passable = True
  24.                 else:
  25.                     passable = False
  26.                 self.nodes.append(Node(
  27.                         row, col, passable))
  28.         self.start = ant_loc
  29.         self.end = target
  30.  
  31.     # return node from self.nodes based on row, col
  32.     def get_node(self, row, col):
  33.         return self.nodes[row * ants.rows + col]
  34.  
  35.     # retrieve adjacent nodes
  36.     def get_adj_nodes(self, node):
  37.         nodes = []
  38.         if col < ants.cols - 1:
  39.             nodes.append(get_node(
  40.                     node.row,
  41.                     node.col + 1))
  42.         if col == ants.cols - 1:
  43.             nodes.append(get_node(
  44.                     node.row,
  45.                     1))
  46.         if row < ants.rows - 1:
  47.             nodes.append(get_node(
  48.                     node.row + 1,
  49.                     node.col))
  50.         if row == ants.rows - 1:
  51.             nodes.append(get_node(
  52.                     1,
  53.                     node.col))
  54.         if col > 0:
  55.             nodes.append(get_node(
  56.                     node.row,
  57.                     node.col - 1))
  58.         if col == 0:
  59.             nodes.append(get_node(
  60.                     node.row,
  61.                     ants.cols - 1))
  62.         if row > 0:
  63.             nodes.append(get_node(
  64.                     node.row - 1,
  65.                     node.col))
  66.         if col == 0:
  67.             nodes.append(get_node(
  68.                     ants.rows - 1,
  69.                     node.col))
  70.         return nodes
  71.  
  72.     # create list with path
  73.     def create_path(self):
  74.         path = []
  75.         node = self.end
  76.         while node.parent is not self.start:
  77.             node = node.parent
  78.             path.insert(0, (node.row, node.col))
  79.  
  80.     # score g, h and set parent node
  81.     def score_node(self, adj, node):
  82.         adj.g = node.g + 1
  83.         # heuristic score: ants.distance()
  84.         adj.h = ants.distance(adj.g, self.end)
  85.         adj.parent = node
  86.         adj.f = adj.g + adj.h
  87.  
  88.     # A* algorithm
  89.     def find_path(self):
  90.         heapq.heappush(self.open,
  91.                        (self.start.f, self.start))
  92.         # as long as there's stuff in
  93.         # open list, process it
  94.         while len(self.open):
  95.             # get cell from heapq and
  96.             # put it in closed list
  97.             h, node = heapq.heappop(self.open)
  98.             self.closed.add(node)
  99.             # at target node, create path
  100.             if node is self.end:
  101.                 self.create_path()
  102.                 break
  103.             # otherwise, get_adj_nodes
  104.             adj_nodes = self.get_adj_nodes(node)
  105.             # check if adj_node is better
  106.             # than previously found
  107.             for adj in adj_nodes:
  108.                 if adj.passable and adj not in self.closed:
  109.                     if adj in self.open:
  110.                         if adj.g > node.g + 1:
  111.                             # score_node and be done with it
  112.                             self.score_node(adj, node)
  113.                         else:
  114.                             # score_node and add to open list
  115.                             self.score_node(adj, node)
  116.                             heapq.heappush(self.open,
  117.                                            (adj.f, adj))
Add Comment
Please, Sign In to add comment