/
domain_fparity_apptree.py
101 lines (79 loc) · 3.59 KB
/
domain_fparity_apptree.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
from collections import OrderedDict
from functools import reduce
from itertools import product
from operator import xor
import app_tree
import domain_koza_apptree
from fitness_cache import FitnessCache
from parsers import parse_typ, parse_ctx
size_d = {}
def d_general_even_parity():
return (parse_typ('Bool'),
parse_ctx(OrderedDict([
("copy", ('a', '->', ('P', 'a', 'a'))),
("seri", (('a', '->', 'b'), '->', (('b', '->', 'c'), '->', ('a', '->', 'c')))),
("para", (('a', '->', 'b'), '->', (('c', '->', 'd'), '->', (('P', 'a', 'c'), '->', ('P', 'b', 'd'))))),
("s_and", (('P', 'Bool', 'Bool'), '->', 'Bool')),
("s_or", (('P', 'Bool', 'Bool'), '->', 'Bool')),
("s_nand", (('P', 'Bool', 'Bool'), '->', 'Bool')),
("s_nor", (('P', 'Bool', 'Bool'), '->', 'Bool')),
('foldr', ((('P', 'a', 'b'), '->', 'b'), '->', ('b', '->', (('List', 'a'), '->', 'b')))),
('True', 'Bool'),
('False', 'Bool'),
('xs', ('List', 'Bool')),
])),
4)
def d_lame_even_parity():
return (parse_typ('Bool'),
parse_ctx(OrderedDict([
("copy", ('a', '->', ('P', 'a', 'a'))),
("seri", (('a', '->', 'b'), '->', (('b', '->', 'c'), '->', ('a', '->', 'c')))),
("para", (('a', '->', 'b'), '->', (('c', '->', 'd'), '->', (('P', 'a', 'c'), '->', ('P', 'b', 'd'))))),
("s_and", (('P', 'Bool', 'Bool'), '->', 'Bool')),
("s_or", (('P', 'Bool', 'Bool'), '->', 'Bool')),
("s_nand", (('P', 'Bool', 'Bool'), '->', 'Bool')),
("s_nor", (('P', 'Bool', 'Bool'), '->', 'Bool')),
('foldr', ((('P', 'Bool', 'Bool'), '->', 'Bool'), '->', ('Bool', '->', (('List', 'Bool'), '->', 'Bool')))),
('True', 'Bool'),
('False', 'Bool'),
('xs', ('List', 'Bool')),
])),
9)
def domain_parity(SIZE):
values = product(*((True, False) for _ in range(SIZE)))
ALL = [(bits, reduce(xor, bits)) for bits in values]
global_symbols = {
'copy': lambda x: (x, x),
'seri': lambda f: (lambda g: (lambda a: g(f(a)))),
'para': lambda f: (lambda g: (lambda t: (f(t[0], g(t[1]))))),
's_and': lambda t: t[0] and t[1],
's_or': lambda t: t[0] or t[1],
's_nand': lambda t: not (t[0] and t[1]),
's_nor': lambda t: not (t[0] or t[1]),
'foldr': lambda f: (lambda acc: (lambda xs: reduce(lambda a, b: f((b, a)), reversed(xs), acc))),
}
goal, gamma, _ = d_lame_even_parity()
cache = FitnessCache()
def format_one(eval_str):
print(repr(eval_str))
return "lambda xs : %s" % (eval_str)
def fitness(individual_app_tree):
global size_d
size = individual_app_tree.count_nodes()[app_tree.Leaf]
size_d[size] = size_d.get(size, 0) + 1
s = format_one(individual_app_tree.eval_str())
cres = cache.d.get(s, None)
if cres is not None:
return cres
fun = eval(s, global_symbols)
assert callable(fun)
score = 0
for values, result in ALL:
if fun(values) == result:
score += 1
cache.update(s, score)
return score
return goal, gamma, fitness, (lambda: len(cache)), cache
def make_env_app_tree(SIZE=6, **kwargs):
return domain_koza_apptree.make_env_app_tree(get_raw_domain=lambda: domain_parity(SIZE),
early_end_limit=2 ** SIZE, **kwargs)