def test_output(self): # To write to an output file, create a new solver and pass it the name of a file to write to # All constraints created in that new solver will be copied to that file test_file = tempfile.NamedTemporaryFile() filename = test_file.name test_file.close() monosat.Monosat().newSolver(output_file=filename) a = monosat.Var() b = monosat.Var() c = monosat.Or(a, monosat.Not(b)) monosat.Assert(c) monosat.Assert(monosat.Not(c)) # trivial contradition, so result should be UNSAT result = monosat.Solve() self.assertFalse(result) monosat.Monosat().newSolver( ) # create a new solver, this time without any output file set. # solver now has no constraints, so it should be SAT self.assertTrue(monosat.Solve()) # read in the previously saved constraints (these can also be read in on the command line, eg ./monosat tutorial.gnf monosat.Monosat().loadConstraints(filename) result2 = monosat.Solve() self.assertEqual(result, result2) os.remove(filename)
def test_setOutputFile(self): # this tests the deprecated setOutputFile interface test_file = tempfile.NamedTemporaryFile() filename = test_file.name test_file.close() monosat.Monosat().newSolver(arguments="-decide-theories") with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") monosat.Monosat().setOutputFile(output_file=filename) self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) self.assertTrue("deprecated" in str(w[-1].message)) a = monosat.Var() b = monosat.Var() c = monosat.Or(a, monosat.Not(b)) monosat.Assert(c) monosat.Assert(monosat.Not(c)) # trivial contradition, so result should be UNSAT result = monosat.Solve() self.assertFalse(result) monosat.Monosat().newSolver( ) # create a new solver, this time without any output file set. # solver now has no constraints, so it should be SAT self.assertTrue(monosat.Solve()) # read in the previously saved constraints (these can also be read in on the command line, eg ./monosat tutorial.gnf monosat.Monosat().loadConstraints(filename) result2 = monosat.Solve() self.assertEqual(result, result2) os.remove(filename)
def test_solve(self): monosat.Monosat().newSolver() self.assertTrue(monosat.solver.Solve()) self.assertTrue(monosat.solver.Solve()) monosat.logic.AssertTrue(monosat.logic.true()) self.assertTrue(monosat.solver.Solve()) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") monosat.logic.AssertFalse(monosat.logic.true()) self.assertTrue( "Asserted a trivial contradiction" in str(w[-1].message)) self.assertFalse(monosat.solver.Solve()) monosat.Monosat().newSolver() self.assertTrue(monosat.solver.Solve())
def test_maximumFlow_geq(self): monosat.Monosat().newSolver() g = monosat.Graph() for i in range(4): g.addNode() # create a directed square graph with one diagonal edge # # 0 *--* 1 # |/ | # 2 *--* 3 e_0_1 = g.addEdge(0, 1, monosat.BitVector(4, 1)) e_0_2 = g.addEdge(0, 2, monosat.BitVector(4, 1)) e_1_3 = g.addEdge(1, 3, monosat.BitVector(4, 1)) e_1_2 = g.addEdge(1, 2, monosat.BitVector(4, 2)) e_2_3 = g.addEdge(2, 3, monosat.BitVector(4, 1)) cmp = monosat.BitVector(4) f = g.maxFlowGreaterOrEqualTo(0, 3, cmp) self.assertTrue(monosat.Solve(f)) self.assertFalse(monosat.Solve(f, cmp.eq(3))) self.assertTrue(monosat.Solve(f, cmp.eq(2))) self.assertTrue(monosat.Solve(f, cmp.eq(1))) self.assertTrue(monosat.Solve(f, e_0_1.Not(), e_2_3.Not())) self.assertFalse(monosat.Solve(f, e_0_1.Not(), e_2_3.Not(), cmp.eq(1))) self.assertTrue(monosat.Solve(f, e_0_2.Not(), e_1_3.Not())) self.assertTrue(monosat.Solve(f))
def __init__(self): super().__init__() ms.Monosat().init('-decide-theories -route') # dictionary to graphs with arbitrary index # often layer --> graph self.graphs = dict() self.at_most_one_builtin_size = 10
def test_distance(self): monosat.Monosat().newSolver() g = monosat.Graph() for i in range(4): g.addNode() # create a directed square graph with one diagonal edge # # 0 *--* 1 # |/ | # 2 *--* 3 e_0_1 = g.addEdge(0, 1, monosat.BitVector(4, 1)) e_0_2 = g.addEdge(0, 2, monosat.BitVector(4, 1)) e_1_3 = g.addEdge(1, 3, monosat.BitVector(4, 1)) e_1_2 = g.addEdge(1, 2, monosat.BitVector(4, 2)) e_2_3 = g.addEdge(2, 3, monosat.BitVector(4, 1)) dist = monosat.BitVector(4) d = g.distance_leq(0, 3, dist) monosat.AssertTrue(d) self.assertTrue(monosat.Solve(dist.gt(0))) self.assertFalse(monosat.Solve(dist.eq(0))) self.assertFalse(monosat.Solve(dist.eq(1))) self.assertTrue(monosat.Solve(dist.eq(2))) self.assertTrue(monosat.Solve(dist.eq(3))) self.assertFalse(monosat.Solve(e_0_1, e_0_2, e_1_3, e_2_3, dist.eq(1))) self.assertTrue(monosat.Solve(e_0_1, e_0_2, e_1_3, e_2_3, dist.eq(2))) self.assertTrue(monosat.Solve(e_0_1, e_0_2, e_1_3, e_2_3, dist.eq(3))) self.assertFalse(monosat.Solve(e_0_1.Not(), e_2_3.Not())) self.assertTrue(monosat.Solve(e_0_2.Not(), e_1_3.Not())) self.assertFalse( monosat.Solve(e_0_2.Not(), e_1_3.Not(), e_2_3.Not(), dist.eq(1)))
def test_assertNand(self): monosat.Monosat().newSolver() a = monosat.logic.Var() b = monosat.logic.Var() monosat.logic.AssertNand(a, b) self.assertFalse(monosat.solver.Solve(a, b)) self.assertTrue(monosat.solver.Solve(monosat.logic.Not(a))) self.assertTrue(monosat.solver.Solve(b)) self.assertTrue(monosat.solver.Solve(monosat.logic.Not(b)))
def test_testSolveAssumps(self): monosat.Monosat().newSolver() self.assertTrue(monosat.solver.Solve()) self.assertTrue(monosat.solver.Solve()) self.assertTrue(monosat.solver.Solve(monosat.logic.true())) self.assertFalse(monosat.solver.Solve(monosat.logic.false())) self.assertFalse( monosat.solver.Solve(monosat.logic.true(), monosat.logic.false())) self.assertTrue(monosat.solver.Solve())
def test_assertNors(self): monosat.Monosat().newSolver() a = monosat.logic.Var() b = monosat.logic.Var() c = monosat.logic.Var() monosat.logic.AssertNor(a, b, c) self.assertFalse(monosat.solver.Solve(a, b, c)) self.assertTrue(monosat.solver.Solve(monosat.logic.Not(a))) self.assertFalse(monosat.solver.Solve(b)) self.assertTrue(monosat.solver.Solve(monosat.logic.Not(c))) self.assertTrue( monosat.solver.Solve(monosat.logic.Not(a), monosat.logic.Not(b), monosat.logic.Not(c)))
def test_assertAtMostOne(self): monosat.Monosat().newSolver() a = monosat.logic.Var() b = monosat.logic.Var() c = monosat.logic.Var() monosat.pbtheory.AssertAtMostOne([a, b, c]) self.assertFalse(monosat.solver.Solve(a, b)) self.assertFalse(monosat.solver.Solve(b, c)) self.assertFalse(monosat.solver.Solve(a, c)) self.assertTrue(monosat.solver.Solve(a, b.Not())) self.assertTrue(monosat.solver.Solve(b)) self.assertTrue(monosat.solver.Solve(c), True)
def test_deprecated_init(self): monosat.Monosat().newSolver() # tests the deprecated init function with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") monosat.Monosat().init("-decide-theories") self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) self.assertTrue("deprecated" in str(w[-1].message)) self.assertTrue(monosat.solver.Solve()) self.assertTrue(monosat.solver.Solve()) monosat.logic.AssertTrue(monosat.logic.true()) self.assertTrue(monosat.solver.Solve()) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") monosat.logic.AssertFalse(monosat.logic.true()) self.assertTrue( "Asserted a trivial contradiction" in str(w[-1].message)) self.assertFalse(monosat.solver.Solve()) monosat.Monosat().newSolver() self.assertTrue(monosat.solver.Solve())
def test_xnor(self): monosat.Monosat().newSolver() a = monosat.logic.Var() b = monosat.logic.Var() result = monosat.logic.Xnor(a, b) self.assertTrue(monosat.solver.Solve(a, b, result)) self.assertFalse(monosat.solver.Solve(a, monosat.logic.Not(b), result)) self.assertTrue( monosat.solver.Solve(a, monosat.logic.Not(b), monosat.logic.Not(result))) self.assertFalse( monosat.solver.Solve(monosat.logic.Not(a), monosat.logic.Not(b), monosat.logic.Not(result))) self.assertTrue(monosat.solver.Solve(a, b, result))
def test_onPath(self): monosat.Monosat().newSolver(output_file="/tmp/test.gnf") g = monosat.Graph() for i in range(4): g.addNode() # create a directed square graph with one diagonal edge # # 0 *--* 1 # |/ | # 2 *--* 3 e_0_1 = g.addEdge(0, 1) e_0_2 = g.addEdge(0, 2) e_1_3 = g.addEdge(1, 3) e_1_2 = g.addEdge(1, 2) e_2_3 = g.addEdge(2, 3) r = g.onPath(1, 0, 3) r2 = g.onPath(1, 0, 3) self.assertFalse(monosat.Solve(r, ~e_0_1)) self.assertFalse(monosat.Solve(r, e_0_1.Not(), e_2_3.Not())) self.assertTrue(monosat.Solve(r, e_0_2.Not(), e_1_3.Not())) self.assertFalse(monosat.Solve(r, e_1_3.Not(), e_2_3.Not())) self.assertFalse( monosat.Solve(r, e_0_2.Not(), e_1_3.Not(), e_2_3.Not())) self.assertTrue(monosat.Solve(r, e_0_2.Not(), e_1_3.Not())) # There should only be one solution to this: 0->1, 1->2, 2->3 nodes = g.getPath(r, False) edges = g.getPath(r, True) self.assertEqual(len(edges), 3) self.assertEqual(len(nodes), 4) self.assertEqual(edges[0], e_0_1) self.assertEqual(edges[1], e_1_2) self.assertEqual(edges[2], e_2_3) self.assertTrue(e_0_1.value()) self.assertTrue(e_1_2.value()) self.assertTrue(e_2_3.value()) self.assertFalse(e_0_2.value()) self.assertFalse(e_1_3.value()) self.assertTrue(monosat.Solve(r)) self.assertTrue(r.value()) self.assertTrue(r2.value()) self.assertEqual(g.getPath(r, False), g.getPath(r2, False)) self.assertEqual(g.getPath(r, True), g.getPath(r2, True))
def test_nors(self): monosat.Monosat().newSolver() a = monosat.logic.Var() b = monosat.logic.Var() c = monosat.logic.Var() result = monosat.logic.Nor(a, b, c) self.assertFalse(monosat.solver.Solve(a, b, c, result)) self.assertFalse( monosat.solver.Solve(a, monosat.logic.Not(b), c, result)) self.assertTrue( monosat.solver.Solve(monosat.logic.Not(a), monosat.logic.Not(b), monosat.logic.Not(c), result)) self.assertFalse( monosat.solver.Solve(monosat.logic.Not(a), monosat.logic.Not(b), c, result)) self.assertFalse(monosat.solver.Solve(a, b, c, result))
def test_nEdges(self): monosat.Monosat().newSolver() g = monosat.Graph() for i in range(4): g.addNode() # create a directed square graph with one diagonal edges # # 0 *--* 1 # |/ | # 2 *--* 3 e_0_1 = g.addEdge(0, 1) e_0_2 = g.addEdge(0, 2) e_1_3 = g.addEdge(1, 3) e_1_2 = g.addEdge(1, 2) e_2_3 = g.addEdge(2, 3) self.assertEqual(g.nEdges(), 5)
def test_newLit(self): monosat.Monosat().newSolver() a = monosat.logic.Var() self.assertNotEqual(a, monosat.logic.true()) b = monosat.logic.Var() self.assertNotEqual(a, b) self.assertNotEqual(a.lit, b.lit) self.assertTrue(monosat.solver.Solve(a)) self.assertTrue(monosat.solver.Solve(b)) self.assertTrue(monosat.solver.Solve(monosat.logic.Not(a))) self.assertTrue(monosat.solver.Solve(monosat.logic.Not(b))) self.assertTrue(monosat.solver.Solve(a)) self.assertTrue(monosat.solver.Solve(b)) self.assertTrue(monosat.solver.Solve(monosat.logic.Not(a), b)) self.assertFalse(monosat.solver.Solve(monosat.logic.Not(a), a)) self.assertTrue(monosat.solver.Solve(a)) self.assertTrue(monosat.solver.Solve(b))
def test_acyclicUndirected(self): monosat.Monosat().newSolver() g = monosat.Graph() for i in range(4): g.addNode() # create a directed square graph with two diagonal edges # # 0 *--* 1 # |/\| # 2 *--* 3 e_0_1 = g.addEdge(0, 1) e_0_2 = g.addEdge(0, 2) e_1_3 = g.addEdge(1, 3) e_1_2 = g.addEdge(1, 2) e_2_3 = g.addEdge(2, 3) e_3_0 = g.addEdge(3, 0) r = g.acyclic(False) self.assertTrue(monosat.Solve(r)) self.assertTrue(monosat.Solve(r, e_0_1.Not(), e_2_3.Not())) self.assertTrue(monosat.Solve(r, e_0_2.Not(), e_1_3.Not())) self.assertTrue(monosat.Solve(r, e_0_2.Not(), e_1_3.Not(), e_2_3.Not())) self.assertFalse(monosat.Solve(r, e_0_1, e_1_2, e_2_3, e_3_0)) self.assertTrue(monosat.Solve(r)) self.assertTrue(monosat.Solve(r.Not())) # valid undirected cycle: e_3_0 -> e_0_2 -> e_1_2 -> e_1_3 self.assertTrue(monosat.Solve(r.Not(), e_0_1.Not(), e_2_3.Not())) self.assertFalse( monosat.Solve(r.Not(), e_0_1.Not(), e_2_3.Not(), e_1_3.Not())) self.assertTrue(monosat.Solve(r.Not(), e_0_2.Not(), e_1_3.Not())) self.assertFalse( monosat.Solve(r.Not(), e_0_2.Not(), e_1_3.Not(), e_2_3.Not())) self.assertTrue(monosat.Solve(r.Not(), e_0_1, e_1_2, e_2_3, e_3_0)) self.assertTrue(monosat.Solve(r.Not())) self.assertTrue(monosat.Solve(r))
def test_unsatCore(self): monosat.Monosat().newSolver() a = monosat.logic.Var() b = monosat.logic.Var() c = monosat.logic.Var() d = monosat.logic.Var() x = monosat.logic.Equal(a, b) y = monosat.logic.Equal(b, c.Not()) z = monosat.logic.Equal(c, a) w = monosat.logic.Equal(b, d.Not()) q = monosat.logic.Equal(d, a) self.assertTrue(monosat.solver.Solve()) self.assertFalse(monosat.solver.Solve(x, y, z, w, q)) conflict = monosat.solver.getConflictClause() self.assertFalse(len(conflict) == 0) test = [] for l in conflict: assert (not l.sign()) test.append(l.Not()) self.assertFalse(monosat.solver.Solve(test)) self.assertFalse(monosat.solver.Solve(x, y, z, w, q)) conflict2 = monosat.solver.getConflictClause(True) self.assertFalse(len(conflict2) == 0) self.assertTrue(len(conflict2) <= len(conflict)) test2 = [] for l in conflict2: assert (not l.sign()) test2.append(l.Not()) self.assertFalse(monosat.solver.Solve(test2)) self.assertTrue(monosat.solver.Solve()) conflict3 = monosat.solver.minimizeUnsatCore([x, y, z, w, q]) self.assertFalse(len(conflict3) == 0) self.assertTrue(len(conflict3) <= 3) self.assertTrue(monosat.solver.Solve())
def test_ite(self): monosat.Monosat().newSolver() a = monosat.logic.Var() b = monosat.logic.Var() c = monosat.logic.Var() result = monosat.logic.Ite(c, a, b) self.assertTrue( monosat.solver.Solve(c, a, monosat.logic.Not(b), result)) self.assertFalse( monosat.solver.Solve(c, a, monosat.logic.Not(b), monosat.logic.Not(result))) self.assertTrue( monosat.solver.Solve(c, monosat.logic.Not(a), monosat.logic.Not(b), monosat.logic.Not(result))) self.assertFalse( monosat.solver.Solve(monosat.logic.Not(c), a, monosat.logic.Not(b), result)) self.assertTrue( monosat.solver.Solve(monosat.logic.Not(c), a, monosat.logic.Not(b), monosat.logic.Not(result))) self.assertTrue( monosat.solver.Solve(monosat.logic.Not(c), monosat.logic.Not(a), b, result))
def reset(self): super().reset() self.graphs = dict() ms.Monosat().init('-decide-theories -route')
def reset(self): super().reset() self.graphs = [] ms.Monosat().init()
def test_nNodes(self): monosat.Monosat().newSolver() g = monosat.Graph() for i in range(10): g.addNode() self.assertEqual(g.nNodes(), i + 1)
def __init__(self): super().__init__() ms.Monosat().init() # could also use -decide-theories self.graphs = []