def test_is_assignable_std(self):
        scenarios = []
        scenarios.append([
            [(1, 2), (2, 1), (2, 3), (4, 3), (5, 4), (5, 6), (6, 5), (7, 6), (7, 7), (7, 5)],
            [[7]],
            [[6]]
        ])
        scenarios.append([
            [(1, 2), (2, 1), (2, 3), (3, 4), (5, 4), (5, 6), (6, 5), (7, 6), (7, 7), (7, 5)],
            [[1, 2]],
            [[3]]
        ])
        scenarios.append([
            [(1, 2), (2, 1), (2, 3), (5, 3), (5, 6), (6, 5), (7, 6), (7, 7), (7, 5)],
            [[1, 2], [7]],
            [[3], [6]]
        ])
        #2nd batch
        scenarios.append([
              [(1,2),(2,1),(3,3),(2,4),(3,4),(3,5)],
              [[1,2],[3]],
              [[4],[4,5]]
        ])

        scenarios.append([
              [(1,2),(2,1),(3,3),(2,4),(3,4),(3,5),(5,4)],
              [[3]],
              [[5]]
        ])

        scenarios.append([
              [(1,2),(2,1),(1,3),(2,4),(3,4),(4,3),(6,6),(6,3)],
              [],
              []
        ])

        for scenario in scenarios:
            print scenario[0]
            G = lightnx.DiGraph()
            G.add_edges_from(scenario[0])
            scc_pm_nt, Gprime = matchings.get_S_nt_rm_Gprime(G)
            assignable_sorted = map(sorted, scenario[1])
            msize = len(matchings.matching(Gprime))
            assign = []
            for scc in scc_pm_nt:
                back_edges = Gprime.edges()[:]
                if matchings.is_assignable(scc, Gprime, msize):
                    assign.append(scc)
                    #print "asig"
                    try:
                        idx = assignable_sorted.index(sorted(scc.graph.nodes()))
                    except:
                        idx = None
                    assert_equals(sorted(back_edges) ,sorted(Gprime.edges()))
                    assert_equals(idx is None, False)
                    assert_equals(scc.assignable_points, set(scenario[2][idx]))
            assert_equals(len(assign),len(assignable_sorted))
 def test_is_compatible(self):
     scenarios = []
     scenarios.append([[(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4),
                        (5, 6), (6, 5), (3, 7), (4, 7)], [[1, 2]], [5, 6],
                       False])
     scenarios.append([[(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4),
                        (5, 6), (6, 5), (3, 7), (4, 7), (3, 8), (4, 8)],
                       [[1, 2]], [5, 6], True])
     scenarios.append([[(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4),
                        (5, 6), (6, 5), (3, 7), (4, 7), (3, 8)], [[1, 2]],
                       [5, 6], True])
     scenarios.append([[(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4),
                        (5, 6), (6, 5), (3, 7), (4, 8)], [[1, 2]], [5, 6],
                       True])
     scenarios.append([[(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4),
                        (5, 6), (6, 5), (3, 7), (4, 8), (10, 9), (9, 10),
                        (9, 3), (9, 4)], [[9, 10]], [5, 6], True])
     scenarios.append([[(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4),
                        (5, 6), (6, 5), (3, 7), (4, 8), (10, 9), (9, 10),
                        (9, 3), (9, 4)], [[1, 2]], [9, 10], True])
     scenarios.append([[(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4),
                        (5, 6), (6, 5), (3, 7), (4, 8), (10, 9), (9, 10),
                        (9, 3), (9, 4)], [[1, 2]], [5, 6], True])
     scenarios.append([[(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4),
                        (5, 6), (6, 5), (3, 7), (4, 8), (10, 9), (9, 10),
                        (9, 3), (9, 4)], [[1, 2], [6, 5]], [10, 9], False])
     for scenario in scenarios:
         print 30 * "="
         print scenario[0]
         G = lightnx.DiGraph()
         G.add_edges_from(scenario[0])
         scc_pm_nt, Gprime = matchings.get_S_nt_rm_Gprime(G)
         msize = len(matchings.matching(Gprime))
         assign = []
         for scc in scc_pm_nt:
             if matchings.is_assignable(scc, Gprime, msize):
                 assign.append(scc)
         compatibles = []
         scc_test = None
         for scc in assign:
             if sorted(scc.graph.nodes()) in map(sorted, scenario[1]):
                 compatibles.append(scc)
             if sorted(scc.graph.nodes()) == sorted(scenario[2]):
                 scc_test = scc
         print[com.graph.nodes() for com in compatibles]
         print scc.graph.nodes()
         assert_equals(len(scenario[1]), len(compatibles))
         assert_equals(scc_test == None, False)
         compatible, compatibleSCCs = matchings.isCompatible(
             compatibles, scc_test, Gprime, msize)
         print compatible
         assert_equals(compatible, scenario[3])
         if compatible:
             outnodes = [scc.comp_node for scc in compatibleSCCs]
             assert_equals(len(outnodes), len(set(outnodes)))
             for scc in compatibleSCCs:
                 print scc.comp_node, scc.graph.nodes()
    def test_get_S_nt_rm_Gprime(self):
        G= lightnx.DiGraph()
        G.add_edges_from([(1,2),(2,1),(2,3),(3,4),(4,3),(5,4),(5,6),(6,5),(7,6),(7,7),(7,5)])
        scc_pm_nt,gprime = matchings.get_S_nt_rm_Gprime(G)
        assert_equals(len(scc_pm_nt),2)
        def check_results(scc_pm_nt):
            assert_equals(set(scc_pm_nt[0].graph.nodes()),set([1,2]))
            assert_equals(set(scc_pm_nt[1].graph.nodes()),set([7]))
            assert_equals(scc_pm_nt[0].outnodes,set([3]))
            assert_equals(set(scc_pm_nt[0].outlinks),set([(2,3)]))
            assert_equals(scc_pm_nt[1].outnodes,set([6,5]))
            assert_equals(set(scc_pm_nt[1].outlinks),set([(7,6),(7,5)]))

        if len(scc_pm_nt[0].graph.nodes())==2:
            check_results(scc_pm_nt)
        else:
            check_results(scc_pm_nt[1:0])
        assert_equals(set(gprime.nodes()),set([3,4,5,6]))
        assert_equals(set(gprime.predecessors(3)),set([4]))
        assert_equals(set(gprime.successors(3)),set([4]))
    def test_is_assignable_std(self):
        scenarios = []
        scenarios.append([[(1, 2), (2, 1), (2, 3), (4, 3), (5, 4), (5, 6),
                           (6, 5), (7, 6), (7, 7), (7, 5)], [[7]], [[6]]])
        scenarios.append([[(1, 2), (2, 1), (2, 3), (3, 4), (5, 4), (5, 6),
                           (6, 5), (7, 6), (7, 7), (7, 5)], [[1, 2]], [[3]]])
        scenarios.append([[(1, 2), (2, 1), (2, 3), (5, 3), (5, 6), (6, 5),
                           (7, 6), (7, 7), (7, 5)], [[1, 2], [7]], [[3], [6]]])
        #2nd batch
        scenarios.append([[(1, 2), (2, 1), (3, 3), (2, 4), (3, 4), (3, 5)],
                          [[1, 2], [3]], [[4], [4, 5]]])

        scenarios.append([[(1, 2), (2, 1), (3, 3), (2, 4), (3, 4), (3, 5),
                           (5, 4)], [[3]], [[5]]])

        scenarios.append([[(1, 2), (2, 1), (1, 3), (2, 4), (3, 4), (4, 3),
                           (6, 6), (6, 3)], [], []])

        for scenario in scenarios:
            print scenario[0]
            G = lightnx.DiGraph()
            G.add_edges_from(scenario[0])
            scc_pm_nt, Gprime = matchings.get_S_nt_rm_Gprime(G)
            assignable_sorted = map(sorted, scenario[1])
            msize = len(matchings.matching(Gprime))
            assign = []
            for scc in scc_pm_nt:
                back_edges = Gprime.edges()[:]
                if matchings.is_assignable(scc, Gprime, msize):
                    assign.append(scc)
                    #print "asig"
                    try:
                        idx = assignable_sorted.index(sorted(
                            scc.graph.nodes()))
                    except:
                        idx = None
                    assert_equals(sorted(back_edges), sorted(Gprime.edges()))
                    assert_equals(idx is None, False)
                    assert_equals(scc.assignable_points, set(scenario[2][idx]))
            assert_equals(len(assign), len(assignable_sorted))
    def test_get_S_nt_rm_Gprime(self):
        G = lightnx.DiGraph()
        G.add_edges_from([(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4),
                          (5, 6), (6, 5), (7, 6), (7, 7), (7, 5)])
        scc_pm_nt, gprime = matchings.get_S_nt_rm_Gprime(G)
        assert_equals(len(scc_pm_nt), 2)

        def check_results(scc_pm_nt):
            assert_equals(set(scc_pm_nt[0].graph.nodes()), set([1, 2]))
            assert_equals(set(scc_pm_nt[1].graph.nodes()), set([7]))
            assert_equals(scc_pm_nt[0].outnodes, set([3]))
            assert_equals(set(scc_pm_nt[0].outlinks), set([(2, 3)]))
            assert_equals(scc_pm_nt[1].outnodes, set([6, 5]))
            assert_equals(set(scc_pm_nt[1].outlinks), set([(7, 6), (7, 5)]))

        if len(scc_pm_nt[0].graph.nodes()) == 2:
            check_results(scc_pm_nt)
        else:
            check_results(scc_pm_nt[1:0])
        assert_equals(set(gprime.nodes()), set([3, 4, 5, 6]))
        assert_equals(set(gprime.predecessors(3)), set([4]))
        assert_equals(set(gprime.successors(3)), set([4]))
 def test_is_compatible(self):
     scenarios = []
     scenarios.append([
         [(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4), (5, 6), (6, 5), (3, 7), (4, 7)],
         [[1, 2]],
         [5, 6],
         False
     ])
     scenarios.append([
         [(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4), (5, 6), (6, 5), (3, 7), (4, 7), (3, 8), (4, 8)],
         [[1, 2]],
         [5, 6],
         True
     ])
     scenarios.append([
         [(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4), (5, 6), (6, 5), (3, 7), (4, 7), (3, 8) ],
         [[1, 2]],
         [5, 6],
         True
     ])
     scenarios.append([
         [(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4), (5, 6), (6, 5), (3, 7), (4, 8) ],
         [[1, 2]],
         [5, 6],
         True
     ])
     scenarios.append([
         [(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4), (5, 6), (6, 5), (3, 7), (4, 8), (10, 9), (9, 10), (9, 3), (9, 4) ],
         [[9, 10]],
         [5, 6],
         True
     ])
     scenarios.append([
         [(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4), (5, 6), (6, 5), (3, 7), (4, 8), (10, 9), (9, 10), (9, 3), (9, 4) ],
         [[1, 2]],
         [9, 10],
         True
     ])
     scenarios.append([
         [(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4), (5, 6), (6, 5), (3, 7), (4, 8), (10, 9), (9, 10), (9, 3), (9, 4) ],
         [[1, 2]],
         [5, 6],
         True
     ])
     scenarios.append([
         [(1, 2), (2, 1), (2, 3), (3, 4), (4, 3), (5, 4), (5, 6), (6, 5), (3, 7), (4, 8), (10, 9), (9, 10), (9, 3), (9, 4) ],
         [[1, 2], [6, 5]],
         [10, 9],
         False
     ])
     for scenario in scenarios:
         print 30 * "="
         print scenario[0]
         G = lightnx.DiGraph()
         G.add_edges_from(scenario[0])
         scc_pm_nt, Gprime = matchings.get_S_nt_rm_Gprime(G)
         msize = len(matchings.matching(Gprime))
         assign = []
         for scc in scc_pm_nt:
             if matchings.is_assignable(scc, Gprime, msize):
                 assign.append(scc)
         compatibles = []
         scc_test = None
         for scc in assign:
             if sorted(scc.graph.nodes()) in map(sorted,scenario[1]):
                 compatibles.append(scc)
             if sorted(scc.graph.nodes()) == sorted(scenario[2]):
                 scc_test = scc
         print [com.graph.nodes() for com in compatibles]
         print scc.graph.nodes()