-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrl_utils.py
executable file
·281 lines (216 loc) · 7.92 KB
/
rl_utils.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
271
272
273
274
275
276
277
278
279
280
281
import math
import numpy as np
import random
import datetime
def read_logs(statespace, mode, hyperstring):
print("Reading Training Logs for Episode Start Point")
try:
file = open("training_logs.txt", "r")
except:
#If that file does not yet exist
file = open("training_logs.txt", "w+")
episode = 1
for line in file:
line.strip("\n")
line_list = line.split("\t")
if line_list[0] == statespace and line_list[1] == mode and line_list[2] == hyperstring:
episode = int(line_list[3]) + 1
return episode
file.close()
def save_logs(statespace, mode, hyperstring, episode):
print("Saving Training Logs")
timestamp = datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
line = statespace + "\t" + mode + "\t" + hyperstring + "\t" + str(episode) + "\t" + timestamp + "\n"
try:
with open("training_logs.txt", "a") as file:
file.write(line)
except:
file = open("training_logs.txt", "w+")
file.write(line)
file.close()
def track_stats(statespace, mode, hyperstring, episode, reward, blind_fraction):
name = "Curves/" + statespace + "_" + mode + "/" + hyperstring + '.csv'
line = str(episode) + "," + str(reward) + ','+ str(blind_fraction) + '\n'
try:
with open(name, "a") as file:
file.write(line)
except:
file = open(name, "w+")
file.write(line)
file.close()
def observe(agent, sun, fuel, width, height):
r = math.sqrt((sun.x - agent.x)**2 + (sun.y - agent.y)**2)
wall_dist = min(agent.x, agent.y, width - agent.x, height - agent.y)
return [round(agent.v_x,3), round(agent.v_y,3), round(r,2), int(wall_dist), agent.m, fuel]
def get_state(observation, statespace):
axis_v_x = 0
axis_v_y = 0
axis_r = 0
axis_wall_dist = 0
axis_m = 0
axis_fuel = 0
if statespace == "Tiny":
def vel_state(v):
if v < 0:
neg = True
else:
neg = False
v = abs(v)
if v < 20:
axis = int((v - (v % 5)) / 5)
if v >= 20 and v < 100:
v -= int(20)
axis = int((v - (v % 40)) / 40) + 4
if v >= 100:
axis = 5
if neg:
axis = 6-1-axis
else:
axis = 6+axis
return axis
axis_v_x = vel_state(observation[0])
axis_v_y = vel_state(observation[1])
if observation[2] >= 2500:
axis_r = 4
else:
axis_r = int((observation[2] - (observation[2] % 500)) / 500)
if observation[3] >= 500:
axis_wall_dist = 4
else:
axis_wall_dist = int((observation[3] - (observation[3] % 100)) / 100)
if observation[4] >= 0.5:
axis_m = 3
else:
val = round(observation[4] - 0.2,1)
# print(val % 0.1)
axis_m = int((val - (val % 0.1)) / 0.1)
if observation[5] >= 200:
axis_fuel = 1
else:
axis_fuel = int((observation[5] - (observation[5] % 100)) / 100)
state = [axis_v_x, axis_v_y, axis_r, axis_wall_dist, axis_m, axis_fuel]
return state
def get_action(q_table, state, epsilon, blind_frames):
action_vals = q_table[state[0], state[1], state[2], state[3], state[4], state[5]]
# Find highest reward action
action = action_vals.argmax()
# Account for space that is blind (all equal rewards)
if action_vals[0] == action_vals[1] == action_vals[2]:
blind_frames += 1
# print("Random")
action = random.randint(0,2)
# Add some degree of randomness based on Epsilon
rand = random.randint(0,100)
if rand <= epsilon*100:
# print("Random")
action = random.randint(0,2)
# print("State: " + str(state) + " Action_Vals: " + str(action_vals) + " Action: " + str(action))
return action, blind_frames
def update_Qtable(q_table, action, reward, pre_action_state, post_action_state,
alpha, gamma):
action_vals_future = q_table[post_action_state[0],
post_action_state[1], post_action_state[2],
post_action_state[3], post_action_state[4],
post_action_state[5]]
Q_s1_a1 = int(max(action_vals_future))
Q_s0_a0 = q_table[pre_action_state[0],
pre_action_state[1], pre_action_state[2],
pre_action_state[3], pre_action_state[4],
pre_action_state[5], action]
learned_val = reward + gamma*(Q_s1_a1)
update_val = (1-alpha) * Q_s0_a0 + alpha * learned_val
#Update
q_table[pre_action_state[0],
pre_action_state[1], pre_action_state[2],
pre_action_state[3], pre_action_state[4],
pre_action_state[5], action] = update_val
return q_table
def init_Qtable(statespace):
'''Considering state space with the following bounds:
v_x , v_y (precision): -20,20
v_x , v_y (large): -100,100
r: 0, 2165
wall_dist: 0, 500
m: 0.2, 0.5
fuel: 0, 200
'''
if statespace == "Large" or statespace == "LargeSmoothed":
'''Considering state space with the following discrete step sizes, bound inclusive:
v_x , v_y (precision): 0.1 40 discrete states
v_x , v_y (large): 5 32 discrete states
Total 432^2 discrete states
r: 5 434 discrete states
wall_dist: 5 101 discrete states
m: 0.1 4 discrete states
fuel: 10 21 discrete states
Total statespace size: 432^2*434*101*4*21 = 690 billion
Actionspace: 3
'''
q_table = np.zeros([433,433,434,101,4,21,3])
if statespace == "Small":
'''Considering state space with the following discrete step sizes, bound inclusive:
v_x , v_y (precision): 1 40 discrete states
v_x , v_y (large): 20 8 discrete states
Total 48^2 discrete states
r: 100 23 discrete states (note that the final state is a smaller range of 2100 - 2165)
wall_dist: 50 11 discrete states
m: 0.1 4 discrete states
fuel: 40 6 discrete states
Total statespace size: 48^2*23*11*4*6= 14 million
Actionspace: 3
'''
q_table = np.zeros([48,48,23,11,4,6,3])
if statespace == "Tiny":
'''Considering state space with the following discrete step sizes, bound inclusive:
v_x , v_y (precision): 5 8 discrete states
v_x , v_y (large): 40 4 discrete states
Total 12^2 discrete states
r: 500 5 discrete states (note that the final state is a smaller range of 2000 - 2165)
wall_dist: 100 4 discrete states
m: 0.1 4 discrete states
fuel: 100 2 discrete states
Total statespace size: 12^2*5*6*4*2= 41,472
Actionspace: 3
'''
q_table = np.zeros([13,13,6,6,4,2,3])
return q_table
def save_Qtable(q_table, statespace, mode, hyperstring):
print("Saving Q Table")
file = "Qtables/" + statespace + "_" + mode + "/" + hyperstring + ".npy"
np.save(file, q_table)
def load_Qtable(statespace, mode, hyperstring):
print("Loading Q-Table")
file = "Qtables/" + statespace + "_" + mode + "/" + hyperstring + ".npy"
return np.load(file)
def smooth_Qtable(q_table):
'''
This function was created to make the statespace seem more continouous.
Why?
Because with a large statespace initialized at zeros, it will take a large amount of time to populate
the Q table with observations, as each Q value will remain at zero until it has been visited, which is highly unlikely.
The alternative is also not ideal: a small statespace. In this case, the Q table is quickly populated, but the response will be jumpy.
The smooth function, at each episode, takes the Q table entries and "bleeds" each Q value into the
directly adjacent unvisited (zero) cells in the matrix.
For example, if after the first episode, my Q table looks like this:
state "Up" "Down" "Stay"
1 0 0 0
2 0 0 0
3 89 12 0
4 0 0 0
5 0 0 0
6 0 34 0
7 0 0 0
and "Smooths" the values into adjacent states
state "Up" "Down" "Stay"
1 0 0 0
2 89 12 0
3 89 12 0
4 89 12 0
5 0 34 0
6 0 34 0
7 0 34 0
The smooth function allows the following:
I can have a large statespace to help make each action more tailored to a narrowly defined state.
I can populate the table more quickly, without actually visiting
'''
return q_table