forked from falktan/deepracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreward_function.py
221 lines (150 loc) · 5.57 KB
/
reward_function.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import math
def dist(point1, point2):
return ((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2) ** 0.5
# thanks to https://stackoverflow.com/questions/20924085/python-conversion-between-coordinates
def rect(r, theta):
"""
theta in degrees
returns tuple; (float, float); (x,y)
"""
x = r * math.cos(math.radians(theta))
y = r * math.sin(math.radians(theta))
return x, y
# thanks to https://stackoverflow.com/questions/20924085/python-conversion-between-coordinates
def polar(x, y):
"""
returns r, theta(degrees)
"""
r = (x ** 2 + y ** 2) ** .5
theta = math.degrees(math.atan2(y,x))
return r, theta
def angle_mod_360(angle):
"""
Maps an angle to the interval -180, +180.
Examples:
angle_mod_360(362) == 2
angle_mod_360(270) == -90
:param angle: angle in degree
:return: angle in degree. Between -180 and +180
"""
n = math.floor(angle/360.0)
angle_between_0_and_360 = angle - n*360.0
if angle_between_0_and_360 <= 180.0:
return angle_between_0_and_360
else:
return angle_between_0_and_360 - 360
def get_waypoints_ordered_in_driving_direction(params):
# waypoints are always provided in counter clock wise order
if params['is_reversed']: # driving clock wise.
return list(reversed(params['waypoints']))
else: # driving counter clock wise.
return params['waypoints']
def up_sample(waypoints, factor):
"""
Adds extra waypoints in between provided waypoints
:param waypoints:
:param factor: integer. E.g. 3 means that the resulting list has 3 times as many points.
:return:
"""
p = waypoints
n = len(p)
return [[i / factor * p[(j+1) % n][0] + (1 - i / factor) * p[j][0],
i / factor * p[(j+1) % n][1] + (1 - i / factor) * p[j][1]] for j in range(n) for i in range(factor)]
def get_target_point(params):
waypoints = up_sample(get_waypoints_ordered_in_driving_direction(params), 20)
car = [params['x'], params['y']]
distances = [dist(p, car) for p in waypoints]
min_dist = min(distances)
i_closest = distances.index(min_dist)
n = len(waypoints)
waypoints_starting_with_closest = [waypoints[(i+i_closest) % n] for i in range(n)]
r = params['track_width'] * 0.9
is_inside = [dist(p, car) < r for p in waypoints_starting_with_closest]
i_first_outside = is_inside.index(False)
if i_first_outside < 0: # this can only happen if we choose r as big as the entire track
return waypoints[i_closest]
return waypoints_starting_with_closest[i_first_outside]
def get_target_steering_degree(params):
tx, ty = get_target_point(params)
car_x = params['x']
car_y = params['y']
dx = tx-car_x
dy = ty-car_y
heading = params['heading']
_, target_angle = polar(dx, dy)
steering_angle = target_angle - heading
return angle_mod_360(steering_angle)
def score_steer_to_point_ahead(params):
best_stearing_angle = get_target_steering_degree(params)
steering_angle = params['steering_angle']
error = (steering_angle - best_stearing_angle) / 60.0 # 60 degree is already really bad
score = 1.0 - abs(error)
return max(score, 0.01) # optimizer is rumored to struggle with negative numbers and numbers too close to zero
def reward_function(params):
return float(score_steer_to_point_ahead(params))
def get_test_params():
return {
'x': 0.7,
'y': 1.05,
'heading': 160.0,
'track_width': 0.45,
'is_reversed': False,
'steering_angle': 0.0,
'waypoints': [
[0.75, -0.7],
[1.0, 0.0],
[0.7, 0.52],
[0.58, 0.7],
[0.48, 0.8],
[0.15, 0.95],
[-0.1, 1.0],
[-0.7, 0.75],
[-0.9, 0.25],
[-0.9, -0.55],
]
}
def test_reward():
params = get_test_params()
reward = reward_function(params)
print("test_reward: {}".format(reward))
assert reward > 0.0
def test_get_target_point():
result = get_target_point(get_test_params())
expected = [0.33, 0.86]
eps = 0.1
print("get_target_point: x={}, y={}".format(result[0], result[1]))
assert dist(result, expected) < eps
def test_get_target_steering():
result = get_target_steering_degree(get_test_params())
expected = 46
eps = 1.0
print("get_target_steering={}".format(result))
assert abs(result - expected) < eps
def test_angle_mod_360():
eps = 0.001
assert abs(-90 - angle_mod_360(270.0)) < eps
assert abs(-179 - angle_mod_360(181)) < eps
assert abs(0.01 - angle_mod_360(360.01)) < eps
assert abs(5 - angle_mod_360(365.0)) < eps
assert abs(-2 - angle_mod_360(-722)) < eps
def test_upsample():
params = get_test_params()
print(repr(up_sample(params['waypoints'], 2)))
def test_score_steer_to_point_ahead():
params_l_45 = {**get_test_params(), 'steering_angle': +45}
params_l_15 = {**get_test_params(), 'steering_angle': +15}
params_0 = {**get_test_params(), 'steering_angle': 0.0}
params_r_15 = {**get_test_params(), 'steering_angle': -15}
params_r_45 = {**get_test_params(), 'steering_angle': -45}
sc = score_steer_to_point_ahead
# 0.828, 0.328, 0.078, 0.01, 0.01
print("Scores: {}, {}, {}, {}, {}".format(sc(params_l_45), sc(params_l_15), sc(params_0), sc(params_r_15), sc(params_r_45)))
def run_tests():
test_angle_mod_360()
test_reward()
test_upsample()
test_get_target_point()
test_get_target_steering()
test_score_steer_to_point_ahead()
print("All tests successful")
# run_tests()