Skip to content

Commit cdda728

Browse files
Create 1st Model
1 parent 4f94b09 commit cdda728

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

1st Model

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import math
2+
def reward_function(params):
3+
# Example of rewarding the agent to follow center line
4+
5+
# Read input parameters
6+
track_width = params['track_width']
7+
distance_from_center = params['distance_from_center']
8+
9+
abs_steering = abs(params['steering_angle'])
10+
11+
all_wheels_on_track = params['all_wheels_on_track']
12+
speed = params['speed']
13+
14+
waypoints = params['waypoints']
15+
closest_waypoints = params['closest_waypoints']
16+
heading = params['heading']
17+
18+
# Calculate 3 markers that are at varying distances away from the center line
19+
marker_1 = 0.1 * track_width
20+
marker_2 = 0.25 * track_width
21+
marker_3 = 0.5 * track_width
22+
23+
# Give higher reward if the car is closer to center line and vice versa
24+
if distance_from_center <= marker_1:
25+
reward = 1.1
26+
elif distance_from_center <= marker_2:
27+
reward = 0.5
28+
elif distance_from_center <= marker_3:
29+
reward = 0.1
30+
else:
31+
reward = 1e-3 # likely crashed/ close to off track
32+
33+
#steering angle
34+
ABS_STEERING_THRESHOLD = 15
35+
if abs_steering > ABS_STEERING_THRESHOLD:
36+
#penalty
37+
reward *= 0.8
38+
39+
## Set the speed threshold based your action space
40+
SPEED_THRESHOLD = 1.0
41+
42+
if not all_wheels_on_track:
43+
# Penalize if the car goes off track
44+
reward = 1e-3
45+
elif speed < SPEED_THRESHOLD:
46+
# Penalize if the car goes too slow
47+
reward = 0.5
48+
else:
49+
# High reward if the car stays on track and goes fast
50+
reward = 1.2
51+
52+
53+
54+
# Initialize the reward with typical value
55+
reward = 1.0
56+
57+
# Calculate the direction of the center line based on the closest waypoints
58+
next_point = waypoints[closest_waypoints[1]]
59+
prev_point = waypoints[closest_waypoints[0]]
60+
61+
# Calculate the direction in radius, arctan2(dy, dx), the result is (-pi, pi) in radians
62+
track_direction = math.atan2(next_point[1] - prev_point[1], next_point[0] - prev_point[0])
63+
# Convert to degree
64+
track_direction = math.degrees(track_direction)
65+
66+
# Calculate the difference between the track direction and the heading direction of the car
67+
direction_diff = abs(track_direction - heading)
68+
if direction_diff > 180:
69+
direction_diff = 360 - direction_diff
70+
71+
# Penalize the reward if the difference is too large
72+
DIRECTION_THRESHOLD = 10.0
73+
if direction_diff > DIRECTION_THRESHOLD:
74+
reward *= 0.5
75+
76+
return float(reward)

0 commit comments

Comments
 (0)