From 599690760476bf7c9c2be226c40cc70c813aa60d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sat, 16 May 2015 14:32:32 +0200 Subject: Implement evaluate --- conf.py | 1 + scripts/boot.py | 2 +- scripts/evaluate.py | 115 +++++++++++++++++++++++++++++++++++++++++ scripts/parse_kconfig/output.c | 6 ++- scripts/parse_kconfig/output.h | 3 +- scripts/parse_kconfig/parse.c | 2 +- scripts/solution.py | 2 +- scripts/utils.py | 20 +++++-- 8 files changed, 141 insertions(+), 10 deletions(-) create mode 100755 scripts/evaluate.py diff --git a/conf.py b/conf.py index f05a674..8700111 100644 --- a/conf.py +++ b/conf.py @@ -69,6 +69,7 @@ iteration_file = build_folder + 'iteration' output_confs = build_folder + 'output_confs' output_folder = 'output/' +result_folder = 'result/' log_folder = 'log/' diff --git a/scripts/boot.py b/scripts/boot.py index d03504a..a055998 100644 --- a/scripts/boot.py +++ b/scripts/boot.py @@ -22,7 +22,7 @@ def boot(): sprc = subprocess.Popen(conf.boot_command, stdout = subprocess.PIPE) - with open(os.path.join(sf(conf.output_folder), utils.get_last_configuration()), "w") as f: + with open(os.path.join(sf(conf.output_folder), utils.get_last_configuration()), "a") as f: for linen in sprc.stdout: line = linen.decode('utf-8') if conf.boot_output: diff --git a/scripts/evaluate.py b/scripts/evaluate.py new file mode 100755 index 0000000..436879d --- /dev/null +++ b/scripts/evaluate.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python3 +import os +import sys + +import numpy.linalg as nplag + +from conf import conf +from conf import sf +import utils + +def reduce_matrix_remove_symbol(A, symrow, indx): + del symrow[indx] + for i in range(0, len(A)): + del A[i][indx] + +def reduce_matrix(A, symrow): + # Remove fixed symbols + i = len(A[0]) - 1 + while i >= 0: + strue = False + sfalse = False + for y in range(0, len(A)): + if A[y][i] == 0: + sfalse = True + else: + strue = True + if (strue and not sfalse) or (sfalse and not strue): + reduce_matrix_remove_symbol(A, symrow, i) + i -= 1 + + # Remove duplicate symbols + i = len(A[0]) - 1 + columns = [] + while i >= 0: + column = [] + for y in range(0, len(A)): + column.append(A[y][i]) + if column in columns: + reduce_matrix_remove_symbol(A, symrow, i) + else: + columns.append(column) + i -= 1 + + +def evaluate(): + print("Collect data...") + hashs = {} + for fl in os.listdir(sf(conf.result_folder)): + if os.path.isfile(os.path.join(sf(conf.result_folder), fl)): + hashs[fl] = [[], []] + try: + hashs.pop('NoConfig') + except KeyError: + pass + + with open(sf(conf.config_map_file)) as f: + for line in f: + w = line.rstrip().split(sep=':') + if not w[0] or not w[0] in hashs: + continue + sol = utils.config_strtoint(w[1], False) + hashs[w[0]][0] = sol + + for hash, data in hashs.items(): + with open(os.path.join(sf(conf.result_folder), hash)) as f: + vec = [] + for ln in f: + vec.append(float(ln)) + hashs[hash][1] = vec + + print('Build matrix...') + A = [] + B = [] + for hash,data in hashs.items(): + A.append(data[0]) + B.append(data[1]) + symrow = [] + for y in range(0, len(A[0])): + symrow.append([abs(A[0][y])]) + for x in range(0, len(A)): + for y in range(0, len(A[0])): + if A[x][y] < 0: + A[x][y] = 0 + else: + A[x][y] = 1 + + # Reduce matrix A + print('Simplify matrix...') + reduce_matrix(A, symrow) + + for x in range(0, len(A)): + A[x].append(1) + symrow.append(0) + + # Calculate value + print('Figuring values...') + R = nplag.lstsq(A, B) + + # Print result + print('--------------------') + utils.build_symbol_map() + for i in range(0, len(R[0])): + if symrow[i] == 0: + print("Base", end=' ') + else: + for s in symrow[i]: + print(utils.smap[s], end=' ') + print("=", end=' ') + print(str(R[0][i])) + + +################################################################################# + +if __name__ == '__main__': + evaluate() diff --git a/scripts/parse_kconfig/output.c b/scripts/parse_kconfig/output.c index 81debaf..f931ba2 100644 --- a/scripts/parse_kconfig/output.c +++ b/scripts/parse_kconfig/output.c @@ -31,9 +31,11 @@ void output_rules_endterm(void) { } // Functions for variable_count -void output_write_variable_count(char *var_file, int count) { +void output_write_variable_count(char *var_file, int count, + unsigned lastoption) { FILE *f; f = fopen(var_file, "w"); - fprintf(f, "%d", count); + fprintf(f, "%d\n", count); + fprintf(f, "%d", lastoption); fclose(f); } diff --git a/scripts/parse_kconfig/output.h b/scripts/parse_kconfig/output.h index 2950f7a..fb3a7cf 100644 --- a/scripts/parse_kconfig/output.h +++ b/scripts/parse_kconfig/output.h @@ -19,6 +19,7 @@ void output_rules_symbol(int id); void output_rules_endterm(void); // Functions for variable_count -void output_write_variable_count(char *var_file, int count); +void output_write_variable_count(char *var_file, int count, + unsigned lastoption); #endif /* _OUTPUT_H_ */ diff --git a/scripts/parse_kconfig/parse.c b/scripts/parse_kconfig/parse.c index d7c2353..b8aded2 100644 --- a/scripts/parse_kconfig/parse.c +++ b/scripts/parse_kconfig/parse.c @@ -66,7 +66,7 @@ int main(int argc, char **argv) { cpy_dep(); output_write_variable_count(variable_count_file, - gsymlist->lastsym - 1); + gsymlist->lastsym - 1, gsymlist->pos); output_finish(); return 0; diff --git a/scripts/solution.py b/scripts/solution.py index 2eed129..930297b 100644 --- a/scripts/solution.py +++ b/scripts/solution.py @@ -114,7 +114,7 @@ def apply(): if not w[0]: break if not w[0] in solved: - solution = utils.config_strtoint(w[1]) + solution = utils.config_strtoint(w[1], True) hash = w[0] break if not solution: diff --git a/scripts/utils.py b/scripts/utils.py index 74cc80c..9c83065 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -103,12 +103,24 @@ def hash_config(cf): hsh = hashlib.md5(bytes(str, sys.getdefaultencoding())) return hsh.hexdigest() -def config_strtoint(str): +def config_strtoint(str, full): """Reads list of configured symbols from string """ rtn = [] - for s in str.rstrip().split(sep=' '): - rtn.append(int(s)) + if full: + for s in str.rstrip().split(sep=' '): + rtn.append(int(s)) + else: + count = 0 + with open(sf(conf.variable_count_file)) as f: + f.readline() + count = int(f.readline()) + for s in str.rstrip().split(sep=' '): + val = int(s) + if abs(val) <= count: + rtn.append(val) + else: + break; try: rtn.remove(0) except ValueError: @@ -120,7 +132,7 @@ def get_config_from_hash(hash): for line in f: w = line.rstrip().split(sep=':') if w[0] == hash: - return config_strtoint(w[1]) + return config_strtoint(w[1], True) return None def get_last_configuration(): -- cgit v1.2.3