예제 #1
0
def test_tree_crossover2():
    np.random.seed(42)
    pset = pg.PrimitiveSet()
    pset.addFunction(op.add, 2)
    pset.addFunction(op.sub, 2)
    pset.addFunction(op.mul, 2)
    pset.addFunction(protected_div, 2)
    num_constants = 10
    for i in range(num_constants):
        pset.addTerminal(np.random.randint(-5, 5))
    pset.addVariable("x")

    t1 = np.array([3, 9, 1, 1, 5, 7, 6, 0, 0, 0])
    t2 = np.array([6, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    i1 = pg.TreeIndividual(tree=t1, nodes=7)
    i2 = pg.TreeIndividual(tree=t2, nodes=1)
    o1, o2 = pg.tree_crossover(i1, i2, pset=pset)
    o1_str = pg.interpreter(pset, o1.genotype)
    o2_str = pg.interpreter(pset, o2.genotype)

    assert o1.depth == 3
    assert o1.nodes == 5
    assert np.array_equal(o1.genotype, np.array([3, 9, 1, 6, 6, 0, 0, 0, 0,
                                                 0]))
    assert o1_str == 'mul(4, add(-2, -2))'

    assert o2.depth == 2
    assert o2.nodes == 3
    assert np.array_equal(o2.genotype, np.array([1, 5, 7, 0, 0, 0, 0, 0, 0,
                                                 0]))
    assert o2_str == 'add(1, 2)'
예제 #2
0
def test_subtree_mutation_typed():
    np.random.seed(42)
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, [int, int, int])
    pset.addFunction(op.sub, 2, [float, int, int])
    pset.addFunction(op.mul, 2, [int, float, float])
    pset.addFunction(op.truediv, 2, [float, float, float])
    pset.addTerminal(1, [int])
    pset.addTerminal(2, [int])
    pset.addTerminal(3, [int])
    pset.addTerminal(4.0, [float])
    pset.addTerminal(5.0, [float])
    pset.addTerminal(6.0, [float])
    pset.addVariable("x", [float])
    pset.addVariable("y", [int])
    pop = pg.make_tree_population(1, pset, 4, 7, init_method=pg.full_tree)
    i1m = pg.subtree_mutation(pop.individuals[0], pset=pset)
    i1m_str = pg.interpreter(pset, i1m.genotype)

    assert i1m.depth == 4
    assert i1m.nodes == 15
    assert np.array_equal(
        i1m.genotype,
        np.array([
            3, 4, 2, 7, 6, 4, 8, 8, 2, 3, 10, 10, 1, 7, 12, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert i1m_str == 'mul(truediv(sub(3, 2), truediv(4.0, 4.0)), sub(mul(6.0, 6.0), add(3, y)))'
예제 #3
0
def test_tree_point_mutation():
    np.random.seed(42)
    pset = pg.PrimitiveSet()
    pset.addFunction(op.add, 2)
    pset.addFunction(op.sub, 2)
    pset.addTerminal(1)
    pset.addTerminal(2)
    pset.addTerminal(3)
    pset.addVariable("x")
    pop = pg.make_tree_population(1, pset, 7, 8, init_method=pg.full_tree)
    i1m = pg.tree_point_mutation(pop.individuals[0], pset=pset, gene_rate=0.5)
    i1m_str = pg.interpreter(pset, i1m.genotype)

    assert pop.individuals[0].depth == i1m.depth
    assert pop.individuals[0].nodes == i1m.nodes
    assert np.array_equal(
        i1m.genotype,
        np.array([
            1, 2, 1, 1, 1, 1, 6, 5, 1, 4, 5, 1, 1, 5, 6, 1, 5, 6, 2, 1, 1, 3,
            4, 2, 6, 4, 1, 2, 6, 5, 1, 3, 6, 2, 1, 1, 1, 5, 3, 1, 5, 3, 2, 2,
            4, 6, 1, 5, 3, 1, 2, 1, 6, 6, 1, 6, 5, 2, 1, 4, 5, 1, 4, 3, 2, 1,
            2, 1, 1, 4, 4, 2, 4, 6, 2, 1, 6, 4, 1, 6, 3, 1, 1, 2, 6, 5, 2, 5,
            3, 1, 2, 6, 4, 1, 4, 6, 2, 2, 2, 2, 5, 4, 2, 3, 6, 2, 2, 3, 4, 2,
            6, 5, 2, 1, 1, 6, 4, 2, 6, 3, 2, 2, 6, 5, 1, 6, 5, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert i1m_str == 'add(sub(add(add(add(add(x, 3), add(2, 3)), add(add(3, x), add(3, x))), sub(add(add(1, 2), sub(x, 2)), add(sub(x, 3), add(1, x)))), sub(add(add(add(3, 1), add(3, 1)), sub(sub(2, x), add(3, 1))), add(sub(add(x, x), add(x, 3)), sub(add(2, 3), add(2, 1))))), sub(add(sub(add(add(2, 2), sub(2, x)), sub(add(x, 2), add(x, 1))), add(add(sub(x, 3), sub(3, 1)), add(sub(x, 2), add(2, x)))), sub(sub(sub(sub(3, 2), sub(1, x)), sub(sub(1, 2), sub(x, 3))), sub(add(add(x, 2), sub(x, 1)), sub(sub(x, 3), add(x, 3))))))'
예제 #4
0
def test_tree_crossover_typed2():
    np.random.seed(42)
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, types=[int, int, int])
    pset.addFunction(op.sub, 2, types=[int, float, float])
    pset.addFunction(op.mul, 2, types=[float, int, int])
    pset.addFunction(protected_div, 2, types=[float, float, float])
    num_constants = 5
    for i in range(num_constants):
        pset.addTerminal(np.random.randint(-5, 5), types=[int])

    for i in range(num_constants):
        pset.addTerminal(np.random.uniform(), types=[float])
    pset.addVariable("x", types=[int])

    pop = pg.make_tree_population(2, pset, 4, 6, init_method=pg.full_tree)
    o1, o2 = pg.tree_crossover(pop.individuals[0],
                               pop.individuals[1],
                               pset=pset)
    o1_str = pg.interpreter(pset, o1.genotype)
    o2_str = pg.interpreter(pset, o2.genotype)

    assert o1.depth == 4
    assert o1.nodes == 13
    assert np.array_equal(
        o1.genotype,
        np.array([
            3, 2, 12, 4, 10, 12, 1, 1, 8, 6, 2, 13, 12, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert o1_str == 'mul(sub(0.33370861113902184, protected_div(0.09997491581800289, 0.33370861113902184)), add(add(-1, -2), sub(0.14286681792194078, 0.33370861113902184)))'

    assert o2.depth == 5
    assert o2.nodes == 17
    assert np.array_equal(
        o2.genotype,
        np.array([
            1, 1, 1, 7, 6, 2, 3, 6, 8, 12, 2, 3, 7, 8, 3, 8, 5, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert o2_str == 'add(add(add(2, -2), sub(mul(-2, -1), 0.33370861113902184)), sub(mul(2, -1), mul(-1, 1)))'
예제 #5
0
def test_tree_crossover_typed1():
    np.random.seed(42)
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, types=[int, int, int])
    pset.addFunction(op.sub, 2, types=[int, int, int])
    pset.addFunction(op.mul, 2, types=[int, int, int])
    pset.addFunction(protected_div, 2, types=[float, float, float])
    num_constants = 5
    for i in range(num_constants):
        pset.addTerminal(np.random.randint(-5, 5), types=[int])
    for i in range(num_constants):
        pset.addTerminal(np.random.uniform(), types=[float])
    pset.addVariable("x", types=[int])

    pop = pg.make_tree_population(2, pset, 4, 6, init_method=pg.full_tree)
    o1, o2 = pg.tree_crossover(pop.individuals[0],
                               pop.individuals[1],
                               pset=pset)
    o1_str = pg.interpreter(pset, o1.genotype)
    o2_str = pg.interpreter(pset, o2.genotype)

    assert o1.depth == 6
    assert o1.nodes == 21
    assert np.array_equal(
        o1.genotype,
        np.array([
            3, 2, 1, 6, 8, 2, 1, 3, 7, 6, 3, 7, 8, 8, 1, 1, 8, 6, 2, 14, 8, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert o1_str == 'mul(sub(add(-2, -1), sub(add(mul(2, -2), mul(2, -1)), -1)), add(add(-1, -2), sub(x, -1)))'

    assert o2.depth == 4
    assert o2.nodes == 9
    assert np.array_equal(
        o2.genotype,
        np.array([
            1, 6, 3, 1, 7, 14, 3, 14, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert o2_str == 'add(-2, mul(add(2, x), mul(x, 1)))'
예제 #6
0
    def regression(solution):
        vars_inputs = {}
        x_evals = np.empty(num_fitness_cases)
        for i in range(num_fitness_cases):
            vars_inputs[variable] = x_points[i]
            x_evals[i] = pg.interpreter(pset,
                                        solution,
                                        run=True,
                                        vars_inputs=vars_inputs)

        return loss(x_evals, y_points)
예제 #7
0
def test_tree_crossover1():
    np.random.seed(42)
    pset = pg.PrimitiveSet()
    pset.addFunction(op.add, 2)
    pset.addFunction(op.sub, 2)
    pset.addTerminal(1)
    pset.addTerminal(2)
    pset.addTerminal(3)
    pset.addVariable("x")
    pop = pg.make_tree_population(2, pset, 4, 6, init_method=pg.full_tree)
    i1 = pop.individuals[0].clone()
    i2 = pop.individuals[1].clone()
    i1m, i2m = pg.tree_crossover(i1, i2, pset=pset)
    i1m_str = pg.interpreter(pset, i1m.genotype)
    i2m_str = pg.interpreter(pset, i2m.genotype)

    assert i1m.depth == 7
    assert i1m.nodes == 29
    assert np.array_equal(
        i1m.genotype,
        np.array([
            1, 2, 1, 5, 1, 2, 2, 6, 5, 2, 3, 4, 2, 2, 4, 4, 2, 6, 6, 2, 3, 3,
            1, 2, 5, 5, 1, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert i1m_str == 'add(sub(add(3, add(sub(sub(x, 3), sub(1, 2)), sub(sub(2, 2), sub(x, x)))), sub(1, 1)), add(sub(3, 3), add(3, x)))'

    assert i2m.depth == 1
    assert i2m.nodes == 1
    assert np.array_equal(
        i2m.genotype,
        np.array([
            5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert i2m_str == '3'
예제 #8
0
def test_interpreter_str():
    pset = pg.PrimitiveSet()
    pset.addFunction(op.add, 2)
    pset.addFunction(op.sub, 2)
    pset.addTerminal(1)
    pset.addTerminal(2)
    pset.addTerminal(3)
    pset.addVariable("x")
    tree = np.array([1, 2, 1, 1, 1, 2, 3, 3, 1, 4, 5, 1, 1, 5, 6, 1, 6, 6, 2, 1, 2, 3, 4,
       2, 6, 4, 2, 2, 6, 6, 1, 3, 6, 2, 2, 1, 2, 3, 3, 1, 5, 5, 2, 2, 6, 6,
       2, 5, 4, 2, 1, 2, 5, 6, 1, 6, 6, 1, 1, 3, 5, 1, 3, 3, 1, 2, 2, 1, 2,
       4, 4, 2, 3, 4, 1, 2, 6, 6, 1, 6, 5, 2, 1, 2, 5, 5, 2, 3, 4, 2, 2, 6,
       4, 2, 4, 4, 2, 2, 2, 1, 5, 4, 2, 6, 4, 2, 2, 6, 4, 1, 6, 5, 2, 2, 1,
       6, 3, 2, 6, 3, 2, 1, 4, 5, 1, 6, 4, 0])
    tree_str = pg.interpreter(pset, tree)
    assert tree_str == 'add(sub(add(add(add(sub(1, 1), add(2, 3)), add(add(3, x), add(x, x))), sub(add(sub(1, 2), sub(x, 2)), sub(sub(x, x), add(1, x)))), sub(sub(add(sub(1, 1), add(3, 3)), sub(sub(x, x), sub(3, 2))), sub(add(sub(3, x), add(x, x)), add(add(1, 3), add(1, 1))))), add(sub(sub(add(sub(2, 2), sub(1, 2)), add(sub(x, x), add(x, 3))), sub(add(sub(3, 3), sub(1, 2)), sub(sub(x, 2), sub(2, 2)))), sub(sub(sub(add(3, 2), sub(x, 2)), sub(sub(x, 2), add(x, 3))), sub(sub(add(x, 1), sub(x, 1)), sub(add(2, 3), add(x, 2))))))'
예제 #9
0
def test_interpreter_run():
    pset = pg.PrimitiveSet()
    pset.addFunction(op.add, 2)
    pset.addFunction(op.sub, 2)
    pset.addTerminal(1)
    pset.addTerminal(2)
    pset.addTerminal(3)
    pset.addTerminal(4)
    tree = np.array([1, 2, 1, 1, 1, 2, 3, 3, 1, 4, 5, 1, 1, 5, 6, 1, 6, 6, 2, 1, 2, 3, 4,
       2, 6, 4, 2, 2, 6, 6, 1, 3, 6, 2, 2, 1, 2, 3, 3, 1, 5, 5, 2, 2, 6, 6,
       2, 5, 4, 2, 1, 2, 5, 6, 1, 6, 6, 1, 1, 3, 5, 1, 3, 3, 1, 2, 2, 1, 2,
       4, 4, 2, 3, 4, 1, 2, 6, 6, 1, 6, 5, 2, 1, 2, 5, 5, 2, 3, 4, 2, 2, 6,
       4, 2, 4, 4, 2, 2, 2, 1, 5, 4, 2, 6, 4, 2, 2, 6, 4, 1, 6, 5, 2, 2, 1,
       6, 3, 2, 6, 3, 2, 1, 4, 5, 1, 6, 4, 0])
    tree_run = pg.interpreter(pset, tree, run=True)
    assert tree_run == 20
예제 #10
0
def test_full_tree_ephemeral_typed1():
    np.random.seed(42)
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, [float, float, float])
    pset.addFunction(op.sub, 2, [float, float, float])
    pset.addTerminal(float_constants, types=[float], ephemeral=True)
    init_max_depth = 3
    max_depth = 6
    tree = pg.full_tree(pset, init_max_depth, max_depth)
    tree_str = pg.interpreter(pset, tree)
    assert np.array_equal(tree, np.array([1, 2, 4, 5, 1, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
    assert tree_str == 'add(sub(0.9507143064099162, 0.7319939418114051), add(0.596850157946487, 0.44583275285359114))'
    assert pset.num_primitives == 7
    assert pset.ephemeral_cache == {3}
    assert pset.ephemeral_constants == {4: (0.9507143064099162, [float]), 5: (0.7319939418114051, [float]), 6: (0.596850157946487, [float]), 7: (0.44583275285359114, [float])}
예제 #11
0
def test_interpreter_run_inputs():
    pset = pg.PrimitiveSet()
    pset.addFunction(op.add, 2)
    pset.addFunction(op.sub, 2)
    pset.addTerminal(1)
    pset.addTerminal(2)
    pset.addTerminal(3)
    pset.addVariable("x")
    tree = np.array([1, 2, 1, 1, 1, 2, 3, 3, 1, 4, 5, 1, 1, 5, 6, 1, 6, 6, 2, 1, 2, 3, 4,
       2, 6, 4, 2, 2, 6, 6, 1, 3, 6, 2, 2, 1, 2, 3, 3, 1, 5, 5, 2, 2, 6, 6,
       2, 5, 4, 2, 1, 2, 5, 6, 1, 6, 6, 1, 1, 3, 5, 1, 3, 3, 1, 2, 2, 1, 2,
       4, 4, 2, 3, 4, 1, 2, 6, 6, 1, 6, 5, 2, 1, 2, 5, 5, 2, 3, 4, 2, 2, 6,
       4, 2, 4, 4, 2, 2, 2, 1, 5, 4, 2, 6, 4, 2, 2, 6, 4, 1, 6, 5, 2, 2, 1,
       6, 3, 2, 6, 3, 2, 1, 4, 5, 1, 6, 4, 0])
    inputs = {"x": 4}
    tree_run = pg.interpreter(pset, tree, run=True, vars_inputs=inputs)
    assert tree_run == 20
예제 #12
0
def test_grow_tree_ephemeral_typed1():
    np.random.seed(45345)
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, [float, float, float])
    pset.addFunction(op.sub, 2, [float, float, float])
    pset.addTerminal(float_constants, types=[float], ephemeral=True)
    init_max_depth = 5
    max_depth = 6
    tree = pg.grow_tree(pset, init_max_depth, max_depth)
    tree_str = pg.interpreter(pset, tree)
    assert np.array_equal(tree, np.array([ 1,  4,  2,  2,  1,  5,  6,  2,  7,  8,  1,  1,  9, 10, 11,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0]))
    assert tree_str == 'add(0.6948470578046806, sub(sub(add(0.23851088045334068, 0.7485138905722925), sub(0.9421360052961212, 0.5983584346144624)), add(add(0.6952518271609337, 0.41227782045980343), 0.13638623666489624)))'
    assert pset.num_primitives == 11
    assert pset.ephemeral_cache == {3}
    assert pset.ephemeral_constants == {4: (0.6948470578046806, [float]), 5: (0.23851088045334068, [float]), 6: (0.7485138905722925, [float]), 7: (0.9421360052961212, [float]), 8: (0.5983584346144624, [float]), 9: (0.6952518271609337, [float]), 10: (0.41227782045980343, [float]), 11: (0.13638623666489624, [float])}
예제 #13
0
def test_tree_point_mutation_st1():
    np.random.seed(42)
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, [int, int, int])
    pset.addFunction(op.sub, 2, [float, int, int])
    pset.addFunction(op.mul, 2, [int, float, float])
    pset.addFunction(op.truediv, 2, [float, float, float])
    pset.addTerminal(1, [int])
    pset.addTerminal(2, [int])
    pset.addTerminal(3, [int])
    pset.addTerminal(4.0, [float])
    pset.addTerminal(5.0, [float])
    pset.addTerminal(6.0, [float])
    pset.addVariable("x", [float])
    pset.addVariable("y", [int])
    pop = pg.make_tree_population(1, pset, 7, 8, init_method=pg.full_tree)
    i1m = pg.tree_point_mutation(pop.individuals[0], pset=pset, gene_rate=0.5)
    i1m_str = pg.interpreter(pset, i1m.genotype)

    assert pop.individuals[0].depth == i1m.depth
    assert pop.individuals[0].nodes == i1m.nodes
    assert np.array_equal(
        i1m.genotype,
        np.array([
            3, 4, 2, 1, 1, 3, 8, 10, 1, 6, 7, 1, 1, 5, 12, 1, 12, 12, 3, 2, 3,
            8, 10, 3, 11, 9, 4, 4, 11, 11, 2, 6, 12, 4, 4, 2, 3, 8, 10, 1, 7,
            7, 4, 4, 11, 11, 4, 10, 9, 4, 2, 3, 10, 11, 1, 12, 12, 2, 1, 5, 7,
            1, 7, 7, 2, 3, 4, 2, 3, 9, 9, 3, 8, 9, 2, 3, 11, 11, 1, 12, 6, 4,
            2, 3, 10, 10, 3, 8, 11, 4, 4, 11, 9, 4, 9, 10, 3, 4, 4, 2, 7, 7, 4,
            11, 8, 4, 4, 11, 9, 2, 12, 5, 4, 4, 2, 12, 7, 4, 11, 8, 4, 2, 6, 7,
            2, 12, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert i1m_str == 'mul(truediv(sub(add(add(mul(4.0, 6.0), add(2, 3)), add(add(1, y), add(y, y))), mul(sub(mul(4.0, 6.0), mul(x, 5.0)), truediv(truediv(x, x), sub(2, y)))), truediv(truediv(sub(mul(4.0, 6.0), add(3, 3)), truediv(truediv(x, x), truediv(6.0, 5.0))), truediv(sub(mul(6.0, x), add(y, y)), sub(add(1, 3), add(3, 3))))), sub(mul(truediv(sub(mul(5.0, 5.0), mul(4.0, 5.0)), sub(mul(x, x), add(y, 2))), truediv(sub(mul(6.0, 6.0), mul(4.0, x)), truediv(truediv(x, 5.0), truediv(5.0, 6.0)))), mul(truediv(truediv(sub(3, 3), truediv(x, 4.0)), truediv(truediv(x, 5.0), sub(y, 1))), truediv(truediv(sub(y, 3), truediv(x, 4.0)), truediv(sub(2, 3), sub(y, 2))))))'
예제 #14
0
def test_interpreter_str_typed():
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, [float, int, int])
    pset.addFunction(op.sub, 2, [int, float, float])
    pset.addFunction(op.mul, 2, [float, int, int])
    pset.addTerminal(1, [int])
    pset.addTerminal(2, [int])
    pset.addTerminal(3, [int])
    pset.addTerminal(4.0, [float])
    pset.addTerminal(5.0, [float])
    pset.addTerminal(6.0, [float])
    pset.addVariable("x", [float])
    pset.addVariable("y", [int])
    tree = np.array([ 1,  2,  3,  2,  1,  2,  9,  9,  2, 10,  7,  1,  2,  9,  8,  2,  9,
        9,  2,  1,  2,  9, 10,  2,  7, 10,  3,  2, 10,  9,  2,  8,  7,  1,
        2,  3,  2, 10,  9,  2,  9,  8,  1,  2, 10,  7,  2,  7, 10,  2,  3,
        2,  7,  9,  2, 10,  7,  1,  2,  9,  9,  2,  8,  8,  2,  3,  2,  3,
        2, 10, 10,  2,  9,  9,  3,  2,  8,  8,  2,  9, 10,  2,  3,  2, 10,
       10,  2,  7,  8,  1,  2,  9,  8,  2,  7,  7,  3,  2,  3,  2, 10,  7,
        2, 10,  8,  3,  2,  7,  9,  2,  9,  8,  2,  3,  2, 10, 10,  2,  9,
       10,  1,  2, 10,  7,  2, 10,  9,  0])
    tree_str = pg.interpreter(pset, tree)
    assert tree_str == 'add(sub(mul(sub(add(sub(6.0, 6.0), sub(x, 4.0)), add(sub(6.0, 5.0), sub(6.0, 6.0))), sub(add(sub(6.0, x), sub(4.0, x)), mul(sub(x, 6.0), sub(5.0, 4.0)))), add(sub(mul(sub(x, 6.0), sub(6.0, 5.0)), add(sub(x, 4.0), sub(4.0, x))), sub(mul(sub(4.0, 6.0), sub(x, 4.0)), add(sub(6.0, 6.0), sub(5.0, 5.0))))), sub(mul(sub(mul(sub(x, x), sub(6.0, 6.0)), mul(sub(5.0, 5.0), sub(6.0, x))), sub(mul(sub(x, x), sub(4.0, 5.0)), add(sub(6.0, 5.0), sub(4.0, 4.0)))), mul(sub(mul(sub(x, 4.0), sub(x, 5.0)), mul(sub(4.0, 6.0), sub(6.0, 5.0))), sub(mul(sub(x, x), sub(6.0, x)), add(sub(x, 4.0), sub(x, 6.0))))))'
예제 #15
0
def test_interpreter_run_typed():
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, [float, int, int])
    pset.addFunction(op.sub, 2, [int, float, float])
    pset.addFunction(op.mul, 2, [float, int, int])
    pset.addTerminal(1, [int])
    pset.addTerminal(2, [int])
    pset.addTerminal(3, [int])
    pset.addTerminal(4.0, [float])
    pset.addTerminal(5.0, [float])
    pset.addTerminal(6.0, [float])
    pset.addTerminal(7.0, [float])
    pset.addTerminal(8, [int])
    tree = np.array([ 1,  2,  3,  2,  1,  2,  9,  9,  2, 10,  7,  1,  2,  9,  8,  2,  9,
        9,  2,  1,  2,  9, 10,  2,  7, 10,  3,  2, 10,  9,  2,  8,  7,  1,
        2,  3,  2, 10,  9,  2,  9,  8,  1,  2, 10,  7,  2,  7, 10,  2,  3,
        2,  7,  9,  2, 10,  7,  1,  2,  9,  9,  2,  8,  8,  2,  3,  2,  3,
        2, 10, 10,  2,  9,  9,  3,  2,  8,  8,  2,  9, 10,  2,  3,  2, 10,
       10,  2,  7,  8,  1,  2,  9,  8,  2,  7,  7,  3,  2,  3,  2, 10,  7,
        2, 10,  8,  3,  2,  7,  9,  2,  9,  8,  2,  3,  2, 10, 10,  2,  9,
       10,  1,  2, 10,  7,  2, 10,  9,  0])
    tree_run = pg.interpreter(pset, tree, run=True)
    assert tree_run == 27.0
예제 #16
0
def test_tree_point_mutation_st2():
    np.random.seed(42)
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, [float, int, int])
    pset.addFunction(op.sub, 2, [int, float, float])
    pset.addFunction(op.mul, 2, [float, int, int])
    pset.addTerminal(1, [int])
    pset.addTerminal(2, [int])
    pset.addTerminal(3, [int])
    pset.addTerminal(4.0, [float])
    pset.addTerminal(5.0, [float])
    pset.addTerminal(6.0, [float])
    pset.addVariable("x", [float])
    pset.addVariable("y", [int])
    pop = pg.make_tree_population(1, pset, 7, 8, init_method=pg.full_tree)
    i1m = pg.tree_point_mutation(pop.individuals[0], pset=pset, gene_rate=0.5)
    i1m_str = pg.interpreter(pset, i1m.genotype)

    assert pop.individuals[0].depth == i1m.depth
    assert pop.individuals[0].nodes == i1m.nodes
    assert np.array_equal(
        i1m.genotype,
        np.array([
            1, 2, 3, 2, 1, 2, 9, 9, 2, 10, 7, 1, 2, 9, 8, 2, 9, 9, 2, 1, 2, 9,
            10, 2, 7, 10, 3, 2, 10, 9, 2, 8, 7, 1, 2, 3, 2, 10, 9, 2, 9, 8, 1,
            2, 10, 7, 2, 7, 10, 2, 3, 2, 7, 9, 2, 10, 7, 1, 2, 9, 9, 2, 8, 8,
            2, 3, 2, 3, 2, 10, 10, 2, 9, 9, 3, 2, 8, 8, 2, 9, 10, 2, 3, 2, 10,
            10, 2, 7, 8, 1, 2, 9, 8, 2, 7, 7, 3, 2, 3, 2, 10, 7, 2, 10, 8, 3,
            2, 7, 9, 2, 9, 8, 2, 3, 2, 10, 10, 2, 9, 10, 1, 2, 10, 7, 2, 10, 9,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]))
    assert i1m_str == 'add(sub(mul(sub(add(sub(6.0, 6.0), sub(x, 4.0)), add(sub(6.0, 5.0), sub(6.0, 6.0))), sub(add(sub(6.0, x), sub(4.0, x)), mul(sub(x, 6.0), sub(5.0, 4.0)))), add(sub(mul(sub(x, 6.0), sub(6.0, 5.0)), add(sub(x, 4.0), sub(4.0, x))), sub(mul(sub(4.0, 6.0), sub(x, 4.0)), add(sub(6.0, 6.0), sub(5.0, 5.0))))), sub(mul(sub(mul(sub(x, x), sub(6.0, 6.0)), mul(sub(5.0, 5.0), sub(6.0, x))), sub(mul(sub(x, x), sub(4.0, 5.0)), add(sub(6.0, 5.0), sub(4.0, 4.0)))), mul(sub(mul(sub(x, 4.0), sub(x, 5.0)), mul(sub(4.0, 6.0), sub(6.0, 5.0))), sub(mul(sub(x, x), sub(6.0, x)), add(sub(x, 4.0), sub(x, 6.0))))))'
예제 #17
0
def test_interpreter_run_typed_inputs():
    pset = pg.PrimitiveSet(typed=True)
    pset.addFunction(op.add, 2, [float, int, int])
    pset.addFunction(op.sub, 2, [int, float, float])
    pset.addFunction(op.mul, 2, [float, int, int])
    pset.addTerminal(1, [int])
    pset.addTerminal(2, [int])
    pset.addTerminal(3, [int])
    pset.addTerminal(4.0, [float])
    pset.addTerminal(5.0, [float])
    pset.addTerminal(6.0, [float])
    pset.addVariable("x", [float])
    pset.addVariable("y", [int])
    inputs = {"x": 7.0, "y": 8}
    tree = np.array([ 1,  2,  3,  2,  1,  2,  9,  9,  2, 10,  7,  1,  2,  9,  8,  2,  9,
        9,  2,  1,  2,  9, 10,  2,  7, 10,  3,  2, 10,  9,  2,  8,  7,  1,
        2,  3,  2, 10,  9,  2,  9,  8,  1,  2, 10,  7,  2,  7, 10,  2,  3,
        2,  7,  9,  2, 10,  7,  1,  2,  9,  9,  2,  8,  8,  2,  3,  2,  3,
        2, 10, 10,  2,  9,  9,  3,  2,  8,  8,  2,  9, 10,  2,  3,  2, 10,
       10,  2,  7,  8,  1,  2,  9,  8,  2,  7,  7,  3,  2,  3,  2, 10,  7,
        2, 10,  8,  3,  2,  7,  9,  2,  9,  8,  2,  3,  2, 10, 10,  2,  9,
       10,  1,  2, 10,  7,  2, 10,  9,  0])
    tree_run = pg.interpreter(pset, tree, run=True, vars_inputs=inputs)
    assert tree_run == 27.0