Beispiel #1
0
    def op_simplify(self):
        # If one of the arguments is zero, avoid the operation
        if self._node_arg_x_is_y(0, 0):
            return LeafNode(self._node_id, self._nodes[1].to_string())
        elif self._node_arg_x_is_y(1, 0):
            return LeafNode(self._node_id, self._nodes[0].to_string())

        # Non of the arguments are zero. Make the operation if they are not sensors
        if not self._nodes[0].is_sensor() and not self._nodes[1].is_sensor():
            arg = float(self._nodes[0].to_string()) + float(self._nodes[1].to_string())
            return LeafNode(self._node_id, process_float(arg))
        else:
            return self
Beispiel #2
0
    def op_simplify(self):
        if not self._nodes[0].is_sensor():
            lg.logger_.debug("[EXP_NODE] Value: " + self._nodes[0].to_string())
            try:
                arg = np.exp(float(self._nodes[0].to_string()))
            except OverflowError:
                # FIXME: See what to do with this expression, because there are problems when
                # an infinite value is the argument of a sinusoidal function
                return LeafNode(self._node_id, process_float(float("inf")))

            return LeafNode(self._node_id, process_float(arg))
        else:
            return self
Beispiel #3
0
    def op_simplify(self):
        # if both arguments are equals, return 0
        if self._nodes[0].to_string() == self._nodes[1].to_string():
            return LeafNode(self._node_id, process_float(0))

        # If the second argument is zero, avoid the operation.
        if self._node_arg_x_is_y(1, 0):
            return LeafNode(self._node_id, self._nodes[0].to_string())

        # Non of the arguments are zero. Make the operation if they are not sensors
        if not self._nodes[0].is_sensor() and not self._nodes[1].is_sensor():
            arg = float(self._nodes[0].to_string()) - float(self._nodes[1].to_string())
            return LeafNode(self._node_id, process_float(arg))
        else:
            return self
Beispiel #4
0
    def op_simplify(self):
        # If one or both of the arguments are zero, return zero
        if self._node_arg_x_is_y(0, 0) or self._node_arg_x_is_y(1, 0):
            return LeafNode(self._node_id, process_float(0))

        # If one of the arguments is zero, avoid the operation
        if self._node_arg_x_is_y(0, 1):
            return LeafNode(self._node_id, self._nodes[1].to_string())
        elif self._node_arg_x_is_y(1, 1):
            return LeafNode(self._node_id, self._nodes[0].to_string())

        if not self._nodes[0].is_sensor() and not self._nodes[1].is_sensor():
            arg = float(self._nodes[0].to_string()) * float(self._nodes[1].to_string())
            return LeafNode(self._node_id, process_float(arg))
        else:
            return self
Beispiel #5
0
    def op_simplify(self):
        # If the first argument is zero, return zero
        if self._node_arg_x_is_y(0, 0):
            return LeafNode(self._node_id, process_float(0))

        # If the second argument is one, return the first argument
        if self._node_arg_x_is_y(1, 1):
            return LeafNode(self._node_id, self._nodes[0].to_string())

        if not self._nodes[0].is_sensor() and not self._nodes[1].is_sensor():
            # FIXME: Harcoded number. Change it
            if abs(float(self._nodes[1].to_string())) < DivisionNode.SIMPLIFY_PROTECTION:
                return LeafNode(self._node_id, process_float(0))
            else:
                arg = float(self._nodes[0].to_string()) / float(self._nodes[1].to_string())
                return LeafNode(self._node_id, process_float(arg))
        else:
            return self
Beispiel #6
0
    def op_simplify(self):
        if not self._nodes[0].is_sensor():
            if float(self._nodes[0].to_string()) < LogarithmNode.SIMPLIFY_PROTECTION:
                arg = np.log(LogarithmNode.SIMPLIFY_PROTECTION)
            else:
                arg = np.log(float(self._nodes[0].to_string()))

            return LeafNode(self._node_id, process_float(arg))
        else:
            return self
    def _generate_leaf_node(self, expr, parent_depth, expr_index):
        # We found a Leaf Node
        param_len = 0

        find_space = expr.find(' ')
        find_close_parenthesis = expr.find(')')

        # Find the first space or colon
        if find_space != -1 and find_space < find_close_parenthesis:
            param_len = find_space
        elif find_close_parenthesis != -1:
            param_len = find_close_parenthesis
        else:
            param_len = len(expr)

        leaf = LeafNode(self._node_id_generator.next_node_id(), expr[:param_len])
        leaf.set_depth(parent_depth)
        leaf.set_expr_index(expr_index)
        leaf.set_subtreedepth(0)
        self._nodes.append(leaf)
        return leaf, param_len + 1
Beispiel #8
0
 def op_simplify(self):
     if not self._nodes[0].is_sensor():
         arg = np.tanh(float(self._nodes[0].to_string()))
         return LeafNode(self._node_id, process_float(arg))
     else:
         return self