Example #1
0
    def sp(self, startn, paths=False):
        """ Dijkstra's algorithm for shortest paths computation. Parameter is source vertex from which the search is
        launched. Computes shortest paths from source to every other node.

        :parameter paths: indicates if list of distances or edges must be returned """

        dist_to = [
            float('inf')
            for _ in range(max(n.get_label() for n in self.adj_list) + 1)
        ]
        dist_to[startn.get_label()] = 0
        edge_to = [
            None for _ in range(max(n.get_label() for n in self.adj_list) + 1)
        ]

        pq = Heap()
        pq.insert(startn, dist_to[startn.get_label()])

        while not pq.isEmpty():
            v = pq.delete()
            for e in v.get_edges():
                self.relax(e, pq, dist_to, edge_to)

        return dist_to if not paths else edge_to
Example #2
0
class RankBasedExpReplay(object): 
    def __init__(self,maxSize, alpha=0.6):
        self.maxSize = maxSize
        self.buffer = Buffer(self.maxSize)
        self.heap = Heap()
        self.weights = None

        #Add two flags to indicate whether alpha or queue size has changed
        self.prevAlpha = alpha
        self.prevSize =0

        # Variables to store current alpha and exp replay size
        self.alpha = alpha
        self.curSize = 0

        #Weightings to each experience
        self.endPoints = []

    def addExperience(self, experience):
        index = self.buffer.getPointer()
        self.buffer.insert(experience)
        weight = self.heap.getMaxPriority()
        self.heap.add(index, weight)
        self.curSize = self.heap.size
        
    def modifyExperience(self, weight, index):
        self.heap.add(index, weight)
        self.curSize = self.heap.size
        
    def sample(self, samplesAmount):

        if (self.prevAlpha != self.alpha) or (self.prevSize != self.curSize) :
                self.endPoints, self.weights = self.computeBoundaries(self.alpha, self.curSize, samplesAmount)
                self.prevAlpha = self.alpha
                self.prevSize = self.curSize
        totalWeights = sum(self.weights)
        startPoint = 0
        expList = []
        weightList = []
        indexList = []
        for a in self.endPoints :
                end = a + 1
                diff = end - startPoint 
                sampledNum = np.random.randint(diff, size=1)[0]
                retrIndex = startPoint + sampledNum
                startPoint = end
                expList.append(self.buffer.getItem(self.heap.getIndex(retrIndex)))
                weightList.append(self.weights[retrIndex]/totalWeights)
                indexList.append(retrIndex)
        return np.asarray(expList),np.asarray(weightList),np.asarray(indexList)

    def computeBoundaries(self, alpha, curSize, samplesAmount):
        ranks = list(range(curSize))
        weights = [(1.0/(rank+1))**alpha for rank in ranks]
        sumAllWeights = sum(weights)
        stops = np.linspace(0,sumAllWeights,samplesAmount+1).tolist()
        del stops[0]
        curSum = 0
        curFounded = 0
        curStop = -1
        results = []
        for a in weights:
                curSum += a
                curStop += 1
                if curSum >= stops[curFounded]:
                        results.append(curStop)
                        curFounded += 1

        return results, weights
    
    def rebalance(self):
        indexList = []
        weightList = []
        while self.heap.size != 0:
            maxIndex = self.heap.p2i[1]
            maxWeight = self.heap.p2w[1]
            indexList.append(maxIndex)
            weightList.append(maxWeight)
            self.heap.delete(maxIndex)
        for a in range(len(indexList)):
            self.add(indexList[a],weightList[a])
            
    def getMaxPriority(self):
        if self.heap.size == 0:
            return sys.float_info.max
        return self.heap.p2w[1]
            
    def dijkstra(self, i, j, i_, j_):
        res = list()
        vist = list()

        destinationNode = j_ * len(self.board[0]) + i_
        startingNode = j * len(self.board[0]) + i

        visited = [False] * len(self.graph)

        ptr = startingNode

        heap = Heap()
        heap.insert(self.graph[ptr])
        self.graph[ptr].h = self.heuristic2(i, j, i_, j_)
        self.graph[ptr].f = self.graph[ptr].h
        self.graph[ptr].g = 0

        while (heap.size != 0):
            ptr = heap.delete().vnum

            if (ptr == destinationNode):
                break

            visited[ptr] = True
            temp = list()

            if (ptr == destinationNode):
                res.append(destinationNode)
                break

            pt = self.graph[ptr].adjLists

            while (pt != None):
                if (pt.vnum == destinationNode):
                    break

                temp.append(pt.vnum)

                x = pt.vnum % len(self.board)
                y = pt.vnum / len(self.board)
                self.graph[pt.vnum].h = self.heuristic2(x, y, i_, j_)
                self.graph[pt.vnum].g = min(self.graph[pt.vnum].g,
                                            self.graph[ptr].g + pt.weight)

                self.graph[
                    pt.vnum].f = self.graph[pt.vnum].h + self.graph[pt.vnum].g

                if (visited[pt.vnum]):
                    pt = pt.next
                    continue

                visited[pt.vnum] = True
                heap.insert(self.graph[pt.vnum])

                pt = pt.next

            if (pt != None and pt.vnum == destinationNode):
                break
            vist.append(temp)

        self.graph[destinationNode].f = 0
        self.graph[destinationNode].h = 0

        #BACKTRACKING

        ptr = destinationNode
        visited = [False] * len(self.graph)
        visited[ptr] = True

        while (ptr != startingNode):
            res.insert(0, ptr)
            pt = self.graph[ptr].adjLists
            minPtr = pt

            while (pt != None):
                if (visited[pt.vnum]):
                    pt = pt.next
                    continue

                visited[pt.vnum] = True

                if (self.graph[minPtr.vnum].f > self.graph[pt.vnum].f):
                    minPtr = pt

                pt = pt.next

            ptr = minPtr.vnum

        res.insert(0, startingNode)
        return (res, vist)
Example #4
0
class AStarSearch(object):
    """class comments here
    """
    def __init__(self, gridMap, start, goal):
        """
        @param start: start node(GridNode)
        @param goal: goal node(GridNode)
        """
        self.__gridMap = gridMap
        self.__start = PathNode(None, start, goal)
        self.__goal = goal
        self.__mOpenList = [] # heap
        self.__mClosedList = []
        self.__width = self.__gridMap.getWidth()
        self.__height = self.__gridMap.getHeight()

    def get_successors(self, node):
        """ find successor.
        """
        successors = []
        x = node.getX()
        y = node.getY()
        # left
        if not x-1 < 0:
            node = GridNode(self.__gridMap, x-1, y)
            if node.getAccessAttribute():
                successors.append(node)
        # up
        if not y-1 < 0:
            node = GridNode(self.__gridMap, x, y-1)
            if node.getAccessAttribute():
                successors.append(node)
        # right
        if x+1 < self.__width:
            node = GridNode(self.__gridMap, x+1, y)
            if node.getAccessAttribute():
                successors.append(node)
        # down
        if y+1 < self.__height:
            node = GridNode(self.__gridMap, x, y+1)
            if node.getAccessAttribute():
                successors.append(node)
        return successors

    def find_path(self):
        node = self.__start
        self.__mOpenList = Heap(None, compare)
        self.__mOpenList.insert(node)
        while True:
            # pop the node which has the smallest f
            cur = self.__mOpenList.delete(0)
            if cur == None: # the heap is empty
                return False
            if cur.gridNode.isSameNode(self.__goal): # success
                self.__mClosedList.append(cur)
                return True
            self.__mClosedList.append(cur)
            candidates = self.get_successors(cur.gridNode)
            # check if the node in closed list or not
            for candidate in candidates:
                i = 0
                length = len(self.__mClosedList)
                while i<length:
                    if candidate.isSameNode(self.__mClosedList[i].gridNode):
                        break
                    i = i+1
                if i<length: # already in closed list, ignore it
                    continue
                # check if the node in open list or not
                j = 0
                length = len(self.__mOpenList.heap)
                while j<length:
                    if candidate.isSameNode(self.__mOpenList.heap[j].gridNode):
                        break
                    j = j+1
                if j<length: # already in open list,
                    new_pathNode = PathNode(cur, candidate, self.__goal)
                    if new_pathNode.g < self.__mOpenList.heap[j].g:
                        self.__mOpenList.delete(j)
                        self.__mOpenList.insert(new_pathNode)
                else:
                    new_pathNode = PathNode(cur, candidate, self.__goal)
                    self.__mOpenList.insert(new_pathNode)
    def print_path(self):
        path = []
        if not self.__mClosedList[-1].gridNode.isSameNode(self.__goal):
            print 'Path not found'
        else:
            parent = self.__mClosedList[-1]
            path.insert(0, parent)
            while True:
                if parent.gridNode.isSameNode(self.__start.gridNode):
                    path.insert(0, parent)
                    break
                else:
                    for x in self.__mClosedList:
                        if parent.gridNode.isSameNode(x.gridNode):
                            path.insert(0, parent)
                            parent = x.parent
            for x in path:
                x.gridNode.printNode()

    def get_pathMap(self):
        pathMap = self.__gridMap
        if not self.__mClosedList[-1].gridNode.isSameNode(self.__goal):
            print 'Path not found'
            return None
        else:
            parent = self.__mClosedList[-1]
            pathMap.setNode(parent.gridNode.getX(), parent.gridNode.getY(), 3)
            parent = parent.parent
            while True:
                if parent.gridNode.isSameNode(self.__start.gridNode):
                    pathMap.setNode(parent.gridNode.getX(), parent.gridNode.getY(), 2)
                    break
                else:
                    for x in self.__mClosedList:
                        if parent.gridNode.isSameNode(x.gridNode):
                            pathMap.setNode(parent.gridNode.getX(), parent.gridNode.getY(), 4)
                            parent = x.parent
            return pathMap
Example #5
0
class AStarSearch(object):
    """class comments here
    """
    def __init__(self, gridMap, start, goal):
        """
        @param start: start node(GridNode)
        @param goal: goal node(GridNode)
        """
        self.__gridMap = gridMap
        self.__start = PathNode(None, start, goal)
        self.__goal = goal
        self.__mOpenList = []  # heap
        self.__mClosedList = []
        self.__width = self.__gridMap.getWidth()
        self.__height = self.__gridMap.getHeight()

    def get_successors(self, node):
        """ find successor.
        """
        successors = []
        x = node.getX()
        y = node.getY()
        # left
        if not x - 1 < 0:
            node = GridNode(self.__gridMap, x - 1, y)
            if node.getAccessAttribute():
                successors.append(node)
        # up
        if not y - 1 < 0:
            node = GridNode(self.__gridMap, x, y - 1)
            if node.getAccessAttribute():
                successors.append(node)
        # right
        if x + 1 < self.__width:
            node = GridNode(self.__gridMap, x + 1, y)
            if node.getAccessAttribute():
                successors.append(node)
        # down
        if y + 1 < self.__height:
            node = GridNode(self.__gridMap, x, y + 1)
            if node.getAccessAttribute():
                successors.append(node)
        return successors

    def find_path(self):
        node = self.__start
        self.__mOpenList = Heap(None, compare)
        self.__mOpenList.insert(node)
        while True:
            # pop the node which has the smallest f
            cur = self.__mOpenList.delete(0)
            if cur == None:  # the heap is empty
                return False
            if cur.gridNode.isSameNode(self.__goal):  # success
                self.__mClosedList.append(cur)
                return True
            self.__mClosedList.append(cur)
            candidates = self.get_successors(cur.gridNode)
            # check if the node in closed list or not
            for candidate in candidates:
                i = 0
                length = len(self.__mClosedList)
                while i < length:
                    if candidate.isSameNode(self.__mClosedList[i].gridNode):
                        break
                    i = i + 1
                if i < length:  # already in closed list, ignore it
                    continue
                # check if the node in open list or not
                j = 0
                length = len(self.__mOpenList.heap)
                while j < length:
                    if candidate.isSameNode(self.__mOpenList.heap[j].gridNode):
                        break
                    j = j + 1
                if j < length:  # already in open list,
                    new_pathNode = PathNode(cur, candidate, self.__goal)
                    if new_pathNode.g < self.__mOpenList.heap[j].g:
                        self.__mOpenList.delete(j)
                        self.__mOpenList.insert(new_pathNode)
                else:
                    new_pathNode = PathNode(cur, candidate, self.__goal)
                    self.__mOpenList.insert(new_pathNode)

    def print_path(self):
        path = []
        if not self.__mClosedList[-1].gridNode.isSameNode(self.__goal):
            print 'Path not found'
        else:
            parent = self.__mClosedList[-1]
            path.insert(0, parent)
            while True:
                if parent.gridNode.isSameNode(self.__start.gridNode):
                    path.insert(0, parent)
                    break
                else:
                    for x in self.__mClosedList:
                        if parent.gridNode.isSameNode(x.gridNode):
                            path.insert(0, parent)
                            parent = x.parent
            for x in path:
                x.gridNode.printNode()

    def get_pathMap(self):
        pathMap = self.__gridMap
        if not self.__mClosedList[-1].gridNode.isSameNode(self.__goal):
            print 'Path not found'
            return None
        else:
            parent = self.__mClosedList[-1]
            pathMap.setNode(parent.gridNode.getX(), parent.gridNode.getY(), 3)
            parent = parent.parent
            while True:
                if parent.gridNode.isSameNode(self.__start.gridNode):
                    pathMap.setNode(parent.gridNode.getX(),
                                    parent.gridNode.getY(), 2)
                    break
                else:
                    for x in self.__mClosedList:
                        if parent.gridNode.isSameNode(x.gridNode):
                            pathMap.setNode(parent.gridNode.getX(),
                                            parent.gridNode.getY(), 4)
                            parent = x.parent
            return pathMap