def newGraph(size, path): G = nx.Graph() for i in range(size): G.add_node(i) for j in range(size): for k in range(j + 1, size): G.add_edge(j, k, weight=round(rand.uniform(0.001, 99.999), 3)) # print(G) parse.write_input_file(G, path)
def make_input_five(n, path): """ Auto generates input #1 (see below notes) based on number of students Args: n: number of students path: str, a filepath to create file Creates: input file (i.e. path.in) by using parse.write_input_file(G, budget, path) """ # TODO: your code here! #G = nx.Graph() s_max = 7*n/2 print("n = " + str(n)) print("s_max = " + str(s_max)) print() print("1 2 h s") print() G = nx.Graph() for i in range(n-1): for j in range(i+1, n): if (i % 2 == 0): #print(i, j, , ) G.add_edges_from([(i, j, {"happiness": (n*2)-(i+1), "stress": (i + 1)})]) else: #print(i, j, , ) G.add_edges_from([(i, j, {"happiness": i+1, "stress": (n*2)-(i + 1)})]) parse.write_input_file(G, s_max, path) """
def make_input_one(n, path): """ Auto generates input #1 (see below notes) based on number of students Args: n: number of students path: str, a filepath to create file Creates: input file (i.e. path.in) by using parse.write_input_file(G, budget, path) """ # TODO: your code here! #G = nx.Graph() s_max = 3 * n / 2 """ print("n = " + str(n)) print("s_max = " + str(s_max)) print() print("1 2 h s") print() for i in range(n-1): for j in range(i+1, n): print(i, j, random.randint(1, 5), 2) """ #ToDo: Make the networkx graph here and test it against brute force solver G = nx.Graph() for i in range(n-1): for j in range(i+1, n): rin = random.randint(1, 5) G.add_edges_from([(i, j, {"happiness": rin, "stress": 2})]) parse.write_input_file(G, s_max, path)
def generate(): try: os.mkdir("./inputs/") print("New inputs generated.") except: print("New inputs updated.") parse.write_input_file(build_graph(Vertex_Number_Small), "inputs/30.in") parse.write_input_file(build_graph(Vertex_Number_Medium), "inputs/50.in") parse.write_input_file(build_graph(Vertex_Number_Large), "inputs/100.in")
def make_input(s): A = np.random.randint(1, high=100 * 1000 - 1, size=(s, s)) A = A / 1000 print(A) G = nx.from_numpy_matrix(A) write_input_file(G, "./part1_inputs/" + str(s) + ".in")
import random import networkx as nx from parse import write_input_file maxStress = 99.69 numPeople = 10 ''' inputs = [[-1 for _ in range(numPeople)] for _ in range(numPeople)] sums = [random.uniform(30, 250) for _ in range(numRooms)] constraints = [random.uniform(40, 200) for _ in range(numRooms)] ''' G = nx.complete_graph(numPeople) for i in range(numPeople): for j in range(i+1, numPeople): """ prev_edge = list(G.edges)[i] print(prev_edge) """ G[i][j]["happiness"] = round(random.uniform(20,100), 3) G[i][j]["stress"] = round(random.uniform(10,70), 3) write_input_file(G, maxStress, "backup/10.in")
counter = 0 for i in range(len(clusters_sizes)): for j in range(clusters_sizes[i]): result[counter] = i counter += 1 return result graph_20 = graph_generator_easier(20, 3, clusters_sizes=[5, 4, 4, 4, 3], starting_nodes=[0, 5, 9, 13, 17], stress_budget=75) graph_20_out = generate_optimal_graph([5, 4, 4, 4, 3]) write_input_file(graph_20, 75, '20.in') write_output_file(graph_20_out, '20.out') graph_50 = graph_generator_easier( 50, 3, clusters_sizes=[5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5], starting_nodes=[0, 5, 9, 14, 18, 23, 27, 32, 36, 41, 45], stress_budget=99) graph_50_out = generate_optimal_graph([5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5]) write_input_file(graph_50, 99, '50.in') write_output_file(graph_50_out, '50.out') def n_generator(num_vertices, filename):
def generate_input(n=10, p=0.1): write_input_file(make_graph(n, p), "./inputs/{}.in".format(n))
from parse import write_input_file, read_input_file, write_output_file from random import randrange import networkx as nx import matplotlib.pyplot as plt V = 100 G = nx.Graph() edges = [] for i in range(V): for j in range(i + 1, V): length = randrange(100000) / 1000 edge = (i, j, length) if j == i + 1 or j == i + randrange(V - i - 1): edges.append(edge) elif randrange(100000) % 2 == 0: edges.append(edge) G.add_weighted_edges_from(edges) fileName = str(V) + '.in' write_input_file(G, fileName) nx.draw(G, with_labels=True, font_weight='bold') plt.show()
import networkx as nx import parse as p from random import random, choice G = nx.Graph() path = "inputsSubmission/" inputs = {"25.in": 25, "50.in": 50, "100.in": 100} for filename, size in inputs.items(): G.add_nodes_from(range(size)) while not nx.is_connected(G): start = choice(list(G.nodes())) end = choice(list(nx.non_neighbors(G, start))) weight = round(random() * 100, 3) G.add_edge(start, end) G[start][end]['weight'] = weight print("=== " + filename + " ===") print("Graph has " + str(len(list(G.nodes()))) + " nodes and " + str(len(list(G.edges()))) + " edges.") print("Graph is connected: " + str(nx.is_connected(G))) print("Write to file: " + path + filename) p.write_input_file(G, path + filename) G.clear() print()
from parse import write_input_file, validate_file import networkx as nx import random if __name__ == '__main__': G_small = nx.complete_graph(24) for (u, v) in G_small.edges(): G_small.edges[u,v]['weight'] = round(random.uniform(0.0, 100.0), 3) G_medium = nx.complete_graph(48) for (u, v) in G_medium.edges(): G_medium.edges[u,v]['weight'] = round(random.uniform(0.0, 100.0), 3) G_large = nx.complete_graph(99) for (u, v) in G_large.edges(): G_large.edges[u,v]['weight'] = round(random.uniform(0.0, 100.0), 3) write_input_file(G_small, "/Users/narenyenuganti/Desktop/170Proj/25.in") write_input_file(G_medium, "/Users/narenyenuganti/Desktop/170Proj/50.in") write_input_file(G_large, "/Users/narenyenuganti/Desktop/170Proj/100.in") if (validate_file("/Users/narenyenuganti/Desktop/170Proj/25.in") == False): raise Exception("incorrect format for 25.in") if (validate_file("/Users/narenyenuganti/Desktop/170Proj/50.in") == False): raise Exception("incorrect format for 50.in") if (validate_file("/Users/narenyenuganti/Desktop/170Proj/100.in") == False): raise Exception("incorrect format for 100.in")
from parse import write_input_file, read_input_file, write_output_file import networkx as nx G = nx.Graph() rows = [[1, 1], [2, 3], [4, 6], [7, 10], [11, 15], [16, 21], [22, 28]] paths = [[0, 29]] for row in rows: start, end = row path = [0] path += list(range(start, end + 1)) path += [29] paths.append(path) for path in paths: print(path) w = 1 for path in paths: nx.add_path(G, path, weight=w) w += 1 nx.draw(G) write_input_file(G, '30.in')