Exemple #1
0
 def test_deepcopy(self):
     tree = ExpTree()
     tree.apply_binary_op(tree.root, operations.add)
     tree2 = tree.copy()
     self.assertEqual(str(tree), str(tree2))
     
     tree2.apply_binary_op(tree2.root.lhs, operations.mul)
     self.assertNotEqual(str(tree), str(tree2))
Exemple #2
0
def example_tree():

    tree = ExpTree()
    leaf = tree.leaves[0]
    print("initial tree: " + str(tree))

    leaf = tree.apply_times_c(leaf)
    print("expanded with apply_times_c(): " + str(tree))

    leaf = tree.apply_plus_c(leaf)
    print("expanded with apply_plus_c(): " + str(tree))

    left_leaf, right_leaf = tree.apply_binary_op(leaf, ops.mul)
    print("expanded with apply_binary_op(mul): " + str(tree))

    tree.apply_unary_op(left_leaf, ops.sin)
    print("expanded left leaf with apply_unary_op(sin): " + str(tree))

    tree.apply_unary_op(right_leaf, ops.log)
    print("expanded right leaf with apply_unary_op(log): " + str(tree))

    expr = tree.to_expr()
    print("tree as sympy expression: " + str(expr))

    print("number of leaves: " + str(len(tree.leaves)))
    print("constants: " + str(tree.constants))
Exemple #3
0
    def generate_random_exptree(self, depth):
        tree = ExpTree()
        d = 0
        while (d < depth):

            # Make SHALLOW copy of leaves array:
            to_expand = []
            for leaf in tree.leaves:
                to_expand.append(leaf)

            for leaf in to_expand:

                # Apply +c or *c possibly:
                r = random.random()
                if r < self.p_addc:
                    tree.apply_plus_c(leaf)
                elif r < 2 * self.p_addc:
                    tree.apply_times_c(leaf)


                # Otherwise, choose a unary or binary op:
                else:
                    op = random.choice(self.all_ops);
                    if op in ops.unary_ops:
                        tree.apply_unary_op(leaf, op)
                    else:
                        tree.apply_binary_op(leaf, op)
            d += 1

        return tree
Exemple #4
0
def problem_case():
    exptree = ExpTree()
    exptree.apply_binary_op(exptree.root, ops.add)
    l1, l2 = exptree.leaves[0], exptree.leaves[1]
    exptree.apply_unary_op(l1, ops.sin)
    exptree.apply_unary_op(l2, ops.tan)

    x, y = gen_data.get_data(exptree, [])

    print exptree.root.collapse()
    print
    astar = AStar(x, y, False)



    err = np.array([x for x in astar.best_err if not math.isnan(x) and not math.isinf(x)])
    print err
    plt.plot(range(len(err)), err)
    plt.axis([-.1, len(err)+1, -1, max(err)+1])
    plt.show()
Exemple #5
0
def problem_case():
    exptree = ExpTree()
    exptree.apply_binary_operation(exptree.root, operations.add)
    exptree.apply_binary_operation(exptree.leaves[0], operations.cos)
    exptree.apply_binary_operation(exptree.leaves[1], operations.sin)
    print exptree