本文整理汇总了Python中PriorityQueue.PriorityQueue.insert方法的典型用法代码示例。如果您正苦于以下问题:Python PriorityQueue.insert方法的具体用法?Python PriorityQueue.insert怎么用?Python PriorityQueue.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.insert方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import insert [as 别名]
def main():
tasks = Dispenser(why_not)
time = 0
cmd, args = get_cmd( COMMANDS )
while cmd != QUIT_CMD:
if cmd == TICK_CMD:
time += 1
if not tasks.isEmpty():
current = tasks.peek()
current.time_left -= 1
if current.time_left == 0:
print( "\nTask '" + current.name + \
"' completed at time " + str( time ) + "." )
tasks.remove()
if not tasks.isEmpty():
print( "New task is '" + tasks.peek().name + "'." )
else:
print( "Nothing else to do." )
else:
print( "Nothing to do." )
elif cmd == ADD_CMD:
new_task = Task( args[ 0 ], int( args[ 1 ] ) )
tasks.insert( new_task )
print( "\nAdded. Current task is '" + tasks.peek().name + \
"'." )
else:
assert True, "PROGRAM ERROR"
cmd, args = get_cmd( COMMANDS )
print( "\nTerminating the simulation." )
示例2: PriorityQueueTest
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import insert [as 别名]
class PriorityQueueTest(unittest.TestCase):
def setUp(self):
self.p_queue = PriorityQueue()
def test(self):
elements = []
for i in range(1000):
x = random()
elements.append(x)
self.p_queue.insert(x)
elements.sort(reverse=True)
for elem in elements:
self.assertEqual(elem, self.p_queue.pop())
示例3: replan
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import insert [as 别名]
def replan(self, location, heading):
"""
:type location: Position
:param location: The current location of the robot
:type heading: Direction
:param heading: The current direction the robot is pointing
"""
open_nodes = PriorityQueue(lifo=False)
closed_nodes = set()
came_from = {} # The key is the end state, the value is a tuple of g, move, and start state
initial_state = (location, heading)
g = 0
h = self.heuristic.get_value(initial_state)
f = g + h
open_nodes.insert(f, initial_state)
came_from[initial_state] = (g, None, None)
current_state = initial_state
while len(open_nodes) > 0:
while current_state in closed_nodes:
current_state = open_nodes.pop_min()
closed_nodes.add(current_state)
if self.heuristic.get_value(current_state) == 0:
pol = self.create_policy(current_state, initial_state, came_from)
return self.policy
moves = self.get_possible_moves(current_state)
for move in moves:
new_state = self.get_move_result(current_state, move)
if new_state in closed_nodes:
continue
g = came_from[current_state][0] + self.get_cost(move)
h = self.heuristic.get_value(new_state)
f = g + h
if new_state not in open_nodes:
open_nodes.insert(f, new_state)
elif new_state in came_from and f >= open_nodes.get_priority(new_state):
continue
came_from[new_state] = (g, move, current_state)
open_nodes.update_priority(new_state,f)
return "No path to goal!"
示例4: test1
# 需要导入模块: from PriorityQueue import PriorityQueue [as 别名]
# 或者: from PriorityQueue.PriorityQueue import insert [as 别名]
def test1():
from PriorityQueue import PriorityQueue
import random
print("test1")
ITERATIONS = 10000
que = PriorityQueue()
heap = BinomialHeap()
length = 0
for i in range(ITERATIONS):
if i % 300 == 0:
print("Progress: {:.0%}".format(float(i) / ITERATIONS))
op = random.randint(0, 99)
if op < 1: # Clear
heap.check_structure()
for j in range(length):
if heap.dequeue() != que.extractMin():
raise AssertionError()
if not que.empty():
raise AssertionError()
length = 0
elif op < 2: # Peek
heap.check_structure()
if length > 0:
val = que.extractMin()
if heap.peek() != val:
raise AssertionError()
que.insert(val)
elif op < 60: # Add
n = random.randint(1, 100)
for j in range(n):
val = random.randint(0, 9999)
que.insert(val)
heap.enqueue(val)
length += n
elif op < 70: # Merge
n = random.randint(1, 100)
temp = BinomialHeap()
for j in range(n):
val = random.randint(0, 9999)
que.insert(val)
temp.enqueue(val)
heap.merge(temp)
if len(temp) != 0:
raise AssertionError()
length += n
elif op < 100: # Remove
n = min(random.randint(1, 100), length)
for j in range(n):
if heap.dequeue() != que.extractMin():
raise AssertionError()
length -= n
else:
raise AssertionError()
if len(heap) != length:
raise AssertionError()
print("Test passed")