def test_equal_tree_individual(): 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) i1 = pop.individuals[0] i2 = i1.clone() assert i1.equal(i2) is True pop = pg.make_tree_population(1, pset, 7, 8, init_method=pg.full_tree) i3 = pop.individuals[0] assert i1.equal(i3) is False
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))))))'
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)))'
def test_make_tree_population_ramped_half_and_half_tree(): import operator as op np.random.seed(42) size = 1000 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") initial_max_depth = 6 max_depth = 8 pop = pg.make_tree_population(size, pset, initial_max_depth, max_depth, init_method=pg.ramped_half_and_half_tree) assert type(pop) is pg.Population assert pop.size == size assert pop.individuals.size == size for i in range(size): assert type(pop.individuals[i]) is pg.TreeIndividual assert pop.individuals[i].depth <= initial_max_depth pos = 0 while pos < pop.individuals[i].genotype.size and pop.individuals[ i].genotype[pos] != 0: node = pop.individuals[i].genotype[pos] assert node in pset.functions or node in pset.terminals or node in pset.variables pos += 1
def test_apply_crossover(): np.random.seed(42) size = 50 rate = 1.0 operator = pg.tree_crossover initial_max_depth = 6 max_depth = 12 pset = pg.PrimitiveSet() pset.addFunction(op.add, 2) pset.addFunction(op.sub, 2) pset.addFunction(op.mul, 2) pset.addFunction(protected_div, 2) pset.addTerminal(1) pset.addTerminal(2) pset.addTerminal(3) pset.addTerminal(4) pset.addTerminal(5) pset.addTerminal(6) pset.addTerminal(7) pset.addTerminal(8) pset.addTerminal(9) pset.addVariable("x") pop = pg.make_tree_population(size, pset, initial_max_depth, max_depth, init_method=pg.full_tree) original_pop = pop.clone() pop = pg.apply_crossover(pop, rate, operator, pset=pset) for i in range(pop.size): assert pop.individuals[i].run_eval is True assert np.array_equal(pop.individuals[i].genotype, original_pop.individuals[i].genotype) is not True
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)))'
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)))'
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))))))'
def test_tree_crossover_maxdepth(): 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, 4, init_method=pg.full_tree) i1 = pop.individuals[0].clone() i2 = pop.individuals[1].clone() i1m, i2m = pg.tree_crossover(i1, i2, pset=pset) # offpsring1 passes max depth, returns parent1 assert i1m.depth == i1.depth assert i1m.nodes == i1.nodes assert np.array_equal(i1.genotype, i1m.genotype) # offspring 2 is legal 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]))
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))))))'
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'
def test_make_tree_population_grow_typed(): import operator as op np.random.seed(42) size = 1000 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]) initial_max_depth = 8 max_depth = 10 pop = pg.make_tree_population(size, pset, initial_max_depth, max_depth, init_method=pg.grow_tree) assert type(pop) is pg.Population assert pop.size == size assert pop.individuals.size == size for i in range(size): assert type(pop.individuals[i]) is pg.TreeIndividual assert pop.individuals[i].depth <= initial_max_depth pos = 0 while pos < pop.individuals[i].genotype.size and pop.individuals[ i].genotype[pos] != 0: node = pop.individuals[i].genotype[pos] assert node in pset.functions or node in pset.terminals or node in pset.variables pos += 1