Beispiel #1
0
def test_tree_space_functions_setter():
    try:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, functions="a")
    except:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, functions=["SUM"])

    assert len(new_tree_space.functions) == 1
Beispiel #2
0
def test_tree_space_functions_setter():
    try:
        new_tree_space = tree.TreeSpace(functions='a')
    except:
        new_tree_space = tree.TreeSpace(functions=['SUM'])

    assert len(new_tree_space.functions) == 1
Beispiel #3
0
def test_tree_space_best_tree_setter():
    try:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1)
        new_tree_space.best_tree = "a"
    except:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1)
        new_tree_space.best_tree = node.Node(name="0", category="FUNCTION")

    assert isinstance(new_tree_space.best_tree, node.Node)
Beispiel #4
0
def test_tree_space_best_tree_setter():
    try:
        new_tree_space = tree.TreeSpace()
        new_tree_space.best_tree = 'a'
    except:
        new_tree_space = tree.TreeSpace()
        new_tree_space.best_tree = node.Node(name='0', type='FUNCTION')

    assert isinstance(new_tree_space.best_tree, node.Node)
Beispiel #5
0
def test_tree_space_trees_setter():
    try:
        new_tree_space = tree.TreeSpace()
        new_tree_space.trees = 'a'
    except:
        new_tree_space = tree.TreeSpace()
        new_tree_space.trees = []

    assert len(new_tree_space.trees) == 0
Beispiel #6
0
def test_tree_space_terminals_setter():
    try:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1)
        new_tree_space.terminals = "a"
    except:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1)
        new_tree_space.terminals = []

    assert len(new_tree_space.terminals) == 0
Beispiel #7
0
def test_tree_space_max_depth_setter():
    try:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, max_depth=0.0)
    except:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, max_depth=1)

    try:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, max_depth=0)
    except:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, max_depth=1)

    assert new_tree_space.max_depth == 1
Beispiel #8
0
def test_tree_space_n_terminals_setter():
    try:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, n_terminals=0.0)
    except:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, n_terminals=1)

    try:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, n_terminals=0)
    except:
        new_tree_space = tree.TreeSpace(1, 1, 0, 1, n_terminals=1)

    assert new_tree_space.n_terminals == 1
Beispiel #9
0
def test_tree_space_min_depth_setter():
    try:
        new_tree_space = tree.TreeSpace(min_depth=0.0)
    except:
        new_tree_space = tree.TreeSpace(min_depth=1)

    try:
        new_tree_space = tree.TreeSpace(min_depth=0)
    except:
        new_tree_space = tree.TreeSpace(min_depth=1)

    assert new_tree_space.min_depth == 1
Beispiel #10
0
def test_tree_space_n_trees_setter():
    try:
        new_tree_space = tree.TreeSpace(n_trees=0.0)
    except:
        new_tree_space = tree.TreeSpace(n_trees=1)

    try:
        new_tree_space = tree.TreeSpace(n_trees=0)
    except:
        new_tree_space = tree.TreeSpace(n_trees=1)

    assert new_tree_space.n_trees == 1
Beispiel #11
0
def test_tree_space_grow():
    new_tree_space = tree.TreeSpace(min_depth=1, max_depth=5)

    new_tree = new_tree_space.grow(new_tree_space.min_depth,
                                   new_tree_space.max_depth)

    assert isinstance(new_tree, node.Node)
Beispiel #12
0
def test_tree_space_create_trees():
    new_tree_space = tree.TreeSpace(n_trees=2)

    new_tree_space.trees, new_tree_space.best_tree = new_tree_space._create_trees(
    )

    assert len(new_tree_space.trees) == 2
Beispiel #13
0
def test_gp_run():
    def square(x):
        return np.sum(x**2)

    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=square)

    new_gp = gp.GP()

    tree_space = tree.TreeSpace(n_trees=10,
                                n_terminals=2,
                                n_variables=1,
                                n_iterations=500,
                                min_depth=1,
                                max_depth=2,
                                functions=['SUM', 'SUB', 'MUL', 'DIV'],
                                lower_bound=[0],
                                upper_bound=[10])

    history = new_gp.run(tree_space, new_function, pre_evaluation=hook)

    print(tree_space.best_tree)
    print(tree_space.best_tree.post_order)

    tree_space = tree.TreeSpace(
        n_trees=10,
        n_terminals=2,
        n_variables=1,
        n_iterations=500,
        min_depth=2,
        max_depth=3,
        functions=['EXP', 'LOG', 'SQRT', 'ABS', 'COS', 'SIN'],
        lower_bound=[0],
        upper_bound=[10])

    history = new_gp.run(tree_space, new_function, pre_evaluation=hook)

    print(tree_space.best_tree)
    print(tree_space.best_tree.post_order)

    assert len(history.agents) > 0
    assert len(history.best_agent) > 0

    best_fitness = history.best_agent[-1][1]
    assert best_fitness <= constants.TEST_EPSILON, 'The algorithm gp failed to converge.'
Beispiel #14
0
def test_tree_space_grow():
    new_tree_space = tree.TreeSpace(
        1, 1, 0, 1, min_depth=1, max_depth=5, functions=['SUM', 'SUB', 'MUL', 'DIV'])

    new_tree = new_tree_space.grow(
        new_tree_space.min_depth, new_tree_space.max_depth)

    assert isinstance(new_tree, node.Node)
Beispiel #15
0
def test_gp_reproduction():
    new_gp = gp.GP()

    tree_space = tree.TreeSpace(n_agents=10,
                                n_terminals=2,
                                n_variables=1,
                                min_depth=1,
                                max_depth=5,
                                functions=['SUM'],
                                lower_bound=[0],
                                upper_bound=[10])

    new_gp._reproduction(tree_space)
Beispiel #16
0
def test_gp_update():
    new_gp = gp.GP()

    tree_space = tree.TreeSpace(n_agents=10,
                                n_terminals=2,
                                n_variables=1,
                                min_depth=1,
                                max_depth=2,
                                functions=['SUM', 'SUB', 'MUL', 'DIV'],
                                lower_bound=[0],
                                upper_bound=[10])

    new_gp.update(tree_space)
Beispiel #17
0
def test_gp_cross():
    new_gp = gp.GP()

    tree_space = tree.TreeSpace(n_agents=10,
                                n_terminals=2,
                                n_variables=1,
                                min_depth=1,
                                max_depth=5,
                                functions=['SUM'],
                                lower_bound=[0],
                                upper_bound=[10])

    new_gp._cross(tree_space.trees[0], tree_space.trees[1], 1, 1)
Beispiel #18
0
def test_tree_space_grow():
    new_tree_space = tree.TreeSpace(1,
                                    1,
                                    0,
                                    1,
                                    min_depth=1,
                                    max_depth=5,
                                    functions=["SUM", "SUB", "MUL", "DIV"])

    new_tree = new_tree_space.grow(new_tree_space.min_depth,
                                   new_tree_space.max_depth)

    assert isinstance(new_tree, node.Node)
Beispiel #19
0
def test_gp_crossover():
    new_gp = gp.GP()

    tree_space = tree.TreeSpace(
        n_agents=10,
        n_terminals=2,
        n_variables=1,
        min_depth=1,
        max_depth=5,
        functions=["SUM"],
        lower_bound=[0],
        upper_bound=[10],
    )

    new_gp._crossover(tree_space)
Beispiel #20
0
def test_gp_update():
    new_gp = gp.GP()

    tree_space = tree.TreeSpace(
        n_agents=10,
        n_terminals=2,
        n_variables=1,
        min_depth=1,
        max_depth=2,
        functions=["SUM", "SUB", "MUL", "DIV"],
        lower_bound=[0],
        upper_bound=[10],
    )

    new_gp.update(tree_space)
Beispiel #21
0
def test_gp_evaluate():
    def square(x):
        return np.sum(x**2)

    new_function = function.Function(pointer=square)

    tree_space = tree.TreeSpace(n_agents=1000,
                                n_terminals=2,
                                n_variables=1,
                                min_depth=1,
                                max_depth=5,
                                functions=['SUM'],
                                lower_bound=[0],
                                upper_bound=[10])

    new_gp = gp.GP()

    new_gp.evaluate(tree_space, new_function)

    assert tree_space.best_agent.fit < sys.float_info.max
Beispiel #22
0
def test_tree_space_create_trees():
    new_tree_space = tree.TreeSpace(2, 1, 0, 1)

    new_tree_space._create_trees()

    assert len(new_tree_space.trees) == 2
Beispiel #23
0
def test_tree_space_trees():
    new_tree_space = tree.TreeSpace(1, 1, 0, 1)

    assert len(new_tree_space.trees) == 1
Beispiel #24
0
def test_tree_space_functions():
    new_tree_space = tree.TreeSpace(1, 1, 0, 1, functions=["SUM"])

    assert len(new_tree_space.functions) == 1
Beispiel #25
0
def test_tree_space_best_tree():
    new_tree_space = tree.TreeSpace(1, 1, 0, 1)

    assert isinstance(new_tree_space.best_tree, node.Node)
Beispiel #26
0
def test_tree_space_max_depth():
    new_tree_space = tree.TreeSpace(1, 1, 0, 1, max_depth=1)

    assert new_tree_space.max_depth == 1
Beispiel #27
0
def test_tree_space_n_terminals():
    new_tree_space = tree.TreeSpace(1, 1, 0, 1)

    assert new_tree_space.n_terminals == 1
Beispiel #28
0
def test_tree_space_initialize_terminals():
    new_tree_space = tree.TreeSpace(1, 1, 0, 1)

    assert new_tree_space.terminals[0].position[0] != 0
Beispiel #29
0
def test_tree_space_initialize_agents():
    new_tree_space = tree.TreeSpace(1, 1, 0, 1)

    assert new_tree_space.agents[0].position[0] != 0
Beispiel #30
0
def test_tree_space_create_terminals():
    new_tree_space = tree.TreeSpace(1, 1, 0, 1, n_terminals=2)

    new_tree_space._create_terminals()

    assert len(new_tree_space.terminals) == 2