Example #1
0
    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)
Example #2
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)
Example #3
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)
Example #4
0
    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)