本文整理匯總了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))