-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_reward.py
270 lines (236 loc) · 12.8 KB
/
test_reward.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""
reward function unit tests
"""
import unittest
import math
import numpy
import time
import reward
class TestReward(unittest.TestCase):
""" Test the reward.py module. Hard-coded numeric values here are
dependent upon reward constants not changing
"""
def default_params(self):
return {
"all_wheels_on_track": True, # flag to indicate if the vehicle
# is on the track
"x": 0.0, # vehicle's x-coordinate in meters
"y": 0.0, # vehicle's y-coordinate in meters
"distance_from_center": 0.0, # distance in meters from the track
# center
"is_left_of_center": False, # Flag to indicate if the vehicle is
# on the left side to the track
# center or not.
"is_reversed": False, # Flat to indicate if the vehicle is
# going the wrong direction
"heading": 0, # vehicle's yaw in degrees
"progress": 0, # percentage of track completed
"steps": 0, # number steps completed
"speed": 0.0, # vehicle's speed in meters per
# second (m/s)
"steering_angle": 0.0, # vehicle's steering angle in degrees
"track_width": 0.10, # width of the track
"waypoints": [[0,0],[0,0]], # list of [x,y] as milestones along
# the track center
"closest_waypoints": [0, 1] # indices of the two nearest waypoints
}
def test_percentage_speed(self):
reward.MAX_SPEED = 5.0
self.assertEqual(reward.percentage_speed(1.0), 0.2)
self.assertEqual(reward.percentage_speed(2.0), 0.4)
self.assertEqual(reward.percentage_speed(3.0), 0.6)
self.assertEqual(reward.percentage_speed(4.0), 0.8)
self.assertEqual(reward.percentage_speed(5.0), 1.0)
# be sure to clamp reward if we get unexpectedly high speed inputs
def test_penalize_downshifting(self):
reward.g_last_speed_value = 5.0
self.assertEqual(reward.penalize_downshifting(5.0), 1.0)
reward.g_last_speed_value = 5.0
self.assertEqual(reward.penalize_downshifting(8.0), 1.0)
reward.g_last_speed_value = 5.0
self.assertEqual(reward.penalize_downshifting(2.0), 1e-3)
def test_reward_upshifting(self):
reward.g_last_speed_value = 5.0
self.assertEqual(reward.reward_upshifting(5.0), 0.5)
reward.g_last_speed_value = 5.0
self.assertEqual(reward.reward_upshifting(8.0), 1.0)
reward.g_last_speed_value = 5.0
self.assertEqual(reward.reward_upshifting(2.0), 0.5)
def test_calculate_speed_factor(self):
reward.g_last_speed_value = 5.0
self.assertEqual(reward.calculate_speed_factor({ 'speed': 5.0 }), 0.625)
reward.g_last_speed_value = 5.0
self.assertEqual(reward.calculate_speed_factor({ 'speed': 8.0 }), 1.0)
reward.g_last_speed_value = 5.0
self.assertEqual(reward.calculate_speed_factor({ 'speed': 2.0 }), 0.25)
def test_all_wheels_must_be_on_track(self):
self.assertEqual(reward.all_wheels_must_be_on_track(True), 1.0)
self.assertEqual(reward.all_wheels_must_be_on_track(False), 1e-3)
def test_calculate_heading_factor(self):
params = self.default_params()
self.assertEqual(reward.calculate_heading_factor(params), 1.0)
def test_percentage_steering_angle(self):
self.assertEqual(reward.percentage_steering_angle(0.0), 1.0)
self.assertTrue(math.isclose(reward.percentage_steering_angle(math.radians(15.0)), 0.5))
self.assertTrue(math.isclose(reward.percentage_steering_angle(math.radians(20.0)), 1/3))
self.assertTrue(math.isclose(reward.percentage_steering_angle(math.radians(30.0)), 0.0))
self.assertTrue(math.isclose(reward.percentage_steering_angle(math.radians(-30.0)), 0.0))
self.assertTrue(math.isclose(reward.percentage_steering_angle(math.radians(-20.0)), 1/3))
def test_penalize_steering_change(self):
# Sharper turning
reward.g_last_steering_angle = 0.0
self.assertEqual(reward.penalize_steering_change(10.0, False, True), 1.0)
reward.g_last_steering_angle = 0.0
self.assertEqual(reward.penalize_steering_change(10.0, True, False), 0.0)
reward.g_last_steering_angle = 0.0
self.assertEqual(reward.penalize_steering_change(10.0, True, True), 0.0)
# Straightening
reward.g_last_steering_angle = 10.0
self.assertEqual(reward.penalize_steering_change(0.0, False, True), 0.0)
reward.g_last_steering_angle = 10.0
self.assertEqual(reward.penalize_steering_change(0.0, True, False), 1.0)
reward.g_last_steering_angle = 10.0
self.assertEqual(reward.penalize_steering_change(0.0, True, True), 0.0)
def test_calculate_progress_factor(self):
self.assertEqual(reward.calculate_progress_factor(self.default_params()), 0.0)
def test_percentage_distance_from_track_center(self):
self.assertEqual(reward.percentage_distance_from_track_center(10.0, 0.0), 1.0)
self.assertEqual(reward.percentage_distance_from_track_center(10.0, 1.0), 0.8)
self.assertTrue(math.isclose(reward.percentage_distance_from_track_center(10.0, 4.0), 0.2))
# Check for case where distance is more than track width / 2
self.assertTrue(math.isclose(reward.percentage_distance_from_track_center(10.0, 6.0), 0.0))
def test_look_ahead_heading(self):
waypoints = [
[0.0,0.0],
[0.0,1.0],
[0.0,2.0]
]
reward.look_ahead_heading(waypoints, 0, 0)
def test_apply_weight(self):
self.assertEqual(reward.apply_weight(0.5, 1.0), 0.5)
self.assertEqual(reward.apply_weight(0.25, 0.1), 0.925)
# Test clamping
self.assertEqual(reward.apply_weight(1.5, 1.0), 1.0)
self.assertEqual(reward.apply_weight(1.5, 0.5), 1.0)
self.assertEqual(reward.apply_weight(1.0, 1.5), 1.0)
self.assertEqual(reward.apply_weight(0.5, 1.5), 0.5)
# Test easing (x) => y
self.assertEqual(reward.apply_weight(0.75, 1.0, 'linear'), 0.75)
self.assertEqual(reward.apply_weight(0.5, 1.0, 'linear'), 0.5)
self.assertEqual(reward.apply_weight(0.25, 1.0, 'linear'), 0.25)
self.assertEqual(reward.apply_weight(1.0, 1.0, 'quadratic'), 1.0)
self.assertEqual(reward.apply_weight(0.9, 1.0, 'quadratic'), 0.99)
self.assertEqual(reward.apply_weight(0.75, 1.0, 'quadratic'), 0.9375)
self.assertEqual(reward.apply_weight(0.5, 1.0, 'quadratic'), 0.75)
self.assertEqual(reward.apply_weight(0.25, 1.0, 'quadratic'), 0.4375)
self.assertEqual(reward.apply_weight(0.0, 1.0, 'quadratic'), 0.0)
self.assertEqual(reward.apply_weight(1.0, 1.0, 'cubic'), 1.0)
self.assertEqual(reward.apply_weight(0.9, 1.0, 'cubic'), 0.999)
self.assertEqual(reward.apply_weight(0.75, 1.0, 'cubic'), 0.984375)
self.assertEqual(reward.apply_weight(0.5, 1.0, 'cubic'), 0.875)
self.assertEqual(reward.apply_weight(0.25, 1.0, 'cubic'), 0.578125)
self.assertEqual(reward.apply_weight(0.0, 1.0, 'cubic'), 0.0)
self.assertEqual(reward.apply_weight(0.5, 1.0, 'quartic'), 0.9375)
self.assertEqual(reward.apply_weight(0.5, 1.0, 'quintic'), 0.96875)
self.assertEqual(reward.apply_weight(0.5, 1.0, 'septic'), 0.9921875)
self.assertEqual(reward.apply_weight(0.5, 1.0, 'septic'), 0.9921875)
self.assertEqual(reward.apply_weight(0.4, 1.0, 'nonic'), 0.989922304)
# Identity
self.assertEqual(reward.apply_weight(0.5, 0.0), 1.0)
def test_reward_function(self):
# TODO: mock out the dependent functions to test weighting
params = self.default_params()
params['speed'] = reward.MAX_SPEED
self.assertEqual(reward.reward_function(params), 1.0)
def test_angle_of_vector(self):
vector = [ [0.0, 0.0], [1.0, 0.0] ] # up
self.assertEqual(reward.angle_of_vector(vector), 0)
vector = [ [0.0, 0.0], [1.0, 1.0] ] # diagonal up, right
self.assertEqual(reward.angle_of_vector(vector), 45)
vector = [ [0.0, 0.0], [1.0, -1.0] ] # diagonal up, left
self.assertEqual(reward.angle_of_vector(vector), -45)
vector = [ [0.0, 0.0], [0.0, 1.0] ] # left
self.assertEqual(reward.angle_of_vector(vector), 90)
vector = [ [0.0, 0.0], [0.0, -1.0] ] # right
self.assertEqual(reward.angle_of_vector(vector), -90)
vector = [ [0.0, 0.0], [-1.0, 1.0] ] # diagonal down, right
self.assertEqual(reward.angle_of_vector(vector), 135)
vector = [ [0.0, 0.0], [-1.0, -1.0] ] # diagonal down, left
self.assertEqual(reward.angle_of_vector(vector), -135)
vector = [ [0.0, 0.0], [-1.0, 0.0] ] # down
self.assertEqual(reward.angle_of_vector(vector), 180)
def test_vector_of_angle(self):
""" Test each quadrant """
vector = reward.vector_of_angle(30)
self.assertListEqual(vector[0], [0.0, 0.0])
self.assertTrue(math.isclose(vector[1][0], 0.5))
self.assertTrue(math.isclose(vector[1][1], math.sqrt(3)/2))
vector = reward.vector_of_angle(-45)
self.assertListEqual(vector[0], [0.0, 0.0])
self.assertTrue(math.isclose(vector[1][0], -math.sqrt(2)/2))
self.assertTrue(math.isclose(vector[1][1], math.sqrt(2)/2))
vector = reward.vector_of_angle(-120)
self.assertListEqual(vector[0], [0.0, 0.0])
self.assertTrue(math.isclose(vector[1][0], -math.sqrt(3)/2))
self.assertTrue(math.isclose(vector[1][1], -0.5))
vector = reward.vector_of_angle(180)
self.assertListEqual(vector[0], [0.0, 0.0])
self.assertTrue(math.isclose(vector[1][0], 0.0, abs_tol=1e-9))
self.assertTrue(math.isclose(vector[1][1], -1.0))
def test_progress_over_time(self):
reward.g_last_progress_value = 20.0
reward.g_last_progress_time = time.time() - 0.067 # 1 frame @ 15fps
# For 20s lap, 20*15 frames = 300 frames => progress 100/300 = 0.33
progress = reward.progress_over_time(20.3)
# expect (21 - 20) / 0.067
self.assertTrue(math.isclose(progress, 4.4771, abs_tol=1e-2))
def test_progress_since_last(self):
reward.g_last_progress_value = 0
factor = reward.progress_since_last(10)
self.assertEqual(factor, 0.1)
def test_progress_target(self):
reward.g_last_progress_value = 0
reward.TARGET_NUMBER_STEPS = 100
self.assertEqual(reward.progress_target(0.05), 1.0)
def test_speed_or_acceleration(self):
reward.g_last_speed_value = 0.0
self.assertEqual(reward.speed_or_acceleration(0.2), 1.0)
def test_calculate_race_line_factor(self):
# Test horizontal straightaways
params = self.default_params()
params['x'] = reward.OVAL_TRACK_RACE_LINE[0][0]
params['y'] = reward.OVAL_TRACK_RACE_LINE[0][1]
self.assertEqual(reward.calculate_race_line_factor(params), 1.0)
# racing line point 10 is close to track centerline on the bottom
params['x'] = reward.OVAL_TRACK_RACE_LINE[9][0]
params['y'] = reward.OVAL_TRACK_RACE_LINE[9][1] - params['track_width'] # 0.1
self.assertTrue(math.isclose(reward.calculate_race_line_factor(params), 0.0, abs_tol=1e-7))
params['x'] = reward.OVAL_TRACK_RACE_LINE[9][0]
params['y'] = reward.OVAL_TRACK_RACE_LINE[9][1] - params['track_width'] / 2.0
self.assertTrue(math.isclose(reward.calculate_race_line_factor(params), 0.5, abs_tol=1e-7))
# Test vertical straightaways
params = self.default_params()
params['x'] = reward.OVAL_TRACK_RACE_LINE[35][0]
params['y'] = reward.OVAL_TRACK_RACE_LINE[35][1]
self.assertEqual(reward.calculate_race_line_factor(params), 1.0)
# racing line point 10 is close to track centerline on the bottom
params['x'] = reward.OVAL_TRACK_RACE_LINE[35][0] + params['track_width']
params['y'] = reward.OVAL_TRACK_RACE_LINE[35][1]
self.assertTrue(math.isclose(reward.calculate_race_line_factor(params), 0.0, abs_tol=1e-2))
params['x'] = reward.OVAL_TRACK_RACE_LINE[35][0] + params['track_width'] / 2.0
params['y'] = reward.OVAL_TRACK_RACE_LINE[35][1]
self.assertTrue(math.isclose(reward.calculate_race_line_factor(params), 0.5, abs_tol=1e-2))
# Test corner (near 45deg)
params = self.default_params()
params['x'] = reward.OVAL_TRACK_RACE_LINE[28][0]
params['y'] = reward.OVAL_TRACK_RACE_LINE[28][1]
self.assertEqual(reward.calculate_race_line_factor(params), 1.0)
# racing line point 10 is close to track centerline on the bottom
params['x'] = reward.OVAL_TRACK_RACE_LINE[28][0] + (params['track_width'] / math.sqrt(2))
params['y'] = reward.OVAL_TRACK_RACE_LINE[28][1] - (params['track_width'] / math.sqrt(2))
self.assertTrue(math.isclose(reward.calculate_race_line_factor(params), 0.0, abs_tol=1e-2))
params['x'] = reward.OVAL_TRACK_RACE_LINE[28][0] + (params['track_width'] / math.sqrt(2)) / 2.0
params['y'] = reward.OVAL_TRACK_RACE_LINE[28][1] - (params['track_width'] / math.sqrt(2)) / 2.0
self.assertTrue(math.isclose(reward.calculate_race_line_factor(params), 0.5, abs_tol=1e-2))
if __name__ == '__main__':
unittest.main()