-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathget_experiment_infor.py
71 lines (63 loc) · 2.82 KB
/
get_experiment_infor.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
#!/usr/bin/env python
# ------------------------------------------------------------------------------------------------------%
# Created by "Bao Hoang" at 21:43, 01/02/2020 %
# %
# Email: hoangnghiabao96@gmail.com %
# Homepage: https://www.researchgate.net/profile/Bao_Hoang19 %
# Github: https://github.com/hoangbao123 %
#-------------------------------------------------------------------------------------------------------%
"""
Get all information (best fit of 15 runtimes and losses for the best run) of all algorithms.
Save all information in overall/algo_dict_info.pkl
"""
import pickle as pkl
import numpy as np
from utils.FunctionUtil import cal_mean, cal_std
from utils.class_utils import AlgoInfor
algos = ['GA', 'PSO', 'ABFOLS', 'CRO', 'ABC', 'WOA', 'QSO', 'IQSO']
path_loss = './history/loss/'
path_best_fit = './history/best_fit/'
algo_dict = {}
# iterate over all algorithms
for name in algos:
al = AlgoInfor()
al.name = name
print(name)
# iterate over 30 benmark functions
for i in range(1, 31):
function_name = 'F' + str(i)
name_file = name + "_" + function_name
loss_file = name_file + '_loss.pkl'
best_fit_file = name_file + '_best_fit.pkl'
path_file_loss = path_loss + loss_file
path_file_best_fit = path_best_fit + best_fit_file
with open(path_file_loss, 'rb') as f:
loss = pkl.load(f)
with open(path_file_best_fit, 'rb') as f:
best_fit = pkl.load(f)
if name == 'PSO':
# PSO returns matrix form of loss and best fit
loss = np.reshape(np.array(loss), -1)
best_fit = np.reshape(np.array(best_fit), -1)
elif name == 'ABFOLS':
# ABFOLS return inversed value of fitness and matrix form of loss
best_fit = 1 / np.array(best_fit)
loss = np.array(loss)[:, 0]
# cal std, mean , worst, best of 15 run times
std = cal_std(best_fit, i*100)
mean = cal_mean(best_fit, i*100)
worst = max(best_fit)
best = min(best_fit)
al.std.append(std)
al.mean.append(mean)
al.best.append(best)
al.worst.append(worst)
al.loss.append(np.array(loss))
al.best_fit.append(np.array(best_fit))
algo_dict[name] = al
# save infor as pickle file
with open('./history/overall/algo_dict_info.pkl', 'wb') as f:
pkl.dump(algo_dict, f, pkl.HIGHEST_PROTOCOL)
with open('./history/overall/algo_dict_info.pkl', 'rb') as f:
alf = pkl.load(f)
print(alf['PSO'].name)