Answered step by step
Verified Expert Solution
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 Stateobject:
def initself startposition, goalposition, startgrid:
self.position startposition
self.goal goalposition
self.grid startgrid
self.totalmoves
# Fill in the rest of the class...
def manhattandistanceself:
Calculates the Manhattan distance to the goal."""
return absselfgoal self.position absselfgoal self.position
def getsuccessorsself:
Generates possible successor states."""
successors
for i j in :
newposition selfposition i self.position j
if newposition lenselfgrid and newposition lenselfgrid:
if self.gridnewpositionnewposition:
newgrid deepcopyselfgrid
newgridnewpositionnewposition
successors.appendStatenewposition, self.goal, newgrid
return successors
def creategrid:
Create and return a randomized grid
s in the grid indcate free squares
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 numrows by numcols grid of all zeros
grid for c in rangenumcols for r in rangenumrows
# Put ones around the boundary
grid for c in rangenumcols
gridnumrows for c in rangenumcols
for r in rangenumrows:
gridr
gridrnumcols
# Sprinkle in obstacles randomly
for r in range numrows :
for c in range numcols :
if random obstacleprob:
gridrc;
# Make sure the goal and start spaces are clear
grid
gridnumrows numcols
return grid
def printgridgrid:
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 rangenumrows:
for c in rangenumcols:
if gridrc:
print end
else:
printgridrc 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 creategrid
printgridgrid
# Initialize the starting state and priority queue
startposition
goalposition numrows numcols
startstate Statestartposition, goalposition, grid
startstate.grid ord
# A priority: implement the Manhattan distance in the State class
priority startstate.totalmoves startstate.manhattandistance
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.putpriority startstate
# 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, currentstate queue.get
currentposition currentstate.position
if currentposition goalposition:
printgridcurrentstate.grid # Print the solution path
return
visited.addcurrentposition
for successor in currentstate.getsuccessors:
if successor.position not in visited:
successor.totalmoves currentstate.totalmoves
priority successor.totalmoves successor.manhattandistance
queue.putpriority successor
# No solution found
printNo path found"
if namemain:
seed
# Easy mode
# Global variables
# Saves us the trouble of continually passing them as parameters
numrows
numcols
obstacleprob
for trial in range:
print
Easy trial strtrial
ma
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started