Created
May 11, 2019 02:34
-
-
Save amaarora/0381ce7e17d801483da127c078bbe1af to your computer and use it in GitHub Desktop.
This file contains hidden or 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): | |
''' | |
Example of rewarding the agent to follow center line | |
''' | |
import math | |
# Read input parameters | |
track_width = params['track_width'] | |
distance_from_center = params['distance_from_center'] | |
steering = abs(params['steering_angle']) | |
all_wheels_on_track = params['all_wheels_on_track'] | |
steering_angle = params['steering_angle'] | |
is_left_of_center = params['is_left_of_center'] | |
waypoints = params['waypoints'] | |
closest_waypoints = params['closest_waypoints'] | |
heading = params['heading'] | |
# Calculate 3 markers that are at varying distances away from the center line | |
marker_1 = 0.1 * track_width | |
marker_2 = 0.25 * track_width | |
marker_3 = 0.4 * track_width | |
# Give higher reward if the car is closer to center line and vice versa | |
if distance_from_center <= marker_1: | |
reward = 1.0 | |
elif distance_from_center <= marker_2: | |
reward = 0.75 | |
elif distance_from_center <= marker_3: | |
reward = 0.3 | |
else: | |
reward = 0.1 # likely crashed/ close to off track | |
# Steering penality threshold, change the number based on your action space setting | |
ABS_STEERING_THRESHOLD = 10 | |
# Penalize reward if the agent is steering too much | |
if steering > ABS_STEERING_THRESHOLD: | |
reward *= 0.8 | |
# Give a high reward if no wheels go off the track and | |
# the agent is somewhere in between the track borders | |
distance_from_border = (0.5*track_width) - distance_from_center | |
if all_wheels_on_track and distance_from_border >= 0.15: | |
reward *= 1.2 | |
if not all_wheels_on_track: | |
reward *= -1 | |
# Calculate the direction of the center line based on the closest waypoints | |
next_point = waypoints[closest_waypoints[1]] | |
prev_point = waypoints[closest_waypoints[0]] | |
# Calculate the direction in radius, arctan2(dy, dx), the result is (-pi, pi) in radians | |
track_direction = math.atan2(next_point[1] - prev_point[1], next_point[0] - prev_point[0]) | |
# Convert to degree | |
track_direction = math.degrees(track_direction) | |
# Cacluate the difference between the track direction and the heading direction of the car | |
direction_diff = abs(track_direction - heading) | |
# Penalize the reward if the difference is too large | |
DIRECTION_THRESHOLD = 10.0 | |
if direction_diff > DIRECTION_THRESHOLD: | |
reward *= -1 | |
return float(reward) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment