|
| 1 | +import math |
| 2 | +def reward_function(params): |
| 3 | + # Example of rewarding the agent to follow center line |
| 4 | + |
| 5 | + # Read input parameters |
| 6 | + |
| 7 | + track_width = params['track_width'] |
| 8 | + distance_from_center = params['distance_from_center'] |
| 9 | + |
| 10 | + abs_steering = abs(params['steering_angle']) |
| 11 | + |
| 12 | + all_wheels_on_track = params['all_wheels_on_track'] |
| 13 | + speed = params['speed'] |
| 14 | + |
| 15 | + waypoints = params['waypoints'] |
| 16 | + closest_waypoints = params['closest_waypoints'] |
| 17 | + heading = params['heading'] |
| 18 | + |
| 19 | + # Calculate 3 markers that are at varying distances away from the center line |
| 20 | + #Distance from centre |
| 21 | + |
| 22 | + marker_1 = 0.1 * track_width |
| 23 | + marker_2 = 0.25 * track_width |
| 24 | + marker_3 = 0.5 * track_width |
| 25 | + |
| 26 | + # Give higher reward if the car is closer to center line and vice versa |
| 27 | + if distance_from_center <= marker_1: |
| 28 | + reward = 1.1 |
| 29 | + elif distance_from_center <= marker_2: |
| 30 | + reward = 0.5 |
| 31 | + elif distance_from_center <= marker_3: |
| 32 | + reward = 0.1 |
| 33 | + else: |
| 34 | + reward = 1e-3 # likely crashed/ close to off track |
| 35 | + |
| 36 | + #steering angle |
| 37 | + ABS_STEERING_THRESHOLD = 7 |
| 38 | + if abs_steering > ABS_STEERING_THRESHOLD: |
| 39 | + #penalty |
| 40 | + reward *= 0.8 |
| 41 | + |
| 42 | + ## Set the speed threshold based your action space |
| 43 | + SPEED_THRESHOLD = 1.0 |
| 44 | + |
| 45 | + if not all_wheels_on_track: |
| 46 | + # Penalize if the car goes off track |
| 47 | + reward = 1e-3 |
| 48 | + elif speed < SPEED_THRESHOLD: |
| 49 | + # Penalize if the car goes too slow |
| 50 | + reward = 0.7 |
| 51 | + else: |
| 52 | + # High reward if the car stays on track and goes fast |
| 53 | + reward = 1.6 |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +# Initialize the reward with typical value |
| 58 | + reward = 1.0 |
| 59 | + |
| 60 | + # Calculate the direction of the center line based on the closest waypoints |
| 61 | + next_point = waypoints[closest_waypoints[1]] |
| 62 | + prev_point = waypoints[closest_waypoints[0]] |
| 63 | + |
| 64 | + # Calculate the direction in radius, arctan2(dy, dx), the result is (-pi, pi) in radians |
| 65 | + track_direction = math.atan2(next_point[1] - prev_point[1], next_point[0] - prev_point[0]) |
| 66 | + # Convert to degree |
| 67 | + track_direction = math.degrees(track_direction) |
| 68 | + |
| 69 | + # Calculate the difference between the track direction and the heading direction of the car |
| 70 | + direction_diff = abs(track_direction - heading) |
| 71 | + if direction_diff > 180: |
| 72 | + direction_diff = 360 - direction_diff |
| 73 | + |
| 74 | + # Penalize the reward if the difference is too large |
| 75 | + DIRECTION_THRESHOLD = 5.0 |
| 76 | + if direction_diff > DIRECTION_THRESHOLD: |
| 77 | + reward *= 0.5 |
| 78 | +## Calculate the distance from each border |
| 79 | + distance_from_border = 0.5 * track_width - distance_from_center |
| 80 | + |
| 81 | + # Reward higher if the car stays inside the track borders |
| 82 | + if distance_from_border >= 0.2: |
| 83 | + reward = 1.5 |
| 84 | + else: |
| 85 | + reward = 1e-3 # Low reward if too close to the border or goes off the track |
| 86 | + |
| 87 | + return float(reward) |
| 88 | + |
0 commit comments