How to use the expression.Node function in Expression

To help you get started, we’ve selected a few Expression examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github StanislavUshakov / ArtificialImmuneSystem / tests.py View on Github external
def test_value_in_point(self):
        node = Node(Operations.MINUS,
                left=Node(Operations.IDENTITY, value='x'),
                right=Node(Operations.IDENTITY, value='y'))
        result = node.value_in_point({'x': 2, 'y': 1})
        self.assertEqual(result, 1.0)
github StanislavUshakov / ArtificialImmuneSystem / tests.py View on Github external
def setUp(self):
        root = Node(Operations.PLUS,
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='x'),
                right=Node(Operations.NUMBER, value=4)),
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='y'),
                right=Node(Operations.NUMBER, value=2)))
        self.f = Expression(root=root, variables=['x', 'y'])
github StanislavUshakov / ArtificialImmuneSystem / tests.py View on Github external
def test_wrong_value(self):
        wrong = Node(Operations.MINUS,
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='x'),
                right=Node(Operations.NUMBER, value=4)),
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='y'),
                right=Node(Operations.NUMBER, value=2)))
        e = Expression(root=wrong, variables=['x', 'y'])
        self.assertGreater(self.f.expression_value(e), 0.0)
github StanislavUshakov / ArtificialImmuneSystem / tests.py View on Github external
def test_pickle_node(self):
        node = Node(Operations.PLUS,
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='x'),
                right=Node(Operations.NUMBER, value=4)),
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='y'),
                right=Node(Operations.NUMBER, value=2)))
        returned_node = pickle.loads(pickle.dumps(node))
        self.assertEqual(node.operation._operation_type, returned_node.operation._operation_type)
        self.assertEqual(node.operation.action, returned_node.operation.action)
        self.assertEqual(node.value, returned_node.value)
        self.assertEqual(node.left.operation._operation_type, returned_node.left.operation._operation_type)
        self.assertEqual(node.left.operation.action, returned_node.left.operation.action)
        self.assertEqual(node.left.value, returned_node.left.value)
        self.assertEqual(node.right.operation._operation_type, returned_node.right.operation._operation_type)
        self.assertEqual(node.right.operation.action, returned_node.right.operation.action)
        self.assertEqual(node.right.value, returned_node.right.value)
github StanislavUshakov / ArtificialImmuneSystem / tests.py View on Github external
def test_wrong_value(self):
        wrong = Node(Operations.MINUS,
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='x'),
                right=Node(Operations.NUMBER, value=4)),
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='y'),
                right=Node(Operations.NUMBER, value=2)))
        e = Expression(root=wrong, variables=['x', 'y'])
        self.assertGreater(self.f.expression_value(e), 0.0)
github StanislavUshakov / ArtificialImmuneSystem / tests.py View on Github external
def test_exact_value(self):
        answer = Node(Operations.PLUS,
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='x'),
                right=Node(Operations.NUMBER, value=4)),
            Node(Operations.MULTIPLICATION,
                left=Node(Operations.IDENTITY, value='y'),
                right=Node(Operations.NUMBER, value=2)))
        e = Expression(root=answer, variables=['x', 'y'])
        self.assertEqual(self.f.expression_value(e), 0.0)
github StanislavUshakov / ArtificialImmuneSystem / tests.py View on Github external
def test_simplify_multiply_by_one_left(self):
        node = Node(Operations.MULTIPLICATION,
            left=Node(Operations.NUMBER, value=1),
            right=Node(Operations.IDENTITY, value='x'))
        result = node.simplify()
        self.assertEqual(result, True)
        self.assertEqual(node.operation, Operations.IDENTITY)
        self.assertEqual(node.value, 'x')
github StanislavUshakov / ArtificialImmuneSystem / expression.py View on Github external
node.value = Expression.generate_number()
                continue

            if node.is_variable():
                node.value = random.choice(variables)
                continue

            if node.is_unary():
                node.left = Node(Expression.generate_operator())
                if root.height() > max_height:
                    node.left = None
                else:
                    current.append(node.left)

            if node.is_binary():
                node.left = Node(Expression.generate_operator())
                node.right = Node(Expression.generate_operator())
                if root.height() > max_height:
                    node.left = None
                    node.right = None
                else:
                    current.append(node.left)
                    current.append(node.right)

        #turn all leaves into numbers or variables
        leaves = []
        def traverse_tree(node):
            if node.is_number() or node.is_variable():
                return
            if node.is_unary():
                if node.left is None:
                    leaves.append(node)