本文整理汇总了Python中PriorityQueue.PriorityQueue.getNodes方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.getNodes方法的具体用法?Python PriorityQueue.getNodes怎么用?Python PriorityQueue.getNodes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.getNodes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PathFinder
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import getNodes [as 别名]
#.........这里部分代码省略.........
except:
print >> sys.stderr, 'Vertex not found in points'
return
# find the neighbors of this point
row = self.visibility_graph[index]
for i in range(0, len(row)):
neighbor = self.points[i]
if row[i] == 1 and neighbor not in self.visited:
if self.is_goal(neighbor):
# If this child is the goal node, we're done!
last_node = BFSNode(neighbor, current_node)
self.frontier.append(last_node)
self.search_snapshots.append(Snapshot(list(self.visited), list(self.frontier), uses_bfs_nodes=True))
self.reconstruct_path_from_last_node(last_node)
return
else:
# Add this child to the end of the queue
self.frontier.append(BFSNode(neighbor, current_node))
# If we get to this point, then the frontier became empty before we found the goal
print "BFS failed to find the goal"
def reconstruct_path_from_last_node(self, last_node):
self.path = []
node = last_node
while node:
self.path.insert(0, node.my_point)
node = node.parent
############################
### A-STAR ALGORITHM ###
############################
def get_a_star_path(self):
print "Running A* to Get Path"
self.a_star_search()
return self.path
def a_star_search(self):
self.clear_history()
self.frontier = PriorityQueue()
self.frontier.put(AStarNode(self.points[0], None, 0), 0)
last_node = None
# iterate until the frontier is empty
while not self.frontier.empty():
snapshot = Snapshot(list(self.visited), self.frontier.getNodes(), uses_a_star_nodes=True)
self.search_snapshots.append(snapshot)
current = self.frontier.get()
if current.my_point in self.visited:
# Never mind, we didn't want that snapshot :)
self.search_snapshots.remove(snapshot)
continue
self.visited.append(current.my_point)
# end immediately if the goal is found
if self.is_goal(current.my_point):
last_node = current
break
# find out which point we're dealing with
try:
index = self.points.index(current.my_point)
except:
print >> sys.stderr, 'Vertex not found in points'
return False
# prepare qualified new neighbors to be added to frontier
row = self.visibility_graph[index]
index = 0
for item in row:
neighbor = self.points[index]
if item == 1 and neighbor not in self.visited:
distance = current.cost
distance += self.distance(neighbor, current.my_point) # distance so far
est_distance_remaining = distance + self.distance(neighbor, self.points[1]) # est. distance to go
self.frontier.put(AStarNode(neighbor, current, distance), est_distance_remaining)
index += 1
# unwind path back to start
while not last_node == None:
self.path.insert(0, last_node.my_point)
last_node = last_node.parent
################################
### SEARCH ALGORITHM HELPERS ###
################################
def clear_history(self):
self.visited = []
self.frontier = []
self.path = []
self.search_snapshots = []
def is_goal(self, point):
return point == self.points[1] # the goal point is always the second one in our list of points
def distance(self, point1, point2):
return math.sqrt((point1[0]-point2[0])**2 + (point1[1] - point2[1]) ** 2)