Exemplo n.º 1
0
def gen_grid_g(grid, idd):
    """
    ===========================================================================
     Description: Generate Grid of G for Idd.
    ===========================================================================
     Arguments:
    ---------------------------------------------------------------------------
        1. grid : Serialized Grid.
        2. idd : int (Node's Id).
    ===========================================================================
     Return: Grid of G values for Idd.
    ===========================================================================
    """
    grid_g = np.full([grid.shape[0], grid.shape[1]], -1, dtype=int)
    g = 0
    closed = set()
    opened = set()
    opened.add(idd)
    while opened:
        opened_temp = opened.copy()
        closed.update(opened_temp)
        opened.clear()
        for cur in opened_temp:
            row, col = u_grid.to_row_col(grid, cur)
            grid_g[row][col] = g
            neighbors = set(u_grid.get_neighbors(grid, row, col))
            opened.update(neighbors - closed)
        g += 1
    return grid_g
Exemplo n.º 2
0
def get_random_idds(grid, idd_center, radius, amount):
    """
    ===========================================================================
     Description: Generate Set of Random Idds in the Rectangle.
    ===========================================================================
     Arguments:
    ---------------------------------------------------------------------------
        1. grid : Grid.
        2. idd_center : int (Idd of Rectangle Center).
        3. radius : int (Radius of Rectangle).
        4. amount : int (Amount of Idds to Random).
    ===========================================================================
     Result:
    ---------------------------------------------------------------------------
        1. set of int (Set of Random Idds in Rectangle).
        2. None if amount of Idds is less then required amount.
    ===========================================================================
    """
    row, col = u_grid.to_row_col(grid, idd_center)
    center = Point(row, col)
    p_min, p_max = u_2d.get_rect(center, radius, radius)
    if not u_grid.is_valid_row_col(grid, p_min.row, p_min.col, by_idd=False):
        return None
    if not u_grid.is_valid_row_col(grid, p_max.row, p_max.col, by_idd=False):
        return None
    grid_sub = grid[p_min.row:p_max.row + 1, p_min.col:p_max.col + 1]
    idds = []
    for idd in np.nditer(grid_sub):
        if idd >= 0:
            idds.append(int(idd))
    if (idds is None) or (amount > len(idds)):
        return None
    random.shuffle(idds)
    return set(idds[:amount])
Exemplo n.º 3
0
def get_expanded_nodes(grid, grid_g, grid_h, goal):
    """
    ===========================================================================
     Description: Return the Number of Expanded Nodes of A* Algorithm.
    ===========================================================================
     Arguments:
    ---------------------------------------------------------------------------
        1. grid_g : Grid of G (of Start).
        2. grid_h : Grid of H (of Goal).
        3. goal : int (Idd number).
    ===========================================================================
     Return: Set of Idds (expanded nodes of A* algorithm).
    ===========================================================================
    """
    grid_f = grid_g + grid_h
    row, col = u_grid.to_row_col(grid_f, goal)
    f = grid_f[row][col]
    grid_f_less = grid * (grid_f < f)
    expanded_nodes = set(np.unique(grid_f_less)) - {-1}

    grid_f_equal = grid * (grid_f == f)
    candidate_idds = list(np.unique(grid_f_equal))
    queue_nodes = queue.PriorityQueue()
    for idd in candidate_idds:
        row, col = u_grid.to_row_col(grid, idd)
        queue_nodes.put((-grid_g[row][col], idd))
    idd_cur = queue_nodes.get()
    expanded_nodes.add(idd_cur[1])
    while not queue_nodes.empty():
        idd = queue_nodes.get()
        if u_grid.manhattan_distance(grid, idd_cur[1], idd[1]) == 1:
            expanded_nodes.add(idd[1])
            idd_cur = idd

    expanded_nodes.remove(0)

    return expanded_nodes
Exemplo n.º 4
0
 def _expand(self):   
     """
     ===================================================================
      Description: Expand the Best Node's Children.
     ===================================================================
     """     
     row, col = u_grid.to_row_col(self.grid, self.best.idd)
     idds = u_grid.get_neighbors(self.grid, row, col)
     children = {Node(x) for x in idds} - self.closed      
     for child in sorted(children):
         g_new = self.best.g + child.w
         if child.g <= g_new:
             continue
         if self.opened.contains(child):
             self.opened.remove(child)
         self._update_node(child,self.best,g_new)
         self.opened.push(child)
Exemplo n.º 5
0
 def _expand(self):
     """
     ===================================================================
      Description: Expand the Best Node's Children.
     ===================================================================
     """
     row, col = u_grid.to_row_col(self._grid, self._best.idd)
     idds = u_grid.get_neighbors(self._grid, row, col)
     children = {self.nodes[x] for x in idds} - self._closed
     for child in children:
         g_new = self._best.g + child.w
         if child.g <= g_new:
             continue
         if self._opened.contains(child):
             self._opened.remove(child)
         self._update_node(child, self._best, g_new, self._goals_active)
         self._opened.push(child)
Exemplo n.º 6
0
def plot_grid(grid, special_rows_cols=[], special_colors=[], path=None):
    """
    ===========================================================================
     Description - Plot Grid with black and white or special colors.
    ---------------------------------------------------------------------------
        1. Plot special cells with special colors.
        2. Other cells plot with black and white colors (valid cell or not).
    ===========================================================================
     Arguments:
    ---------------------------------------------------------------------------
        1. grid : Grid
        2. special_rows_cols : list of list of tuples of row and col.
        3. special_colors : list of 3-element tuples (RGB representation).
        4. path : str (where to store the picture file).
    ===========================================================================
    """
    arr = [[(-1, -1, -1) for x in range(grid.shape[1])]
           for y in range(grid.shape[0])]

    for i, rows_cols in enumerate(special_rows_cols):
        color = special_colors[i]
        for row, col in rows_cols:
            arr[row][col] = color

    for idd in range(grid.size):
        row, col = u_grid.to_row_col(grid, idd)
        if arr[row][col] == (-1, -1, -1):
            if u_grid.is_valid_idd(grid, idd):
                arr[row][col] = (255, 255, 255)
            else:
                arr[row][col] = (0, 0, 0)

    fig, ax = plt.subplots()
    ax.imshow(arr)
    plt.setp(ax.get_xticklabels(),
             rotation=45,
             ha="right",
             rotation_mode="anchor")
    fig.tight_layout()
    plt.show()

    if (path):
        plt.savefig(path)
Exemplo n.º 7
0
def get_shape(grid, idds):
    """
    ===========================================================================
     Description: Return Shape of the Rectangle that Surrounds the Idds.
    ===========================================================================
     Arguments:
    ---------------------------------------------------------------------------
        1. grid : Grid
        2. idds : set of int (Set of Idds).
    ===========================================================================
     Return:
    ---------------------------------------------------------------------------
        1. int : Amount of Rows in the Rectangle.
        2. int : Amount of Cols in the Rectangle.
    ===========================================================================
    """
    if idds is None: return None
    rows = set()
    cols = set()
    for idd in idds:
        row, col = u_grid.to_row_col(grid, idd)
        rows.add(row)
        cols.add(col)
    return max(rows) - min(rows) + 1, max(cols) - min(cols) + 1
Exemplo n.º 8
0
def gen_grid_h(grid, goal, lookups=set(), grid_lookup=None):
    """
    ===========================================================================
     Description: Generate Grid of Heuristic values for Idd_1.
    ===========================================================================
     Arguments:
    ---------------------------------------------------------------------------
        1. grid : Serialized Grid.
        2. goal : int (Node's Id).
        3. lookup : Set of Idds.
        4. grid_lookup : Grid with True Heuristic.
    ===========================================================================
     Return: Grid of Heuristic values for Idd_1.
    ===========================================================================
    """
    grid_h = np.full([grid.shape[0], grid.shape[1]], -1, dtype=int)
    for idd in range(grid.size):
        row, col = u_grid.to_row_col(grid, idd)
        if grid[row][col] >= 0:
            if idd in lookups:
                grid_h[row][col] = grid_lookup[row][col]
            else:
                grid_h[row][col] = u_grid.manhattan_distance(grid, idd, goal)
    return grid_h