-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreward_updated.py
48 lines (39 loc) · 1.71 KB
/
reward_updated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def reward_function(params):
'''
Example of penalize steering, which helps mitigate zig-zag behaviors
'''
# Read input parameters
distance_from_center = params['distance_from_center']
track_width = params['track_width']
steering = abs(params['steering_angle']) # Only need the absolute steering angle
progress = abs(params['progress'])
waypoints = params['waypoints']
# 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 agent is closer to center line and vice versa
# future improvement: add an exception to the rule where the car can reach near the edge
# at certain waypoints (corners) but reduces speed so that it doesn't cross border
if 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 for getting off centre during big turns
for i, point in enumerate(waypoints):
waypoint = (point[2], point[3])
# Steering penality threshold, change the number based on your action space setting
ABS_STEERING_THRESHOLD = 15
# Penalize reward if the agent is steering too much
if steering > ABS_STEERING_THRESHOLD:
reward *= 0.5
# reward for finishing the track
if progress == 100:
reward = 1000
# print waypoints for local plotting
print(*waypoints, sep=',')
return float(reward)