def construct_diagram(): pool = Pool() pool.int_var("x", "y") b = Builder(pool) bounds = b.test("x", ">=", 0) & b.test("x", "<=", 8) & b.test("y", ">=", 1) & b.test("y", "<=", 10) return bounds * b.ite(b.test("x", ">=", "y"), b.terminal("2*x + 3*y"), b.terminal("3*x + 2*y"))
def test_multiplication(self): pool = Pool() pool.int_var("x1", "x2") x_two = Diagram(pool, pool.terminal("x2")) two = Diagram(pool, pool.terminal("2")) three = Diagram(pool, pool.terminal("3")) four = Diagram(pool, pool.terminal("4")) test11 = Diagram(pool, pool.bool_test(LinearTest("x1", ">="))) test12 = Diagram(pool, pool.bool_test(LinearTest("x1 - 1", "<="))) test13 = Diagram(pool, pool.bool_test(LinearTest("x1 - 3", ">"))) test21 = Diagram(pool, pool.bool_test(LinearTest("x2", ">="))) test22 = Diagram(pool, pool.bool_test(LinearTest("x2", ">"))) test23 = Diagram(pool, pool.bool_test(LinearTest("x2 - 1", ">"))) test24 = Diagram(pool, pool.bool_test(LinearTest("x2 - 2", ">"))) x_twos = test12 * ~test23 * x_two twos = test12 * test23 * two threes = ~test12 * ~test22 * three fours = ~test12 * test22 * four unlimited = x_twos + twos + threes + fours restricted = unlimited * test11 * ~test13 * test21 * ~test24 vector = test21 * ~test24 * Diagram(pool, pool.terminal("x2 + 1")) result = Diagram(pool, matrix_multiply(pool, restricted.root_node.node_id, vector.root_node.node_id, ["x2"])) for x1 in range(0, 4): self.assertEqual(8 if x1 < 2 else 23, result.evaluate({"x1": x1}))
def test_not(self): pool = Pool() pool.int_var("x") dd_true = Diagram(pool, pool.bool_test(LinearTest("x", ">="))) dd_false = Diagram(pool, pool.invert(dd_true.root_node.node_id)) for i in range(-5, 6): assignment = {"x": i} self.assertEqual((dd_true.evaluate(assignment) + 1) % 2, dd_false.evaluate(assignment))
def construct_diagram(): pool = Pool(empty=True) pool.int_var("x") x = pool.terminal("x") zero = pool.terminal("0") test1 = pool.internal(LinearTest("x - 5", "<="), x, zero) test2 = pool.internal(LinearTest("x + 1", ">="), test1, zero) test3 = pool.internal(LinearTest("x + 2", "<="), x, test2) root = pool.internal(LinearTest("x", ">="), test1, test3) return Diagram(pool, root)
def get_looping_diagram(): pool = Pool() pool.int_var("x") test = LinearTest("x", "<=", "2") zero = pool.terminal(0) one = pool.terminal(1) node1 = pool.internal(test, one, zero) node2 = pool.internal(test, node1, zero) diagram = pool.diagram(node2) return diagram
def setUp(self): pool = Pool() pool.int_var("x") lb = Diagram(pool, pool.bool_test(LinearTest("x - 1", ">="))) ub = Diagram(pool, pool.bool_test(LinearTest("x - 10", "<="))) test = Diagram(pool, pool.bool_test(LinearTest("x - 5", "<="))) term_one = Diagram(pool, pool.terminal("x + 2")) term_two = Diagram(pool, pool.terminal("7 - 2 * (x - 5)")) b1 = lb & ub & test * term_one b2 = lb & ub & ~test * term_two self.diagram = b1 + b2
def get_unordered_diagram(): pool = Pool() pool.int_var("x") test1 = LinearTest("x", "<=", "2") test2 = LinearTest("x", "<=", "3") zero = pool.terminal(0) one = pool.terminal(1) pool.internal(test1, one, zero) pool.internal(test2, one, zero) # test2 => test1 => 1 node1 = pool.internal(test1, one, zero) node2 = pool.internal(test2, node1, zero) diagram = pool.diagram(node2) return diagram
def setUp(self): pool = Pool() pool.int_var("x") pool.int_var("y") pool.int_var("z") b = Builder(pool) self.diagrams = [] d = b.ite(b.test("x", "<=", "y"), b.terminal(1), b.test("x", "<=", 2)) self.diagrams.append(({"x", "y"}, d)) d = b.terminal("x * 2 * y + 5 * z") self.diagrams.append(({"x", "y", "z"}, d)) d = b.ite(b.test("x", "<", "y"), b.terminal("z"), b.terminal("z * y")) self.diagrams.append(({"x", "y", "z"}, d))
def setUp(self): pool = Pool() pool.int_var("x") lb = Diagram(pool, pool.bool_test(LinearTest("x - 1", ">="))) ub = Diagram(pool, pool.bool_test(LinearTest("x - 10", "<="))) test = Diagram(pool, pool.bool_test(LinearTest("x - 5", "<="))) redundant_test = Diagram(pool, pool.bool_test(LinearTest("x - 6", "<="))) term_one = Diagram(pool, pool.terminal("x + 2")) term_two = Diagram(pool, pool.terminal("7 - 2 * (x - 5)")) b1 = (lb & ub & test & redundant_test) * term_one b2 = (lb & ub & ~test & redundant_test) * term_two self.diagram = b1 + b2 self.exporter = Exporter(os.path.join(os.path.dirname(os.path.realpath(__file__)), "visual"), "reduce")
def test_multiplication(self): pool = Pool() pool.int_var("x") two = pool.terminal("2") x = pool.terminal("x") test1 = pool.bool_test(LinearTest("x", ">=")) test2 = pool.apply(Multiplication, pool.bool_test(LinearTest("x - 5", "<=")), x) product = pool.apply(Multiplication, test1, test2) result = Diagram(pool, pool.apply(Multiplication, product, two)) for i in range(0, 10): evaluated = result.evaluate({"x": i}) if 0 <= i <= 5: self.assertEqual(2 * i, evaluated) else: self.assertEqual(0, evaluated)
def setUp(self): pool = Pool() pool.int_var("x") self.test1 = pool.bool_test(LinearTest("x", ">=")) self.test2 = pool.bool_test(LinearTest("x + 2", ">")) self.test3 = pool.bool_test(LinearTest("x + 1", "<=")) self.test4 = pool.bool_test(LinearTest("x - 5", "<=")) self.x = pool.terminal("x") p1 = pool.apply(Multiplication, self.test1, self.test4) p2 = pool.apply(Multiplication, pool.invert(self.test1), self.test2) p3 = pool.apply(Multiplication, pool.apply(Multiplication, pool.apply(Multiplication, pool.invert(self.test1), pool.invert(self.test2)), self.test3), self.test4) result = pool.apply(Summation, pool.apply(Summation, p1, p2), p3) result = pool.apply(Multiplication, result, self.x) self.diagram = Diagram(pool, result)
def setUp(self): pool = Pool() pool.int_var("x") self.test1 = pool.bool_test(LinearTest("x", ">=")) self.test2 = pool.bool_test(LinearTest("x + 2", ">")) self.test3 = pool.bool_test(LinearTest("x + 1", "<=")) self.test4 = pool.bool_test(LinearTest("x - 5", "<=")) self.x = pool.terminal("x") p1 = pool.apply(Multiplication, self.test1, self.test4) p2 = pool.apply(Multiplication, pool.invert(self.test1), self.test2) p3 = pool.apply( Multiplication, pool.apply( Multiplication, pool.apply(Multiplication, pool.invert(self.test1), pool.invert(self.test2)), self.test3), self.test4) result = pool.apply(Summation, pool.apply(Summation, p1, p2), p3) result = pool.apply(Multiplication, result, self.x) self.diagram = Diagram(pool, result)
def test_leaf_transform_simple_test(self): pool = Pool() pool.int_var("x") test1 = pool.bool_test(LinearTest("x", ">=")) leaf_transform.transform_leaves(lambda t, d: d.pool.terminal(2), pool.diagram(test1))