Improve this reward function for an aws deepracer robot on the re:Invent 2018 - Counterclockwise track import math
def reward_function(params):
track_width = params['track_width']
distance_from_center = params['distance_from_center']
speed = params['speed']
all_wheels_on_track = params['all_wheels_on_track']
steering = abs(params['steering_angle']) # use the absolute value of steering angle
code
# Define markers that are at varying distances from the center
marker_1 = 0.1 * track_width
marker_2 = 0.25 * track_width
# Give higher reward if the car is closer to center line
if distance_from_center <= marker_1:
reward = 1.0
elif distance_from_center <= marker_2:
reward = 0.5
else:
reward = 0.1 # likely close to the track boundary
# Penalize if the car goes off track
if not all_wheels_on_track:
reward = 0.01
speed_threshold=1.75
# Reward higher speeds up to a threshold
speed_reward = speed / speed_threshold if speed <= speed_threshold else 1.0
reward *= speed_reward
# Encourage the car to minimize the steering changes; this reduces zig-zag movement
STEERING_THRESHOLD = 20.0
if steering > STEERING_THRESHOLD:
reward *= 0.75
return float(reward)
Assistant
To enhance the effectiveness of your reward function for the AWS DeepRacer navigating the re:Invent 2018 - Counterclockwise track, we will focus on promoting smoother driving at an optimal speed, while staying close to the track center, with refined steering control. Here’s an updated version with modifications to subtly adjust the behavior according to these aspects, and further encouraging consistent, fast laps:
python
import math
def reward_function(params):
track_width = params['track_width']
distance_from_center = params['distance_from_center']
speed = params['speed']
all_wheels_on_track = params['all_wheels_on_track']
steering = abs(params['steering_angle']) # use the absolute value of steering angle
# Initialize the reward with a small number to not to give 0
reward = 1e-3
# Define thresholds
marker_1 = 0.1 * track_width
marker_2 = 0.25 * track_width
speed_threshold = 2.0 # Raise the speed threshold
steering_threshold = 15.0 # steering angle threshold to start penalizing
# Give higher reward if the car is closer to the center line
if distance_from_center <= marker_1:
reward += 1.0 # Near the center gives the higher reward
elif distance_from_center <= marker_2:
reward += 0.5 # Still on track but a little far from center
else:
reward += 0.1 # Likely close to the track boundary
# Give a penalty if any wheels are off the track
if not all_wheels_on_track:
reward *= 0.01 # Major penalty for leaving the track
# Reward for speed: adding a bonus for high speeds proportional to the current speed/speed threshold
if speed > speed_threshold:
reward += (speed / speed_threshold)
else:
reward += (speed / (2 * speed_threshold))
# Penalty for high steering angles to ensure smoother turns
if steering > steering_threshold:
reward *= 0.8 # Apply larger penalties if the steering angle is too high
elif steering > (steering_threshold / 2):
reward *= 0.9 # Smaller penalty for moderate steering angles
# Business logic for additional factors like the progression along the track
# and performance in curves could also be added here to fine-tune the rewards further.
return float(reward)
Key Enhancements:
Reduced Sensitivity for Smaller Distances: More nuanced rewards closer to track center with distinct zones.
Increased Optimal Speed Threshold: Encourages the agent to maintain a higher speed when possible, with additional rewards past a certain optimal point.
Steering Penalty Intervals: Introduces graduated penalties for increases in steering angle, encouraging smoother driving.
Reward Rescaling: Proportional bonus for high speeds to balance speed and staying on track.
This approach balances the need to stay on the optimal track while promoting higher speeds and ensuring the driving style is smooth. The penalties and incentives align with the overall objective of faster, smoother laps.