-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathideal2.txt
181 lines (149 loc) · 5.71 KB
/
ideal2.txt
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
Skip to content
Pull requests
Issues
Marketplace
Explore
@Lavkushwaha
1
0
0
TheGeekiestOne/AWS-DeepRacer-League-Udacity
Code
Issues 0
Pull requests 0
Projects 0
Wiki
Security
Insights
AWS-DeepRacer-League-Udacity/Training-models/Little_Goblin-v2.py
@TheGeekiestOne TheGeekiestOne upload model for Sudu track 18050a1 23 days ago
143 lines (117 sloc) 5.2 KB
"""AWS DeepRacer Challenge
LittleGoblin
Created By: Ayran Olckers AKA. The Geekiest One
Last Modified: 05/08/2019
Motivation: To Learn Reinforment Learning through Udacity and AWS
"""
def reward_function(params):
'''
In @params object:
{
"all_wheels_on_track": Boolean, # flag to indicate if the vehicle is on the track
"x": float, # vehicle's x-coordinate in meters
"y": float, # vehicle's y-coordinate in meters
"distance_from_center": float, # distance in meters from the track center
"is_left_of_center": Boolean, # Flag to indicate if the vehicle is on the left side to the track center or not.
"heading": float, # vehicle's yaw in degrees
"progress": float, # percentage of track completed
"steps": int, # number steps completed
"speed": float, # vehicle's speed in meters per second (m/s)
"streering_angle": float, # vehicle's steering angle in degrees
"track_width": float, # width of the track
"waypoints": [[float, float], … ], # list of [x,y] as milestones along the track center
"closest_waypoints": [int, int] # indices of the two nearest waypoints.
}
'''
###############
### Imports ###
###############
import math
#################
### Constants ###
#################
MAX_REWARD = 1e2
MIN_REWARD = 1e-3
DIRECTION_THRESHOLD = 10.0
ABS_STEERING_THRESHOLD = 30
########################
### Input parameters ###
########################
on_track = params['all_wheels_on_track']
distance_from_center = params['distance_from_center']
track_width = params['track_width']
steering = abs(params['steering_angle']) # Only need the absolute steering angle for calculations
speed = params['speed']
waypoints = params['waypoints']
closest_waypoints = params['closest_waypoints']
heading = params['heading']
# negative exponential penalty
reward = math.exp(-6 * distance_from_center)
########################
### Helper functions ###
########################
########################
### Reward functions ###
########################
def on_track_reward(current_reward, on_track):
if not on_track:
current_reward = MIN_REWARD
else:
current_reward = MAX_REWARD
return current_reward
def distance_from_center_reward(current_reward, track_width, distance_from_center):
# Calculate 3 marks that are farther and father 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 car is closer to center line and vice versa
if distance_from_center <= marker_1:
current_reward *= 1.2
elif distance_from_center <= marker_2:
current_reward *= 0.8
elif distance_from_center <= marker_3:
current_reward += 0.5
else:
current_reward = MIN_REWARD # likely crashed/ close to off track
return current_reward
def straight_line_reward(current_reward, steering, speed):
# Positive reward if the car is in a straight line going fast
if abs(steering) < 0.1 and speed > 3:
current_reward *= 1.2
return current_reward
def direction_reward(current_reward, waypoints, closest_waypoints, heading):
'''
Calculate the direction of the center line based on the closest waypoints
'''
next_point = waypoints[closest_waypoints[1]]
prev_point = waypoints[closest_waypoints[0]]
# Calculate the direction in radius, arctan2(dy, dx), the result is (-pi, pi) in radians
direction = math.atan2(next_point[1] - prev_point[1], next_point[0] - prev_point[0])
# Convert to degrees
direction = math.degrees(direction)
# Cacluate difference between track direction and car heading angle
direction_diff = abs(direction - heading)
# Penalize if the difference is too large
if direction_diff > DIRECTION_THRESHOLD:
current_reward *= 0.5
return current_reward
def steering_reward(current_reward, steering):
# Penalize reward if the car is steering too much (your action space will matter)
if abs(steering) > ABS_STEERING_THRESHOLD:
current_reward += 0.8
return current_reward
def throttle_reward(current_reward, speed, steering):
# Decrease throttle while steering
if speed > 2.5 - (0.4 * abs(steering)):
current_reward *= 0.8
return current_reward
########################
### Execute Rewards ###
########################
reward = on_track_reward(reward, on_track)
reward = distance_from_center_reward(reward, track_width, distance_from_center)
reward = straight_line_reward(reward, steering, speed)
reward = direction_reward(reward, waypoints, closest_waypoints, heading)
reward = steering_reward(reward, steering)
reward = throttle_reward(reward, speed, steering)
return float(reward)
© 2019 GitHub, Inc.
Terms
Privacy
Security
Status
Help
Contact GitHub
Pricing
API
Training
Blog
About