Esempio n. 1
0
    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))
Esempio n. 2
0
    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)))
Esempio n. 3
0
    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))
Esempio n. 4
0
    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)
Esempio n. 5
0
    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))
Esempio n. 6
0
 def test_nNodes(self):
     monosat.Monosat().newSolver()
     g = monosat.Graph()
     for i in range(10):
         g.addNode()
         self.assertEqual(g.nNodes(), i + 1)
Esempio n. 7
0
def build_mgraph(fab, placed_comps):
    '''
        Build a MonoSAT graph. Includes every connection box and switch box but only placed components
        Note: For now, there are connection boxes below and to the right of every CLB and a switch box for each CB
              This is not representative of the true fabric -- will be updated later

        Example of coordinate system and naming scheme:

                        (i,j)CLB---(i,j)CBr
                           |          |
                        (i,j)CBb---(i,j)SB

                        r = right
                        b = bottom
                        (i,j) = zero-indexed (x,y) coordinates with origin in top left of fabric
    '''
    g = ms.Graph()
    #add all the placed components to the graph
    fab.populate_CLBs(fab, placed_comps, g)
    #add all internal connection boxes and switch boxes on fabric
    CBr = [[g.addNode('({},{})CB_r'.format(x,y)) for y in range(fab.rows)] for x in range(fab.cols - 1)]
    CBb = [[g.addNode('({},{})CB_b'.format(x,y)) for y in range(fab.rows - 1)] for x in range(fab.cols)]
    SB = [[g.addNode('({},{})SB'.format(x,y)) for y in range(fab.rows-1)] for x in range(fab.cols-1)]

    #Create dictionary to keep track of edges used by CBs
    CBedges = {}
    for row in CBr:
        for n in row:
            CBedges[n] = []
    for row in CBb:
        for n in row:
            CBedges[n] = []
    
    #add edges
    for y in range(fab.rows):
        for x in range(fab.cols):
            #TODO make less checks (or set up outside of loop)
            if x < fab.cols-1 and y < fab.rows-1:
                e1 = g.addUndirectedEdge(CBr[x][y], SB[x][y])
                e2 = g.addUndirectedEdge(CBb[x][y], SB[x][y])
                CBedges[CBr[x][y]].append(e1)
                CBedges[CBb[x][y]].append(e2)
            if (x,y) in fab.CLBs:
                if x < fab.cols - 1:
                    e = g.addUndirectedEdge(fab.CLBs[(x,y)], CBr[x][y])
                    CBedges[CBr[x][y]].append(e)
                if y < fab.rows - 1:
                    e = g.addUndirectedEdge(fab.CLBs[(x,y)], CBb[x][y])
                    CBedges[CBb[x][y]].append(e)
                if x > 0:
                    e = g.addUndirectedEdge(CBr[x-1][y], fab.CLBs[(x,y)])
                    CBedges[CBr[x-1][y]].append(e)
                if y > 0:
                    e = g.addUndirectedEdge(CBb[x][y-1], fab.CLBs[(x,y)])
                    CBedges[CBb[x][y-1]].append(e)
            if x > 0 and x <= fab.cols - 1 and y < fab.rows - 1:
                e = g.addUndirectedEdge(SB[x-1][y], CBb[x][y])
                CBedges[CBb[x][y]].append(e)
            if y > 0 and x < fab.cols - 1 and y <= fab.rows - 1:
                e = g.addUndirectedEdge(SB[x][y-1], CBr[x][y])
                CBedges[CBr[x][y]].append(e)
    fab.populate_edge_dict(g.getEdgeVars())
    return g, CBr, CBb, SB, CBedges
Esempio n. 8
0
 def add_graph(self, layer):
     g = ms.Graph()
     self.graphs[layer] = g
     return g
Esempio n. 9
0
 def add_graph(self):
     g = ms.Graph()
     self.graphs.append(g)
     return g