-
Notifications
You must be signed in to change notification settings - Fork 6
/
gen_result_tex.py
152 lines (136 loc) · 6.69 KB
/
gen_result_tex.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
#!/usr/bin/env python
# ------------------------------------------------------------------------------------------------------%
# Created by "Bao Hoang" at 21:43, 01/02/2020 %
# %
# Email: [email protected] %
# Homepage: https://www.researchgate.net/profile/Bao_Hoang19 %
# Github: https://github.com/hoangbao123 %
#-------------------------------------------------------------------------------------------------------%
"""
Generate unimodal, multimodal, hybrid, compostion result table
Input: read input from history/overall/algo_dict_info.pkl which is generated after running get_experiment_infor.py
Output: tex().txt in history/generated_labtex_table/
"""
import pickle as pkl
from decimal import Decimal
def load_data():
with open('./history/overall/algo_dict_info.pkl', 'rb') as f:
alf = pkl.load(f)
flist = []
for fi in range(30):
std_rank = []
mean_rank = []
worst_rank = []
best_rank = []
names = set()
for name, algo in alf.items():
names.add(name)
# std_rank = [['abc', 1.23], ['woa', 4.56]]
std_rank.append([name, algo.std[fi]])
mean_rank.append([name, algo.mean[fi]])
worst_rank.append([name, algo.worst[fi]])
best_rank.append([name, algo.best[fi]])
std_rank = sorted(std_rank, key=lambda x: x[1])
mean_rank = sorted(mean_rank, key=lambda x: x[1])
worst_rank = sorted(worst_rank, key=lambda x: x[1])
best_rank = sorted(best_rank, key=lambda x: x[1])
std_infor = {}
mean_infor = {}
worst_infor = {}
best_infor = {}
for si in range(len(std_rank)):
# std_infor = {'abc': {'value': 3, 'rank': 3}, 'bca': {..}}
std_infor[std_rank[si][0]] = {'value': round(std_rank[si][1], 4),
'rank': si + 1}
mean_infor[mean_rank[si][0]] = {'value': round(mean_rank[si][1], 4),
'rank': si + 1}
worst_infor[worst_rank[si][0]] = {'value': round(worst_rank[si][1], 4),
'rank': si + 1}
best_infor[best_rank[si][0]] = {'value': round(best_rank[si][1], 4),
'rank': si + 1}
# flist = [{std: std_infor, 'mean': mean_infor, ...},{std: std_infor,}]
flist.append({'std': std_infor,
'mean': mean_infor,
'worst': worst_infor,
'best': best_infor
})
return flist, names
def gen_latex_table(flist, names, list_fun, font_size='footnotesize'):
with open("tex " + str(list_fun) + '.txt', 'w', encoding='utf-8') as f:
for fun_break in range(len(list_fun) - 1):
f.write("{ \n")
f.write('\\begin{table}[h!] \n')
f.write('\\begin{' + font_size + '}\n')
f.write('\\begin{center} \n')
format_table = ''
for i in range(len(names) + 2):
format_table += 'c'
# \begin{tablular}{|c|c|c|}
f.write(' \\begin{tabular}{' + format_table + '} \n')
# ------------------------------
f.write(' \hline \n')
# Function & Criteria & ABC & PSO & WO
f.write(' Function & Criteria ')
for name in names:
if name == 'ABFOLS':
name = 'ABFO'
f.write(' & ' + name + ' ')
f.write('\\\ \n')
f.write(' \hline \n')
# ---------------------------
# F1 mean 0.3 0.4 0.5
# std 0.6 0.7 0.8
# worst 0.5 0.8 0.9
# fit 0.3 0.5 0.5
# rank 1 2 3
for i in range(list_fun[fun_break], list_fun[fun_break + 1]):
# F1 &
f.write(' \multirow{5}{1em}{F' + str(i + 1) + '}')
# line 0 = F1 mean 0.3 0.3 0.2
# line 1 = std 0.2 1 3
line = 0
# flist[i] = {'std':{'abc': {'value': 3, 'rank': 3}, 'bca': {..}}}
# k = 'std', v = {'abc' : {'value': 3, 'rank': 3}}
for k, v in flist[i].items():
# F & std & v['abc']['value'] & v['woa']['value'] \\
if line == 0:
f.write(' & ' + str(k) + ' ')
else:
f.write(' & ' + str(k) + ' ')
line += 1
for al_name in names:
value = v[al_name]['value']
value = '%.2E' % Decimal(value)
if v[al_name]['rank'] == 1:
str_value = '\\textbf{' + str(value) + '}'
else:
str_value = str(value)
f.write(' & ' + str_value + ' ')
f.write(' \\' + "\\ ") # new line in latex
f.write(' \n ') # write new line in file
f.write(' & rank')
for name in names:
rank = flist[i]['mean'][name]['rank']
if rank == 1:
str_rank = '\\textbf{' + str(rank) + '}'
else:
str_rank = str(rank)
f.write(' & ' + str_rank + ' ')
f.write(' \\' + "\\ ") # new line in latex
f.write(' \n') # write new line in file
f.write(' \hline \n')
f.write(' \end{tabular} \n')
# f.write(' \\' + "\\ \n") # new line in latex
f.write('\end{center} \n')
# f.write(' \\' + "\\ \n") # new line in latex
f.write('\\end{' + font_size + '} \n')
# f.write(' \\' + "\\ \n") # new line in latex
f.write('\\caption{Result and comparison of different algorithms based on several metrics for multimodal functions} \n')
f.write('\\label{tb:} \n')
f.write('\\end{table} \n')
f.write("} \n")
if __name__ == "__main__":
flist, names = load_data()
list_fun = (0, 3, 16, 22, 30)
names = ['GA', 'PSO', 'ABC', 'CRO', 'WOA', 'QSO', 'IQSO']
gen_latex_table(flist, names, list_fun)