def test_full_tree2_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]) init_max_depth = 7 max_depth = 7 tree = pg.full_tree(pset, init_max_depth, max_depth) assert np.array_equal(tree, np.array([ 3, 4, 2, 1, 1, 3, 8, 8, 1, 6, 7, 1, 1, 7, 12, 1, 12, 12, 3, 2, 3, 8, 9, 3, 11, 9, 4, 4, 11, 11, 2, 5, 12, 4, 4, 2, 3, 8, 8, 1, 7, 7, 4, 4, 11, 11, 4, 10, 9, 4, 2, 3, 10, 11, 1, 12, 12, 2, 1, 5, 7, 1, 5, 5, 2, 3, 4, 2, 3, 9, 9, 3, 8, 9, 2, 3, 11, 11, 1, 12, 7, 4, 2, 3, 10, 10, 3, 8, 11, 4, 4, 11, 9, 4, 9, 9, 3, 4, 4, 2, 7, 6, 4, 11, 9, 4, 4, 11, 9, 2, 12, 7, 4, 4, 2, 12, 5, 4, 11, 8, 4, 2, 6, 7, 2, 12, 6, 0]))
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_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_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_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)'
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_primitive_set_add_functions(): pset = pg.PrimitiveSet() pset.addFunction(op.add, 2) pset.addFunction(op.sub, 2) pset.addFunction(op.mul, 2) assert pset.typed is False assert pset.num_primitives == 3 assert list(pset.functions.keys()) == [1, 2, 3] assert pset.arity_cache == {2: [1, 2, 3]} assert pset.functions_types == {}
def test_primitive_set_add_typed_functions(): pset = pg.PrimitiveSet(typed=True) pset.addFunction(op.add, 2, [int, int, int]) pset.addFunction(op.sub, 2, [int, int, int]) pset.addFunction(op.mul, 2, [float, float, float]) assert pset.typed is True assert pset.num_primitives == 3 assert list(pset.functions.keys()) == [1, 2, 3] assert pset.arity_cache == {2: [1, 2, 3]} assert pset.functions_types == {int: [1, 2], float: [3]}
def test_transverse_tree(): 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") tree = np.array([1, 3, 1, 3, 4, 0, 0]) assert pg.transverse_tree(pset, tree, 0) == 5 assert pg.transverse_tree(pset, tree, 1) == 2 assert pg.transverse_tree(pset, tree, 2) == 5
def test_make_ephemeral_constants(): pset = pg.PrimitiveSet() pset.addFunction(op.add, 2) pset.addFunction(op.sub, 2) pset.addTerminal(float_constants, types=None, ephemeral=True) assert pset.num_primitives == 3 assert list(pset.functions.keys()) == [1, 2] assert list(pset.terminals.keys()) == [3] assert list(pset.variables.keys()) == [] assert pset.arity_cache == {2: [1, 2]} assert pset.primitives == set([op.add, op.sub, float_constants]) assert pset.ephemeral_cache == set([3])
def setup_primitive_set(): pset = pg.PrimitiveSet() # functions pset.addFunction(op.add, 2) pset.addFunction(op.sub, 2) pset.addFunction(op.mul, 2) pset.addFunction(protected_div, 2) # constants pset.addTerminal(random_ints, ephemeral=True) # variables pset.addVariable("x") return pset
def test_grow_tree3(): np.random.seed(45345) 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") init_max_depth = 2 max_depth = 2 tree = pg.full_tree(pset, init_max_depth, max_depth) assert np.array_equal(tree, np.array([1, 6, 5, 0]))
def test_count_tree_internals_typed(): 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]) tree1 = 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]) depth, nodes = pg.count_tree_internals(pset, tree1) assert depth == 6 assert nodes == 21 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]) tree2 = 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]) depth, nodes = pg.count_tree_internals(pset, tree2) assert depth == 5 assert nodes == 17
def test_duplicate_entries(): pset = pg.PrimitiveSet() pset.addFunction(op.add, 2) pset.addFunction(op.add, 2) pset.addTerminal(2) pset.addTerminal(2) pset.addVariable("x") pset.addVariable("x") assert pset.num_primitives == 3 assert list(pset.functions.keys()) == [1] assert list(pset.terminals.keys()) == [2] assert list(pset.variables.keys()) == [3] assert pset.arity_cache == {2: [1]} assert pset.primitives == set([op.add, 2, "x"])
def setup_primitive_set(): pset = pg.PrimitiveSet() # functions pset.addFunction(op.add, 2) pset.addFunction(op.sub, 2) pset.addFunction(op.mul, 2) pset.addFunction(protected_div, 2) # constants num_constants = 10 for i in range(num_constants): pset.addTerminal(np.random.randint(-5, 5)) # variables pset.addVariable("x") return pset
def test_grow_tree1(): np.random.seed(45345) pset = pg.PrimitiveSet(typed=True) pset.addFunction(op.add, 2, [int, int, int]) pset.addFunction(op.sub, 2, [int, int, int]) pset.addTerminal(1, [int]) pset.addTerminal(2, [int]) pset.addTerminal(3, [int]) pset.addVariable("x", [int]) init_max_depth = 3 max_depth = 6 tree = pg.grow_tree(pset, init_max_depth, max_depth) assert np.array_equal(tree, np.array([1, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
def test_grow_tree1(): 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") init_max_depth = 3 max_depth = 6 tree = pg.grow_tree(pset, init_max_depth, max_depth) assert np.array_equal(tree, np.array([4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
def setup_primitive_set(): pset = pg.PrimitiveSet(typed=True) # functions pset.addFunction(op.add, 2, [int, float, float]) pset.addFunction(op.sub, 2, [int, float, float]) pset.addFunction(float_add, 2, [float, int, int]) pset.addFunction(float_sub, 2, [float, int, int]) pset.addFunction(op.mul, 2, [int, int, int]) pset.addFunction(protected_div, 2, [float, float, float]) # constants pset.addTerminal(random_ints, [int], ephemeral=True) pset.addTerminal(random_floats, [float], ephemeral=True) # variables pset.addVariable("x", [float]) return pset
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_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))))))'
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
def test_max_size_from_tree_max_depth_arity3(): pset = pg.PrimitiveSet() pset.addFunction(op.add, 2) pset.addFunction(op.sub, 3) pset.addTerminal(1) pset.addTerminal(2) pset.addTerminal(3) pset.addTerminal(4) assert pg.max_size_from_tree_max_depth(pset, 4) == 40 assert pg.max_size_from_tree_max_depth(pset, 6) == 364 assert pg.max_size_from_tree_max_depth(pset, 8) == 3280 assert pg.max_size_from_tree_max_depth(pset, 10) == 29524 assert pg.max_size_from_tree_max_depth(pset, 12) == 265720 assert pg.max_size_from_tree_max_depth(pset, 14) == 2391484 assert pg.max_size_from_tree_max_depth(pset, 16) == 21523360 assert pg.max_size_from_tree_max_depth(pset, 18) == 193710244 assert pg.max_size_from_tree_max_depth(pset, 20) == 1743392200
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])}
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
def test_max_size_from_tree_max_depth_arity2(): 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) assert pg.max_size_from_tree_max_depth(pset, 4) == 16 assert pg.max_size_from_tree_max_depth(pset, 6) == 64 assert pg.max_size_from_tree_max_depth(pset, 8) == 256 assert pg.max_size_from_tree_max_depth(pset, 10) == 1024 assert pg.max_size_from_tree_max_depth(pset, 12) == 4096 assert pg.max_size_from_tree_max_depth(pset, 14) == 16384 assert pg.max_size_from_tree_max_depth(pset, 16) == 65536 assert pg.max_size_from_tree_max_depth(pset, 18) == 262144 assert pg.max_size_from_tree_max_depth(pset, 20) == 1048576
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])}
def test_count_tree_internals(): 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") tree1 = 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]) depth, nodes = pg.count_tree_internals(pset, tree1) assert depth == 7 assert nodes == 29 tree2 = 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]) depth, nodes = pg.count_tree_internals(pset, tree2) assert depth == 1 assert nodes == 1
def setup_primitive_set(): pset = pg.PrimitiveSet(typed=True) # functions pset.addFunction(op.add, 2, [int, float, float]) pset.addFunction(op.sub, 2, [int, float, float]) pset.addFunction(float_add, 2, [float, int, int]) pset.addFunction(float_sub, 2, [float, int, int]) pset.addFunction(op.mul, 2, [int, int, int]) pset.addFunction(protected_div, 2, [float, float, float]) # constants num_constants = 5 for i in range(num_constants): pset.addTerminal(np.random.randint(-5, 5), [int]) for i in range(num_constants): pset.addTerminal(np.random.uniform(), [float]) # variables pset.addVariable("x", [float]) return pset
def test_grow_tree3(): np.random.seed(12345) 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]) init_max_depth = 2 max_depth = 2 tree = pg.grow_tree(pset, init_max_depth, max_depth) assert np.array_equal(tree, np.array([3, 9, 9, 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)))'