def saveGraphs(): g = snap.LoadEdgeList(snap.PUNGraph, sys.argv[1],0,1) Graphs = [] a = snap.TIntV() for file in sys.argv[2:]: Graphs.append(snap.LoadEdgeList(snap.PUNGraph, file, 0, 1)) folder = str(file).split("/")[0] if folder != "data": sys.exit("comparison should be made with file in 'data' folder") data = sys.argv[2].split("/")[1] path = "/" if "DCS_LP" in sys.argv[1]: path = "results/snap/"+data+"DCS_LP/" else :# DCS_GREEDY path = "results/snap/"+data+"DCS_GREEDY/" if not os.path.exists(path) : os.makedirs(path) for i in g.Nodes() : a.Add(i.GetId()) for i in range(0,len(Graphs)): Graphs[i] = snap.GetSubGraph(Graphs[i],a) filename = raw_input('Enter a snapgraph name: ') print "saving graphs in: " + path +" under names "+filename+"i.txt" for i in range(0,len(Graphs)): snap.SaveEdgeList(Graphs[i],path+filename+str(i+1)+".txt","Minimal graph from" + sys.argv[1])
def main(): printUsageSummary() (options, args) = parseArguments() if not cleanUpArguments(options): sys.exit(2) # 2 is usually command line error? print("graphs: " + str(options.numGraphs)) print("nodes: " + str(options.numNodes)) print("random edges: " + str(options.numRandomEdges)) for cs in options.cliqueSize: print("clique size: " + str(cs)) # Create the cliques. They will make sure the graphs have something # in common and that the optimal solution is not the entire graph. nodesInClique = [] blacklist = [] # This list is to make sure the cliques don't overlap. for i in range(0, len(options.cliqueSize)): if options.cliqueSize[i] > 0: nodesInClique.append( sorted( createCliqueNodes(options.numNodes, options.cliqueSize[i], blacklist))) print 'Clique: ', nodesInClique[-1] blacklist += nodesInClique[-1] # The files will be called 01.txt, 02.txt and so on. How many zeroes # we need to pad with depends on the number of files to have room for # the largest number. fieldWidth = int(1 + math.floor(math.log10(options.numGraphs))) # For as many graphs as we need, create a minimal randomized connected # graph, then add the edges from the clique previously created to it, # and save to a file. directory = createNewDir() for g in range(0, options.numGraphs): graph = snap.TUNGraph.New(options.numNodes, options.numNodes) createConnectedGraph(graph, options.numNodes) addRandomEdges(graph, options.numRandomEdges) for c in nodesInClique: addCliqueToGraph(graph, c) fileName = os.path.join(directory, str(1 + g).zfill(fieldWidth) + ".txt") snap.SaveEdgeList(graph, fileName)
usage() usr_in = "" print "Program: Verify..." print "1) print graph stats" print "2) Save graphs" print "3) find smallest graph" while usr_in not in ["1","2","3"]: usr_in = raw_input(">") print "You have Choosen " + usr_in +"!" if usr_in == "1": printStats() if usr_in == "2": saveGraphs() if usr_in =="3": name,g = findSmallestGraph() print "Would you like to save this graph?" print "y/n" a = "" while a not in ["y","n"]: a = raw_input(">") if a == "y": smallest = raw_input("input name: ") snap.SaveEdgeList(g,smallest+".txt","Smallest graph from" + sys.argv[1]) print "saving..." sys.exit("exiting...")
def main(argv): if len(argv) < 1: usage() gs = [] for file in argv[1:]: gs.append(snap.LoadEdgeList(snap.PUNGraph, file, 0, 1)) # Load and solve LP usr_in = "" print "Program: minimize..." print "use the following input" print "data/oregonFAULT-2/oregon2_010421.txt data/oregonFAULT-2/oregon2_010428.txt data/oregonFAULT-2/oregon2_010407.txt" print "1) minimize nodes wrt sys.argv[1:]" print "2) Does this input fail (sys.argv[1:]) yes/no" print "3) Run this: Example nodes" while usr_in not in ["1", "2", "3"]: usr_in = raw_input(">") print "You have Choosen " + usr_in + "!" if usr_in == "1": lp.printMoreGraphs(gs) dense = cplex.Cplex("dense.lp") os.remove("dense.lp") alg = dense.parameters.lpmethod.values dense.parameters.lpmethod.set(alg.barrier) dense.solve() # get the nodes for the LP nodes = getNodes(getNonZero(dense)) print "size nodes:" + str(nodes) # minimize the nodes and respective graphs n, gs = ddmin(2, nodes, gs) gs0 = [] for g in gs: gs0.append(getSubgraph(g, n)) lp.printMoreGraphs(gs) dense = cplex.Cplex("dense.lp") os.remove("dense.lp") alg = dense.parameters.lpmethod.values dense.parameters.lpmethod.set(alg.barrier) dense.solve() t = getSolution(getNonZero(dense)) (name, graph, density) = findSmallestGraph(n, gs) print "t* = " + str(t) print "density = " + str(density) print "t* =? density : " + str(abs(t - density) < 0.000005) print "Local minimal nodes: " + str(n) print "---" print "Would you like to save the fault inducing minimized graphs? y/n" while usr_in not in ["y", "n"]: usr_in = raw_input(">") if usr_in == "y": data = sys.argv[1].split("/")[1] path = "results/snap/minimal/" if not os.path.exists(path): os.makedirs(path) filename = raw_input('Enter a snapgraph name: ') print "saving graphs in: " + path + " under names " + filename + "i.txt" for i in range(0, len(gs)): snap.SaveEdgeList(gs[i], path + filename + str(i + 1) + ".txt", "Minimal graph from" + data) else: print "exiting..." if usr_in == "2": if testGraphs(gs, True): print "No, input does not fail, e.g t* = density" else: print "Yes, input does fail, e.g t* != density" if usr_in == "3": nodes = [5388, 6659, 13237, 5466, 9019, 9013, 5611] for g in gs: g = getSubgraph(g, nodes) lp.printMoreGraphs(gs) dense = cplex.Cplex("dense.lp") os.remove("dense.lp") alg = dense.parameters.lpmethod.values dense.parameters.lpmethod.set(alg.barrier) dense.solve() t = getSolution(getNonZero(dense)) (name, graph, density) = fsg(nodes) print "t* = " + str(t) print "density = " + str(density) print "t* =? density : " + str(abs(t - density) < 0.000005) print "Local minimal nodes: " + str(nodes)
def usage(): sys.exit("usage: python logToSnap.py <file.log> <resultGraph.txt>") if (len(sys.argv) != 3): usage() cName, cExt = os.path.splitext(sys.argv[1]) sName, sExt = os.path.splitext(sys.argv[2]) if cExt != ".log": usage() if sExt != ".txt": usage() input = open(sys.argv[1]) line = "" g = snap.TUNGraph.New() for line in input: # Add Nodes if line[0] == 'y': if not g.IsNode(int(line.split()[0][1:])): g.AddNode(int(line.split()[0][1:])) # Add Edges if line[0] == 'x': fr_to = line.split('_', 1)[1].split('#', 1) fr_to[1] = fr_to[1].split(' ', 1)[0] if not g.IsEdge(int(fr_to[0]), int(fr_to[1])): g.AddEdge(int(fr_to[0]), int(fr_to[1])) snap.SaveEdgeList(g, sys.argv[2], "SnapGraph from logfile" + cName + "\n") input.close() print("cplexToSnap Completed.")
def saveGraphs(Graphs, dirPath): timestamp = datetime.datetime.now() comment = "Preprocessed on " + timestamp.isoformat() for i in range(0, len(Graphs)): fileName = path.join(dirPath, str(i).zfill(2) + ".txt") snap.SaveEdgeList(Graphs[i], fileName, comment)