Skip to content

Instantly share code, notes, and snippets.

@ashishlahoti
Last active December 11, 2020 01:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishlahoti/bf0b8cfeff63ac93a628f628f4d810c0 to your computer and use it in GitHub Desktop.
Save ashishlahoti/bf0b8cfeff63ac93a628f628f4d810c0 to your computer and use it in GitHub Desktop.
def reward_function(params):
'''
Example of rewarding the agent to follow center line
'''
# Read input parameters
track_width = params['track_width']
distance_from_center = params['distance_from_center']
all_wheels_on_track = params['all_wheels_on_track']
speed = params['speed']
SPEED_THRESHOLD = 1.0
# 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.5 * 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.5
elif distance_from_center <= marker_3:
reward = 0.1
else:
reward = 1e-3 # likely crashed/ close to off track
if not all_wheels_on_track:
reward = 1e-3 # Penalize if the car goes off track
elif speed < SPEED_THRESHOLD:
reward *= 0.5 # Penalize if the car goes too slow
return float(reward)
@0emanresuym
Copy link

trying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment