]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/evaluate.py
Add multiple bases support
[linux-conf-perf.git] / scripts / evaluate.py
1 #!/usr/bin/env python3
2 import os
3 import sys
4
5 import numpy.linalg as nplag
6
7 from conf import conf
8 from conf import sf
9 import utils
10
11 def reduce_matrix_search_for_base_recurse(wset, columns, contains, ignore):
12         bases = []
13         for x in range(0, len(columns)):
14                 if x in contains or x in ignore:
15                         continue
16                 colide = False
17                 for i in range(0, len(wset)):
18                         if wset[i] == 1 and columns[x][i] == 1:
19                                 colide = True
20                                 break
21                 if not colide:
22                         newset = list(wset)
23                         onecount = 0
24                         for i in range(0, len(newset)):
25                                 newset[i] = newset[i] | columns[x][i]
26                                 if (newset[i] == 1):
27                                         onecount += 1
28                         contains.add(x)
29                         if onecount == len(newset):
30                                 bases.append(set(contains))
31                         else:
32                                 rbases = reduce_matrix_search_for_base_recurse(newset, columns, contains, ignore)
33                                 for rbase in rbases:
34                                         if not rbase in bases:
35                                                 bases.append(rbase)
36                         contains.remove(x)
37         return bases
38 0
39 def reduce_matrix_search_for_base(columns):
40         bases = []
41         ignore = []
42         for i in range(0, len(columns)):
43                 wset = list(columns[i])
44                 ignore.append(i)
45                 bases.extend(reduce_matrix_search_for_base_recurse(wset, columns, {i}, ignore))
46         return bases
47
48 def reduce_matrix_remove_symbol(A, symrow, indx):
49         del symrow[indx]
50         for i in range(0, len(A)):
51                 del A[i][indx]
52
53 def reduce_matrix(A, symrow, bases):
54         # Remove fixed symbols
55         i = len(A[0]) - 1
56         while i >= 0:
57                 strue = False
58                 sfalse = False
59                 for y in range(0, len(A)):
60                         if A[y][i] == 0:
61                                 sfalse = True
62                         else:
63                                 strue = True
64                 if (strue and not sfalse) or (sfalse and not strue):
65                         reduce_matrix_remove_symbol(A, symrow, i)
66                 i -= 1
67
68         # Remove duplicate symbols
69         i = len(A[0]) - 1
70         columns = []
71         while i >= 0:
72                 column = []
73                 for y in range(0, len(A)):
74                         column.append(A[y][i])
75                 if column in columns:
76                         reduce_matrix_remove_symbol(A, symrow,  i)
77                 else:
78                         columns.append(column)
79                 i -= 1
80
81         # Search for Bases
82         basesx = reduce_matrix_search_for_base(columns)
83         if bases:
84                 for base in basesx:
85                         bases[0].append(base)
86
87         # Generate new Base
88         if not bases:
89                 for x in range(0, len(A)):
90                         A[x].append(1)
91                 symrow.append(0)
92
93 def collect_data():
94         hashs = {}
95         for fl in os.listdir(sf(conf.result_folder)):
96                 if os.path.isfile(os.path.join(sf(conf.result_folder), fl)):
97                         hashs[fl] = [[], []]
98         try:
99                 hashs.pop('NoConfig')
100         except KeyError:
101                 pass
102
103         with open(sf(conf.config_map_file)) as f:
104                 for line in f:
105                         w = line.rstrip().split(sep=':')
106                         if not w[0] or not w[0] in hashs:
107                                 continue
108                         sol = utils.config_strtoint(w[1], False)
109                         hashs[w[0]][0] = sol
110         
111         for hash, data in hashs.items():
112                 with open(os.path.join(sf(conf.result_folder), hash)) as f:
113                         vec = []
114                         for ln in f:
115                                 vec.append(float(ln))
116                         hashs[hash][1] = vec
117         return hashs
118
119 def build_matrix(hashs):
120         A = []
121         B = []
122         for hash,data in hashs.items():
123                 A.append(data[0])
124                 B.append(data[1])
125         symrow = []
126         for y in range(0, len(A[0])):
127                 symrow.append([abs(A[0][y])])
128         for x in range(0, len(A)):
129                 for y in range(0, len(A[0])):
130                         if A[x][y] < 0:
131                                 A[x][y] = 0
132                         else:
133                                 A[x][y] = 1
134         return A, B, symrow
135
136 def evaluate():
137         print("Collect data...")
138         hashs = collect_data()
139
140         print('Build matrix...')
141         A, B, symrow = build_matrix(hashs)
142
143         # Reduce matrix A
144         print('Simplify matrix...')
145         bases = []
146         reduce_matrix(A, symrow, [bases])
147
148         # Calculate value
149         print('Figuring values...')
150         R = nplag.lstsq(A, B)
151
152         # Print result
153         print('--------------------')
154         utils.build_symbol_map()
155         for i in range(0, len(R[0])):
156                 if symrow[i] == 0:
157                         print("Base", end=' ')
158                 else:
159                         if len(bases) > 0:
160                                 if i in bases[0]:
161                                         print("Base", end=' ')
162                         elif len(bases) > 1:
163                                 for x in range(0, len(bases)):
164                                         if i in bases[x]:
165                                                 print("Base" + x, end=' ')
166                         for s in symrow[i]:
167                                 print(utils.smap[s], end=' ')
168                 print("=", end=' ')
169                 print(str(R[0][i]))
170
171
172 #################################################################################
173
174 if __name__ == '__main__':
175         evaluate()