-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_driver.py
201 lines (171 loc) · 8.7 KB
/
test_driver.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
"""
MIT License from https://github.com/marmotlab/CAtNIPP/
Copyright (c) 2022 MARMot Lab @ NUS-ME
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import copy
import csv
import os
import ray
import torch
import time
from multiprocessing import Pool
import numpy as np
import time
from attention_net import AttentionNet
from runner import Runner
from test_worker import WorkerTest
from test_parameters import *
def run_test():
time0 = time.time()
if not os.path.exists(result_path):
os.makedirs(result_path)
device = torch.device('cuda') if USE_GPU_GLOBAL else torch.device('cpu')
local_device = torch.device('cuda') if USE_GPU else torch.device('cpu')
global_network = AttentionNet(INPUT_DIM, EMBEDDING_DIM).to(device)
if not USE_GPU_GLOBAL:
checkpoint = torch.load(f'{model_path}/checkpoint.pth', map_location=torch.device('cpu'))
else:
checkpoint = torch.load(f'{model_path}/checkpoint.pth')
global_network.load_state_dict(checkpoint['model'])
print(f'Loading model: {FOLDER_NAME}...')
print(f'Total budget range: {BUDGET_RANGE}')
# init meta agents
meta_agents = [RLRunner.remote(i) for i in range(NUM_META_AGENT)]
weights = global_network.to(local_device).state_dict() if device != local_device else global_network.state_dict()
curr_test = 1
metric_name = ['remain_budget', 'success_rate', 'RMSE', 'delta_cov_trace', 'F1Score', 'cov_trace', 'node_utils', 'entropy', 'detection_rate']
perf_metrics = {}
for n in metric_name:
perf_metrics[n] = []
cov_trace_list = []
time_list = []
episode_number_list = []
budget_history = []
obj_history = []
obj2_history = []
try:
while True:
jobList = []
for i, meta_agent in enumerate(meta_agents):
jobList.append(meta_agent.job.remote(weights, curr_test, budget_range=BUDGET_RANGE, sample_length=SAMPLE_LENGTH))
curr_test += 1
done_id, jobList = ray.wait(jobList, num_returns=NUM_META_AGENT)
done_jobs = ray.get(done_id)
for job in done_jobs:
metrics, info = job
episode_number_list.append(info['episode_number'])
cov_trace_list.append(metrics['cov_trace'])
time_list.append(metrics['planning_time'])
for n in metric_name:
perf_metrics[n].append(metrics[n])
budget_history += metrics['budget_history']
obj_history += metrics['obj_history']
obj2_history += metrics['obj2_history']
if curr_test > NUM_TEST:
print('#Test sample:', NUM_SAMPLE_TEST, '|#Total test:', NUM_TEST, '|Budget range:', BUDGET_RANGE, '|Sample size:', SAMPLE_SIZE, '|K size:', K_SIZE)
print('Avg time per test:', (time.time()-time0)/NUM_TEST)
perf_data = []
for n in metric_name:
perf_data.append(np.nanmean(perf_metrics[n]))
for i in range(len(metric_name)):
print(metric_name[i], ':\t', perf_data[i])
idx = np.array(episode_number_list).argsort()
cov_trace_list = np.array(cov_trace_list)[idx]
time_list = np.array(time_list)[idx]
if SAVE_TRAJECTORY_HISTORY:
idx = np.array(budget_history).argsort()
budget_history = np.array(budget_history)[idx]
obj_history = np.array(obj_history)[idx]
obj2_history = np.array(obj2_history)[idx]
break
Budget = int(perf_data[0])+1
if SAVE_CSV_RESULT:
if TRAJECTORY_SAMPLING:
csv_filename = f'result/CSV/Budget_'+str(Budget)+'_ts_'+str(PLAN_STEP)+'_'+str(NUM_SAMPLE_TEST)+'_'+str(SAMPLE_SIZE)+'_'+str(K_SIZE)+'_results.csv'
csv_filename3 = f'result/CSV3/Budget_'+str(Budget)+'_ts_'+str(PLAN_STEP)+'_'+str(NUM_SAMPLE_TEST)+'_'+str(SAMPLE_SIZE)+'_'+str(K_SIZE)+'_planning_time.csv'
else:
csv_filename = f'result/CSV/Budget_'+str(Budget)+'_greedy'+'_'+str(SAMPLE_SIZE)+'_'+str(K_SIZE)+'_results.csv'
csv_filename3 = f'result/CSV3/Budget_'+str(Budget)+'_greedy'+'_'+str(SAMPLE_SIZE)+'_'+str(K_SIZE)+'_planning_time.csv'
csv_data = [cov_trace_list]
csv_data3 = [time_list]
with open(csv_filename, 'a') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(csv_data)
with open(csv_filename3, 'a') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(csv_data3)
if SAVE_TRAJECTORY_HISTORY:
if TRAJECTORY_SAMPLING:
csv_filename2 = f'result/CSV2/Budget_'+str(Budget)+'_ts_'+str(PLAN_STEP)+'_'+str(NUM_SAMPLE_TEST)+'_'+str(SAMPLE_SIZE)+'_'+str(K_SIZE)+'_trajectory_result.csv'
else:
csv_filename2 = f'result/CSV2/Budget_'+str(Budget)+'_greedy_'+'_'+str(SAMPLE_SIZE)+'_'+str(K_SIZE)+'_trajectory_result.csv'
new_file = False if os.path.exists(csv_filename2) else True
field_names = ['budget','obj','obj2']
with open(csv_filename2, 'a') as csvfile:
writer = csv.writer(csvfile)
if new_file:
writer.writerow(field_names)
csv_data = np.concatenate((budget_history.reshape(-1,1), obj_history.reshape(-1,1), obj2_history.reshape(-1,1)), axis=-1)
writer.writerows(csv_data)
except KeyboardInterrupt:
print("CTRL_C pressed. Killing remote workers")
for a in meta_agents:
ray.kill(a)
@ray.remote(num_cpus=8/NUM_META_AGENT, num_gpus=NUM_GPU/NUM_META_AGENT)
class RLRunner(Runner):
def __init__(self, metaAgentID):
super().__init__(metaAgentID)
def singleThreadedJob(self, episodeNumber, budget_range, sample_length):
save_img = True if episodeNumber % SAVE_IMG_GAP == 0 else False
np.random.seed(SEED + 100 * episodeNumber)
worker = WorkerTest(self.metaAgentID, self.localNetwork, episodeNumber, budget_range, SAMPLE_SIZE, sample_length, 15, self.device, save_image=save_img, greedy=False, seed=SEED + 100 * episodeNumber)
worker.work(episodeNumber, 0)
perf_metrics = worker.perf_metrics
return perf_metrics
def multiThreadedJob(self, episodeNumber, budget_range, sample_length):
save_img = True if (SAVE_IMG_GAP != 0 and episodeNumber % SAVE_IMG_GAP == 0) else False
np.random.seed(SEED + 100 * episodeNumber)
worker = WorkerTest(self.metaAgentID, self.localNetwork, episodeNumber, budget_range, sample_length, self.device, save_image=save_img, greedy=False, seed=SEED + 100 * episodeNumber)
subworkers = [copy.deepcopy(worker) for _ in range(NUM_SAMPLE_TEST)]
p = Pool(processes=NUM_SAMPLE_TEST)
results = []
for testID, subw in enumerate(subworkers):
results.append(p.apply_async(subw.work, args=(episodeNumber, testID+1)))
p.close()
p.join()
all_results = []
best_score = np.inf
perf_metrics = None
for res in results:
metric = res.get()
all_results.append(metric)
if metric['cov_trace'] < best_score: # TODO
perf_metrics = metric
best_score = metric['cov_trace']
return perf_metrics
def job(self, global_weights, episodeNumber, budget_range, sample_length=None):
self.set_weights(global_weights)
metrics = self.singleThreadedJob(episodeNumber, budget_range, sample_length)
info = {
"id": self.metaAgentID,
"episode_number": episodeNumber,
}
return metrics, info
if __name__ == '__main__':
ray.init()
run_test()