Exemple #1
0
#print i1
#print i2

best_avg = []
best_intervals = []

initIntervals = [(0, len(edgesTS) - 1)]

edgesTSBi = copy.deepcopy(edgesTS)
edgesTSGr = copy.deepcopy(edgesTS)
G = utils.getGraph(edgesTS)

for ind in xrange(0, comN):

    try:
        S, avg_char = charikar.charikar(copy.deepcopy(G))
        G.remove_edges_from(S.edges())
        Sedges = S.edges()
        Sedges = np.array(S.edges())
        Sedges.sort(1)
        Sedges = map(tuple, Sedges)
        minspanChar, _, _ = utils.shortestInt(edgesTS, Sedges)
        minspanChar = minspanChar.total_seconds() / (60 * 60 * 24.0)
    except:
        print S.number_of_nodes()
        minspanChar = -1
        avg_char = -1

    if runMainAlgs == 'grbi':

        #avg_greedy, _, num_nodes_gr = main.main(outpath, 'greedy', charikar_version, 'unweighted', pics, k, B, edgesTS, nodes, edges, initIntervals, False)
def main(outpath,
         alg,
         charikar_version,
         charikar_mode,
         pics,
         k,
         B,
         edgesTS,
         nodes,
         edges,
         timeIntervals,
         baseline=False,
         n_disc=101,
         fast=True,
         entire=True):

    G = nx.Graph()
    S = nx.Graph()

    nodes_covered = list(nodes)
    edges_covered = edges

    baseAvg, baseInt, baseEdges, baseNodes = 0.0, 0.0, 0.0, 0.0
    if baseline == True:
        baseAvg, baseInt, baseS = utils.getBaseline(edgesTS, B)
        baseEdges, baseNodes = baseS.number_of_edges(), baseS.number_of_nodes()

    counter = 0
    out = 0.0
    maxiter = 0
    first = True
    initCom = nx.Graph()
    #for maxiter in xrange(10):
    for maxiter in xrange(1):
        maxiter += 1
        #print timeIntervals
        rest_k, rest_B = k, B
        G.clear()
        S.clear()

        if not timeIntervals:
            timeIntervals = [(0, len(edgesTS) - 1)]

        tic = time.clock()
        #############  pile a graph (unweighted)
        G = utils.getGraphFrimIntervals(edgesTS, timeIntervals, charikar_mode)
        #G = utils.getGraphFrimIntervals(edgesTS, timeIntervals, 'weighted_emails')
        #print G.adj
        #print len(G.edges())
        #exit()
        toc = time.clock()
        #print 'pile graph', toc-tic

        if pics:
            plotting.plotGraph(outpath, counter, 'graph_G', G)
            counter += 1

        tic = time.clock()
        if alg == 'dynprogr':
            S, avg = charikar.charikar(copy.deepcopy(G), 'weighted',
                                       charikar_version)
        else:
            S, avg = charikar.charikar(copy.deepcopy(G), 'unweighted',
                                       charikar_version)

        toc = time.clock()
        #print 'Charikar', toc-tic

        if first == True:
            first = False
            initCom = copy.deepcopy(S)

        #print len(S.edges()), avg

        if pics:
            plotting.plotGraph(outpath, counter, 'subgraph_S', S, 'subgraph',
                               baseNodes, baseEdges, baseAvg)
            counter += 1

        nodes = S.nodes()

        # simple rule:
        timeIntervals = [(0, len(edgesTS) - 1)]

        tic = time.clock()

        if entire == True:
            S = G

        if alg == 'greedy':
            if fast == True:
                timeIntervals, edges_covered, usedB = greedy_speedup_log.greedy(
                    S, edgesTS, k, timeIntervals, B)
            #timeIntervals, edges_covered, rest_B = greedy_emails.greedy(S, edgesTS, k, timeIntervals, B)
            if fast == False:
                timeIntervals, edges_covered, usedB = greedy_log.greedy(
                    S, edgesTS, k, timeIntervals, B)
            #print timeIntervals, edges_covered, rest_B
        if alg == 'binary':
            if fast == True:
                #timeIntervals, edges_covered, usedB = greedy_speedup.binary(S, edgesTS, k, timeIntervals, B)
                timeIntervals, edges_covered, usedB = greedy_speedup_log.binary(
                    S, edgesTS, k, timeIntervals, B)
            if fast == False:
                #timeIntervals, edges_covered, usedB = greedy.binary(S, edgesTS, k, timeIntervals, B)
                timeIntervals, edges_covered, usedB = greedy_log.binary(
                    S, edgesTS, k, timeIntervals, B)
        if alg == 'dynprogr':
            timeIntervals, edges_covered, usedB, D = dynprogr.dynprogr(
                S, edgesTS, k, timeIntervals, B, n_disc)
            #print timeIntervals, edges_covered, rest_B, D
        #print timeIntervals, edges_covered, rest_B
        #exit()

        toc = time.clock()

        # if fast == True:
        # print 'fast interval search', toc-tic
        # if fast == False:
        # print 'basic interval search', toc-tic

        if pics:
            plotting.plotPoints(outpath, S, edgesTS, timeIntervals, counter, B,
                                rest_B, k, edges_covered)
            counter += 1

        if pics:
            plotting.coveredSubgraphs(outpath, counter, edgesTS, timeIntervals,
                                      edges_covered)
            counter += 1

        nodes_covered = set()
        for i in edges_covered:
            nodes_covered.add(i[0])
            nodes_covered.add(i[1])

        #e1 = set([set(i) for i in S.edges()])
        #e2 = set([set(i) for i in edges_covered])

        e1 = np.array(S.edges())
        e1.sort(1)
        e1 = map(tuple, e1)
        #print e1

        #print list(edges_covered)
        e2 = np.array(list(edges_covered))
        e2.sort(1)
        e2 = map(tuple, e2)

        #if (set(S.edges())).issubset(set(edges_covered)):
        #print 2.0*S.number_of_edges()/S.number_of_nodes()
        #print len(e1), len(e2)

        if set(e1).issubset(set(e2)):
            #if set(S.nodes()).issubset(set(nodes_covered)):
            out = 2.0 * len(edges_covered) / len(nodes_covered)

            # print edges_covered
            # print ' '.join(map(str,nodes_covered))
            # print 2.0*len(edges_covered)/len(nodes_covered), B, B-rest_B
            # print baseAvg, baseInt, baseEdges, baseS.number_of_nodes()
            # #print timeIntervals.keys()
            # for i in timeIntervals:
            # sys.stdout.write(str(i[0]) + ',' + str(i[1]) + ';')
            # print ''
            # for i in timeIntervals:
            # sys.stdout.write(str(edgesTS[i[0]][0]) + ',' + str(edgesTS[i[1]][0]) + ';')
            break
    print toc - tic, out
    sys.stdout.flush()

    return out, baseAvg, nodes_covered
                                                  charikar_version,
                                                  'unweighted', pics, k, B,
                                                  edgesTS, nodes, edges,
                                                  initIntervals, False)
    usedBgr = usedBgr.total_seconds() / (60 * 60 * 24)

    avg_greedy = 2.0 * S.number_of_edges() / S.number_of_nodes()
    nodes_covered_gr = S.number_of_nodes()
    toc = time.time()

    print 'greedy done in: ', toc - tic

    minspanGr, st, end = utils.shortestInt(edgesTS, edges_coveredGr)
    minspanGr = minspanGr.total_seconds() / (60 * 60 * 24)
    S1 = utils.getGraph(edgesTS[st:end + 1])
    _, avg_new_greedy = charikar.charikar(copy.deepcopy(S1))
    sys.stdout.flush()

    tic = time.time()
    #avg_binary, _, nodes_covered_bi = main.main(outpath, 'binary', charikar_version, 'unweighted', pics, k, B, edgesTS, nodes, edges, initIntervals, False)
    _, _, usedBbi, S, edges_coveredBi = main.main(outpath, 'binary',
                                                  charikar_version,
                                                  'unweighted', pics, k, B,
                                                  edgesTS, nodes, edges,
                                                  initIntervals, False)
    usedBbi = usedBbi.total_seconds() / (60 * 60 * 24)

    avg_binary = 2.0 * S.number_of_edges() / S.number_of_nodes()
    nodes_covered_bi = S.number_of_nodes()
    toc = time.time()
    print 'binary done in: ', toc - tic