本文整理汇总了Python中PriorityQueue.PriorityQueue.top方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.top方法的具体用法?Python PriorityQueue.top怎么用?Python PriorityQueue.top使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.top方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: astar
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import top [as 别名]
def astar(start, successors, goal, g, h):
path = []
states = PriorityQueue()
firststate = (start,None)
states.put((-g(firststate) - h(firststate), firststate))
i = 0
while True:
i += 1
if i % 2500 == 0:
print '%8d: # states: %5d, h of best state: %5d' % \
(i, len(states), min(h(state[1]) for state in states))
try:
if goal(states.top()[1]):
return g(states.top()[1]), getpath(states.top()[1])
except IndexError:
return None
best = states.top()[1]
states.pop()
for s in successors(best):
prev = [x for x in states if x[1][0] == s]
s = (s, best)
#if len(prev)>0:print prev[0][1]
#print s
if len(prev) > 0 and g(s) < g(prev[0][1]):
states.remove(prev[0])
if len(prev) > 0:
continue
states.put((-g(s) - h(s), s))
示例2: astar
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import top [as 别名]
def astar(start, successors, goal, g, h):
path = []
states = PriorityQueue()
firststate = start
states.put((-g(firststate) - h(firststate), firststate))
i = 0
nexti = 1
while True:
i += 1
# if i == 40000:
# return (0, None)
if i == nexti:
nexti += 1 # 3*len(states)
curbest = states.top()[1]
print "%8d: # states: %5d, h of best state: %5d" % (i, len(states), h(curbest))
print "\tcurrent top: score = %6d; %r" % (g(curbest) + h(curbest), curbest)
try:
if goal(states.top()[1]):
print i
return (states.top()[1], g(states.top()[1]))
except IndexError:
return (0, None)
best = states.top()[1]
states.pop()
for s in successors(best):
prev = [x for x in states if x[1] == s]
# if len(prev)>0:print prev[0][1]
# print s
if len(prev) > 0 and g(s) < g(prev[0][1]):
states.remove(prev[0])
if len(prev) > 0:
continue
states.put((-g(s) - h(s), s))