Question: I am trying to create a search tree visulisation for the given code in python. I am unable to how do I do it ?

I am trying to create a search tree visulisation for the given code in python. I am unable to how do I do it?.
import math
import heapq
import matplotlib.pyplot as plt
# Define the Cell class
class Cell:
def __init__(self):
self.parent_i =0 # Parent cell's row index
self.parent_j =0 # Parent cell's column index
self.f = float('inf') # Total cost of the cell (g + h)
self.g = float('inf') # Cost from start to this cell
self.h =0 # Heuristic cost from this cell to destination
# Define the size of the grid
ROW =16
COL =16
# Check if a cell is valid (within the grid)
def is_valid(row, col):
return (row >=0) and (row < ROW) and (col >=0) and (col < COL)
# Check if a cell is unblocked
def is_unblocked(grid, row, col):
return grid[row][col]==1
# Check if a cell is the destination
def is_destination(row, col, dest):
return row == dest[0] and col == dest[1]
# Calculate the heuristic value of a cell (Euclidean distance to destination)
def calculate_h_value(row, col, dest):
return abs(row - dest[0])+ abs(col - dest[1])
# Implement the A* search algorithm
def a_star_search(grid, src, dest):
# Check if the source and destination are valid
if not is_valid(src[0], src[1]) or not is_valid(dest[0], dest[1]):
print("Source or destination is invalid")
return
# Check if the source and destination are unblocked
if not is_unblocked(grid, src[0], src[1]) or not is_unblocked(grid, dest[0], dest[1]):
print("Source or the destination is blocked")
return
# Initialize the closed list (visited cells)
closed_list =[[False for _ in range(COL)] for _ in range(ROW)]
# Initialize the details of each cell
cell_details =[[Cell() for _ in range(COL)] for _ in range(ROW)]
# Initialize the start cell details
i = src[0]
j = src[1]
cell_details[i][j].f =0
cell_details[i][j].g =0
cell_details[i][j].h =0
cell_details[i][j].parent_i = i
cell_details[i][j].parent_j = j
# Initialize the open list (cells to be visited) with the start cell
open_list =[]
heapq.heappush(open_list, (0.0, i, j))
# Initialize lists to store explored and open cells for plotting
explored_cells =[]
open_cells =[]
# Main loop of A* search algorithm
while len(open_list)>0:
# Pop the cell with the smallest f value from the open list
p = heapq.heappop(open_list)
# Mark the cell as visited
i = p[1]
j = p[2]
closed_list[i][j]= True
explored_cells.append((i, j))
# For visualization, also store open cells
for item in open_list:
open_cells.append((item[1], item[2]))
# For each direction, check the successors
directions =[(0,1),(0,-1),(1,0),(-1,0)]
for dir in directions:
new_i = i + dir[0]
new_j = j + dir[1]
# If the successor is valid, unblocked, and not visited
if is_valid(new_i, new_j) and is_unblocked(grid, new_i, new_j) and not closed_list[new_i][new_j]:
# If the successor is the destination
if is_destination(new_i, new_j, dest):
# Set the parent of the destination cell
cell_details[new_i][new_j].parent_i = i
cell_details[new_i][new_j].parent_j = j
print("The destination cell is found")
return explored_cells, open_cells
else:
# Calculate the new f, g, and h values
g_new = cell_details[i][j].g +1.0
h_new = calculate_h_value(new_i, new_j, dest)
f_new = g_new + h_new
# If the cell is not in the open list or the new f value is smaller
if cell_details[new_i][new_j].f == float('inf') or cell_details[new_i][new_j].f > f_new:
# Add the cell to the open list
heapq.heappush(open_list, (f_new, new_i, new_j))
# Update the cell details
cell_details[new_i][new_j].f = f_new
cell_details[new_i][new_j].g = g_new
cell_details[new_i][new_j].h = h_new
cell_details[new_i][new_j].parent_i = i
cell_details[new_i][new_j].parent_j = j
def plot_search_tree(grid, explored_cells, open_cells, src, dest):
fig, ax = plt.subplots()
ax.set_aspect('equal', 'box')
for i in range(ROW):
for j in range(COL):
if (i, j) in explored_cells:
ax.add_patch(plt.Rectangle((j, i),1,1, color='lightblue'))
elif (i, j) in open_cells:
ax.add_patch(plt.Rectangle((j, i),1,1, color='lightgreen'))
elif (i, j)== tuple(src):
ax.add_patch(plt.Rectangle((j, i),1,1, color='red'))
elif (i, j)== tuple(dest):
ax.add_patch(plt.Rectangle((j, i),1,1, color='orange'))
elif is_unblocked(grid, i, j):
ax.add_patch(plt.Rectangle((j, i),1,1, color='white'))
else:
ax.add_patch(plt.Rectangle((j, i),1,1, color='black'))
ax.autoscale_view()
ax.invert_yaxis()
plt.show()
def main():
# Define the grid (1 for unblocked, 0 for blocked)
grid =[
[0,0,0,1,1,1,1,1,1,1,1,0,1,1,0,0],
[0,0,0,1,0,0,0,1,1,1,1,1,1,1,0,0],
[0,0,1,1,1,1,0,1,1,1,1,1,1,1,0,0],
[0,0,1,1,1,1,0,1,1,1,0,0,0,1,0,0],
[1,1,1,1,1,1,1,1,1,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!