Last active
March 14, 2023 06:51
-
-
Save JimboFromLimbo/f283a68374f4479831ec254f781dd1b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reward_function(params): | |
# Read input parameters | |
all_wheels_on_track = params['all_wheels_on_track'] | |
distance_from_center = params['distance_from_center'] | |
track_width = params['track_width'] | |
progress = params['progress'] | |
waypoints = params['waypoints'] | |
closest_waypoints = params['closest_waypoints'] | |
# Give a very low reward by default | |
reward = 1e-3 | |
if all_wheels_on_track: | |
# Reward for making progress around the track | |
reward = progress | |
# Calculate the direction to the next waypoint | |
next_waypoint = waypoints[closest_waypoints[1]] | |
prev_waypoint = waypoints[closest_waypoints[0]] | |
track_direction = math.atan2(next_waypoint[1] - prev_waypoint[1], next_waypoint[0] - prev_waypoint[0]) | |
# Calculate the direction of the car | |
direction = params['heading'] | |
# Calculate the angle between the track direction and the car direction | |
direction_diff = abs(track_direction - direction) | |
# Penalize for deviating from the shortest route | |
if direction_diff > math.pi / 2: | |
reward *= 0.8 | |
# Reward for staying near the center line | |
reward -= distance_from_center / (track_width / 2) | |
# Adjust speed based on the track geometry | |
speed = params['speed'] | |
straight_ahead = abs(direction_diff) < math.pi / 4 | |
if straight_ahead: | |
# Speed up in straight sections | |
reward += 0.2 * (speed / 5.0) | |
else: | |
# Slow down before corners | |
reward -= 0.2 * (speed / 5.0) | |
return reward |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment