当前位置: 首页>>代码示例>>Python>>正文


Python heapq.heappush方法代码示例

本文整理汇总了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 
开发者ID:mahdavipanah,项目名称:pynpuzzle,代码行数:22,代码来源:best_first_seach.py

示例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) 
开发者ID:mahdavipanah,项目名称:pynpuzzle,代码行数:26,代码来源:a_star_tree_manhattan_distance.py

示例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() 
开发者ID:sneddy,项目名称:pneumothorax-segmentation,代码行数:27,代码来源:Learning.py

示例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 
开发者ID:elsigh,项目名称:browserscope,代码行数:23,代码来源:dev_appserver.py

示例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 
开发者ID:amitrajitbose,项目名称:Competitive_Programming,代码行数:23,代码来源:merge_k_Sorted_lists.py

示例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()) 
开发者ID:amitrajitbose,项目名称:Competitive_Programming,代码行数:20,代码来源:Dijkstra.py

示例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 
开发者ID:amitrajitbose,项目名称:Competitive_Programming,代码行数:22,代码来源:Dijkstra_Optimized.py

示例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() 
开发者ID:openstack,项目名称:ec2-api,代码行数:20,代码来源:colorizer.py

示例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 
开发者ID:HoussemCharf,项目名称:FunUtils,代码行数:24,代码来源:skyline.py

示例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() 
开发者ID:SalikLP,项目名称:classification-of-encrypted-traffic,代码行数:24,代码来源:scheduler.py

示例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 
开发者ID:fluentpython,项目名称:concurrency2017,代码行数:24,代码来源:launchpad.py

示例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 
开发者ID:tensorflow,项目名称:lingvo,代码行数:19,代码来源:cluster.py

示例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 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:20,代码来源:gc.py

示例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] 
开发者ID:ailabstw,项目名称:justcopy-backend,代码行数:22,代码来源:generate.py

示例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). 
开发者ID:dandrino,项目名称:terrain-erosion-3-ways,代码行数:25,代码来源:river_network.py


注:本文中的heapq.heappush方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。