def test_equal(self): # create nodes left_node_1 = Node(NodeType.CONSTANT, value=1.0) right_node_1 = Node(NodeType.CONSTANT, value=2.0) left_node_2 = Node(NodeType.CONSTANT, value=3.0) right_node_2 = Node(NodeType.CONSTANT, value=4.0) cos_func_1 = Node(NodeType.FUNCTION, name="COS", arity=1, branches=[left_node_1]) sin_func_1 = Node(NodeType.FUNCTION, name="SIN", arity=1, branches=[right_node_1]) cos_func_2 = Node(NodeType.FUNCTION, name="COS", arity=1, branches=[left_node_2]) sin_func_2 = Node(NodeType.FUNCTION, name="SIN", arity=1, branches=[right_node_2]) add_func = Node(NodeType.FUNCTION, name="ADD", arity=2, branches=[cos_func_1, sin_func_1]) sub_func = Node(NodeType.FUNCTION, name="SUB", arity=2, branches=[sin_func_2, cos_func_2]) # create tree_1 tree_1 = Tree() tree_1.root = add_func tree_1.update() # create tree_2 tree_2 = Tree() tree_2.root = sub_func tree_2.update() self.assertTrue(tree_1.equals(tree_1)) self.assertFalse(tree_1.equals(tree_2)) self.assertTrue(tree_2.equals(tree_2)) self.assertFalse(tree_2.equals(tree_1))
def test_eval_tree(self): # create terminal nodes term_node = Node(NodeType.CONSTANT, value=100.0) input_node = Node(NodeType.INPUT, name="x") # create function nodes mul_func = Node( NodeType.FUNCTION, name="MUL", arity=2, branches=[input_node, term_node] ) rad_func = Node( NodeType.FUNCTION, name="RAD", arity=1, branches=[mul_func] ) sin_func = Node( NodeType.FUNCTION, name="SIN", arity=1, branches=[rad_func] ) # create tree tree = Tree() tree.root = sin_func tree.update() # evaluate tree score, output = evaluator.eval_tree(tree, self.functions, self.config) self.assertEquals(round(score, 7), 0.5000001)
def test_replace_node(self): # setup node_x = Node(NodeType.INPUT, name="x") node_y = Node(NodeType.INPUT, name="y") add_func = Node( NodeType.FUNCTION, name="ADD", arity=2, branches=[node_x, node_y] ) # build tree tree = Tree() tree.root = add_func tree.update_program() # replace input node new_node = Node(NodeType.INPUT, name="z") before_replace = list(tree.program) tree.replace_node(node_x, new_node) after_replace = list(tree.program) # assert self.assertTrue(before_replace == before_replace) self.assertTrue(after_replace == after_replace) self.assertFalse(before_replace == after_replace) self.assertTrue(add_func.branches[0] is new_node)
def test_generate_eq_function(self): # create terminal nodes term_node = Node(NodeType.CONSTANT, value=100.0) input_node = Node(NodeType.INPUT, name="x") # create function nodes mul_func = Node(NodeType.FUNCTION, name="MUL", arity=2, branches=[input_node, term_node]) rad_func = Node(NodeType.FUNCTION, name="RAD", arity=1, branches=[mul_func]) sin_func = Node(NodeType.FUNCTION, name="SIN", arity=1, branches=[rad_func]) # create tree tree = Tree() tree.root = sin_func tree.update() # generate equation function eq_func = evaluator.generate_eq_function(tree, self.functions, self.config) # assert self.assertIsNotNone(eq_func) self.assertEquals(round(eq_func(1), 4), 0.9848)
def test_eval_tree(self): # create terminal nodes term_node = Node(NodeType.CONSTANT, value=100.0) input_node = Node(NodeType.INPUT, name="x") # create function nodes mul_func = Node(NodeType.FUNCTION, name="MUL", arity=2, branches=[input_node, term_node]) rad_func = Node(NodeType.FUNCTION, name="RAD", arity=1, branches=[mul_func]) sin_func = Node(NodeType.FUNCTION, name="SIN", arity=1, branches=[rad_func]) # create tree tree = Tree() tree.root = sin_func tree.update() # evaluate tree score, output = evaluator.eval_tree(tree, self.functions, self.config) self.assertEquals(round(score, 7), 0.5000001)
def test_replace_node(self): # setup node_x = Node(NodeType.INPUT, name="x") node_y = Node(NodeType.INPUT, name="y") add_func = Node(NodeType.FUNCTION, name="ADD", arity=2, branches=[node_x, node_y]) # build tree tree = Tree() tree.root = add_func tree.update_program() # replace input node new_node = Node(NodeType.INPUT, name="z") before_replace = list(tree.program) tree.replace_node(node_x, new_node) after_replace = list(tree.program) # assert self.assertTrue(before_replace == before_replace) self.assertTrue(after_replace == after_replace) self.assertFalse(before_replace == after_replace) self.assertTrue(add_func.branches[0] is new_node)
def full_method(self): # initialize tree tree = Tree() tree.size = 1 tree.depth = self.max_depth tree.root = self.generate_func_node() tree.tree_type = self.gen_config.get("tree_type", None) # build tree via full method self.full_method_build_tree(tree, tree.root, 0) tree.update() return tree
def test_generate_eq_function_multivars(self): # create terminal nodes term_node = Node(NodeType.INPUT, name="var2") input_node = Node(NodeType.INPUT, name="var1") # create function nodes div_func = Node( NodeType.FUNCTION, name="DIV", arity=2, branches=[input_node, term_node] ) # create tree tree = Tree() tree.root = div_func tree.update() # generate equation function config = { "input_variables": [ { "type": "INPUT", "name": "var1" }, { "type": "INPUT", "name": "var2" } ], "functions": { "ADD": "+", "SUB": "-", "MUL": "*", "DIV": "/", "POW": "**", "SIN": "math.sin", "COS": "math.cos", "RAD": "math.radians", "LN": "math.ln", "LOG": "math.log" } } eq_func = evaluator.generate_eq_function(tree, self.functions, config) # assert self.assertIsNotNone(eq_func) self.assertEquals(eq_func(1.0, 2.0), 0.5)
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
def generate_tree_from_dict(self, tree_dict): tree = Tree() stack = [] tree.tree_id = tree_dict["id"] for node_dict in tree_dict["program"]: node_type = node_dict["type"] node = None if node_type == NodeType.INPUT: node = Node( NodeType.INPUT, name=node_dict.get("name", None) ) tree.program.append(node) stack.append(node) elif node_type == NodeType.CONSTANT: node = Node( NodeType.CONSTANT, name=node_dict.get("name", None), value=node_dict.get("value", None) ) tree.program.append(node) stack.append(node) elif node_type == NodeType.FUNCTION: value_nodes = [] for i in xrange(node_dict["arity"]): value_nodes.append(stack.pop()) node = Node( NodeType.FUNCTION, name=node_dict["name"], arity=node_dict["arity"], branches=value_nodes ) tree.program.append(node) stack.append(node) if node_dict.get("root", False): tree.root = node return tree
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
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)
def test_generate_eq_function_multivars(self): # create terminal nodes term_node = Node(NodeType.INPUT, name="var2") input_node = Node(NodeType.INPUT, name="var1") # create function nodes div_func = Node(NodeType.FUNCTION, name="DIV", arity=2, branches=[input_node, term_node]) # create tree tree = Tree() tree.root = div_func tree.update() # generate equation function config = { "input_variables": [{ "type": "INPUT", "name": "var1" }, { "type": "INPUT", "name": "var2" }], "functions": { "ADD": "+", "SUB": "-", "MUL": "*", "DIV": "/", "POW": "**", "SIN": "math.sin", "COS": "math.cos", "RAD": "math.radians", "LN": "math.ln", "LOG": "math.log" } } eq_func = evaluator.generate_eq_function(tree, self.functions, config) # assert self.assertIsNotNone(eq_func) self.assertEquals(eq_func(1.0, 2.0), 0.5)
def test_generate_eq_function(self): # create terminal nodes term_node = Node(NodeType.CONSTANT, value=100.0) input_node = Node(NodeType.INPUT, name="x") # create function nodes mul_func = Node( NodeType.FUNCTION, name="MUL", arity=2, branches=[input_node, term_node] ) rad_func = Node( NodeType.FUNCTION, name="RAD", arity=1, branches=[mul_func] ) sin_func = Node( NodeType.FUNCTION, name="SIN", arity=1, branches=[rad_func] ) # create tree tree = Tree() tree.root = sin_func tree.update() # generate equation function eq_func = evaluator.generate_eq_function( tree, self.functions, self.config ) # assert self.assertIsNotNone(eq_func) self.assertEquals(round(eq_func(1), 4), 0.9848)
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)
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)
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)
def test_equal(self): # create nodes left_node_1 = Node(NodeType.CONSTANT, value=1.0) right_node_1 = Node(NodeType.CONSTANT, value=2.0) left_node_2 = Node(NodeType.CONSTANT, value=3.0) right_node_2 = Node(NodeType.CONSTANT, value=4.0) cos_func_1 = Node( NodeType.FUNCTION, name="COS", arity=1, branches=[left_node_1] ) sin_func_1 = Node( NodeType.FUNCTION, name="SIN", arity=1, branches=[right_node_1] ) cos_func_2 = Node( NodeType.FUNCTION, name="COS", arity=1, branches=[left_node_2] ) sin_func_2 = Node( NodeType.FUNCTION, name="SIN", arity=1, branches=[right_node_2] ) add_func = Node( NodeType.FUNCTION, name="ADD", arity=2, branches=[cos_func_1, sin_func_1] ) sub_func = Node( NodeType.FUNCTION, name="SUB", arity=2, branches=[sin_func_2, cos_func_2] ) # create tree_1 tree_1 = Tree() tree_1.root = add_func tree_1.update() # create tree_2 tree_2 = Tree() tree_2.root = sub_func tree_2.update() self.assertTrue(tree_1.equals(tree_1)) self.assertFalse(tree_1.equals(tree_2)) self.assertTrue(tree_2.equals(tree_2)) self.assertFalse(tree_2.equals(tree_1))