本文整理匯總了Python中heapq.heappush方法的典型用法代碼示例。如果您正苦於以下問題:Python heapq.heappush方法的具體用法?Python heapq.heappush怎麽用?Python heapq.heappush使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類heapq
的用法示例。
在下文中一共展示了heapq.heappush方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: search
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def search(state, goal_state, fn):
"""Best-first search"""
queue = []
entrance = 0
node = Node(state)
while not node.is_goal(goal_state):
node.expand()
for child in node.children:
queue_item = (fn(child), entrance, child)
heapq.heappush(queue, queue_item)
entrance += 1
node = heapq.heappop(queue)[2]
output = []
output.append(node.state)
for parent in node.parents():
output.append(parent.state)
output.reverse()
return output
示例2: search
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def search(state, goal_state):
"""A* tree search using manhattan distance heuristic"""
def gn(node):
return node.gn()
tiles_places = []
for i in range(len(goal_state)):
for j in range(len(goal_state)):
heapq.heappush(tiles_places, (goal_state[i][j], (i, j)))
def hn(node):
cost = 0
for i in range(len(node.state)):
for j in range(len(node.state)):
tile_i, tile_j = tiles_places[node.state[i][j]][1]
if i != tile_i or j != tile_j:
cost += abs(tile_i - i) + abs(tile_j - j)
return cost
def fn(node):
return gn(node) + hn(node)
return bfs.search(state, goal_state, fn)
示例3: post_processing
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def post_processing(self, score, epoch, model):
if self.freeze_model:
return
checkpoints_history_path = Path(
self.checkpoints_history_folder,
'{}_epoch{}.pth'.format(self.calculation_name, epoch)
)
torch.save(self.get_state_dict(model), checkpoints_history_path)
heapq.heappush(self.score_heap, (score, checkpoints_history_path))
if len(self.score_heap) > self.checkpoints_topk:
_, removing_checkpoint_path = heapq.heappop(self.score_heap)
removing_checkpoint_path.unlink()
self.logger.info('Removed checkpoint is {}'.format(removing_checkpoint_path))
if score > self.best_score:
self.best_score = score
self.best_epoch = epoch
torch.save(self.get_state_dict(model), self.best_checkpoint_path)
self.logger.info('best model: {} epoch - {:.5}'.format(epoch, score))
if self.scheduler.__class__.__name__ == 'ReduceLROnPlateau':
self.scheduler.step(score)
else:
self.scheduler.step()
示例4: UpdateEvent
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def UpdateEvent(self, service, event_id, eta):
"""Update a runnable event in the heap with a new eta.
TODO: come up with something better than a linear scan to
update items. For the case this is used for now -- updating events to
"time out" channels -- this works fine because those events are always
soon (within seconds) and thus found quickly towards the front of the heap.
One could easily imagine a scenario where this is always called for events
that tend to be at the back of the heap, of course...
Args:
service: the service that owns this event.
event_id: the id of the event.
eta: the new eta of the event.
"""
for id in xrange(len(self._events)):
item = self._events[id]
if item[2] == service and item[3] == event_id:
item = (eta, item[1], item[2], item[3])
del(self._events[id])
heapq.heappush(self._events, item)
break
示例5: merge
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def merge(lists):
merged_list = []
heap = [(lst[0], i, 0) for i, lst in enumerate(lists) if lst]
heapq.heapify(heap)
while heap:
#print(heap)
val, list_ind, element_ind = heapq.heappop(heap)
merged_list.append(val)
if element_ind + 1 < len(lists[list_ind]):
next_tuple = (lists[list_ind][element_ind + 1],
list_ind,
element_ind + 1)
heapq.heappush(heap, next_tuple)
return merged_list
#Test Cases
示例6: calculate_distances
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def calculate_distances(graph, starting_vertex):
distances = {vertex: float('inf') for vertex in graph}
distances[starting_vertex] = 0
pq = [(0, starting_vertex)]
visit = [False for vertex in graph]
visit[starting_vertex] = True
while len(pq) > 0:
# print (pq)
current_distance, current_vertex = heapq.heappop(pq)
visit[current_vertex] = True
for neighbor, weight in graph[current_vertex].items():
distance = current_distance + weight
# Only consider this new path if it's better than any path we've already found.
if not visit[neighbor] and distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))
return sorted(distances.items())
示例7: calculate_distances
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def calculate_distances(graph, starting_vertex):
distances = {vertex: float('inf') for vertex in graph}
distances[starting_vertex] = 0
pq = [(0, starting_vertex)]
visited = 0
visit = [False] * len(graph)
while len(pq) > 0:
if visited == len(graph):
break
# print (pq)
current_distance, current_vertex = heapq.heappop(pq)
if not visit[current_vertex]:
visit[current_vertex] = True
visited += 1
for neighbor, weight in graph[current_vertex].items():
distance = current_distance + weight
if not visit[neighbor] and distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))
return distances
示例8: _addResult
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def _addResult(self, test, *args):
try:
name = test.id()
except AttributeError:
name = 'Unknown.unknown'
test_class, test_name = name.rsplit('.', 1)
elapsed = (self._now() - self.start_time).total_seconds()
item = (elapsed, test_class, test_name)
if len(self.slow_tests) >= self.num_slow_tests:
heapq.heappushpop(self.slow_tests, item)
else:
heapq.heappush(self.slow_tests, item)
self.results.setdefault(test_class, [])
self.results[test_class].append((test_name, elapsed) + args)
self.last_time[test_class] = self._now()
self.writeTests()
示例9: get_skyline
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def get_skyline(lrh):
"""
Wortst Time Complexity: O(NlogN)
:type buildings: List[List[int]]
:rtype: List[List[int]]
"""
skyline, live = [], []
i, n = 0, len(lrh)
while i < n or live:
if not live or i < n and lrh[i][0] <= -live[0][1]:
x = lrh[i][0]
while i < n and lrh[i][0] == x:
heapq.heappush(live, (-lrh[i][2], -lrh[i][1]))
i += 1
else:
x = -live[0][1]
while live and -live[0][1] <= x:
heapq.heappop(live)
height = len(live) and -live[0][0]
if not skyline or height != skyline[-1][1]:
skyline += [x, height],
return skyline
示例10: run
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def run(self):
self.__running = True
while self.__running:
self.__signal.acquire()
if not self.__jobs:
self.__signal.wait()
else:
now = datetime.datetime.now()
while (self.__jobs[0] < now):
job = heapq.heappop(self.__jobs)
action = job()
if action is not False:
self.__runner(action)
heapq.heappush(self.__jobs, job)
logging.getLogger("scheduler").debug("Sleeping %s seconds", (self.__jobs[0] - now))
self.__signal.wait((self.__jobs[0] - now).total_seconds())
self.__signal.release()
示例11: run_until_complete
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def run_until_complete(self):
# Start all the coroutines.
for coro in self._new:
wait_for = coro.send(None)
heapq.heappush(self._waiting, Task(wait_for, coro))
# Keep running until there is no more work to do.
while self._waiting:
now = datetime.datetime.now()
# Get the coroutine with the soonest resumption time.
task = heapq.heappop(self._waiting)
if now < task.waiting_until:
# We're ahead of schedule; wait until it's time to resume.
delta = task.waiting_until - now
time.sleep(delta.total_seconds())
now = datetime.datetime.now()
try:
# It's time to resume the coroutine.
wait_until = task.coro.send(now)
heapq.heappush(self._waiting, Task(wait_until, task.coro))
except StopIteration:
# The coroutine is done.
pass
示例12: _AssignVar
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def _AssignVar(self, var_op):
size = var_op.get_attr('dtype').size
shape = tf.TensorShape(var_op.get_attr('shape'))
assert self._var_space_pq, ('No ps devices to use.')
allocated, device = heapq.heappop(self._var_space_pq)
if shape.num_elements() is None:
assert var_op.name.endswith(
'wb/var'), 'Unexpected name pattern: %s' % var_op.name
# CuDNN RNN vars shape aren't known statically, decide to make a constant
# estimate to avoid introducing more complexities.
allocated += 10 * 1024**2 * size
else:
allocated += shape.num_elements() * size
heapq.heappush(self._var_space_pq, (allocated, device))
tf.logging.info('Place variable %s on %s %d', var_op.name, device,
allocated)
return device
示例13: largest_export_versions
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def largest_export_versions(n):
"""Creates a filter that keeps the largest n export versions.
Args:
n: number of versions to keep.
Returns:
A filter function that keeps the n largest paths.
"""
def keep(paths):
heap = []
for idx, path in enumerate(paths):
if path.export_version is not None:
heapq.heappush(heap, (path.export_version, idx))
keepers = [paths[i] for _, i in heapq.nlargest(n, heap)]
return sorted(keepers)
return keep
示例14: search_docs
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def search_docs(inputs, max_ex=5, opts=None):
"""Given a set of document ids (returned by ranking for a question), search
for top N best matching (by heuristic) paragraphs that contain the answer.
"""
if not opts:
raise RuntimeError('Options dict must be supplied.')
doc_ids, q_tokens, answer = inputs
examples = []
for i, doc_id in enumerate(doc_ids):
for j, paragraph in enumerate(re.split(r'\n+', fetch_text(doc_id))):
found = find_answer(paragraph, q_tokens, answer, opts)
if found:
# Reverse ranking, giving priority to early docs + paragraphs
score = (found[0], -i, -j, random.random())
if len(examples) < max_ex:
heapq.heappush(examples, (score, found[1]))
else:
heapq.heappushpop(examples, (score, found[1]))
return [e[1] for e in examples]
示例15: compute_height
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappush [as 別名]
def compute_height(points, neighbors, deltas, get_delta_fn=None):
if get_delta_fn is None:
get_delta_fn = lambda src, dst: deltas[dst]
dim = len(points)
result = [None] * dim
seed_idx = min_index([sum(p) for p in points])
q = [(0.0, seed_idx)]
while len(q) > 0:
(height, idx) = heapq.heappop(q)
if result[idx] is not None: continue
result[idx] = height
for n in neighbors[idx]:
if result[n] is not None: continue
heapq.heappush(q, (get_delta_fn(idx, n) + height, n))
return util.normalize(np.array(result))
# Same as above, but computes height taking into account river downcutting.
# `max_delta` determines the maximum difference in neighboring points (to
# give the effect of talus slippage). `river_downcutting_constant` affects how
# deeply rivers cut into terrain (higher means more downcutting).