def d3(): return (parse_typ(('a', '->', 'b')), parse_ctx( OrderedDict([ ("s", (("a", "->", ("b", "->", "c")), '->', (("a", "->", "b"), "->", ("a", "->", "c")))), ("k", ("a", "->", ("b", "->", "a"))), ])), 5)
def d2(): return (parse_typ('B'), parse_ctx( OrderedDict([ ("f", ("A", "->", 'B')), ("x", "A"), ("y", "B"), ])), 5)
def domain_primes(): PRIME_LIMIT = 2000000 PRIMES = set(primes(PRIME_LIMIT)) global_symbols = { 'plus': lambda x: (lambda y: x + y), 'minus': lambda x: (lambda y: x - y), 'times': lambda x: (lambda y: x * y), 'rdiv': lambda p: (lambda q: p / q if q else 1), } R = 'R' numbers = [(str(i + 1), R) for i in range(10)] primes_lt_100 = primes(100) goal = parse_typ(R) stuff = [('plus', (R, '->', (R, '->', R))), ('minus', (R, '->', (R, '->', R))), ('times', (R, '->', (R, '->', R))), ('rdiv', (R, '->', (R, '->', R))), ('x', R), ('c', R)] # + numbers + [(str(p), R) for p in primes_lt_100] gamma = parse_ctx(OrderedDict(stuff)) cache = FitnessCache() 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 = "lambda x,c : %s" % 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) try: got = 0 i = 0 old = set() while True: candidate = fun(i, 0) assert candidate < PRIME_LIMIT if candidate not in PRIMES: break if candidate not in old: got += 1 old.add(candidate) i += 1 score = got except (OverflowError, ValueError): score = 0.0 cache.update(s, score) return score return goal, gamma, fitness, (lambda: len(cache)), cache
def make_family_1(): t_img = parse_typ('I') # simple Image type t_op2 = fun_typ((t_img, t_img), t_img) # Simple binary operation t_op4 = fun_typ((t_img, t_img, t_img, t_img), t_img) # Simple tetra operation goal = t_img gamma = parse_ctx( OrderedDict([(H, t_op2), (V, t_op2), (Q, t_op4), (W, t_img), (B, t_img)])) return goal, gamma
def domain_parity(SIZE): values = product(*((True, False) for _ in range(SIZE))) ALL = [(bits, reduce(xor, bits)) for bits in values] global_symbols = { # 's_xor': lambda x: (lambda y: x ^ y), 's_and': lambda x: (lambda y: x and y), 's_or': lambda x: (lambda y: x or y), 's_nand': lambda x: (lambda y: not (x and y)), 's_nor': lambda x: (lambda y: not (x or y)), } R = 'R' goal = parse_typ(R) vars = ['b%d' % i for i in range(SIZE)] var_str = ','.join(vars) stuff = [ # ('s_xor', (R, '->', (R, '->', R))), ('s_and', (R, '->', (R, '->', R))), ('s_or', (R, '->', (R, '->', R))), ('s_nor', (R, '->', (R, '->', R))), ('s_nand', (R, '->', (R, '->', R))), ] + [(v, R) for v in vars] gamma = parse_ctx(OrderedDict(stuff)) cache = FitnessCache() def format_one(eval_str): return "lambda %s : %s" % (var_str, 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_simple_motion_domain(lib_defs): R = parse_typ('R') gamma = parse_ctx( OrderedDict([(x0_sym, R), (v0_sym, R), (t_sym, R), (a_sym, R), (half_sym, R), (plus_sym, fun_typ((R, R), R)), (times_sym, fun_typ((R, R), R)), (pair_sym, pair_cons_typ)])) goal = parse_typ((P, R, R)) title = 'Simple Motion Lib' optimal_size = 19 return title, goal, gamma, optimal_size
def d(): R = 'R' return (parse_typ(R), parse_ctx( OrderedDict([ ('plus', (R, '->', (R, '->', R))), ('minus', (R, '->', (R, '->', R))), ('times', (R, '->', (R, '->', R))), ('rdiv', (R, '->', (R, '->', R))), ('sin', (R, '->', R)), ('cos', (R, '->', R)), ('exp', (R, '->', R)), ('rlog', (R, '->', R)), ('x', R), ])), 20)
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 d_general_even_parity_sk(): return ( parse_typ('Bool'), parse_ctx( OrderedDict([ ('xs', ('List', 'Bool')), ("s", (("a", "->", ("b", "->", "c")), '->', (("a", "->", "b"), "->", ("a", "->", "c")))), ("k", ("a", "->", ("b", "->", "a"))), ("and", ('Bool', '->', ('Bool', '->', 'Bool'))), ("or", ('Bool', '->', ('Bool', '->', 'Bool'))), ("nand", ('Bool', '->', ('Bool', '->', 'Bool'))), ("nor", ('Bool', '->', ('Bool', '->', 'Bool'))), ('foldr', (('a', '->', ('b', '->', 'b')), '->', ('b', '->', (('List', 'a'), '->', 'b')))), ('true', 'Bool'), ('false', 'Bool') # ("head", (('List', 'Bool'), '->', ('Maybe', 'Bool'))), # ("tail", (('List', 'Bool'), '->', ('Maybe', ('List', 'Bool')))), ])), 5)
def d1(): return (parse_typ( (('P', 'A', ('P', 'A', 'A')), '->', ('P', 'A', ('P', 'A', 'A')))), parse_ctx( OrderedDict([ ("s", (("a", "->", ("b", "->", "c")), '->', (("a", "->", "b"), "->", ("a", "->", "c")))), ("k", ("a", "->", ("b", "->", "a"))), ("seri", (("Dag", 'a', 'b'), '->', (("Dag", 'b', 'c'), '->', ("Dag", 'a', 'c')))), ("para", (("Dag", 'a', 'b'), '->', (("Dag", 'c', 'd'), '->', ("Dag", ('P', 'a', 'c'), ('P', 'b', 'd'))))), ("mkDag", (("a", "->", "b"), '->', ("Dag", "a", "b"))), ("deDag", ( ("Dag", "a", "b"), '->', ("a", "->", "b"), )), ("mkP", ("a", "->", ("b", "->", ('P', "a", 'b')))), ("fst", (('P', "a", 'b'), '->', 'a')), ("snd", (('P', "a", 'b'), '->', 'b')), ])), 4)
def make_smart_motion_domain(lib_defs): u_typ_fun_sym = parse_typ('U') def u(*args): return parse_typ((u_typ_fun_sym, ) + args) pos_typ = u('1', '0') speed_typ = u('1', '-1') time_typ = u('0', '1') acceleration_typ = u('1', '-2') dimensionless_typ = u('0', '0') m, s, m1, m2, s1, s2 = map(parse_typ, ('m', 's', 'm1', 'm2', 's1', 's2')) u_ms = u(m, s) u_m1s1 = u(m1, s1) u_m2s2 = u(m2, s2) plus_m, plus_s = map(parse_typ, ('Pm', 'Ps')) m_vals = range(0, 2) s_vals = range(-2, 2) def make_plus_eqs(plus_pred, name_prefix, vals): eqs = [] i = 1 for a in vals: for b in vals: c = a + b if c in vals: eq_typ = parse_typ((plus_pred, str(a), str(b), str(c))) eq_sym = name_prefix + str(i) eqs.append((eq_sym, eq_typ)) lib_defs[eq_sym] = None # equations need no implementation i += 1 return eqs m_eq_typs = make_plus_eqs(plus_m, 'pm', m_vals) s_eq_typs = make_plus_eqs(plus_s, 'ps', s_vals) inputs = [(x0_sym, pos_typ), (v0_sym, speed_typ), (t_sym, time_typ), (a_sym, acceleration_typ)] constants = [(half_sym, dimensionless_typ)] typ_plus = fun_typ((u_ms, u_ms), u_ms) typ_times = fun_typ( ((plus_m, m1, m2, m), (plus_s, s1, s2, s), u_m1s1, u_m2s2), u_ms) operators = [(plus_sym, typ_plus), (times_smart_sym, typ_times)] pair_stuff = [(pair_sym, pair_cons_typ)] gamma_list = inputs + constants + operators + m_eq_typs + s_eq_typs + pair_stuff gamma = parse_ctx(OrderedDict(gamma_list)) goal = parse_typ((P, pos_typ, speed_typ)) title = 'Smart Motion Lib' optimal_size = 29 # TODO ! return title, goal, gamma, optimal_size