Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This Motion planning code doesn't print anything out besides the Easy Trial 1 . What am I not printing out correctly for the grids:

This Motion planning code doesn't print anything out besides the "Easy Trial 1". What am I not printing out correctly for the grids:
""" 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
def manhattan_distance(self):
""" Calculates the Manhattan distance to the goal """
return abs(self.position[0]- self.goal[0])+ abs(self.position[1]- self.goal[1])
def get_possible_moves(self):
""" Returns a list of possible moves from the current position """
moves =[]
for dx in [-1,1]:
new_r = self.position[0]+ dx
if 0< new_r < num_rows -1:
for dy in [-1,1]:
new_c = self.position[1]+ dy
if 0< new_c < num_cols -1 and self.grid[new_r][new_c]==0:
moves.append((new_r, new_c))
return moves
def __lt__(self, other):
""" Overloads the < operator for priority queue comparison """
return (self.total_moves + self.manhattan_distance())<(other.total_moves + other.manhattan_distance())
def create_grid():
# ...(Code for creating a randomized 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):
# ...(Code for printing the 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():
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)+'-----')
main()
#--- Uncomment the following sets of trials when you're ready
#--- Hard mode
num_rows =15
num_cols =30
obstacle_prob =.30
for trial in range(5):
print('
-----Harder trial '+ str(trial +1)+'-----')
###main()
#--- INSANE mode
num_rows =20
num_cols =60
obstacle_prob =.35
for trial in range(5):
print('
-----INSANE trial '+ str(trial +1)+'-----')
###main()
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)+'-----')
main()
#--- Uncomment the following sets of trials when you're ready
#--- Hard mode
num_rows =15
num_cols =30
obstacle_prob =.30
for trial in range(5):
print('
-----Harder trial '+ str(trial +1)+'-----')
###main()
#--- INSANE mode
num_rows =20
num_cols =60
obstacle_prob =.35
for trial in range(5):
print('
-----INSANE trial '+ str(trial +1)+'-----')
###main()

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

To find integral of sin(logx) .

Answered: 1 week ago

Question

What is Centrifugation?

Answered: 1 week ago

Question

To find integral of ?a 2 - x 2

Answered: 1 week ago

Question

To find integral of e 3x sin4x

Answered: 1 week ago