Ejemplo n.º 1
0
def regression_domain_koza():
    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),
        'rlog': lambda x: math.log(abs(x)) if x else 0,
        'sin': math.sin,
        'cos': math.cos,
        'exp': math.exp,
    }

    goal, gamma, _ = d()
    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 : %s" % individual_app_tree.eval_str()
        cres = cache.d.get(s, None)
        if cres is not None:
            return cres
        fun = eval(s, global_symbols)

        score = eval_regression(fun, koza_poly, 20)

        cache.update(s, score)
        return score

    return goal, gamma, fitness, (lambda: len(cache)), cache
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo n.º 4
0
def domain_parity_stack(SIZE=6):
    values = product(*((True, False) for _ in range(SIZE)))
    ALL = [(bits, reduce(xor, bits)) for bits in values]

    def format_one(eval_str):
        return "lambda %s : %s" % (var_str, eval_str)

    def raiserun():
        raise RuntimeError()

    symbols_d = {
        #'s_xor': (2, lambda x, y: x ^ y),
        's_and': (2, lambda x, y: x and y),
        's_or': (2, lambda x, y: x or y),
        's_nand': (2, lambda x, y: not (x and y)),
        's_nor': (2, lambda x, y: not (x or y)),
    }
    vars = ['b%d' % i for i in range(SIZE)]
    var_str = ','.join(vars)
    symbols_d.update({v: (0, raiserun) for v in vars})
    sutils = Stack(symbols_d)
    cache = FitnessCache()

    def fitness(stack):
        global size_d
        size_d[len(stack)] = size_d.get(len(stack), 0) + 1

        assert sutils.count_missing(stack)[0] == 0
        s = repr(stack)

        cres = cache.d.get(s, None)
        if cres is not None:
            return cres

        def fun(*args):
            assert len(args) == len(vars)
            return sutils.eval_stack(
                stack, {var: val
                        for var, val in zip(vars, args)})

        score = 0
        for values, result in ALL:
            if fun(*values) == result:
                score += 1

        cache.update(s, score)
        return score

    return sutils.finish, sutils.is_finished, sutils.successors, fitness, sutils.eval_stack, (
        lambda: len(cache)), cache
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
def regression_domain_koza_stack():
    def raiserun():
        raise RuntimeError()

    symbols_d = {
        'plus': (2, (lambda x, y: x + y)),
        'minus': (2, (lambda x, y: x - y)),
        'times': (2, (lambda x, y: x * y)),
        'rdiv': (2, (lambda p, q: p / q if q else 1)),
        'rlog': (1, (lambda x: math.log(abs(x)) if x else 0)),
        'sin': (1, math.sin),
        'cos': (1, math.cos),
        'exp': (1, math.exp),
        'x': (0, raiserun),
    }
    sutils = Stack(symbols_d)

    cache = FitnessCache()

    def fitness(stack):
        global size_d
        size_d[len(stack)] = size_d.get(len(stack), 0) + 1

        assert sutils.count_missing(stack)[0] == 0
        s = repr(stack)

        cres = cache.d.get(s, None)
        if cres is not None:
            return cres

        fun = lambda val: sutils.eval_stack(stack, {'x': val})
        score = eval_regression(fun, koza_poly, 20)

        cache.update(s, score)
        return score

    return sutils.finish, sutils.is_finished, sutils.successors, fitness, sutils.eval_stack, (
        lambda: len(cache)), cache
Ejemplo n.º 7
0
def domain_physics(domain_maker=make_simple_motion_domain):
    title, goal, gamma, optimal_size = domain_maker(physics_lib_defs)
    cache = FitnessCache()
    inputs = make_tuple_samples(len(motion_lambda_head), 0, 100, 9)

    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 = get_code_str(motion_lambda_head, individual_app_tree)

        cres = cache.d.get(s, None)
        if cres is not None:
            return cres
        fun = eval(s, physics_lib_defs)
        err = compute_error_on_inputs(inputs, motion_model, fun)
        score = 1 / (1 + err)

        cache.update(s, score)
        return score

    return goal, gamma, fitness, (lambda: len(cache)), cache