Exemplo n.º 1
0
def init_central_trading_system():
	global glob

	stockInfoList = StockInfo.objects.all()
	securityStockInfoList = SecurityStockInfo.objects.all()	# this is accounts!!!
	
	for iStock in stockInfoList:
		glob.InstQueue[iStock.StockID] = (PriorityQueue(),PriorityQueue())
	
	for iAccount in securityStockInfoList:	# iSecStock
		glob.InstNotDealt[iAccount.SecurityID] = {}

	return True
Exemplo n.º 2
0
def prims(graphObj, start):
    pq = PriorityQueue()

    for vertex in graphObj.verticesList:
        if vertex == start:
            g.verticesList[vertex].distance = 0
            pq.insert([0, vertex])
            continue
        g.verticesList[vertex].distance = INFINITY
        pq.insert([INFINITY, vertex])

    while(len(pq.pqueue)):
        currentVertex = pq.extractMin()

        if len(pq.pqueue) == 1:
            return

        for adjNode in graphObj.verticesList[currentVertex[1]].getConnections():
            if adjNode in pq.lookup(adjNode):
                newDistance = graphObj.verticesList[currentVertex[1]].getCost(adjNode)
                if newDistance < graphObj.verticesList[adjNode].distance:
                    graphObj.verticesList[adjNode].distance = newDistance
                    graphObj.verticesList[adjNode].predecessor = currentVertex[1]

        return graphObj
Exemplo n.º 3
0
    def singleSourceShortestPath(self, srcID):
        assert (srcID >= 0 and srcID < self.n)
        # Implement Dijkstra's algorithm
        # Input:
        # self --> a reference to a MyGraph instance
        # srcID: the id of the source vertex.
        # Expected Output: (d,pi)
        #    d  --> Map each vertex id v to the distance from srcID
        #    pi --> Map each reachable vertex id v (except for srcID) to a parent.

        # Initialize the priority queue
        pq = PriorityQueue(self.n)  #create the priority queue

        for i in range(0, self.n):  # Iterate through all vertex ID
            if (i == srcID):  # If ID is srcID
                pq.set(i, 0.0)  # Distance of srcID should be zero
            else:  # ID is not srcID
                pq.set(i, pq.Inf)  # Distance should be infinity

        d = {}  # Initialize the map with distances to nodes
        pi = {}  # Initialize the map with parents of vertices

        # COMPLETE the Dijkstra code here

        return (d, pi)
Exemplo n.º 4
0
def eventFC(scorer, url_scorer, options):
    #     seedUrls = ["http://www.cnn.com/2013/09/27/world/africa/kenya-mall-attack/index.html",
    #                 "http://www.youtube.com/watch?v=oU9Oop892BQ",
    #                 "http://ifrc.org/en/news-and-media/press-releases/africa/kenya/kenya-red-cross-society-continues-to-provide-vital-support-to-victims-and-families-of-the-westgate-shopping-mall-attack/"
    #                 ]
    #keywords = ['demonstrations','protest','elections','egypt','revolution','uprising','arab','spring','tunisia','libya','military']

    t = [(-1, p, -1) for p in options['seeds']]
    #t = [(-1,Url(p)) for p in seedUrls]
    priorityQueue = PriorityQueue(t)

    crawler = Crawler(priorityQueue, scorer, options)
    crawler.set_url_scorer(url_scorer)
    crawler.enhanced_crawl()
    print crawler.relevantPagesCount
    print crawler.pagesCount

    f = open("harverstRatioData.txt", "w")
    for r, p in crawler.harvestRatioData:
        f.write(str(r) + "," + str(p) + "\n")
    f.close()

    f = open("logData.txt", "w")
    furl = open("Output-URLs.txt", "w")
    for p in crawler.relevantPages:
        f.write(str(p.pageId) + "," + str(p.pageUrl[2]) + "\n")
        furl.write(p.pageUrl[1] + "\n")
    f.close()

    furl.close()
Exemplo n.º 5
0
def AStar(maze, x, y, fpoint):
    startPoint = Point(x, y, None)
    startPoint.f = startPoint.g = startPoint.h = 0

    openList = PriorityQueue()

    openList.insert(startPoint)

    while (not (openList.isEmpty())):

        current_node = openList.delete()
        maze[current_node.x][current_node.y] = 3

        if (current_node.isEqual(fpoint)):
            return current_node

        children = []
        for pos in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
            curr_x = current_node.x + pos[0]
            curr_y = current_node.y + pos[1]

            if (not (isFeasible(maze, curr_x, curr_y))):
                continue

            child = Point(curr_x, curr_y, current_node)
            children.append(child)

        for child in children:
            child.g = current_node.g + 1
            child.h = manhattanDist(child, fpoint)
            child.f = child.g + child.h

            openList.insert(child)
Exemplo n.º 6
0
    def djisktra(self, a, b):
        distances = [float("inf") for _ in self.vertices]
        distances[a] = 0
        predecessors = [None for _ in self.vertices]
        # queue = PriorityQueue()
        queue = PriorityQueue()
        queue.enqueue((distances[a], self.vertices[a].label))

        while True:
            if queue.empty():
                break
            # print(queue)
            _, current = queue.dequeue()
            if current is b:
                break
            # dequeue a node we already looked at
            # this may not be necessary because it will just not decrease any travel times anyway
            else:
                # traverse adj list, and see if any are shorter than current dist
                for v in self.vertices[current].adj:
                    temp = distances[current] + weight(self.vertices[current],
                                                       self.vertices[v])
                    if distances[v] > temp:
                        distances[v] = temp
                        predecessors[v] = current
                        queue.enqueue((temp, v))  # shouldn't this be (distances[v], v)

        count = 0
        for distance in distances:
            if (distance != float("inf")):
                count+=1

        #print((distances[b], predecessors[b], count))

        return (distances, predecessors, count)
Exemplo n.º 7
0
    def performUCS(self, position, goal):
        to_explore = PriorityQueue()
        explored = set()
        prevNode = dict({position: 0})

        to_explore.push(position, 0)

        while (not(to_explore.is_empty())):
            aux = to_explore.pop()
            node = aux[0]
            prevCost = aux[1]
            to_explore.remove(node)
            if (goal == node):
                return prevNode
            explored.add(node)

            actions = ['N','S','E','W']

            for action in actions:
                child, actionCost = self.step(node, action)
                if not(child == node):
                    cost = prevCost + actionCost
                    if not(child in explored or child in to_explore):
                        to_explore.push(child, cost)
                        prevNode[child] = (node, action)
                    elif child in to_explore and cost < to_explore.getCost(child):
                        to_explore.update(child, cost)
                        prevNode[child] = (node, action)
        return None
Exemplo n.º 8
0
def color_most_constrained_first(graphs, color):
    global spills, unspillable, num_unused, used_registers

    #print 'starting color_most_constrained_first'

    for v in graphs.vertices():
        used_registers[v] = set([])
        num_unused[v] = len(registers) - len(used_registers[v])

    left = set(graphs.vertices()) - set(reserved_registers) - set(registers)
    queue = PriorityQueue(left, avail_reg_then_unspill)

    while not queue.empty():
        v = queue.pop()
        if debug:
            print 'next to color is ' + v
        if v not in color.keys():
            c = choose_color(v, color, graphs, False)
            color[v] = c
            for u in graphs.interferes_with(v):
                #if u in graphs.vertices():
                queue.update(u)
                used_registers[u] |= set([c])
                num_unused[u] = len(registers) - len(used_registers[u])

            if not is_reg(c):
                spills += 1

    return color
Exemplo n.º 9
0
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        dict_num = dict()
        for num in nums:
            if num not in dict_num:
                dict_num[num] = 1
            else:
                dict_num[num] += 1

        pq = PriorityQueue()
        for key in dict_num.keys():
            if pq.getSize() < k:
                pq.enqueue(self.__Freq(key, dict_num[key]))
                continue

            if pq.getFront().freq < dict_num[key]:
                pq.dequeue()
                pq.enqueue(self.__Freq(key, dict_num[key]))
        re = []
        while pq.isEmpty() is not True:
            re.insert(0, pq.dequeue().value)
        return re
Exemplo n.º 10
0
def dijkstra(graphObj, startVertex):
    pq = PriorityQueue()

    for vertex in graphObj.getVertices():
        if vertex == startVertex:
            pq.insert([0, vertex])
            graphObj.verticesList[startVertex].distance = 0
        else:
            pq.insert([INFINITY, vertex])

    while(len(pq.pqueue)):
        currentVertex = pq.extractMin()

        if len(pq.pqueue) == 1:
            break

        # print(pq.pqueue, pq.lookup)
        for adjNode in graphObj.verticesList[currentVertex[1]].getConnections():
            newDistance = graphObj.verticesList[currentVertex[1]].distance + graphObj.verticesList[currentVertex[1]].adjList[adjNode]
            if newDistance < graphObj.verticesList[adjNode].distance:
                graphObj.verticesList[adjNode].distance = newDistance
                graphObj.verticesList[adjNode].predecessor = currentVertex[1]
                index = pq.lookup[adjNode]
                pq.decreaseKey(index, newDistance)

    return graphObj
    def hillClimb(self):
        PQ = PriorityQueue()

        ricList = self.findRIC()

        for nn in range(len(ricList)):
            # Add left
            leftMap = self.moveLeft(ricList[nn])
            if (leftMap != None):
                PQ.push(leftMap, UPGetScore.getMapScore(leftMap))

            # Add right
            rightMap = self.moveRight(ricList[nn])
            if (rightMap != None):
                PQ.push(rightMap, UPGetScore.getMapScore(rightMap))

            # Add up
            upMap = self.moveUp(ricList[nn])
            if (upMap != None):
                PQ.push(upMap, UPGetScore.getMapScore(upMap))

            # Add down
            downMap = self.moveDown(ricList[nn])
            if (downMap != None):
                PQ.push(downMap, UPGetScore.getMapScore(downMap))

        return PQ
Exemplo n.º 12
0
    def performAStar(self, position, goal):
        to_explore = PriorityQueue()
        explored = set()
        prevNode = dict({position: 0})
        costs = dict({position: 0})

        to_explore.push(position, 0 + self.model.h(position, goal))
        stepCounter = 0

        while (not (to_explore.is_empty())):
            node = to_explore.pop()[0]
            to_explore.remove(node)
            if (goal == node):
                return prevNode
            explored.add(node)

            actions = ['N', 'S', 'E', 'W']

            for action in actions:
                child, actionCost = self.step(node, action)
                if not (child == node):
                    prevCost = costs[node] + actionCost
                    cost = prevCost + self.model.h(child, goal)

                    if not (child in explored or child in to_explore):
                        to_explore.push(child, cost)
                        costs[child] = prevCost
                        prevNode[child] = (node, action)
                    elif child in to_explore and cost < to_explore.getCost(
                            child):
                        to_explore.update(child, cost)
                        costs[child] = prevCost
                        prevNode[child] = (node, action)
        return None
Exemplo n.º 13
0
def baseFC(crawlParams):
    seedURLs = crawlParams['seedURLs']
    t = [(-1, p, -1, "") for p in seedURLs]
    priorityQueue = PriorityQueue(t)
    classifierFileName = crawlParams['classifierFileName']
    crawlParams["priorityQueue"] = priorityQueue
    try:
        classifierFile = open(classifierFileName, "rb")
        vsmClf = pickle.load(classifierFile)
        #self.classifier.error = 0.05
        #self.classifier.relevanceth = 0.75
        classifierFile.close()
    except:
        #vsmClf = VSMClassifier()
        vsmClf = VSM_CentroidClassifier()
        vsmClf.buildModel(crawlParams['model'], topK=10)
        #vsmClf.buildVSMClassifier(crawlParams['seedURLs'],classifierFileName,crawlParams['eventType'], crawlParams['minCollFreq'])
        print 'Saving model to file'
        classifierFile = open(classifierFileName, "wb")
        pickle.dump(vsmClf, classifierFile)
        classifierFile.close()
    crawlParams['scorer'] = vsmClf
    #keywordModel = KeywordModel()
    #keywordModel.buildModel(crawlParams['model'])#,crawlParams['No_Keywords'])
    #crawlParams['scorer']=keywordModel

    #crawler = Crawler(priorityQueue,scorer,options)
    crawler = Crawler(crawlParams)
    qu = crawler.crawl()
    quS = '\n'.join([str(-1 * s[0]) + "," + s[1] for s in qu])
    with open('queueBase.txt', 'w') as fw:
        fw.write(quS.encode('utf8'))
    return crawler.relevantPages
Exemplo n.º 14
0
 def __init__(self,newsSeedList,socialSeedList,govSeedList):        
     self.turn = 0
     '''
     if newsSeedList is not None:
         self.newsQueue = PriorityQueue(newsSeedList)
     if socialSeedList is not None:
         self.socialQueue = PriorityQueue(socialSeedList)
     if govSeedList is not None:
         self.govQueue = PriorityQueue(govSeedList)
     '''
     self.newsQueue = PriorityQueue(newsSeedList)
     self.socialQueue = PriorityQueue(socialSeedList)
     self.govQueue = PriorityQueue(govSeedList)
     
     heapq.heapify(self.newsQueue.queue)
     heapq.heapify(self.socialQueue.queue)
     heapq.heapify(self.govQueue.queue)
Exemplo n.º 15
0
def bipartite(w, discrepancy):
    Ebp_edges = []
    Q = PriorityQueue()
    incident_edges = dict()
    for e in w:
        Q.add_task(e, -w[e])
        incident_edges.setdefault(e[0], []).append((e[1], w[e]))
        incident_edges.setdefault(e[1], []).append((e[0], w[e]))
    processed_edges = []
    while len(processed_edges) < len(w):
        (e, weight) = Q.pop_item()
        processed_edges.append(e)
        try:
            incident_edges[e[0]].remove((e[1], -weight))
            incident_edges[e[1]].remove((e[0], -weight))
        except ValueError:
            pass
        Ebp_edges.append(e)

        # discard all edges in Q incident to b (i.e. e[1])
        for (a, weight) in incident_edges[e[1]]:
            try:
                Q.remove_task((a, e[1]))
                processed_edges.append((a, e[1]))
            except KeyError:
                pass
            try:
                incident_edges[a].remove((e[1], weight))
                incident_edges[e[1]].remove((a, weight))
            except ValueError:
                pass
        discrepancy[e[0]] += 1
        discrepancy[e[1]] += 1

        if -1 < discrepancy[e[0]] < .5:
            for (x, _) in incident_edges[e[0]]:
                try:
                    Q.remove_task((e[0], x))
                except KeyError:
                    pass
                new_weight = abs(
                    discrepancy[e[0]]) + 2 * abs(discrepancy[x]) - abs(
                        discrepancy[e[0]]) - 1
                if new_weight > 0:
                    Q.add_task((e[0], x), -new_weight)
                else:
                    processed_edges.append((e[0], x))
        elif discrepancy[e[0]] > .5:
            for (x, _) in incident_edges[e[0]]:
                try:
                    Q.remove_task((e[0], x))
                    processed_edges.append((e[0], x))
                except KeyError:
                    pass
    return Ebp_edges
Exemplo n.º 16
0
    def test_enqueue(self):
        priority_queue = PriorityQueue()
        priority_queue.enqueue(10, 6)
        priority_queue.enqueue(20, 5)
        priority_queue.enqueue(30, 4)
        priority_queue.enqueue(40, 3)
        priority_queue.enqueue(50, 2)
        priority_queue.enqueue(60, 1)
        priority_queue.enqueue(70, 0)

        self.assertEqual([{0: 70}, {3: 40}, {1: 60}, {6: 10}, {4: 30}, {5: 20}, {2: 50}], priority_queue.returnQueue())
Exemplo n.º 17
0
    def run(layout, heuristic):
        layoutText = load_maze(layout)
        currentMaze = Maze(layoutText)
        aMaze = Maze(layoutText)
        aProblem = Problem(aMaze)
        numberOfMoves = 0

        fringe = PriorityQueue()
        visitedNodes = set()

        goal = a_star_search(aProblem, heuristic, fringe, visitedNodes)
        path = list()

        while goal is not None:
            path.insert(0, goal)
            numberOfMoves += 1
            goal = goal.get_parent()

        move = 0
        print("For Heuristics: ", heuristic)
        if len(path) > 0:
            print("|------------- STARTING MAZE--------------|\n")
            currentMaze.update_maze(path[0].get_x_coordinate(),
                                    path[0].get_y_coordinate(), "S")
            currentMaze.printMaze()
            print(
                "\n|------------- STARTING DICE ORIENTATION--------------|\n")
            path[0].dice.display()

        for currentNode in path:
            print("\n|-------------------- MOVE: " + str(move) +
                  " -------------------|\n")
            print("|------------- MAZE--------------|\n")
            currentMaze.update_maze(currentNode.get_x_coordinate(),
                                    currentNode.get_y_coordinate(), '#')
            currentMaze.printMaze()
            print("\n|------------- DICE--------------|\n")
            currentNode.dice.display()
            move += 1

        print("\n|---------------- PERFORMANCE METRICS -----------------|\n")
        print("No. of moves in the solution                    : ",
              numberOfMoves - 1)
        print("No. of nodes put on the queue                   : ",
              fringe.nodesPutOnQueue)
        print("No. of nodes visited / removed from the queue   : ",
              len(visitedNodes))
        print("\n|------------------------------------------------------|\n")

        result = [
            heuristic, numberOfMoves - 1, fringe.nodesPutOnQueue,
            len(visitedNodes)
        ]
        return result
Exemplo n.º 18
0
 def __init__(self, maze):
     #super already sets the start as the current cell
     super(Astar, self).__init__(maze, maze.start())
     self.strategy = "Euclidean" #make it possible to choose an option which heuristic can be used
     #current position not necessary, stored in walker
     #open list containing all cells to explore
     self.open = PriorityQueue()
     self.closed = dict()# node:parent
     self.g = 10 #total costs the agent took so far
     self.last = None
     self.closed[self._cell] = None
def test_priority_and_sort_order():

    p = PriorityQueue()
    p.add(1, 1)
    p.add(2, 1)
    p.add(3, 2)
    p.add(4, 1)
    assert p.pop() == 3
    assert p.pop() == 1
    assert p.pop() == 2
    assert p.pop() == 4
    assert p.peek() is None
Exemplo n.º 20
0
    def run(layout, heuristic):
        """
        It initialize the configuration parameters and run the a star
        algorithm on the maze data and gets the output.
        :param layout: Two dimensional array of maze configuration
        :param heuristic: Type of heuristic
        :return: return a list which contains heuristic name, number of moves
                 it took, number of node generated and visited
        """
        layoutText = loadMaze(layout)
        currentMaze = Maze(layoutText)
        aMaze = Maze(layoutText)
        aProblem = Problem(aMaze)
        numberOfMoves = 0

        fringe = PriorityQueue()
        visitedNodes = set()

        goal = aStarSearch(aProblem, heuristic, fringe, visitedNodes)
        path = list()

        while goal is not None:
            path.insert(0, goal)
            numberOfMoves += 1
            goal = goal.getParent()

        move = 0
        print("For Heuristics: ", heuristic)
        if len(path) > 0:
            print("|------------- STARTING MAZE--------------|\n")
            currentMaze.updateMaze(path[0].getxCoordinate(), path[0].getyCoordinate(), "S")
            currentMaze.printMaze()
            print("\n|------------- STARTING DICE ORIENTATION--------------|\n")
            path[0].dice.display()

        for currentNode in path:
            print("\n|-------------------- MOVE: " + str(move) + "--------------------|\n")
            print("|------------- MAZE--------------|\n")
            currentMaze.updateMaze(currentNode.getxCoordinate(), currentNode.getyCoordinate(), '#')
            currentMaze.printMaze()
            print("\n|------------- DICE--------------|\n")
            currentNode.dice.display()
            move += 1

        print("\n|---------------- PERFORMANCE METRICS -----------------|\n")
        print("No. of moves in the solution                    : ", numberOfMoves - 1)
        print("No. of nodes put on the queue                   : ", fringe.nodesPutOnQueue)
        print("No. of nodes visited / removed from the queue   : ", len(visitedNodes))
        print("\n|------------------------------------------------------|\n")

        result = [heuristic, numberOfMoves - 1, fringe.nodesPutOnQueue, len(visitedNodes)]
        return result
Exemplo n.º 21
0
    def singleSourceShortestPath(self, srcID):
        assert (srcID >= 0 and srcID < self.n)

        # Implement Dijkstra's algorithm
        # Input:
        # self --> a reference to a MyGraph instance
        # srcID: the id of the source vertex.
        # Expected Output: (d,pi)
        #    d  --> Map each vertex id v to the distance from srcID
        #    pi --> Map each reachable vertex id v (except for srcID) to a parent.

        # Initialize the priority queue
        pq = PriorityQueue(self.n)  #create the priority queue
        cur = self
        for i in range(0, self.n):  # Iterate through all vertex ID
            if (i == srcID):  # If ID is srcID
                pq.set(i, 0.0)  # Distance of srcID should be zero
            else:  # ID is not srcID
                pq.set(i, pq.Inf)  # Distance should be infinity

        d = {}  # Initialize the map with distances to nodes
        pi = {}  # Initialize the map with parents of vertices

        minKey, minDist = pq.extractMin()

        d[minKey] = minDist  #set orignal source distance to 0
        pi[minKey] = minKey  #set sources to orginal source

        while (pq.isEmpty() == False):  # COMPLETE the Dijkstra code here

            lst = self.adjList[
                minKey]  #grap array of lists of format (vertices,weight) for node minKey
            for n in lst:  #loop through said list

                ##grap the next node attached to this node somehow
                dist = n[1] + minDist  #grab distance from list

                node = n[0]  #grab node from list
                if (pq.hasKey(node)
                    ):  #check if that nodes min dist has been found
                    if (
                            pq.get(node) > dist
                    ):  #check if path to the node from minKey is less then current path
                        pq.set(node,
                               dist)  #set the nodes distance in the queue
                        d[node] = dist  #set the nodes distance in the return array
                        pi[node] = minKey  #sets the nodes parents

            minKey, minDist = pq.extractMin(
            )  #extract next node dont need to extract last node as all the paths will be filled would probably work better in a do while loop

        return (d, pi)
Exemplo n.º 22
0
 def __init__(self):
     '''
     Method for initializing an empty Network.
     '''
     self._nodes = set()
     self._topLevelNodes = set()
     self._bottomLevelNodes = set()
     self._allLinks = set()
     self._topLevelLinks = set()
     self._cutLinks = set()
     self._sortedLinks = PriorityQueue()
     self._idDict = {}
     self._idCounter = 0
Exemplo n.º 23
0
def Dijkstras(graph,start):   
	pq=PriorityQueue()
	start.setDistance(0)
	pq.buildHeap([(v.getDistance(),v) for v in graph])   # distance is the key in the priority queue
	while not pq.isEmpty():
		currentvertex=pq.delMin()
		for newvertex in currentvertex.getConnections():
			newDist=currentvertex.getDistance()+currentvertex.getWeight(newvertex)
			if newDist<newvertex.getDistance(): 
				# at the start the distance of all the vertices is set to maximum. That's why the if statement will be executed
				newvertex.setDistance(newDist)
				newvertex.setPredecessor(currentvertex)
				pq.decreaseKey(newvertex,newDist)
Exemplo n.º 24
0
    def test_dequeue(self):
        priority_queue = PriorityQueue()
        priority_queue.enqueue(10, 6)
        priority_queue.enqueue(20, 5)
        priority_queue.enqueue(30, 4)
        priority_queue.enqueue(40, 3)
        priority_queue.enqueue(50, 2)
        priority_queue.enqueue(60, 1)
        priority_queue.enqueue(70, 0)

        returned_node = priority_queue.dequeue()
        self.assertEqual(70, returned_node.get_value())
        self.assertEqual([{1: 60}, {3: 40}, {2: 50}, {6: 10}, {4: 30}, {5: 20}], priority_queue.returnQueue())
Exemplo n.º 25
0
def dijkstra(aGraph, start):
    pq = PriorityQueue()
    start.setDistance(0)
    pq.buildHeap([(v.getDistance(), v) for v in aGraph])
    while not pq.isEmpty():
        currentVert = pq.delMin()
        for nextVert in currentVert.getConnections():
            newDist = currentVert.getDistance() \
                      + currentVert.getWeight(nextVert)
            if newDist < nextVert.getDistance():
                nextVert.setDistance(newDist)
                nextVert.setPred(currentVert)
                pq.decreaseKey(nextVert, newDist)
Exemplo n.º 26
0
    def buildTree(self, text):
        queue = PriorityQueue(cmp=lambda a,b: a.frequency > b.frequency)
        for v,f in dict(Counter(list(text))).items():
            queue.enqueue(BinaryNode(v, f))

        while queue.size() > 1:
            nodeL = queue.dequeue()
            nodeR = queue.dequeue()
            parent = BinaryNode(None, nodeL.frequency+nodeR.frequency)
            parent.left = nodeL
            parent.right = nodeR
            queue.enqueue(parent)

        self.root = queue.dequeue()
    def test_pop(self):
        main_list = []
        pqueue = PriorityQueue()

        for i in range(0, 1000):
            main_list.insert(i, i)

        random.shuffle(main_list)
        for i in range(0, 1000):
            pqueue.push(main_list[i], main_list[i])

        for i in range(999, -1, -1):
            self.assertEqual(pqueue.pop(), 999 - i)
            self.assertEqual(pqueue.size(), i)
Exemplo n.º 28
0
def aStarSearch(problem, evaluator, verbose=None, limit=None):
    startTime = time.process_time()
    fringe = PriorityQueue()
    max_fringe_size = 0
    visited = {}

    initialWorldState = problem.initial()
    initialValue = evaluator.value(initialWorldState, [])
    initialSearchState = SearchState(initialWorldState, [])
    fringe.update(initialSearchState, initialValue)

    numVisited = numSkipped = 0
    while (True):
        if len(fringe.heap) > max_fringe_size:
            max_fringe_size = len(fringe.heap)
        if fringe.isEmpty():
            return (None, (time.process_time() - startTime, numVisited,
                           numSkipped, max_fringe_size))
        nextNode = fringe.pop()  # A search state (state, actions)
        numVisited += 1
        if (limit and numVisited > limit):
            return (None, (time.process_time() - startTime, numVisited,
                           numSkipped, max_fringe_size))
        if (verbose and numVisited % verbose == 0):
            print("Visited " + str(numVisited) + " world is " +
                  str(nextNode._worldState))
            print("Skipped " + str(numSkipped) + " Fringe is size " +
                  str(len(fringe.heap)))
            print("Evaluation is " + str(evaluator.value(nextNode._worldState, nextNode._actions)) + \
                  " with actions " + str(len(nextNode._actions)))

        if (problem.isGoal(nextNode.worldState())):
            return (nextNode._actions,
                    (time.process_time() - startTime, numVisited, numSkipped,
                     max_fringe_size))

        if (nextNode._worldState in visited):
            numSkipped += 1
        else:
            visited[nextNode.worldState()] = True
            successors = nextNode.worldState().successors()
            for successor in successors:
                state, action = successor
                actions = list(nextNode.actions())
                actions.append(action)
                newSS = SearchState(state, actions)
                newValue = evaluator.value(state, actions)
                fringe.update(newSS, newValue)
    raise "Impossible search execution path."
Exemplo n.º 29
0
def prim(graph,start):     # it belongs to the family of greedy algorithms
	pq=PriorityQueue()
	for v in graph:
		v.setDistance(sys.maxsize)
		v.setPredecessor(None)
	start.setDistance(0)
	pq.buildHeap([(v.getDistance(),v) for v in graph])
	while not pq.isEmpty()
		currentvertex=pq.delMin()
		for newvertex in currentvertex.getConnections():
			newDist=currentvertex.getWeight(newvertex)
			if newDist<newvertex.getDistance() and newvertex in pq:
				newvertex.setDistance(newDist)
				newvertex.setPredecessor(currentvertex)
				pq.decreaseKey(newvertex,newDist)
    def test_push(self):
        main_list = []
        for i in range(0, 1000):
            main_list.insert(i, i)

        random.shuffle(main_list)

        pqueue = PriorityQueue()

        for i in range(0, 1000):
            pqueue.push(main_list[i], main_list[i])
            self.assertEqual(pqueue.size(), i + 1)
            self.assertFalse(pqueue.isEmpty())

        self.assertEqual(pqueue.pop(), 0)
        self.assertEqual(pqueue.pop(), 1)