Exemple #1
0
    def test_rename_diagram(self):
        translation = {"b": "c", "c": "b"}
        result1 = self.diagram1.pool.diagram(rename.rename(self.diagram1, translation))
        print(order.is_ordered(result1))

        translation = {"b": "c", "c": "b"}
        result2 = self.diagram1.pool.diagram(rename.rename(self.diagram2, translation))
        print(order.is_ordered(result2))
    def test_rename_diagram(self):
        translation = {"b": "c", "c": "b"}
        result1 = self.diagram1.pool.diagram(
            rename.rename(self.diagram1, translation))
        print(order.is_ordered(result1))

        translation = {"b": "c", "c": "b"}
        result2 = self.diagram1.pool.diagram(
            rename.rename(self.diagram2, translation))
        print(order.is_ordered(result2))
Exemple #3
0
    def test_inversion(self):
        pool = Pool()
        build = Builder(pool)
        build.vars("bool", "a", "b")
        build.vars("int", "x")

        test1 = build.test("a")
        test2 = build.test("b")
        test3 = build.test("x", "<=", 5)

        node3 = build.ite(test3, 1, 0)
        diagram = build.ite(test1, build.ite(test2, node3, 1), node3)

        self.assertTrue(is_ordered(diagram))

        def inversion1(root_id):
            minus_one = pool.terminal("-1")
            return pool.apply(Multiplication, pool.apply(Summation, root_id, minus_one), minus_one)

        def transform(terminal_node, d):
            if terminal_node.expression == 1:
                return d.pool.zero_id
            elif terminal_node.expression == 0:
                return d.pool.one_id
            else:
                raise RuntimeError("Could not invert value {}".format(terminal_node.expression))

        def inversion2(root_id):
            to_invert = pool.diagram(root_id)
            profile = WalkingProfile(diagram)
            return leaf_transform.transform_leaves(transform, to_invert)

        iterations = 1000
        timer = Timer(precision=6)
        timer.start("Legacy inversion")
        for _ in range(iterations):
            inversion1(diagram.root_id)
        time_legacy = timer.stop()

        inverted1 = pool.diagram(inversion1(diagram.root_id))

        timer.start("New inversion")
        for _ in range(iterations):
            inversion2(diagram.root_id)
        time_new = timer.stop()

        inverted2 = pool.diagram(inversion2(diagram.root_id))

        for a in [True, False]:
            for b in [True, False]:
                for x in range(10):
                    assignment = {"a": a, "b": b, "x": x}
                    self.assertNotEqual(diagram.evaluate(assignment), inverted1.evaluate(assignment))
                    self.assertNotEqual(diagram.evaluate(assignment), inverted2.evaluate(assignment))

        self.assertTrue(time_legacy > time_new, "New inversion ({}) not faster than legacy implementation ({})"
                        .format(time_new, time_legacy))
Exemple #4
0
    def build_diagram2():
        build = Builder()
        build.vars("int", "x", "y")

        test1 = build.test("x", "<=", "y")
        test2 = build.test("y", "<=", 5)
        test3 = build.test("x", ">=", 5)

        exp1 = build.exp(5)
        exp2 = build.exp("2 * x")

        node3 = build.ite(test3, exp1, exp2)
        node2 = build.ite(test2, node3, exp1)
        node1 = build.ite(test1, node2, node3)

        if not is_ordered(node1):
            raise RuntimeError("Diagram not ordered")

        return node1
Exemple #5
0
    def build_diagram2():
        build = Builder()
        build.vars("int", "x", "y")

        test1 = build.test("x", "<=", "y")
        test2 = build.test("y", "<=", 5)
        test3 = build.test("x", ">=", 5)

        exp1 = build.exp(5)
        exp2 = build.exp("2 * x")

        node3 = build.ite(test3, exp1, exp2)
        node2 = build.ite(test2, node3, exp1)
        node1 = build.ite(test1, node2, node3)

        if not is_ordered(node1):
            raise RuntimeError("Diagram not ordered")

        return node1
Exemple #6
0
    def test_inversion(self):
        pool = Pool()
        build = Builder(pool)
        build.vars("bool", "a", "b")
        build.vars("int", "x")

        test1 = build.test("a")
        test2 = build.test("b")
        test3 = build.test("x", "<=", 5)

        node3 = build.ite(test3, 1, 0)
        diagram = build.ite(test1, build.ite(test2, node3, 1), node3)

        self.assertTrue(is_ordered(diagram))

        def inversion1(root_id):
            minus_one = pool.terminal("-1")
            return pool.apply(Multiplication,
                              pool.apply(Summation, root_id, minus_one),
                              minus_one)

        def transform(terminal_node, d):
            if terminal_node.expression == 1:
                return d.pool.zero_id
            elif terminal_node.expression == 0:
                return d.pool.one_id
            else:
                raise RuntimeError("Could not invert value {}".format(
                    terminal_node.expression))

        def inversion2(root_id):
            to_invert = pool.diagram(root_id)
            profile = WalkingProfile(diagram)
            return leaf_transform.transform_leaves(transform, to_invert)

        iterations = 1000
        timer = Timer(precision=6)
        timer.start("Legacy inversion")
        for _ in range(iterations):
            inversion1(diagram.root_id)
        time_legacy = timer.stop()

        inverted1 = pool.diagram(inversion1(diagram.root_id))

        timer.start("New inversion")
        for _ in range(iterations):
            inversion2(diagram.root_id)
        time_new = timer.stop()

        inverted2 = pool.diagram(inversion2(diagram.root_id))

        for a in [True, False]:
            for b in [True, False]:
                for x in range(10):
                    assignment = {"a": a, "b": b, "x": x}
                    self.assertNotEqual(diagram.evaluate(assignment),
                                        inverted1.evaluate(assignment))
                    self.assertNotEqual(diagram.evaluate(assignment),
                                        inverted2.evaluate(assignment))

        self.assertTrue(
            time_legacy > time_new,
            "New inversion ({}) not faster than legacy implementation ({})".
            format(time_new, time_legacy))
Exemple #7
0
 def test_is_ordered_on_ordered_diagram(self):
     diagram = self.get_ordered_diagram()
     self.assertTrue(is_ordered(diagram))
Exemple #8
0
 def test_reorder_unordered(self):
     ordered = order(self.get_unordered_diagram())
     self.assertTrue(is_ordered(ordered))
Exemple #9
0
 def test_reorder_looping(self):
     ordered = order(self.get_looping_diagram())
     self.assertTrue(is_ordered(ordered))
Exemple #10
0
 def test_is_ordered_on_unordered_diagram(self):
     diagram = self.get_unordered_diagram()
     self.assertFalse(is_ordered(diagram))