Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using this code as a guide: Motion planning on a rectangular grid using A * search from random import
Using this code as a guide:
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 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
# 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...
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
main
# Uncomment the following sets of trials when you're ready
# Hard mode
numrows
numcols
obstacleprob
for trial in range:
print
Harder trial strtrial
###main
# INSANE mode
numrows
numcols
obstacleprob
for trial in range:
print
INSANE trial strtrial
###main
Help code a Python assignment using these guidelines:
Motion planning
Consider a robot planning a path through a gridbased world. Its goal is to move from a start square in the upperleft to a goal square in the lowerright, moving around any obstacles in its way.
Write a program that uses A search to find the shortest path from the start to the goal in a randomly generated grid world, or discover that no path exists. Use the Manhattan distance from the current position to the goal square as your heuristic.
Use the motion.py script as a starting point, which includes code to randomly generate ten trial worlds. Your program should print out the solution grid, showing the shortest path in stars, as in the example above, or a message that no path exists if the search fails.
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