get_function_len: Added count_symmetricals method

This commit is contained in:
2020-12-18 14:41:48 +03:00
parent b2431d07a7
commit c2ccb5e60f

View File

@@ -1,14 +1,18 @@
from __future__ import print_function, unicode_literals
import lzma
import sys
import os
import itertools
def get_rank_file_object(args_count, rank_ind):
base_name = "base_{}_rank_{}.txt".format(args_count, rank_ind)
if os.path.exists(base_name):
return open(base_name, 'rb')
return open(base_name, "rb")
elif os.path.exists(base_name + ".xz"):
return lzma.open(base_name + ".xz", 'rb')
return lzma.LZMAFile(base_name + ".xz", "rb")
else:
return None
@@ -20,11 +24,11 @@ def is_function_exists(function_number, file_object):
def function_values_to_number(values):
assert set(values).issubset({'0','1'})
assert set(values).issubset({"0", "1"})
res = 0
for ch in values:
res *= 2
if ch == '1':
if ch == "1":
res += 1
return res
@@ -39,19 +43,54 @@ def count_lens(args_count):
len_to_count[rank_ind] = sum(map(lambda x: bit_counts[x], file_object.read()))
total = sum(len_to_count.values())
for rank_ind, count in len_to_count.items():
print("len {} count {} share {}".format(
rank_ind,
count,
1. * count / total
))
print("average: {}".format(
sum([key*value for key,value in len_to_count.items()]) / total
))
print("len {} count {} share {}".format(rank_ind, count, 1.0 * count / total))
print("average: {}".format(sum([key * value for key, value in len_to_count.items()]) / total))
def count_symmetricals(args_count):
functions_to_check = set()
for layer_values in itertools.product([0, 1], repeat=args_count + 1):
func_values = []
for argument_number in range(2 ** args_count):
if layer_values[bin(argument_number).count("1")]:
func_values.append("1")
else:
func_values.append("0")
function_number = function_values_to_number("".join(func_values))
functions_to_check.add(function_number)
print("".join(func_values), function_number)
function_ranks = {}
for rank_ind in range(1, 10):
if not functions_to_check:
break
file_object = get_rank_file_object(args_count, rank_ind)
if not file_object:
break
function_ranks[rank_ind] = []
for fn in list(functions_to_check):
if is_function_exists(fn, file_object):
function_ranks[rank_ind].append(fn)
functions_to_check.remove(fn)
print(function_ranks)
len_to_count = {key: len(value) for key, value in function_ranks.items()}
total = sum(len_to_count.values())
for rank_ind, count in len_to_count.items():
print("len {} count {} share {}".format(rank_ind, count, 1.0 * count / total))
print(
"average: {}".format(
sum([1.0 * key * value for key, value in len_to_count.items()]) / total
)
)
def main():
if sys.argv[1] == "count_lens":
count_lens(sys.argv[2])
return
elif sys.argv[1] == "count_symmetricals":
count_symmetricals(int(sys.argv[2]))
return
args_count = {
8: 3,
16: 4,
@@ -68,5 +107,6 @@ def main():
print("Result len is {}".format(rank_ind))
return
if __name__ == "__main__":
main()