Ejemplo n.º 1
0
def get_far_node(grid, src, dist_min, dist_max=float('Infinity')):
    """
    ===========================================================================
     Description:
    ---------------------------------------------------------------------------
        1. Get Grid, Source Node and Distances Boundaries.
        2. Return Random Valid Idd within the Boundaries.
    ===========================================================================
     Arguments:
    ---------------------------------------------------------------------------
        1. grid : Grid
        2. src : Node
        3. dist_min : int
        4. dist_max : float
    ===========================================================================
     Return: Random and Valid Node Idd (int).
    ===========================================================================
    """
    idds = u_grid.get_valid_idds(grid)
    idds_valid = list()
    for idd in idds:
        dist = u_grid.manhattan_distance(grid, src, idd)
        if (dist >= dist_min) and (dist <= dist_max):
            idds_valid.append(idd)
    random.shuffle(idds_valid)
    return idds_valid[0]
Ejemplo n.º 2
0
def gen_random_pairs(grid, distance_min, epochs):
    """
    ===========================================================================
     Description: Generate Random Pairs with Epochs where Min Distance.
    ===========================================================================
     Arguments: 
    ---------------------------------------------------------------------------
        1. grid : Grid.
        2. distance_min : int (Min Distance between the Pair Idds).
        3. epochs : int (Amount of Attempts).
    ===========================================================================
     Return: set of tuples (int,int) : Random Pairs.
    ===========================================================================
    """
    idds = u_grid.get_valid_idds(grid)
    pairs = set()
    if len(idds) < 2:
        return pairs
    for i in range(epochs):
        random.shuffle(idds)
        idd_1 = idds[0]
        idd_2 = idds[1]
        if (u_grid.manhattan_distance(grid, idd_1, idd_2) >= distance_min):
            pairs.add((idd_1, idd_2))
    return pairs
Ejemplo n.º 3
0
def check(lists):
    grid = u_grid.lists_to_grid(lists)
    grid = u_grid.gen_obstacles_grid(20, 75)
    #grid = u_grid.gen_symmetric_grid(10)
    idds = u_grid.get_valid_idds(grid)
    random.shuffle(idds)
    start = idds[0]
    goals = idds[1:11]

    kastar = KAStar(grid, start, goals)
    kastar.run()
    for goal in goals:
        astar = AStar(grid, start, goal)
        astar.run()
        if (len(astar.get_path()) != len(kastar.get_path(goal))):
            #print('{0} Ok'.format(goal))
            #else:
            #print('{0} Failed'.format(goal))
            print('start=[{0}]'.format(start))
            print('goal=[{0}]'.format(goal))
            for row in range(grid.shape[0]):
                li = list()
                for col in range(grid.shape[1]):
                    li.append(grid[row][col])
                li = [str(x) for x in li]
                print(','.join(li))
            print('goals={0}'.format(goals))
            print(astar.get_path())
            print(kastar.get_path(goal))
            return
Ejemplo n.º 4
0
    def __init__(self, grid, start, goals):
        """
        ===================================================================
         Description: KA* Algorithm.
        ===================================================================
         Arguments:
        -------------------------------------------------------------------
            1. grid : Grid.
            2. start : int (Start Idd).
            3. goals : set of int (Goal Idd).
        ===================================================================
        """
        self.start = start
        self.goals = goals
        self._goals_active = set(goals)
        self._grid = grid
        self.nodes = dict()
        idds_valid = u_grid.get_valid_idds(grid)
        for idd in idds_valid:
            self.nodes[idd] = Node(idd)

        self._best = self.nodes[start]
        self._best.g = 0

        self._closed = set()
        self._opened = Opened()
        self._opened.push(self._best)

        self.counter_heuristic = 0
Ejemplo n.º 5
0
    def tester_get_path():
        grid = u_grid.gen_symmetric_grid(4)
        start = 0
        goal = 12
        astar = KAStar(grid, start, {goal})
        astar.run()
        optimal_path = [0, 4, 8, 12]
        p1 = astar.get_path(goal) == optimal_path

        grid = u_grid.gen_symmetric_grid(4)
        grid[1][1] = -1
        grid[2][1] = -1
        start = 8
        goal = 10
        astar = KAStar(grid, start, {goal})
        astar.run()
        optimal_path = [8, 12, 13, 14, 10]
        p2 = astar.get_path(goal) == optimal_path

        p3 = True
        for i in range(1000):
            n = u_random.get_random_int(4, 4)
            grid = u_grid.gen_symmetric_grid(n)
            idds_valid = u_grid.get_valid_idds(grid)
            random.shuffle(idds_valid)
            start = idds_valid[0]
            goals = idds_valid[1:3]
            kastar = KAStar(grid, start, goals)
            kastar.run()
            for goal in goals:
                len_optimal = u_grid.manhattan_distance(grid, start, goal) + 1
                if len(kastar.get_path(goal)) != len_optimal:
                    p3 = False
                    print('start={0}'.format(start))
                    print('goal={0}'.format(goals))
                    print('grid:')
                    for row in range(grid.shape[0]):
                        li = list()
                        for col in range(grid.shape[1]):
                            li.append(grid[row][col])
                        li = [str(x) for x in li]
                        print(','.join(li))
                    print('goal[{0}]: {1}'.format(goal, kastar.get_path(goal)))

        fname = sys._getframe().f_code.co_name[7:]
        if (p1 and p2 and p3):
            print('OK: {0}'.format(fname))
        else:
            print('Failed: {0}'.format(fname))
Ejemplo n.º 6
0
 def tester_get_path():
     grid = u_grid.gen_symmetric_grid(4)
     start = 0
     goal = 12
     astar = AStar(grid,start,goal)
     astar.run()
     optimal_path = [0,4,8,12]
     p1 = astar.get_path() == optimal_path
     
     grid = u_grid.gen_symmetric_grid(4)
     grid[1][1] = -1
     grid[2][1] = -1
     start = 8
     goal = 10
     astar = AStar(grid,start,goal)
     astar.run()
     optimal_path = [8,12,13,14,10]
     p2 = astar.get_path() == optimal_path
     
     p3 = True
     for i in range(1000):
         n = u_random.get_random_int(3,10)
         grid = u_grid.gen_symmetric_grid(n)
         idds_valid = u_grid.get_valid_idds(grid)
         random.shuffle(idds_valid)
         start = idds_valid[0]
         goal = idds_valid[1]
         astar = AStar(grid,start,goal)
         astar.run()
         len_optimal = u_grid.manhattan_distance(grid,start,goal)+1
         if len(astar.get_path()) != len_optimal:
             p3 = False
     
     fname = sys._getframe().f_code.co_name[7:]
     if (p1 and p2 and p3):        
         print('OK: {0}'.format(fname))
     else:
         print('Failed: {0}'.format(fname))