Skip to content

Instantly share code, notes, and snippets.

@cmar
Created December 5, 2018 13:24
Show Gist options
  • Save cmar/7428e93f29c9a8e9b8de50cc5bb423d0 to your computer and use it in GitHub Desktop.
Save cmar/7428e93f29c9a8e9b8de50cc5bb423d0 to your computer and use it in GitHub Desktop.
Example Deep Racer Reward Function
# example reward function for reinforcement training
def reward_function (on_track, x, y, distance_from_center, car_orientation, progress,
steps, throttle, steering, track_width, waypoints, closest_waypoint):
import math
marker_1 = 0.1 * track_width
marker_2 = 0.25 * track_width
marker_3 = 0.5 * track_width
reward = 1e-3
if distance_from_center >= 0.0 and distance_from_center <= marker_1:
reward = 1
elif distance_from_center <= marker_2:
reward = 0.5
elif distance_from_center <= marker_3:
reward = 0.1
else:
reward = 1e-3 # likely crashed/ close to off track
# penalize reward if the car is steering way too much
ABS_STEERING_THRESHOLD = 0.5
if abs(steering) > ABS_STEERING_THRESHOLD:
reward *= 0.8
# penalize reward for the car taking slow actions
THROTTLE_THRESHOLD = 0.5
if throttle < THROTTLE_THRESHOLD:
reward *= 0.8
# add steering penalty
if abs(steering) > 0.5:
reward *= 0.80
# add throttle penalty
if throttle < 0.5:
reward *= 0.80
# if on a straight
if abs(steering) < 0.1 and throttle > .9:
reward *= 1.2
return float(reward)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment