Ejemplo n.º 1
0
def _test_stoer_wagner(G, answer, weight="weight"):
    cut_value, partition = nx.stoer_wagner(G, weight, heap=nx.utils.PairingHeap)
    assert cut_value == answer
    _check_partition(G, cut_value, partition, weight)
    cut_value, partition = nx.stoer_wagner(G, weight, heap=nx.utils.BinaryHeap)
    assert cut_value == answer
    _check_partition(G, cut_value, partition, weight)
Ejemplo n.º 2
0
def min_k_cut(H, K, force_k=False):
    '''
    1. Respect only the lower triangle of Hessian H, which is the default option of networkx.from_numpy().
    2. The Hessian H(s) is associated with the state and we have the access of the H(s) function.
    3. The Hessian is with noise both from the FM system, the neural network system and the noise of RL.
    '''
    G = nx.from_numpy_matrix(H)
    if not force_k:
        partition = np.zeros(H.shape[0], dtype=np.int64)
        for idx, component in enumerate(nx.connected_components(G)):
            partition[list(component)] = idx
    else:
        subs = []
        for idx, component in enumerate(nx.connected_components(G)):
            subs.append(component)
        K -= len(subs)
        if K > 0:
            for idx, component in enumerate(subs):
                subs[idx] = nx.stoer_wagner(
                    G.subgraph(component)) + (component, )
        while K > 0:
            K -= 1
            #handle cases where len(component)==1
            idx = subs.find(min(subs))
            subs.append(
                nx.stoer_wagner(G.subgraph(subs[idx][1][0])) +
                (subs[idx][1][0], ))
            subs.append(
                nx.stoer_wagner(G.subgraph(subs[idx][1][1])) +
                (subs[idx][1][1], ))
            del subs[idx]

    return partition
Ejemplo n.º 3
0
def _test_stoer_wagner(G, answer):
    cut_value, partition = nx.stoer_wagner(G, heap=nx.utils.PairingHeap)
    assert_equal(cut_value, answer)
    _check_partition(G, cut_value, partition)
    cut_value, partition = nx.stoer_wagner(G, heap=nx.utils.BinaryHeap)
    assert_equal(cut_value, answer)
    _check_partition(G, cut_value, partition)
Ejemplo n.º 4
0
def _test_stoer_wagner(G, answer, weight='weight'):
    cut_value, partition = nx.stoer_wagner(G,
                                           weight,
                                           heap=nx.utils.PairingHeap)
    assert_equal(cut_value, answer)
    _check_partition(G, cut_value, partition, weight)
    cut_value, partition = nx.stoer_wagner(G, weight, heap=nx.utils.BinaryHeap)
    assert_equal(cut_value, answer)
    _check_partition(G, cut_value, partition, weight)
def get_clusters(A, num_clusters, algo='stoer_wagner'):
    # Cluster columns of A
    if num_clusters == A.shape[1]:
        return [[i] for i in range(num_clusters)]
    if algo == 'kruskal':
        # Use kruskal's algorithm to find maximally spaced clusters
        D = -1 * np.abs(A.T @ A)
        num_vertices = A.shape[1]
        vertices = list(range(num_vertices))
        edges = [(D[i, j], i, j) for i in range(num_vertices)
                 for j in range(num_vertices) if (i < j)]
        cluster_assignments = cluster_kruskal(vertices, edges, num_clusters)
        clusters = [[] for c in range(num_clusters)]
        for p in range(A.shape[1]):
            clusters[cluster_assignments[p]].append(p)
        return clusters
    elif algo == 'stoer_wagner':
        D = np.abs(A.T @ A)
        G = nx.from_numpy_matrix(D)
        assert (num_clusters < G.order())

        total_cut_val = 0
        cut_val, partition = nx.stoer_wagner(G)
        subgraphs = {G: (cut_val, partition)}
        while num_clusters > len(subgraphs):
            last_iteration = (num_clusters == (len(subgraphs) + 1))
            # Pick smallest cut sg; divide
            min_g = None
            min_val = float('inf')
            for g in subgraphs:
                if subgraphs[g][0] < min_val:
                    min_val = subgraphs[g][0]
                    min_g = g
            # print("New stoer_wagner cut of value={}".format(min_val))
            total_cut_val += min_val
            v1, v2 = subgraphs[min_g][1]
            g1 = G.subgraph(v1)
            g2 = G.subgraph(v2)
            del subgraphs[min_g]
            subgraphs[g1] = nx.stoer_wagner(g1) if (g1.order() > 1) and (
                not last_iteration) else (float('inf'), None)
            subgraphs[g2] = nx.stoer_wagner(g2) if (g2.order() > 1) and (
                not last_iteration) else (float('inf'), None)

        print("Total stoer_wagner cut_value={}".format(total_cut_val))
        clusters = [list(g) for g in subgraphs]
        return clusters
    else:
        raise ValueError("Unrecognized algorith: {}".format(algo))
Ejemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser(
        usage="This script gets statistics of the 3 algorithms in algo by generating random graphs and comparing the "
              "results of the algorithms over several iterations over those graphs, you can also load examples to "
              "display statistics about them, and load a csv file generated by the script to display charts")
    parser.add_argument("--example", help="Example to load")
    parser.add_argument("--loadcsv", help="CSV to load to display")
    parser.add_argument("--noplot",
                        help="Deactivates the display of a chart with statistics at the end of the execution",
                        action="store_true")
    parser.add_argument("--min_graph_size", help="Minimum graph size when generating random graphs")
    parser.add_argument("--max_graph_size", help="Maximum graph size when generating random graphs")

    args = parser.parse_args()
    if args.example is None:
        if args.loadcsv is None:
            execute_and_save_stats()
            if args.noplot is None:
                load_and_display_plots("results.csv")
        else:
            load_and_display_plots(args.loadcsv)
    else:
        graph = graph_from_file(args.example)
        nx_graph = convert_to_nx_graph(graph)
        results = get_stats_for_graph(graph, nx_graph)
        print_statistics(results, nx.stoer_wagner(nx_graph)[0])
Ejemplo n.º 7
0
def _optimize_node_order(adjacency_matrix):
    """
    Improve node order by grouping strongly connected nodes closer to each other.
    Essentially, the graph is recursively partitioned using minimum flow cuts and
    the order of the nodes in the resulting hierarchical clustering is returned.
    """

    import networkx

    # make networkx compatible
    w = adjacency_matrix.copy()
    w[np.isnan(w)] = 0.

    # graph cuts only implemented for undirected graphs with positive weights
    # in networkx
    w = np.abs(w)
    w = w + w.T

    g = networkx.from_numpy_matrix(w)
    partitions = [range(len(w))]
    while np.max([len(p) for p in partitions]) > 2:
        new_partitions = []
        for ii, p0 in enumerate(partitions):
            if len(p0) > 2:
                c, (p1, p2) = networkx.stoer_wagner(g.subgraph(p0))
                new_partitions.append(p1)
                new_partitions.append(p2)
            else:  # nothing to partition
                new_partitions.append(p0)
        partitions = new_partitions

    node_order = np.concatenate(partitions)
    return node_order
Ejemplo n.º 8
0
    def minCut(self):
        if self.nodesSelected is False:
            return (print("No cities selected!"))
        try:
            cut_value, partition = nx.stoer_wagner(
                self.selected, weight='distance'
            )  #get the weight of the min-cut and the edges in the min-cut
        except nx.NetworkXError:
            return (
                print("The graph must be connected to get the minimum cut!"))
        print("The edges in a min-cut are as follows.\n")
        for i in partition[0]:
            for j in self.selected.edges(i):
                print("(",
                      j[0],
                      ", ",
                      self.selected.nodes[j[0]]['state'],
                      " , ",
                      j[1],
                      ", ",
                      self.selected.nodes[j[1]]['state'],
                      ")",
                      sep='')

        print("\nWeight of the min-cut: ", cut_value)
Ejemplo n.º 9
0
def test_edge_connectivity_flow_vs_stoer_wagner():
    graph_funcs = [
        nx.icosahedral_graph, nx.octahedral_graph, nx.dodecahedral_graph
    ]
    for graph_func in graph_funcs:
        G = graph_func()
        assert nx.stoer_wagner(G)[0] == nx.edge_connectivity(G)
Ejemplo n.º 10
0
def test_edge_connectivity_flow_vs_stoer_wagner():
    graph_funcs = [
        nx.icosahedral_graph,
        nx.octahedral_graph,
        nx.dodecahedral_graph,
    ]
    for graph_func in graph_funcs:
        G = graph_func()
        assert_equal(nx.stoer_wagner(G)[0], nx.edge_connectivity(G))
Ejemplo n.º 11
0
def stoer_wagner_volume(recorder, graph, labels, params):
    assert params.n_way == 2
    try:
        _, (part_a, part_b) = nx.stoer_wagner(graph)
    except nx.exception.NetworkXError:
        assert params.n_shot == 1
        part_a = nx.node_connected_component(graph, 0)
        part_b = nx.node_connected_component(graph, 1)
    label_0, label_1 = torch.min(labels).item(), torch.max(labels).item()
    assert (labels == label_0).sum().item() * 2 == len(graph)
    return connect_parts_labels(recorder, graph, part_a, part_b, labels,
                                label_0, label_1)
Ejemplo n.º 12
0
def graph_separation(G, threshold, output):
    if len(G.nodes()) < (threshold + 1):
        output.append(G)
        return output

    cut_value, xer = nx.stoer_wagner(G)
    H1 = G.subgraph(xer[0])
    H2 = G.subgraph(xer[1])
    graph_separation(H1, threshold, output)
    graph_separation(H2, threshold, output)

    return output
Ejemplo n.º 13
0
def target_cut(G):
    """Returns a list of lists each of which contain nodes that correspond to unique people represented 
    in the graph G."""
    #First separate the graph into its connected components
    graphs = list(nx.connected_component_subgraphs(G))
    after_cut = []
    #For each connected component we decide whether or the graph should be cut, based
    #on our feas2 function. Then the component is merged and finally cut.
    for sub_G in graphs:
        #If this returns 1 or True then the component should be cut
        if_cut = GraphFeas(sub_G)
        #In the case the component is not cut then store the nodes associated 
        #to that component and move on.
        if if_cut == 0:
            after_cut = after_cut + [sub_G.nodes()]
        #Other wise we merge and cut the component    
        else:
            #First we look at all the edges that should be merged via the 
            #edges_to_merge function
            edges = edges_to_merge(sub_G)
            A = sub_G
            #Then the edges are merged via the merge function
            for Eg in edges:
                A = merge(A,Eg)
            #After all of the merging is done the graph is cut along a minimum 
            #cut into two connected components via stoer_wagner    
            cut_val, cuts = nx.stoer_wagner(A)
            #This is a small note that would take the weights into consideration
            #before cutting. Thus eliminating possible false cuts on high weights.
#                if cut_val>=some reasonably big number:
#                    after_cut=after_cut+A.nodes()
#                else:
#                    Indent the following code to be in this else statement
            nodelist1 = []
            #The nodes for each component after cutting are stored
            for nd in cuts[0]:
            #To get the nodes in these components we must look "inside" at the
            #list attribute of the merged nodes
                nodelist1 = nodelist1 + A.node[nd]['list']
                nodelist1 = nodelist1 + [nd]
            nodelist2 = []
            #Do the samething for the second component
            for nd in cuts[1]:
                nodelist2 = nodelist2 + A.node[nd]['list']
                nodelist2 = nodelist2 + [nd]  
            #Turn the node lists into subgraphs so the target_cut can be 
            #reinitiated on them until they should not be cut anymore.
            sub1 = sub_G.subgraph(nodelist1)
            sub2 = sub_G.subgraph(nodelist2)
            after_cut = after_cut + target_cut(sub1)
            after_cut = after_cut + target_cut(sub2)
    return after_cut
Ejemplo n.º 14
0
    def _cut_connected(self, problem, graph):
        """ Given a connected graph, perform subtour cut, using Stoer-Wagner to separate """

        # print("Cutting connected solution...")
        cut_value, (left_set, right_set) = nx.stoer_wagner(graph)

        if len(left_set) < len(right_set):
            cut_set = left_set
        else:
            cut_set = right_set

        if cut_value < 1.99:
            self._presolve_subtour(problem, cut_set)
Ejemplo n.º 15
0
Archivo: lp.py Proyecto: wborgeaud/tspy
 def bound(self, tsp):
     conn = Connected_LP_bound()
     opt_res = conn.bound(tsp)
     if not conn.constraints:
         return opt_res
     SS = conn.constraints
     sol = opt_res['x']
     N = tsp.N
     mat = tsp.mat
     k = 1
     while k < 1.95:
         graph = np.zeros((N, N))
         count = 0
         for i in range(N):
             for j in range(i + 1, N):
                 graph[i, j] = sol[count]
                 graph[j, i] = sol[count]
                 count += 1
         CC = connected_components(graph, directed=False)
         is_not_conn = CC[0] != 1
         while is_not_conn:
             for ii in range(CC[0]):
                 SS.append(np.arange(N)[CC[1] == ii])
             opt_res = _lp(mat, SS)
             opt_res['x'] = _clean_sol(opt_res['x'])
             sol = opt_res['x']
             graph = np.zeros((N, N))
             count = 0
             for i in range(N):
                 for j in range(i + 1, N):
                     if sol[count] > 0:
                         graph[i, j] = 1
                         graph[j, i] = 1
                     count += 1
             CC = connected_components(graph, directed=False)
             is_not_conn = CC[0] != 1
         cut = nx.stoer_wagner(nx.Graph(graph))
         k = cut[0]
         if k > 1.95:
             break
         part = cut[1]
         smpart = part[0] if len(part[0]) < len(part[1]) else part[1]
         SS.append(smpart)
         opt_res = _lp(mat, SS)
         opt_res['x'] = _clean_sol(opt_res['x'])
         sol = opt_res['x']
     self.sol = opt_res
     return opt_res
Ejemplo n.º 16
0
def get_stats_for_graph(gr, g):
    min_cut = nx.stoer_wagner(g)[0]
    # plt.subplot(111)
    # nx.draw(g, with_labels=True, font_weight='bold')
    # plt.show()
    i = 0
    n = len(gr)
    iterations = int(log(n) ** 2)
    results = create_results(["karger", "recursive", "custom"])
    print(f"Iterations {iterations}")
    print(f"Graph Size {n}")

    while i < iterations:
        get_results(results, "karger", karger, min_cut, gr)
        get_results(results, "recursive", recursive_karger, min_cut, gr)
        get_results(results, "custom", recursive_karger, min_cut, gr, a, b)
        i += 1
    # print_statistics(results, min_cut[0])
    return results
def graph_separation(G,sizetreshold,output,septreshold,notseparate):
    
    if len(G.nodes())<(sizetreshold+1):
        output.append(G)
        return output

    cut_value,xer=nx.stoer_wagner(G)
    H1=G.subgraph(xer[0])
    H2=G.subgraph(xer[1])
    if nx.average_node_connectivity(H1)>septreshold:
        notseparate.append(H1)
    else:
        graph_separation(H1,treshold,output,notseparate)
    if nx.average_node_connectivity(H2)>septreshold:
        notseparate.append(H2)
    else:
        graph_separation(H2,treshold,output,notseparate)        
   
    return output,notseparate
Ejemplo n.º 18
0
 def __call__(self):
     
     G = self.G
     n = len(G)
     
     # Get list of edges with positive value in the current LP solution
     elist = [e for e in G.edges if self.get_values(G[e[0]][e[1]]['var'])>1.0E-5]
     
     # If with animation, show the (fractional) solution
     if self.anim:
         objv = self.get_objective_value()
         xval = [ self.get_values( G[e[0]][e[1]]['var'] ) for e in elist ]
         efrac = [ e for e in elist if self.get_values(G[e[0]][e[1]]['var'])< 0.99 ]
         plotEdgeList(self.problem, elist, specialEdges=efrac,\
               title="Fractional LP solution of objval="+str(objv) )
     
     # Build the solution's support graph, i.e. the sub-graph induced by
     # the edges in the list "elist" found above.
     supG = nx.edge_subgraph(G, elist)
     
     # If the support graph G is not connected, build the SCE constraint
     # based on the graph's smallest component.
     S = min( nx.connected_components(supG), key=len )
     weight = 1.0
     
     # If the graph is connected, obtain the minimum-weight cut set
     # using the algorithm of Stoer and Wagner
     if len(S)==n:
         # Set the edge weights equal to the variable's solution values
         for e in elist:
             i, j =  e[0], e[1]
             supG[i][j]['weight'] = self.get_values( G[i][j]['var'] )
         weight, part = nx.stoer_wagner(supG)    
         S = part[0] if len(part[0]) < len(part[1]) else part[1]            
     
     # If the cutset constraint is violated, include the cut in 
     # form of the equivalent subcycle elimination constraint
     if ( len(S) > 2 ) and ( weight < 1.98):
         E = list(combinat(S,2))
         cycVars = [ G[e[0]][e[1]]['var'] for e in E ]
         self.add( cplex.SparsePair(cycVars, [1.0]*len(E) ), sense="L", \
                   rhs=len(S)-1)           
Ejemplo n.º 19
0
def TSPSYM(G):
    n = G.number_of_nodes()

    m = ConcreteModel()

    m.N = RangeSet(n)

    m.A = Set(initialize=((i, j) for i, j in G.edges()), dimen=2)

    m.x = Var(m.A, domain=NonNegativeReals, bounds=lambda m: (0, 1))

    m.obj = Objective(expr=sum(G[i][j]['weight'] * m.x[i, j]
                               for i, j in G.edges()))

    m.degree = ConstraintList()
    for i in m.N:
        Es = []
        for v, w in G.edges(i):
            if v > w:
                v, w = w, v
            Es.append((v, w))
        m.degree.add(sum(m.x[v, w] for v, w in Es) == 2)

    m.subtour = ConstraintList()

    solver = SolverFactory('gurobi')

    it = 0
    Cold = []
    while it <= 100:
        it += 1
        sol = solver.solve(m, tee=False, load_solutions=False)

        sol_json = sol.json_repn()
        if sol_json['Solver'][0]['Status'] != 'ok':
            return None, []

        m.solutions.load_from(sol)

        print(it, 'LP:', m.obj())

        selected = []
        values = []
        for i, j in m.A:
            if i < j:
                if m.x[i, j]() > 0.0001:
                    selected.append((i - 1, j - 1))
                    values.append(m.x[i, j]())

        PlotTour(Ls, selected, values)

        # Costruiamo un grafo supporto
        H = nx.Graph()
        for i, j in m.A:
            H.add_edge(i, j, weight=m.x[i, j]())

        cut_value, S = nx.stoer_wagner(H)  # Taglio di peso minimo

        if cut_value >= 2:
            break

        Es = []
        for i in S[0]:
            for j in S[1]:
                if i < j:
                    Es.append((i, j))
                else:
                    Es.append((j, i))

        m.subtour.add(sum(m.x[i, j] for i, j in Es) >= 2)

    return m.obj(), selected
print sorted(nx.pagerank(nxg).items(),key=operator.itemgetter(1),reverse=True)

try:
	print "=========================================================================================================="
	print "Alternative Quantitative Intrinsic Merit  - connectivity of RGO Definition Graph - Mengers Theorem"
	print "=========================================================================================================="
	print nx.node_connectivity(nxg)
except:
	pass 
try:
	print "=========================================================================================================="
	print "Alternative Quantitative Intrinsic Merit  - Maxflow-Mincut of RGO Definition Graph - Minimum Edge Cut"
	print "=========================================================================================================="
	print nx.minimum_edge_cut(nxg)
except:
	pass 
try:
	print "=========================================================================================================="
	print "Alternative Quantitative Intrinsic Merit  - Maxflow-Mincut of RGO Definition Graph - Stoer-Wagner"
	print "=========================================================================================================="
	print nx.stoer_wagner(nxg)
except:
	pass 
#try:
print "=========================================================================================================="
print "Alternative Quantitative Intrinsic Merit  - Junction Tree Width"
print "=========================================================================================================="
print TreeWidth.tree_width(nxg,3)
#except:
#	pass 
Ejemplo n.º 21
0
                self._parent[y] = self._parent[x]
                self._root.remove(y)
                return True
            t = self._parent[y]
            self._parent[y] = self._parent[x]
            y = t
        return False


if __name__ == 'builtins':
    import networkx as nx
    from random import shuffle
    grph = nx.erdos_renyi_graph(400, 0.1, seed=None, directed=False)
    edg = list(grph.edges())
    print("edges  -  ", len(edg))
    cut_value, partition = nx.stoer_wagner(grph)
    print("stoer_wagner  -  ", cut_value)
    gmin_cut = float('inf')
    for __ in range(400):
        shuffle(edg)
        ds = DisjointRankedSet(range(400), pathop='splitting')
        i = 0
        while ds.size() > 2 and i < len(edg):
            ds.join(edg[i][0], edg[i][1])
            i += 1
        #print(ds.size(),  i)
        min_cut = 0
        while i < len(edg):
            if not ds.joined(edg[i][0], edg[i][1]):
                min_cut += 1
            if min_cut >= gmin_cut:
Ejemplo n.º 22
0
             reverse=True)

try:
    print "=========================================================================================================="
    print "Alternative Quantitative Intrinsic Merit  - connectivity of RGO Definition Graph - Mengers Theorem"
    print "=========================================================================================================="
    print nx.node_connectivity(nxg)
except:
    pass
try:
    print "=========================================================================================================="
    print "Alternative Quantitative Intrinsic Merit  - Maxflow-Mincut of RGO Definition Graph - Minimum Edge Cut"
    print "=========================================================================================================="
    print nx.minimum_edge_cut(nxg)
except:
    pass
try:
    print "=========================================================================================================="
    print "Alternative Quantitative Intrinsic Merit  - Maxflow-Mincut of RGO Definition Graph - Stoer-Wagner"
    print "=========================================================================================================="
    print nx.stoer_wagner(nxg)
except:
    pass
#try:
print "=========================================================================================================="
print "Alternative Quantitative Intrinsic Merit  - Junction Tree Width"
print "=========================================================================================================="
print TreeWidth.tree_width(nxg, 3)
#except:
#	pass
Ejemplo n.º 23
0
#!/usr/bin/env python3

import networkx as nx
import sys, os
sys.path.insert(1,
                os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from utils import io_utils

print(nx.stoer_wagner(io_utils.read_stdin()))
def InterviewAlgorithm_main(argv1):
	#----------------------------------------------
	#1.Get the input documents
	#----------------------------------------------

	corpus = [argv1]
	#get keywords

	files = [argv1]

	#----------------------------------------------
	#2.Initialize MemCached Client
	#----------------------------------------------
	graphcache=memcache.Client(["127.0.0.1:11211"], debug=1)
	InterviewAlgorithmWithIntrinisicMerit_SparkMapReducer.flushCache(graphcache)

	#---------------------------------------------------------------------------------
	#3.Compute intrinsic merit (either using linear or quadratic overlap)
	#---------------------------------------------------------------------------------

	definitiongraphedges=defaultdict(list)
	definitiongraphedgelabels=defaultdict(list)
	weight_str_map=defaultdict()

	for filestr in files:
		outputfile = 'Output-Webspider-HTML.out'
		output = open(outputfile, 'w')
		file1 = open(filestr)
		raw1 = file1.read()
		doc1 = nltk.word_tokenize(raw1.decode("utf-8"))
		#fdist1 = FreqDist(doc1)
		stopwords = nltk.corpus.stopwords.words('english')
		stopwords = stopwords + [' ','or','and','who','he','she','whom','well','is','was','were','are','there','where','when','may', 'The', 'the', 'In', 		'in','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
		puncts = [' ','.', '"', ',', '{', '}', '+', '-', '*', '/', '%', '&', '(', ')', '[', ']', '=', '@', '#', ':', '|', ';','\'s']
		#freqterms1 = [w for w in fdist1.keys() if w not in stopwords and w not in puncts and (fdist1.freq(w) * compute_idf(corpus, w))]
		freqterms1 = [w for w in doc1 if w not in stopwords and w not in puncts]
	
		current_level = 1
		nodewithmaxparents = ''
		noofparents = 0
		maxparents = 0
		relatedness = 0
		first_convergence_level = 1
		tokensofthislevel = []
		convergingterms = []
		convergingparents = []
		tokensofprevlevel = []
		prevlevelsynsets = []
		commontokens = []
		vertices = 0
		edges = 0
		overlap = 0
		iter = 0
		#recurse down to required depth and update intrinsic merit score
		#relatedness is either sum(overlaps) or sum((overlapping_parents)*(overlaps)^2) also called convergence factor
		while current_level < 3:
			#crucial - gather nodes which converge/overlap (have more than 1 parent)
			if current_level > 1:
				print current_level
				prevlevelsynsets_tokens=[]
				for s in prevlevelsynsets:
					s_lemma=s.lemma_names()
					prevlevelsynsets_tokens.append(s_lemma[0])
				for x in freqterms1:
					#prevlevelsynsets_tokens=[]
					#for s in prevlevelsynsets:
					#	s_lemma=s.lemma_names()
					#	prevlevelsynsets_tokens.append(s_lemma[0])
					if parents_computation_spark:
						parents_x = InterviewAlgorithmWithIntrinisicMerit_SparkMapReducer.Spark_MapReduce_Parents(x,prevlevelsynsets_tokens,graphcache)
						if len(parents_x) > 1:
							convergingterms.append(x)
					else:
						#parents_x = parents(x,prevlevelsynsets)
						parents_x = parents_tokens(x,prevlevelsynsets_tokens)
						if len(parents_x) > 1:
							convergingterms.append(x)
					convergingparents = convergingparents + ([w for w in parents_x if len(parents_x) > 1])
					noofparents = len(parents_x)
					if noofparents > maxparents:
						maxparents = noofparents
						nodewithmaxparents = x 
					for y in parents_x:
						if parents_computation_spark:
							definitiongraphedges[x].append(y)
						else:
							y_lemma_names=y.lemma_names()
							definitiongraphedges[x].append(y_lemma_names[0])
				output.write('converging terms(terms with more than 1 parent):\n ')
				output.write('converging parents :\n')
	
			print "InterviewAlgorithmWithIntrinisicMerit_Crawl_Visual_Spark.py:freqterms1=",freqterms1	
			tokensofthislevel=InterviewAlgorithmWithIntrinisicMerit_SparkMapReducer.Spark_MapReduce(current_level, freqterms1, graphcache).tokensatthislevel
			print "InterviewAlgorithmWithIntrinisicMerit_Crawl_Visual_Spark.py:tokensofthislevel:",tokensofthislevel
			picklef=open("RecursiveGlossOverlap_MapReduce_Persisted.txt","r")
			prevlevelsynsets=InterviewAlgorithmWithIntrinisicMerit_SparkMapReducer.asfer_pickle_load(picklef)
			print "prevlevelsynsets:",prevlevelsynsets
			picklef=open("RecursiveGlossOverlap_MapReduce_Persisted.txt","w")
			picklef.seek(0)
			picklef.truncate()
			output.write('At level:\n')
			output.write(str(current_level))
			output.write('\n')
			output.write('tokens grasped at this level:\n')
			#pickle.dump(tokensofthislevel, output)
			output.write('\n')
			listcount = len(tokensofthislevel)
			setcount = len(set(tokensofthislevel))
			overlap =  listcount-setcount
			if overlap > 0 and iter == 0 :
				first_convergence_level = current_level
				iter = 1
			#choose between two relatedness/convergence criteria :- 
			#1) simple linear overlap or 2) zipf distributed quadratic overlap
			#relatedness = relatedness + len(convergingparents)*overlap 
			relatedness = relatedness + overlap + len(convergingparents)
			#relatedness = relatedness + ((len(convergingparents)*overlap*overlap) + 1) 
			#find out common tokens of this and previous level so that same token does not get grasped again - 	
			#relatedness must be increased since repetition of keywords in two successive levels is a sign of 
			#interrelatedness(a backedge from child-of-one-of-siblings to one-of-siblings). Remove vertices and edges 					#corresponding to common tokens
			commontokens = set(tokensofthislevel).intersection(set(tokensofprevlevel))
			tokensofthislevel = list(set(tokensofthislevel).difference(commontokens))
			relatedness = relatedness + len(commontokens)
			output.write('removing tokens already grasped:\n')
			#pickle.dump(commontokens,output)
			output.write('\n')
			output.write('Relatedness:\n')
			output.write(str(relatedness))
			output.write('\n')
			#decrease the vertices count to address common tokens removed above - edges should remain same since they 
			#would just point elsewhere
			vertices = vertices + setcount - len(commontokens)
			output.write('Vertices:\n')
			output.write(str(vertices))
			output.write('\n')
			edges = edges + listcount
			output.write('Edges:\n')
			output.write(str(edges))
			output.write('\n')
			current_level = current_level + 1
			freqterms1 = tokensofthislevel
			tokensofprevlevel = tokensofthislevel
			tokensofthislevel = []
		
		intrinsic_merit = vertices*edges*relatedness / first_convergence_level
		output.write('Intrinsic merit of this document is:\n')
		output.write(str(intrinsic_merit))
		output.write('\n')
		output.write('Node with maximum parents (and hence the most likely class of document) is:\n')
		output.write(nodewithmaxparents)
		output.write('\n')
	
	print definitiongraphedges


	nxg=nx.DiGraph()
	pos=nx.spectral_layout(nxg)
	for k,v in definitiongraphedges.iteritems():
		for l in v:
			nxg.add_edge(k,l)
			nxg.add_edge(l,k)
			ksynset=wn.synsets(k)
			lsynset=wn.synsets(l)
			if ksynset and lsynset:
				print "ksynset=",ksynset[0]
				print "lsynset=",lsynset[0]
				hypoksynsets=set([i for i in ksynset[0].closure(lambda n:n.hyponyms())])
				hyperlsynsets=set([i for i in lsynset[0].closure(lambda n:n.hypernyms())])
				for m in hypoksynsets:
					try:
						mlemmanames=m.lemma_names()
						weight_str_map[k+" - "+l]=weight_str_map[k+" - "+l]+" contains "+mlemmanames[0]
					except KeyError:
						weight_str_map[k+" - "+l]=""
				for n in hyperlsynsets:
					try:
						nlemmanames=n.lemma_names()
						weight_str_map[l+" - "+k]=weight_str_map[l+" - "+k]+" is part of "+nlemmanames[0]
					except KeyError:
						weight_str_map[l+" - "+k]=""
	if not required_none_vertices:
		filter_none_vertices(nxg)
	
	nx.draw_networkx(nxg)
	try:
		nx.write_dot(nxg,"InterviewAlgorithmWithIntrinisicMerit_Crawl_Visual_RGOGraph.dot")
	except:
		pass
	plt.show()
	nxg.remove_edges_from(nxg.selfloop_edges())
	#print "Core number =",nx.core_number(nxg)
	sorted_core_nxg=sorted(nx.core_number(nxg).items(),key=operator.itemgetter(1), reverse=True)
	print "Core number (sorted) :",sorted_core_nxg
	print "============================================================================================================="
	print "Unsupervised Classification based on top percentile Core numbers of the definition graph(subgraph of WordNet)"
	print "============================================================================================================="
	no_of_classes=len(nx.core_number(nxg))
	top_percentile=0
	max_core_number=0
	for n in sorted_core_nxg:
		print "This document belongs to class:",n[0],",core number=",n[1]
		if top_percentile < no_of_classes*0.10:
			top_percentile+=1
		else:	
			break
		if n[1] > max_core_number:
			max_core_number=n[1]
	print "max_core_number",max_core_number

	print "==================================================================="
	print "Page Rank of the vertices of RGO Definition Graph"
	print "==================================================================="
	print sorted(nx.pagerank(nxg).items(),key=operator.itemgetter(1),reverse=True)

	try:
		print "=========================================================================================================="
		print "Alternative Quantitative Intrinsic Merit  - connectivity of RGO Definition Graph - Mengers Theorem"
		print "=========================================================================================================="
		print nx.node_connectivity(nxg)
	except:
		pass 
	try:
		print "=========================================================================================================="
		print "Alternative Quantitative Intrinsic Merit  - Maxflow-Mincut of RGO Definition Graph - Minimum Edge Cut"
		print "=========================================================================================================="
		print nx.minimum_edge_cut(nxg)
	except:
		pass 
	try:
		print "=========================================================================================================="
		print "Alternative Quantitative Intrinsic Merit  - Maxflow-Mincut of RGO Definition Graph - Stoer-Wagner"
		print "=========================================================================================================="
		print nx.stoer_wagner(nxg)
	except:
		pass 
	try:
		print "=========================================================================================================="
		print "Alternative Quantitative Intrinsic Merit  - Average Clustering Coefficient"
		print "=========================================================================================================="
		print nx.average_clustering(nxg)
	except:
		pass
Ejemplo n.º 25
0
def TSPSYM(G, TIME_LIMIT=600):
    # Number of places
    n = G.number_of_nodes()

    # TODO: Implement the model of your choice
    m = ConcreteModel()

    # 1. Data and ranges
    m.N = RangeSet(n)

    m.A = Set(initialize=((i, j) for i, j in G.edges()), dimen=2)

    # 2. Variables
    # TODO: introduce only usefull variables (no arc, no variable)
    m.x = Var(m.A, domain=NonNegativeReals, bounds=lambda m: (0, 1))

    # 3. Objective function
    # Objective function of arc variables
    m.obj = Objective(expr=sum(G[i][j]['weight'] * m.x[i, j] for i, j in m.A))

    # 4. Constraints
    # Vincoli archi uscenti
    m.degree = ConstraintList()
    for i in m.N:
        Es = []
        for v, w in G.edges(i):
            if v > w:
                v, w = w, v
            Es.append((v, w))
        m.degree.add(expr=sum(m.x[v, w] for v, w in Es) == 2)

    m.subtour = ConstraintList()

    solver = SolverFactory('gurobi')

    # 5. Solution
    # Solve the model
    SOLVER_NAME = 'gurobi'
    # SOLVER_NAME = 'glpk'

    solver = SolverFactory(SOLVER_NAME)

    if SOLVER_NAME == 'glpk':
        solver.options['tmlim'] = TIME_LIMIT
    elif SOLVER_NAME == 'gurobi':
        solver.options['TimeLimit'] = TIME_LIMIT

    it = 0
    Cold = []
    while it <= 100:
        it += 1
        sol = solver.solve(m, tee=False, load_solutions=False)

        # Get a JSON representation of the solution
        sol_json = sol.json_repn()
        # Check solution status
        if sol_json['Solver'][0]['Status'] != 'ok':
            return None, []

        # Load the solution
        m.solutions.load_from(sol)

        selected = []
        values = []
        for i, j in m.A:
            if m.x[i, j]() > 0:
                selected.append((i - 1, j - 1))
                values.append(m.x[i, j]())

        PlotTour(Ls, selected, values)

        # Build graph
        H = nx.Graph()

        for i, j in m.A:
            H.add_edge(i, j, weight=m.x[i, j]())

        # Cs = nx.connected_components(H)
        # Cs = list(Cs)
        # Cs = nx.cycle_basis(H)

        cut_value, S = nx.stoer_wagner(H)
        print(it, m.obj(), sum(values))
        flag = True

        if cut_value >= 2:
            print(cut_value)
            # Separate blossom
            H = nx.Graph()
            for i, j in m.A:
                if m.x[i, j]() > 0.1 and m.x[i, j]() < 0.9:
                    if i < j:
                        H.add_edge(i, j)
                    else:
                        H.add_edge(j, i)

            selected = []
            values = []
            for i, j in m.A:
                selected.append((i - 1, j - 1))
                values.append(m.x[i, j]())
            Cs = nx.cycle_basis(H)
            for cycle in Cs:
                NS = len(cycle)
                if NS == 3:
                    S = set()
                    for i in range(NS):
                        if cycle[i - 1] < cycle[i]:
                            S.add((cycle[i - 1], cycle[i]))
                        else:
                            S.add((cycle[i], cycle[i - 1]))

                    for i in cycle:
                        for j in G.neighbors(i):
                            if (i, j) not in S:
                                v, w = i, j
                                if i > j:
                                    v, w = j, i
                                if m.x[v, w]() > 0.9:
                                    S.add((v, w))

                    if False and len(S) > NS + 2:
                        m.subtour.add(sum(m.x[i, j] for i, j in S) <= NS + 1)
                        flag = False
                        print('added', S)

        else:
            Es = []
            for i in S[0]:
                for j in S[1]:
                    if i < j:
                        Es.append((i, j))
                    else:
                        Es.append((j, i))

            if len(Es) > 0:
                m.subtour.add(sum(m.x[i, j] for i, j in Es) >= 2)
                flag = False
        if flag:
            break

        # sleep(1)

        # if Cs == Cold:
        #     break

        # Cold = Cs
        # for cycle in Cs:
        #     Es = []
        #     for i,j in m.A:
        #         if (i in cycle and j not in cycle) or (i not in cycle and j in cycle):
        #             if i < j:
        #                 Es.append( (i,j) )
        #             else:
        #                 Es.append( (j,i) )
        #             Es.append( (i,j) )

        #     if len(Es) > 0:
        #         m.subtour.add( sum(m.x[i,j] for i,j in Es ) >= 2 )

    selected = []
    values = []
    for i, j in m.A:
        if m.x[i, j]() > 0:
            selected.append((i - 1, j - 1))
            values.append(m.x[i, j]())
    print(values)
    PlotTour(Ls, selected, values)

    return m.obj(), selected
Ejemplo n.º 26
0
import networkx as nx
import numpy as np
BenchmarkNetwork=np.loadtxt('data/BenchmarkNetwork.txt',dtype=int)
Corruption_Gcc=np.loadtxt('data/Corruption_Gcc.txt',dtype=int)
Crime_Gcc=np.loadtxt('data/Crime_Gcc.txt',dtype=int)
PPI_gcc=np.loadtxt('data/PPI_gcc.txt',dtype=int)
RodeEU_gcc=np.loadtxt('data/RodeEU_gcc.txt',dtype=int)
# 用Networkx
BenchmarkNetwork_Graph=nx.Graph()
for line in BenchmarkNetwork:
    BenchmarkNetwork_Graph.add_edge(int(line[0]),int(line[1]),{'weight':1})
cut_value, partition = nx.stoer_wagner(BenchmarkNetwork_Graph)
print("BenchmarkNetwork的最小割是:",cut_value,partition)

RodeEU_gcc_Graph=nx.Graph()
for line in RodeEU_gcc:
    RodeEU_gcc_Graph.add_edge(int(line[0]),int(line[1]),{'weight':1})
cut_value, partition = nx.stoer_wagner(RodeEU_gcc_Graph)
print("RodeEU_gcc的最小割是:",cut_value,partition)

Corruption_Gcc_Graph=nx.Graph()
for line in Corruption_Gcc:
    Corruption_Gcc_Graph.add_edge(int(line[0]),int(line[1]),{'weight':1})
cut_value, partition = nx.stoer_wagner(Corruption_Gcc_Graph)
print("Corruption_Gcc的最小割是:",cut_value,partition)

Crime_Gcc_Graph=nx.Graph()
for line in Crime_Gcc:
    Crime_Gcc_Graph.add_edge(int(line[0]),int(line[1]),{'weight':1})
cut_value, partition = nx.stoer_wagner(Crime_Gcc_Graph)
print("Crime_Gcc的最小割是:",cut_value,partition)