示例#1
0
文件: test_typ.py 项目: tomkren/TFGPy
    def test_parse2(self):
        typ = TypTerm((TypVar('x1'),
                       TypTerm((TypTerm((TypSymbol('A'), TypSymbol('B'))),
                                TypVar('x2'), TypVar('x1')))))

        self.assertEqual(parse_typ(('x1', (('A', 'B'), 'x2', 'x1'))), typ)
        self.assertEqual(parse_typ(['x1', [['A', 'B'], 'x2', 'x1']]), typ)
        self.assertEqual(parse_typ(['x1', [('A', 'B'), 'x2', 'x1']]), typ)
示例#2
0
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
示例#3
0
def d2():
    return (parse_typ('B'),
            parse_ctx(
                OrderedDict([
                    ("f", ("A", "->", 'B')),
                    ("x", "A"),
                    ("y", "B"),
                ])), 5)
示例#4
0
def d3():
    return (parse_typ(('a', '->', 'b')),
            parse_ctx(
                OrderedDict([
                    ("s", (("a", "->", ("b", "->", "c")), '->',
                           (("a", "->", "b"), "->", ("a", "->", "c")))),
                    ("k", ("a", "->", ("b", "->", "a"))),
                ])), 5)
示例#5
0
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
示例#6
0
def make_example_types():
    to_parse = [
        ((0, '->', (11, '->', 1)), '->', ((0, '->', 11), '->', (0, '->', 1))),
        ((TypSkolem(0), '->', (11, '->', 1)), '->', ((TypSkolem(0), '->', 11), '->', (TypSkolem(0), '->', 1))),
        ((TypSkolem(0), '->', (11, '->', 1)), '->', ((0, '->', 11), '->', (TypSkolem(0), '->', 1))),
        ((2, '->', (1, '->', 0)), '->', ((2, '->', 1), '->', (2, '->', 0))),
        ((TypSkolem(666), '->', (1, '->', 0)), '->', ((TypSkolem(555), '->', 1), '->', (TypSkolem(444), '->', 0)))
    ]
    return [parse_typ(t) for t in to_parse]
示例#7
0
def separate_error_404_sub():

    goal, gamma, max_k = d3()
    gene = Generator(gamma)
    k = 1
    n = 4
    typ = parse_typ((1, '->', (2, '->', 3)))
    tree = gene.subs(k, typ, n)
    print(tree.typ)
示例#8
0
def t1():
    return [parse_typ(t) for t in [((0, '->', (11, '->', 1)), '->', ((0, '->', 11), '->', (0, '->', 1))),
                                   ((2, '->', (1, '->', 0)), '->', ((2, '->', 1), '->', (2, '->', 0))),
                                   ((2, '->', (0, '->', 1)), '->', ((2, '->', 0), '->', (2, '->', 1))),
                                   (1, '->', (4, '->', (4, '->', (5, '->', (66, '->', (0, '->', (0, '->', (
                                       3, '->', (77, '->', (
                                           4, '->',
                                           (66, '->', (5, '->', (77, '->', (88, '->', (1, '->', 2))))))))))))))),
                                   (10, '->', (0, '->', (4, '->', (55, '->', (4, '->', (55, '->', (
                                       0, '->', (33, '->', (
                                           8, '->', (
                                           7, '->', (6, '->', (5, '->', (7, '->', (8, '->', (6, '->', 2)))))))))))))))]]
示例#9
0
def separate_error_404():
    # seed = random.randint(0, sys.maxsize)
    seed = 7669612278400467845
    random.seed(seed)
    print(seed)

    goal, gamma, max_k = d3()
    gene = Generator(gamma)
    hax_k = 3
    hax_typ = parse_typ(('_P_', 4, (5, '->', (6, '->', 7))))
    hax_tree = gene.gen_one(hax_k, hax_typ)
    print(hax_tree.typ)
示例#10
0
 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
示例#11
0
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
示例#12
0
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
示例#13
0
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)
示例#14
0
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)
示例#15
0
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)
示例#16
0
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)
示例#17
0
 def u(*args):
     return parse_typ((u_typ_fun_sym, ) + args)
示例#18
0
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
示例#19
0
size_d = {}

x0_sym = 'x0'
v0_sym = 'v0'
t_sym = 't'
a_sym = 'a'
half_sym = '0.5'
plus_sym = 'plus'
times_sym = 'times'
times_smart_sym = 'tms'
pair_sym = 'pair'

motion_lambda_head = (x0_sym, v0_sym, a_sym, t_sym)

P = parse_typ('P')
pair_cons_typ = fun_typ(('a', 'b'), (P, 'a', 'b'))

physics_lib_defs = {
    plus_sym: lambda x: (lambda y: x + y),
    times_sym: lambda x: (lambda y: x * y),
    times_smart_sym: lambda _: (lambda _: (lambda x: (lambda y: x * y))),
    pair_sym: lambda x: (lambda y: (x, y))
}

# print(physics_lib_defs[plus_sym](2)(8))


def get_code_str(lambda_head, tree):
    return 'lambda %s : %s' % (','.join(lambda_head), tree.eval_str())
示例#20
0
文件: test_typ.py 项目: tomkren/TFGPy
 def test_parse1(self):
     self.assertEqual(parse_typ("x"), TypVar("x"))