-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
172 lines (158 loc) · 6.67 KB
/
test.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
import os
from sys import exit
from numpy import isnan, isinf, nanmean, nanstd
from pandas import DataFrame as DF, read_csv, read_excel
os.chdir(os.path.abspath(os.path.dirname(__file__)))#解析进入程序所在目录
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
EOF = (-1)
def readCsvExcel(filepath) -> DF:
if filepath.lower().endswith(".csv"):
return read_csv(filepath)
else:
return read_excel(filepath)
def getArrayDict(folder = ".", resultName = "result.csv") -> dict:
arrayDict = {} # round -> dict
for item in os.listdir(folder):
if os.path.isdir(item) and item.lower().startswith("round_"):
try:
pf = readCsvExcel(os.path.join(os.path.join(folder, item), resultName))
arrayDict[int(item[6:])] = pf
except Exception as e:
print(e)
return arrayDict
def getSchemeIDList(arrayDict) -> list:
schemeIDList = []
for pf in list(arrayDict.values()):
for id in pf["schemeID"]:
if id not in schemeIDList:
schemeIDList.append(id)
return schemeIDList
def getFunctionIDList(arrayDict) -> list:
functionIDList = []
for pf in list(arrayDict.values()):
for id in pf["functionID"]:
if id not in functionIDList:
functionIDList.append(id)
return functionIDList
def removeNanInf(lists) -> list:
for i in range(len(lists) - 1, -1, -1):
if isnan(lists[i]) or isinf(lists[i]):
del lists[i]
return lists
def getTotalInfo(arrayDict, schemeIDList) -> dict:
scheme_best_fitness = {schemeID:[] for schemeID in schemeIDList}
scheme_iteration = {schemeID:[] for schemeID in schemeIDList}
scheme_iteration_time = {schemeID:[] for schemeID in schemeIDList}
scheme_info = {}
for schemeID in schemeIDList:
for pf in list(arrayDict.values()):
scheme_best_fitness[schemeID] += removeNanInf(pf[pf["schemeID"] == schemeID]["best_fitness"].values.tolist())
scheme_iteration[schemeID] += removeNanInf(pf[pf["schemeID"] == schemeID]["iteration"].values.tolist())
scheme_iteration_time[schemeID] += removeNanInf(pf[pf["schemeID"] == schemeID]["iteration_time"].values.tolist())
scheme_info[schemeID] = { \
"best_fitness":{ \
"mean":nanmean(scheme_best_fitness[schemeID]), \
"std":nanstd(scheme_best_fitness[schemeID]), \
"var":nanstd(scheme_best_fitness[schemeID]) ** 2 \
}, \
"iteration":{ \
"mean":nanmean(scheme_iteration[schemeID]), \
"std":nanstd(scheme_iteration[schemeID]), \
"var":nanstd(scheme_iteration[schemeID]) ** 2 \
}, \
"iteration_time":{ \
"mean":nanmean(scheme_iteration_time[schemeID]), \
"std":nanstd(scheme_iteration_time[schemeID]), \
"var":nanstd(scheme_iteration_time[schemeID]) ** 2 \
} \
}
return scheme_info
def getSchemeToFunctionInfo(arrayDict, schemeIDList, functionIDList) -> dict:
scheme_best_fitness = {schemeID:{functionID:[] for functionID in functionIDList} for schemeID in schemeIDList}
scheme_iteration = {schemeID:{functionID:[] for functionID in functionIDList} for schemeID in schemeIDList}
scheme_iteration_time = {schemeID:{functionID:[] for functionID in functionIDList} for schemeID in schemeIDList}
scheme_to_function_info = {}
for schemeID in schemeIDList:
for functionID in functionIDList:
for pf in list(arrayDict.values()):
scheme_best_fitness[schemeID][functionID] += removeNanInf(pf[(pf["schemeID"] == schemeID) & (pf["functionID"] == functionID)]["best_fitness"].values.tolist())
scheme_iteration[schemeID][functionID] += removeNanInf(pf[(pf["schemeID"] == schemeID) & (pf["functionID"] == functionID)]["iteration"].values.tolist())
scheme_iteration_time[schemeID][functionID] += removeNanInf(pf[(pf["schemeID"] == schemeID) & (pf["functionID"] == functionID)]["iteration_time"].values.tolist())
scheme_to_function_info.setdefault(schemeID, {})
scheme_to_function_info[schemeID][functionID] = { \
"best_fitness":( \
{ \
"mean":nanmean(scheme_best_fitness[schemeID][functionID]), \
"std":nanstd(scheme_best_fitness[schemeID][functionID]), \
"var":nanstd(scheme_best_fitness[schemeID][functionID]) ** 2 \
} if scheme_best_fitness[schemeID][functionID] else { \
"mean":float("nan"), \
"std":float("nan"), \
"var":float("nan") \
} \
), \
"iteration":( \
{ \
"mean":nanmean(scheme_iteration[schemeID][functionID]), \
"std":nanstd(scheme_iteration[schemeID][functionID]), \
"var":nanstd(scheme_iteration[schemeID][functionID]) ** 2 \
} if scheme_iteration[schemeID][functionID] else { \
"mean":float("nan"), \
"std":float("nan"), \
"var":float("nan") \
} \
), \
"iteration_time":( \
{ \
"mean":nanmean(scheme_iteration_time[schemeID][functionID]), \
"std":nanstd(scheme_iteration_time[schemeID][functionID]), \
"var":nanstd(scheme_iteration_time[schemeID][functionID]) ** 2 \
} if scheme_iteration_time[schemeID][functionID] else { \
"mean":float("nan"), \
"std":float("nan"), \
"var":float("nan") \
} \
) \
}
return scheme_to_function_info
def dump_r(fp, current_pointer, line, layer = 0) -> None:
if type(current_pointer) == dict:
for key in sorted(list(current_pointer.keys())):
line[layer] = key
dump_r(fp, current_pointer[key], line, layer + 1)
elif type(current_pointer) in (tuple, list, set):
for ele in current_pointer:
dump_r(fp, ele, line, layer + 1)
else: # element
line[layer] = current_pointer
line_to_write = [""] * (max(list(line.keys())) + 1)
for index in list(line.keys()):
line_to_write[index] = str(line[index])
fp.write(",".join(line_to_write))
fp.write("\n")
line.clear()
def dump(dicts, filepath, encoding = "utf-8") -> bool:
line = {}
try:
with open(filepath, "w", encoding = encoding) as f:
dump_r(f, dicts, line)
return True
except Exception as e:
print(e)
return False
def main() -> int:
arrayDict = getArrayDict() # round -> dict
schemeIDList = getSchemeIDList(arrayDict)
print("schemeIDList:", schemeIDList)
functionIDList = getFunctionIDList(arrayDict)
print("functionIDList:", functionIDList)
scheme_info = getTotalInfo(arrayDict, schemeIDList)
print("scheme_info:", scheme_info)
dump(scheme_info, "scheme_info.csv")
scheme_to_function_info = getSchemeToFunctionInfo(arrayDict, schemeIDList, functionIDList)
print("scheme_to_function_info:", scheme_to_function_info)
dump(scheme_to_function_info, "scheme_to_function_info.csv")
return EXIT_SUCCESS
if __name__ == "__main__":
exit(main())