Esempio n. 1
0
    def test_edit_tree_inputs_and_terminals(self):
        # TEST INPUTS AND TERMINALS
        term_node_1 = Node(NodeType.CONSTANT, value=2.0)
        term_node_2 = Node(NodeType.INPUT, name="x")
        func_node = Node(NodeType.FUNCTION,
                         name="ADD",
                         arity=2,
                         branches=[term_node_1, term_node_2])

        print "BEFORE:", func_node
        tree = Tree()
        tree.root = func_node
        tree.depth = 3
        editor.edit_tree(tree, tree.root, self.functions)
        print "AFTER:", func_node
        print
Esempio n. 2
0
    def test_edit_tree_zero_only(self):
        # TEST CONTAINS ZERO
        term_node_1 = Node(NodeType.CONSTANT, value=0.0)
        term_node_2 = Node(NodeType.CONSTANT, value=1.0)
        func_node = Node(NodeType.FUNCTION,
                         name="ADD",
                         arity=2,
                         branches=[term_node_1, term_node_2])

        print "BEFORE:", func_node
        tree = Tree()
        tree.root = func_node
        tree.depth = 3
        editor.edit_tree(tree, tree.root, self.functions)
        print "AFTER:", func_node
        print

        self.assertEquals(func_node.value, 1.0)
Esempio n. 3
0
    def test_edit_tree_inputs_and_terminals(self):
        # TEST INPUTS AND TERMINALS
        term_node_1 = Node(NodeType.CONSTANT, value=2.0)
        term_node_2 = Node(NodeType.INPUT, name="x")
        func_node = Node(
            NodeType.FUNCTION,
            name="ADD",
            arity=2,
            branches=[term_node_1, term_node_2]
        )

        print "BEFORE:", func_node
        tree = Tree()
        tree.root = func_node
        tree.depth = 3
        editor.edit_tree(tree, tree.root, self.functions)
        print "AFTER:", func_node
        print
Esempio n. 4
0
    def test_edit_tree_prune(self):
        # TEST PRUNE
        term_node_1 = Node(NodeType.CONSTANT, value=0.0)
        term_node_2 = Node(NodeType.INPUT, name="x")
        func_node = Node(NodeType.FUNCTION,
                         name="MUL",
                         arity=2,
                         branches=[term_node_1, term_node_2])

        print "BEFORE:", func_node
        tree = Tree()
        tree.root = func_node
        tree.depth = 3
        editor.edit_tree(tree, tree.root, self.functions)
        print "AFTER:", func_node
        print

        self.assertEquals(func_node.node_type, NodeType.CONSTANT)
        self.assertIsNone(func_node.name)
        self.assertEquals(func_node.value, 0)
Esempio n. 5
0
    def test_edit_tree_terminals_only(self):
        # TEST TERMINALS ONLY
        term_node_1 = Node(NodeType.CONSTANT, value=2.0)
        term_node_2 = Node(NodeType.CONSTANT, value=1.0)
        func_node = Node(
            NodeType.FUNCTION,
            name="ADD",
            arity=2,
            branches=[term_node_1, term_node_2]
        )

        print "BEFORE:", func_node
        tree = Tree()
        tree.root = func_node
        tree.depth = 3
        editor.edit_tree(tree, tree.root, self.functions)
        print "AFTER:", func_node
        print

        self.assertEquals(func_node.value, 3.0)
Esempio n. 6
0
    def test_edit_tree_prune(self):
        # TEST PRUNE
        term_node_1 = Node(NodeType.CONSTANT, value=0.0)
        term_node_2 = Node(NodeType.INPUT, name="x")
        func_node = Node(
            NodeType.FUNCTION,
            name="MUL",
            arity=2,
            branches=[term_node_1, term_node_2]
        )

        print "BEFORE:", func_node
        tree = Tree()
        tree.root = func_node
        tree.depth = 3
        editor.edit_tree(tree, tree.root, self.functions)
        print "AFTER:", func_node
        print

        self.assertEquals(func_node.node_type, NodeType.CONSTANT)
        self.assertIsNone(func_node.name)
        self.assertEquals(func_node.value, 0)