Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code keeps giving a TypeError: Motion planning on a rectangular grid using A * search from random import

This code keeps giving a TypeError:
"""
Motion planning on a rectangular grid using A* search
"""
from random import random
from random import seed
from queue import PriorityQueue
from copy import deepcopy
class State(object):
def __init__(self, start_position, goal_position, start_grid):
self.position = start_position
self.goal = goal_position
self.grid = start_grid
self.total_moves =0
#--- Fill in the rest of the class...
def manhattan_distance(self):
"""Calculates the Manhattan distance to the goal."""
return abs(self.goal[0]- self.position[0])+ abs(self.goal[1]- self.position[1])
def get_successors(self):
"""Generates possible successor states."""
successors =[]
for i, j in [(0,1),(0,-1),(1,0),(-1,0)]:
new_position =(self.position[0]+ i, self.position[1]+ j)
if 0<= new_position[0]< len(self.grid) and 0<= new_position[1]< len(self.grid[0]):
if self.grid[new_position[0]][new_position[1]]==0:
new_grid = deepcopy(self.grid)
new_grid[new_position[0]][new_position[1]]='*'
successors.append(State(new_position, self.goal, new_grid))
return successors
def create_grid():
"""
Create and return a randomized grid
0's in the grid indcate free squares
1's indicate obstacles
DON'T MODIFY THIS ROUTINE.
DON'T MODIFY THIS ROUTINE.
DON'T MODIFY THIS ROUTINE.
DON'T MODIFY THIS ROUTINE.
ARE YOU MODIFYING THIS ROUTINE?
IF SO, STOP IT.
"""
# Start with a num_rows by num_cols grid of all zeros
grid =[[0 for c in range(num_cols)] for r in range(num_rows)]
# Put ones around the boundary
grid[0]=[1 for c in range(num_cols)]
grid[num_rows -1]=[1 for c in range(num_cols)]
for r in range(num_rows):
grid[r][0]=1
grid[r][num_cols -1]=1
# Sprinkle in obstacles randomly
for r in range(1, num_rows -1):
for c in range(2, num_cols -2):
if random()< obstacle_prob:
grid[r][c]=1;
# Make sure the goal and start spaces are clear
grid[1][1]=0
grid[num_rows -2][num_cols -2]=0
return grid
def print_grid(grid):
"""
Print a grid, putting spaces in place of zeros for readability
DON'T MODIFY THIS ROUTINE.
DON'T MODIFY THIS ROUTINE.
DON'T MODIFY THIS ROUTINE.
DON'T MODIFY THIS ROUTINE.
ARE YOU MODIFYING THIS ROUTINE?
IF SO, STOP IT.
"""
for r in range(num_rows):
for c in range(num_cols):
if grid[r][c]==0:
print('', end='')
else:
print(grid[r][c], end='')
print('')
print('')
return
def main():
"""
Use A* search to find a path from the upper left to the lower right
of the puzzle grid
Complete this method to implement the search
At the end, print the solution state
Each State object has a copy of the grid
When you make a move by generating a new State, put a * on its grid
to show the solution path
"""
# Setup the randomized grid
grid = create_grid()
print_grid(grid)
# Initialize the starting state and priority queue
start_position =(1,1)
goal_position =(num_rows -2, num_cols -2)
start_state = State(start_position, goal_position, grid)
start_state.grid[1][1]= ord('*')
# A* priority: implement the Manhattan distance in the State class
priority = start_state.total_moves + start_state.manhattan_distance()
queue = PriorityQueue()
# Insert as a tuple
# The queue orders elements by the first tuple value
# A call to queue.get() returns the tuple with the minimum first value
queue.put((priority, start_state))
# Maybe you should use a dictionary to keep track of visited positions?
#--- Fill in the rest of the search...
visited = set() # Keep track of visited positions
while not queue.empty():
priority, current_state = queue.get()
current_position = current_state.position
if current_position == goal_position:
print_grid(current_state.grid) # Print the solution path
return
visited.add(current_position)
for successor in current_state.get_successors():
if successor.position not in visited:
successor.total_moves = current_state.total_moves +1
priority = successor.total_moves + successor.manhattan_distance()
queue.put((priority, successor))
# No solution found
print("No path found")
if __name__=='__main__':
seed(0)
#--- Easy mode
# Global variables
# Saves us the trouble of continually passing them as parameters
num_rows =8
num_cols =16
obstacle_prob =.20
for trial in range(5):
print('
-----Easy trial '+ str(trial +1)+'-----')
ma

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis In Microsoft Excel

Authors: Alex Holloway

1st Edition

B0CCCPKTTX, 979-8852388452

More Books

Students also viewed these Databases questions

Question

3. Outline the four major approaches to informative speeches

Answered: 1 week ago

Question

4. Employ strategies to make your audience hungry for information

Answered: 1 week ago