left_turn_policy.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # ----------
  2. # User Instructions:
  3. #
  4. # Implement the function optimum_policy2D below.
  5. #
  6. # You are given a car in grid with initial state
  7. # init. Your task is to compute and return the car's
  8. # optimal path to the position specified in goal;
  9. # the costs for each motion are as defined in cost.
  10. #
  11. # There are four motion directions: up, left, down, and right.
  12. # Increasing the index in this array corresponds to making a
  13. # a left turn, and decreasing the index corresponds to making a
  14. # right turn.
  15. forward = [[-1, 0], # go up
  16. [ 0, -1], # go left
  17. [ 1, 0], # go down
  18. [ 0, 1]] # go right
  19. forward_name = ['up', 'left', 'down', 'right']
  20. # action has 3 values: right turn, no turn, left turn
  21. action = [-1, 0, 1]
  22. action_name = ['R', '#', 'L']
  23. # EXAMPLE INPUTS:
  24. # grid format:
  25. # 0 = navigable space
  26. # 1 = unnavigable space
  27. grid = [[1, 1, 1, 0, 0, 0],
  28. [1, 1, 1, 0, 1, 0],
  29. [0, 0, 0, 0, 0, 0],
  30. [1, 1, 1, 0, 1, 1],
  31. [1, 1, 1, 0, 1, 1]]
  32. init = [4, 3, 0] # given in the form [row,col,direction]
  33. # direction = 0: up
  34. # 1: left
  35. # 2: down
  36. # 3: right
  37. goal = [2, 0] # given in the form [row,col]
  38. cost = [2, 1, 20] # cost has 3 values, corresponding to making
  39. # a right turn, no turn, and a left turn
  40. # EXAMPLE OUTPUT:
  41. # calling optimum_policy2D with the given parameters should return
  42. # [[' ', ' ', ' ', 'R', '#', 'R'],
  43. # [' ', ' ', ' ', '#', ' ', '#'],
  44. # ['*', '#', '#', '#', '#', 'R'],
  45. # [' ', ' ', ' ', '#', ' ', ' '],
  46. # [' ', ' ', ' ', '#', ' ', ' ']]
  47. # ----------
  48. # ----------------------------------------
  49. # modify code below
  50. # ----------------------------------------
  51. def optimum_policy2D(grid,init,goal,cost):
  52. return policy2D