-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUCBtest.py
More file actions
385 lines (292 loc) · 10.7 KB
/
UCBtest.py
File metadata and controls
385 lines (292 loc) · 10.7 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""
RNNtest script for 'Resource Optimization for Facial Recognition Systems (ROFARS)' project
author: Jasper Bruin @ UvA-MNS
date: 23/02/2023
"""
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from rofarsEnv import ROFARS_v1
from agents import SlidingWindowUCBAgent, UCBAgent, DiscountedUCBAgent, baselineAgent
import time
def SWUCBExperiment():
np.random.seed(0)
env = ROFARS_v1()
max_window_size = 100
best_window_size = 1
best_reward = -np.inf
window_sizes = []
total_rewards = []
# Find the best sliding window in the training session
for window_size in range(1, max_window_size + 1):
agent = SlidingWindowUCBAgent(window_size=window_size * 60)
agent.initialize(env.n_camera)
# Training loop
env.reset(mode='train')
for t in tqdm(range(env.length), initial=2):
action = agent.get_action()
reward, state, stop = env.step(action)
# Update the UCB Agent
agent.update(action, state)
if stop:
break
total_reward = env.get_total_reward()
print(f'=== TRAINING === window size: {window_size}')
print('[total reward]:', total_reward)
# Save the best window size and total reward
if total_reward > best_reward:
best_reward = total_reward
best_window_size = window_size
# Record the window size and its total reward
window_sizes.append(window_size)
total_rewards.append(total_reward)
# Use the best sliding window for testing
agent = SlidingWindowUCBAgent(window_size=best_window_size * 60)
agent.initialize(env.n_camera)
env.reset(mode='test')
for t in tqdm(range(env.length), initial=2):
action = agent.get_action()
reward, state, stop = env.step(action)
# Update the UCB Agent
agent.update(action, state)
if stop:
break
test_total_reward = env.get_total_reward()
print(f'====== TESTING window size ======')
print('[total reward]:', test_total_reward)
print(f'Best window size: {best_window_size}')
print(f'Best [total reward]: {best_reward}')
# Plot the window size and its total reward
plt.plot(window_sizes, total_rewards,
label=f"Best window size: {best_window_size}, Total reward: {best_reward:.3f}")
plt.xlabel('Window Size', fontsize=12)
plt.ylabel('Total Reward', fontsize=12)
plt.title('Sliding Window UCB: Window Size vs Total Reward', fontsize=14)
plt.legend(fontsize=10)
plt.grid()
plt.tight_layout()
plt.savefig('UCB.png')
plt.show()
def DiscountedUCBExperiment():
np.random.seed(0)
env = ROFARS_v1()
min_gamma = 0.99
max_gamma = 1.0
gamma_step = 0.00025
best_gamma = min_gamma
best_reward = -np.inf
gammas = []
total_rewards = []
# Find the best gamma in the training session
for gamma in np.arange(min_gamma, max_gamma, gamma_step):
agent = DiscountedUCBAgent(gamma=gamma)
agent.initialize(env.n_camera)
# Training loop
env.reset(mode='train')
for t in tqdm(range(env.length), initial=2):
action = agent.get_action()
reward, state, stop = env.step(action)
# Update the UCB Agent
agent.update(action, state)
if stop:
break
total_reward = env.get_total_reward()
print(f'=== TRAINING gamma {gamma} ===')
print('[total reward]:', total_reward)
# Save the best gamma and total reward
if total_reward > best_reward:
best_reward = total_reward
best_gamma = gamma
# Record the gamma and its total reward
gammas.append(gamma)
total_rewards.append(total_reward)
# Use the best gamma for testing
agent = DiscountedUCBAgent(gamma=best_gamma)
agent.initialize(env.n_camera)
env.reset(mode='test')
for t in tqdm(range(env.length), initial=2):
action = agent.get_action()
reward, state, stop = env.step(action)
# Update the UCB Agent
agent.update(action, state)
if stop:
break
test_total_reward = env.get_total_reward()
print(f'====== TESTING gamma ======')
print('[total reward]:', test_total_reward)
print(f'Best gamma: {best_gamma}')
print(f'Best [total reward]: {best_reward}')
# Plot the gamma and its total reward
plt.plot(gammas, total_rewards,
label=f"Best gamma: {best_gamma}, Total reward: {best_reward:.3f}")
plt.xlabel('Gamma', fontsize=12)
plt.ylabel('Total Reward', fontsize=12)
plt.title('Discounted UCB: Gamma vs Total Reward', fontsize=14)
plt.legend(fontsize=10)
plt.grid()
plt.tight_layout()
plt.savefig('DiscountedUCB.png')
plt.show()
def SWUCBOpt(agent_type):
if agent_type == 1:
print("UCB-1")
elif agent_type == 2:
print("SW-UCB")
elif agent_type == 3:
print("D-UCB")
np.random.seed(0)
env = ROFARS_v1()
best_window_size = 0
best_gamma = 0
"""TRAINING"""
if agent_type == 1:
agent = UCBAgent()
elif agent_type == 2:
inp = int(input("Enter the window size: "))
best_window_size = inp * 60
agent = SlidingWindowUCBAgent(window_size=best_window_size)
elif agent_type == 3:
best_gamma = float(input("Enter the gamma: "))
agent = DiscountedUCBAgent(gamma=best_gamma)
agent.initialize(env.n_camera)
# Training loop
env.reset(mode='train')
for t in tqdm(range(env.length), initial=2):
action = agent.get_action()
reward, state, stop = env.step(action)
# Update the UCB Agent
agent.update(action, state)
if stop:
break
total_reward = env.get_total_reward()
print(f'=== TRAINING===')
print('[total reward]:', total_reward)
"""TESTING"""
if agent_type == 1:
agent = UCBAgent()
elif agent_type == 2:
agent = SlidingWindowUCBAgent(window_size=best_window_size)
elif agent_type == 3:
agent = DiscountedUCBAgent(gamma=best_gamma)
agent.initialize(env.n_camera)
env.reset(mode='test')
for t in tqdm(range(env.length), initial=2):
action = agent.get_action()
reward, state, stop = env.step(action)
# Update the UCB Agent
agent.update(action, state)
if stop:
break
print(f'====== TESTING======')
print('[total reward]:', env.get_total_reward())
def timeexperiment(agent_type):
if agent_type == 1:
print("UCB-1")
elif agent_type == 2:
print("SW-UCB")
elif agent_type == 3:
print("D-UCB")
np.random.seed(0)
env = ROFARS_v1()
if agent_type == 1:
agent = UCBAgent()
elif agent_type == 2:
best_window_size = 50 * 60
agent = SlidingWindowUCBAgent(window_size=best_window_size * 60)
elif agent_type == 3:
agent = DiscountedUCBAgent(gamma=0.999)
agent.initialize(env.n_camera)
env.reset(mode='train')
inference_times = []
for t in tqdm(range(env.length), initial=2):
start_time = time.time()
action = agent.get_action()
end_time = time.time()
inference_time = (end_time - start_time) * 1000
inference_times.append(inference_time)
reward, state, stop = env.step(action)
agent.update(action, state)
if stop:
break
total_reward = env.get_total_reward()
print(f'=== TRAINING===')
print('[total reward]:', total_reward)
return np.mean(inference_times)
def robustness_test(agent_type, budget_ratios):
if agent_type == 1:
print("UCB-1")
elif agent_type == 2:
print("SW-UCB")
elif agent_type == 3:
print("D-UCB")
elif agent_type == 4:
print("Simple Baseline")
elif agent_type == 5:
print("Strong Baseline")
np.random.seed(0)
best_window_size = 50 * 60 # Best window size obtained from previous experiments
best_gamma = 0.999 # Best gamma obtained from previous experiments
rewards = []
for budget_ratio in budget_ratios:
env = ROFARS_v1(budget_ratio=budget_ratio)
env.reset(mode='test')
if agent_type == 1:
agent = UCBAgent()
agent.initialize(env.n_camera)
for t in range(env.length):
action = agent.get_action()
reward, state, stop = env.step(action)
agent.update(action, state)
if stop:
break
total_reward = env.get_total_reward()
rewards.append(total_reward)
elif agent_type == 2:
agent = SlidingWindowUCBAgent(window_size=best_window_size)
agent.initialize(env.n_camera)
for t in range(env.length):
action = agent.get_action()
reward, state, stop = env.step(action)
agent.update(action, state)
if stop:
break
total_reward = env.get_total_reward()
rewards.append(total_reward)
elif agent_type == 3:
agent = DiscountedUCBAgent(gamma=best_gamma)
agent.initialize(env.n_camera)
for t in range(env.length):
action = agent.get_action()
reward, state, stop = env.step(action)
agent.update(action, state)
if stop:
break
total_reward = env.get_total_reward()
rewards.append(total_reward)
elif agent_type == 4:
agent = baselineAgent(agent_type='simple', theta=0)
# give random scores as the initial action
init_action = np.random.rand(env.n_camera)
reward, state, stop = env.step(init_action)
for t in tqdm(range(env.length), initial=2):
action = agent.get_action(state)
reward, state, stop = env.step(action)
# do sth to update your algorithm here
if stop:
break
total_reward = env.get_total_reward()
rewards.append(total_reward)
elif agent_type == 5:
agent = baselineAgent(agent_type='strong', theta=0)
# give random scores as the initial action
init_action = np.random.rand(env.n_camera)
reward, state, stop = env.step(init_action)
for t in tqdm(range(env.length), initial=2):
action = agent.get_action(state)
reward, state, stop = env.step(action)
# do sth to update your algorithm here
if stop:
break
total_reward = env.get_total_reward()
rewards.append(total_reward)
return budget_ratios, rewards