當前位置: 首頁>>代碼示例>>Python>>正文


Python PriorityQueue.reprioritize方法代碼示例

本文整理匯總了Python中PriorityQueue.PriorityQueue.reprioritize方法的典型用法代碼示例。如果您正苦於以下問題:Python PriorityQueue.reprioritize方法的具體用法?Python PriorityQueue.reprioritize怎麽用?Python PriorityQueue.reprioritize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在PriorityQueue.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.reprioritize方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: dijkstra

# 需要導入模塊: from PriorityQueue import PriorityQueue [as 別名]
# 或者: from PriorityQueue.PriorityQueue import reprioritize [as 別名]
    def dijkstra(self, start, goal, exceptions=None):
        '''Dijkstra's algorithm, conceived by Dutch computer scientist Edsger 
        Dijkstra in 1956 and published in 1959, is a graph search algorithm 
        that solves the single-source shortest path problem for a graph with
        nonnegative edge path costs, producing a shortest path tree.
        
        .. note::
            Unmodified, Dijkstra's algorithm searches outward in a circle from
            the start node until it reaches the goal. It is therefore slower
            than other methods like A* or Bi-directional Dijkstra's. The
            algorithm is included here for performance comparision against
            other algorithms only.
        
        .. seealso::
            :func:`aStarPath`, :func:`dijkstraBi`
        '''
        dist = {}       # dictionary of final distances
        
        came_from = {} # dictionary of predecessors
        
        # nodes not yet found
        queue = PriorityQueue()

        # The set of nodes already evaluated
        closedset = []
        
        queue.push(0, start)
        
        while len(queue) > 0:
            #log.debug("queue: " + str(queue))
            weight, x = queue.pop()
            dist[x] = weight
            if x == goal:
                #log.debug("came_from: " + str(came_from))
                path = self.reconstructPath(came_from, goal)
                #log.info("Path: %s" % path)
                return path
                        
            closedset.append(x)
            
            for y in self.neighborNodes(x):
                if y in closedset:
                    continue                
                if(exceptions is not None and y in exceptions):
                    continue

                costxy = self.timeBetween(x,y)
                
                if not dist.has_key(y) or dist[x] + costxy < dist[y]:
                    dist[y] = dist[x] + costxy
                    queue.reprioritize(dist[y], y)
                    came_from[y] = x
                    #log.debug("Update node %s's weight to %g" % (y, dist[y]))

        return None
開發者ID:steven-nichols,項目名稱:ShortQut,代碼行數:57,代碼來源:Pathfinding.py

示例2: test_reprioritize

# 需要導入模塊: from PriorityQueue import PriorityQueue [as 別名]
# 或者: from PriorityQueue.PriorityQueue import reprioritize [as 別名]
 def test_reprioritize(self):
     
     pq = PriorityQueue()
     for letter in range(ord('A'), ord('Z')+1):
         letter = chr(letter)
         pq.push(0, letter)
         pq.reprioritize(1, letter)
     self.assertEqual(len(pq), 26, "Incorrect length")
     
     for letter in range(ord('A'), ord('Z')+1):
         letter = chr(letter)
         pri, val = pq.pop()
         self.assertEqual(letter, val)
         self.assertEqual(pri, 1)
     self.assertEqual(len(pq), 0, "Incorrect length")
開發者ID:steven-nichols,項目名稱:ShortQut,代碼行數:17,代碼來源:UnitTestPriorityQueue.py

示例3: aStarPath

# 需要導入模塊: from PriorityQueue import PriorityQueue [as 別名]
# 或者: from PriorityQueue.PriorityQueue import reprioritize [as 別名]
    def aStarPath(self, start, goal, exceptions=None):
        '''A* is an algorithm that is used in pathfinding and graph traversal. 
        Noted for its performance and accuracy, it enjoys widespread use. It
        is an extension of Edger Dijkstra's 1959 algorithm and achieves better 
        performance (with respect to time) by using heuristics.
    
        Takes in the ``start`` node and a ``goal`` node and returns the
        shortest path between them as a list of nodes. Use pathCost() to find
        the cost of traversing the path.
        
        .. note::
            Does not currently use the heuristic function, making it less
            efficient than the bi-directional Dijkstra's algorithm used in 
            :func:`dijkstraBi`.
            
        .. deprecated:: 0.5
            Use :func:`shortestPath` instead.
            
        .. seealso::
            :func:`dijkstra`, :func:`dijkstraBi`
        '''
        
        # The set of nodes already evaluated
        closedset = []
        # The set of tentative nodes to be evaluated.
        openset = [start]
        # The map of navigated nodes.
        came_from = {}
        # Distance from start along optimal path.
        g_score = {start: 0}
        h_score = {start: self.heuristicEstimateOfDistance(start, goal)}
        # The estimated total distance from start to goal through y.
        f_score = PriorityQueue()
        f_score.push(h_score[start], start) 
        
        while len(openset) != 0:
            # the node in openset having the lowest f_score[] value
            heur, x = f_score.pop()
            if x == goal:
                path = self.reconstructPath(came_from, goal)
                #log.info("Path found of weight: %g" % self.pathCost(path))
                #log.info("Path: %s" % path)
                return path
            
            try:
                openset.remove(x)
            except ValueError as e:
                log.critical("Remove %s from the openset: %s" % (str(x), e))
                raise
            
            closedset.append(x)
            for y in self.neighborNodes(x):
                if y in closedset:
                    continue
                
                if(exceptions is not None and (x,y) in exceptions):
                    costxy = float('infinity')
                else:
                    costxy = self.timeBetween(x,y)
                tentative_g_score = g_score[x] + costxy
                
                if y not in openset:
                    openset.append(y)
                    tentative_is_better = True
                elif tentative_g_score < g_score[y]:
                    tentative_is_better = True
                else:
                    tentative_is_better = False

                if tentative_is_better == True:
                    #log.debug("Update node %s's weight to %g" % (y,
															#tentative_g_score))
                    came_from[y] = x
                    g_score[y] = tentative_g_score
                    h_score[y] = self.heuristicEstimateOfDistance(y, goal)
                    f_score.reprioritize(g_score[y] + h_score[y], y)
        return None # Failure
開發者ID:steven-nichols,項目名稱:ShortQut,代碼行數:79,代碼來源:Pathfinding.py


注:本文中的PriorityQueue.PriorityQueue.reprioritize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。