Advertisement
Guest User

Untitled

a guest
May 21st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. def reward_function(params):
  2. '''
  3. Example of rewarding the agent to follow center line
  4. '''
  5. import math
  6. # Read input parameters
  7. track_width = params['track_width']
  8. distance_from_center = params['distance_from_center']
  9. steering = abs(params['steering_angle'])
  10. all_wheels_on_track = params['all_wheels_on_track']
  11. steering_angle = params['steering_angle']
  12. is_left_of_center = params['is_left_of_center']
  13. waypoints = params['waypoints']
  14. closest_waypoints = params['closest_waypoints']
  15. heading = params['heading']
  16.  
  17. # Calculate 3 markers that are at varying distances away from the center line
  18. marker_1 = 0.1 * track_width
  19. marker_2 = 0.25 * track_width
  20. marker_3 = 0.4 * track_width
  21.  
  22. # Give higher reward if the car is closer to center line and vice versa
  23. if distance_from_center <= marker_1:
  24. reward = 1.0
  25. elif distance_from_center <= marker_2:
  26. reward = 0.75
  27. elif distance_from_center <= marker_3:
  28. reward = 0.3
  29. else:
  30. reward = 0.1 # likely crashed/ close to off track
  31.  
  32. # Steering penality threshold, change the number based on your action space setting
  33. ABS_STEERING_THRESHOLD = 10
  34.  
  35. # Penalize reward if the agent is steering too much
  36. if steering > ABS_STEERING_THRESHOLD:
  37. reward *= 0.8
  38.  
  39. # Give a high reward if no wheels go off the track and
  40. # the agent is somewhere in between the track borders
  41. distance_from_border = (0.5*track_width) - distance_from_center
  42. if all_wheels_on_track and distance_from_border >= 0.15:
  43. reward *= 1.2
  44. if not all_wheels_on_track:
  45. reward *= -1
  46.  
  47. # Calculate the direction of the center line based on the closest waypoints
  48. next_point = waypoints[closest_waypoints[1]]
  49. prev_point = waypoints[closest_waypoints[0]]
  50.  
  51. # Calculate the direction in radius, arctan2(dy, dx), the result is (-pi, pi) in radians
  52. track_direction = math.atan2(next_point[1] - prev_point[1], next_point[0] - prev_point[0])
  53. # Convert to degree
  54. track_direction = math.degrees(track_direction)
  55.  
  56. # Cacluate the difference between the track direction and the heading direction of the car
  57. direction_diff = abs(track_direction - heading)
  58.  
  59. # Penalize the reward if the difference is too large
  60. DIRECTION_THRESHOLD = 10.0
  61. if direction_diff > DIRECTION_THRESHOLD:
  62. reward *= -1
  63.  
  64. return float(reward)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement