This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
experimental_script.py
357 lines (263 loc) · 12.9 KB
/
experimental_script.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 1 19:16:22 2019
@author: Yan Yang u6169130
Jiayan Liu u6107041
"""
import os
import time
import matplotlib.pyplot as plt
import numpy as np
import getopt
import sys
def running(conf_files,custom_name,experimental_result,conf_directory,logging):
'''
call running to generate the system log files,
please gives correct directory of the conf files,
also gives the directory where to store the
system log
'''
for conf_file in conf_files:
result = conf_file[:conf_file.index(".")] + "_" + custom_name + ".log"
if result in os.listdir(f"{parent_path}{sep}{experimental_result}"):
os.system(f"rm {experimental_result}{sep}{result}")
os.system(f"python aixi.py -v {conf_directory}{sep}{conf_file} " + logging + result)
def read(file,reward):
'''
read the log file and specify which type of reward to read
if reward is true, then we read reward,
otherwise we read average reward.
'''
cycles = []
average_rewards = []
with open(file) as f:
last_line = None
for line in f.readlines():
line = line.strip()
if "cycle:" in line:
cycles.append(int(line[6:]))
if reward:
average_rewards.append(float(last_line.split(",")[2]))
elif not reward and "average reward:" in line:
average_rewards.append(float(line[15:]))
last_line = line
if len(cycles) != len(average_rewards):
return cycles, average_rewards[:-1]
else:
return cycles, average_rewards
def performance_increase(file,name,reward):
'''
perfomrmance increase graph
'''
cycles,average_rewards = read(file,reward)
num = int(len(cycles)/10)
plt.figure(figsize=(9, 6))
plt.plot(cycles,average_rewards,'-',color = "cyan",lw = 2)
plt.plot(cycles,average_rewards,'D',color = "r", markevery = [i for i in cycles if i%num == 0])
#plt.ylim([0.0, 1.0])
plt.xlabel('Experience (cycles)')
plt.ylabel("average reward of per cycle")
plt.title(f"Learning Scalability of {name}")
plt.show()
def smooth(cycles,average_rewards,num):
'''
calculate time interval based average reward
'''
interval = int(len(cycles)/num)
smoothed_cycles = [cycles[0]]
smoothed_average_rewards = [average_rewards[0]]
index = 1
for epoch in range(num):
avg_reward = np.mean(average_rewards[index:index+interval])
index+=interval
smoothed_cycles.append(index)
smoothed_average_rewards.append(avg_reward)
return smoothed_cycles,smoothed_average_rewards
def interval_performance_increase(file,name,num,reward):
'''
Generate graph about change of reward based on time interval.
'''
cycles,average_rewards = read(file,reward)
smoothed_cycles,smoothed_average_rewards = smooth(cycles,average_rewards,num)
plt.figure(figsize=(9, 6))
plt.plot(smoothed_cycles,smoothed_average_rewards,'-',color = "cyan",lw = 2)
plt.plot(smoothed_cycles,smoothed_average_rewards,'D',color = "r")
#plt.ylim([0.0, 1.0])
plt.xlabel('Experience (cycles)')
plt.ylabel("average reward per cycle")
plt.title(f"Learning Scalability of {name}")
plt.show()
def compare_performance(files,names,num,compare_name,reward):
'''
Generate the graph with based on list of conf files. In order to find out
the performance change with respect to different configurations.
'''
plt.figure(figsize=(9, 6))
for index in range(len(files)):
file = files[index]
name = names[index]
cycles,average_rewards = read(file,reward)
smoothed_cycles,smoothed_average_rewards = smooth(cycles,average_rewards,num)
p = plt.plot(smoothed_cycles,smoothed_average_rewards,'-',lw = 2,label = name)
plt.plot(smoothed_cycles,smoothed_average_rewards,'D',color = p[0].get_color())
#plt.ylim([0.0, 1.0])
plt.legend(loc="lower right")
plt.title(f"Learning Scalability of {compare_name}")
plt.plot()
plt.show()
parent_path = os.getcwd()
sep = "/" if "sep" in parent_path else '''/'''
default_options = {}
default_options["experimental_result"] = "experimental_result"
default_options["conf_directory"] = "experimental_conf"
default_options["custom_name"] = str(time.time())
default_options["interval"] = 50
default_options["type_of_reward"] = "reward"
command_line_options = {}
def main(argv):
'''
experimental_result: The folder to store the experimental result.
The script will create it if there doesn't existes one.
And, all information is stored as log file.
conf_director: The folder contains the different configure files.
Only the file endup with ".conf" will be experimented.
performance_increase_graph: Reading the specifed log file, and generate a single graph about reward.
The format for setting the parameter is < log file >$\text{~}$<'title of the graph'>
interval_performance_increase_graph: Reading the specifed log file,
and generate a single smoothed graph about the average reward.
The interval can be set by using interval parameter.
And, the format for setting the parameter is
< log file >$\text{~}$<'title of the graph'>
compare_performance_graph: By supplying the director, it will reading a list of log file.
Then, generate the graph for them. The format to giving the value is
< where store the log files >$\text{~}$<"['name of labels',]">$\text{~}$<'title of graph'>.
Be careful, the order for the name of labels need to corresponds with the alphabeta order of log files.
custom_name: The log file for different configure files wil be stored as the name of configure file plus the custom_name.
if no custom name is supplied, the current time will be considered as custom name.
interval: In order to smooth the history reward, the interval need to be supplied.
The default value will be 50.
type of reward: Used to indicate whether we should read average reward or reward from the log file.
'''
try:
opts, args = getopt.gnu_getopt(
argv,
'e:c:p:i:v:n:g:t:',
['experimental_result=', 'conf_directory=',
'performance_increase_graph=','interval_performance_increase_graph=',
'compare_performance_graph=','custom_name=','interval=',"type_of_reward",]
)
for opt, arg in opts:
if opt == '--help':
usage()
if opt in ('-e', '--experimental_result'):
command_line_options["experimental_result"] = str(arg)
continue
if opt in ('-c', '--conf_directory'):
command_line_options["conf_directory"] = str(arg)
continue
if opt in ('-p', '--performance_increase_graph'):
command_line_options["performance_increase"] = arg.split("~")
if len(arg.split("~"))!= 2:
raise SystemExit("incorrect input")
continue
if opt in ('-i', '--interval_performance_increase_graph'):
command_line_options["interval_performance_increase"] = arg.split("~")
if len(arg.split("~"))!= 2:
raise SystemExit("incorrect input")
continue
if opt in ('-v', '--compare_performance_graph'):
command_line_options["compare_performance"] = arg.split("~")
if len(arg.split("~"))!= 3:
raise SystemExit("incorrect input")
continue
if opt in ('-n', '--custom_name'):
command_line_options["custom_name"] = str(arg)
continue
if opt in ('-g', '--interval'):
command_line_options["interval"] = int(arg)
continue
if opt in ('-t', '--type_of_reward'):
command_line_options["type_of_reward"] = str(arg)
continue
except getopt.GetoptError as e:
usage()
#please give the directory that contains your conf file
if "conf_directory" in command_line_options:
conf_directory = command_line_options["conf_directory"]
else:
conf_directory = default_options["conf_directory"]
conf_path = parent_path + f"{sep}{conf_directory}"
try:
conf_files = [f for f in os.listdir(conf_path) if f.endswith(".conf")]
except FileNotFoundError:
pass
#where your store ur log information
if "experimental_result" in command_line_options:
experimental_result = command_line_options["experimental_result"]
else:
experimental_result = default_options["experimental_result"]
if "experimental_result" not in os.listdir(parent_path):
os.system(f"mkdir {experimental_result}")
#add different name for different running
#e.g custom_name = "1"
if "custom_name" in command_line_options:
custom_name = command_line_options["custom_name"]
else:
custom_name = default_options["custom_name"]
if "interval" in command_line_options:
interval = command_line_options["interval"]
else:
interval = default_options["interval"]
if "experimental_result" in command_line_options:
logging = f"| tee -a {experimental_result}{sep}"
running(conf_files,custom_name,experimental_result,conf_directory,logging)
if "type_of_reward" in command_line_options:
if command_line_options["type_of_reward"] == default_options["type_of_reward"]:
reward = True
else:
#put in any string to get average reward instead of reward
reward = False
else:
reward = True
if "performance_increase" in command_line_options:
path = command_line_options["performance_increase"][0]
#"experimental_result/coin_flip.log"
name = command_line_options["performance_increase"][1]
#"coin flip"
performance_increase(path,name,reward)
if "interval_performance_increase" in command_line_options and command_line_options["interval_performance_increase"]:
path = command_line_options["interval_performance_increase"][0]
#"experimental_result/coin_flip.log"
name = command_line_options["interval_performance_increase"][1]
#"coin flip"
interval_performance_increase(path,name,interval,reward)
if "compare_performance" in command_line_options and command_line_options["compare_performance"]:
director = command_line_options["compare_performance"][0]
#["experimental_result/coin_flip.log","experimental_result/coin_flip_v1.log"]
paths = sorted([f"{director}{sep}{f}" for f in os.listdir(director) if f.endswith(".log")])
names = eval(command_line_options["compare_performance"][1])
title = str(command_line_options["compare_performance"][2])
#["coin flip","coin flip compare"]
compare_performance(paths,names,interval,f"parameter settings for {title}",reward)
def usage():
message = "Usage: python experimental_script.py [-e | --experimental_result" + os.linesep + \
" [-c | --conf_directory" + os.linesep + \
" [-p | --performance_increase_graph" + os.linesep + \
" [-i | --interval_performance_increase_graph" + os.linesep + \
" [-v | --compare_performance_graph" + os.linesep + \
" [-n | --custom_name" + os.linesep + \
" [-g | --interval" + os.linesep +\
" [-t | --type_of_reward" + os.linesep +\
"Example of Usage" + os.linesep +\
''' python experimental_script.py -c experimental_conf -n test -e experimental_result''' + + os.linesep +\
''' python experimental_script.py -v experimental_result~"['coin flip','coin flip compare']"~'coin flip' -g 10 ''' + os.linesep +\
''' python experimental_script.py -i 'experimental_result/coin_flip.log'~'coin flip' -g 10 ''' + os.linesep +\
''' python experimental_script.py -p experimental_result/coin_flip.log~'coin flip' -t avg ''' + os.linesep +\
" The performance increase function expect 2 inputs, file and tile, please using ~ to split your inputs" + os.linesep +\
" Include any space without using '' may leads a error. " + os.linesep +\
" For the compare performance graph please given the names of label correspond with the alphabeta order of log file" + os.linesep
sys.stderr.write(message)
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])