def run_simulation(n, A1, clebsch_graph, iterations, perturbation_scale=0.1, epsilon=0): correct_count = 0 epsilon_vector = epsilon * np.ones(n) for i in range(0, iterations): # perturb matrix (introduce noise to the weights) clebsch_matrix = perturb_matrix(clebsch_graph.adjacency_matrix, perturbation_scale) A2 = Graph.create_adjacency_list( n, clebsch_matrix ) #need to do this as it hasn't updated the internal adjacency list representation # graph deconvolver with new components graph_deconvolver = GraphDeconvolver(n, A1, A2) #convolved graphs A_matrix = Graph.create_adjacency_matrix(n, A1) + clebsch_matrix status, problem_value, A1_star, A2_star = graph_deconvolver.deconvolve( A_matrix, epsilon_vector) if status == 'optimal': print('Problem status: ', status) cycle = is_cycle(Graph.create_adjacency_list(n, A1_star)) print('Is cycle: ', cycle) if cycle: correct_count += 1 return correct_count
def run_simulation(n, A1, multipartite_graph_matrix, iterations, partitions): correct_count = 0 for i in range(0,iterations): A2 = Graph.create_adjacency_list(n,multipartite_graph_matrix) #need to do this as it hasn't updated the internal adjacency list representation # graph deconvolver with new components graph_deconvolver = GraphDeconvolver(n,A1,A2) #convolved graphs A_matrix = Graph.create_adjacency_matrix(n,A1) + multipartite_graph_matrix status,problem_value,A1_star,A2_star= graph_deconvolver.deconvolve(A_matrix) # first check has the correct degree sequence before check multipartite, (needs to be complete) correct_degree_sequence = check_degree_sequence(partitions,np.round(A2_star)) # check_degree_sequence can't deal with weighted edges so round if correct_degree_sequence: adjacency_table = Graph.create_adjacency_table(n,A2_star) #print('p=',partitions,'\ng=',adjacency_table) multipartite_graph_recognizer=Multipartite_graph_recognizer(partitions,adjacency_table) ok=multipartite_graph_recognizer.check() print('multipartite:',multipartite_graph_recognizer.match) else: print('incorrect degree sequence') ok=False np.set_printoptions(suppress=True) print('Problem status: ',status) cycle = is_cycle(Graph.create_adjacency_list(n,A1_star)) print('Is cycle: ', cycle) print('A2 correct: ',ok) if cycle and ok: correct_count +=1 return correct_count
def run_simulation(n, A1, clebsch_graph, iterations, number_of_pertubations): correct_count = 0 for i in range(0, iterations): # perturb matrix (add/remove edge randomly) clebsch_matrix = clebsch_graph.adjacency_matrix.copy() clebsch_matrix = perturb_matrix(n, clebsch_matrix, number_of_pertubations) A2 = Graph.create_adjacency_list( n, clebsch_matrix ) #need to do this as it hasn't updated the internal adjacency list representation # graph deconvolver with new components graph_deconvolver = GraphDeconvolver(n, A1, A2) #convolved graphs A_matrix = Graph.create_adjacency_matrix(n, A1) + clebsch_matrix status, problem_value, A1_star, A2_star = graph_deconvolver.deconvolve( A_matrix) print('Problem status: ', status) cycle = is_cycle(Graph.create_adjacency_list(n, A1_star)) print('Is cycle: ', cycle) if cycle: correct_count += 1 return correct_count
def run_simulation(n, cycle, itererations, perturbation_scale=0.1, epsilon=0): correct_count = 0 graph_denoiser = GraphDenoiser(n, cycle) A_matrix = Graph.create_adjacency_matrix(n, A) # vector of small parameters for spectral hull epsilon_vector = epsilon * np.ones(n) # adjacency matrix to compare against to check is denoised correctly #test_clebsch_adjacency_matrix = Graph.create_adjacency_matrix(n,cycle) for i in range(0, iterations): # perturb matrix (introduce noise to the weights) A_matrix_noisy = perturb_matrix(A_matrix, perturbation_scale) status, problem_value, A_recovered = graph_denoiser.denoise( A_matrix_noisy, epsilon_vector) if status == 'optimal': print('Problem status: ', status) cycle = is_cycle(Graph.create_adjacency_list(n, A_recovered)) print('Is cycle: ', cycle) # check for clebsch graph #cycle = np.allclose(test_clebsch_adjacency_matrix,A_recovered, atol=1e-3) #print('Is clebsch graph: ', cycle) if cycle: correct_count += 1 return correct_count
def run_simulation(n, A1, clebsch_graph, iterations, perturbation_scale=0.1): correct_count = 0 epsilon_vector = np.zeros(n) # no small parameter # graph deconvolver with new components graph_deconvolver = GraphDeconvolver(n, A1, clebsch_graph.adjacency_list) for i in range(0, iterations): # perturb matrix (introduce noise to the weights) clebsch_matrix = perturb_matrix(clebsch_graph.adjacency_matrix, perturbation_scale) #convolved graphs A_matrix = Graph.create_adjacency_matrix(n, A1) + clebsch_matrix status, problem_value, A1_star, A2_star = graph_deconvolver.deconvolve( A_matrix, epsilon_vector) print('Problem status: ', status) cycle = is_cycle(Graph.create_adjacency_list(n, A1_star)) print('Is cycle: ', cycle) if cycle: correct_count += 1 return correct_count
def run_simulation(n, cycle,itererations, perturbation_scale = 0.1): correct_count = 0 graph_denoiser = GraphDenoiser(n,cycle) A_matrix = Graph.create_adjacency_matrix(n,A) # generate cum sum of mean shift in eigenvalue distribution for i in range(0,iterations): # perturb matrix (introduce noise to the weights) A_matrix_noisy = perturb_matrix(A_matrix,perturbation_scale) epsilon_vector = calculate_epsilon_vector(A_matrix,A_matrix_noisy) print status,problem_value,A_recovered= graph_denoiser.denoise(A_matrix_noisy,epsilon_vector) print('Problem status: ',status) cycle = is_cycle(Graph.create_adjacency_list(n,A_recovered)) print('Is cycle: ', cycle) if cycle: correct_count +=1 return correct_count
def run_simulation(n, cycle, itererations, perturbation_scale=0.1, epsilon=0): correct_count = 0 graph_denoiser = GraphDenoiser(n, cycle) A_matrix = Graph.create_adjacency_matrix(n, A) # vector of small parameters for spectral hull epsilon_vector = epsilon * np.ones(n) for i in range(0, iterations): # perturb matrix (introduce noise to the weights) A_matrix_noisy = perturb_matrix(A_matrix, perturbation_scale) status, problem_value, A_recovered = graph_denoiser.denoise( A_matrix_noisy, epsilon_vector) if status == 'optimal': print('Problem status: ', status) cycle = is_cycle(Graph.create_adjacency_list(n, A_recovered)) print('Is cycle: ', cycle) if cycle: correct_count += 1 return correct_count
def run_simulation(n, multipartite_graph_matrix, iterations, partitions, perturbation_scale=0.1): correct_count = 0 graph_denoiser = GraphDenoiser( n, Graph.create_adjacency_list(n, multipartite_graph_matrix)) # vector of small parameters for spectral hull epsilon_vector = epsilon * np.ones(n) for i in range(0, iterations): A_matrix_noisy = perturb_matrix(multipartite_graph_matrix, perturbation_scale) status, problem_value, A_recovered = graph_denoiser.denoise( A_matrix_noisy, epsilon_vector) # first check has the correct degree sequence before check multipartite, (needs to be complete) correct_degree_sequence = check_degree_sequence( partitions, np.round(A_recovered) ) # check_degree_sequence can't deal with weighted edges so round if correct_degree_sequence: adjacency_table = Graph.create_adjacency_table(n, A_recovered) multipartite_graph_recognizer = Multipartite_graph_recognizer( partitions, adjacency_table) ok = multipartite_graph_recognizer.check() print('multipartite:', multipartite_graph_recognizer.match) else: print('incorrect degree sequence') ok = False if ok: correct_count += 1 return correct_count
np.set_printoptions(suppress=True) print('Problem status: ',status) cycle = is_cycle(Graph.create_adjacency_list(n,A1_star)) print('Is cycle: ', cycle) print('A2 correct: ',ok) if cycle and ok: correct_count +=1 return correct_count if __name__ == '__main__': n=16 # (8,8) graph p=(8,8) multipartite_graph_matrix =complete_multipartite(p) A2 = Graph.create_adjacency_list(n,multipartite_graph_matrix ) #16 cycle A1 = ((0,5),(5,13),(13,14),(14,6),(6,1),(1,7),(7,2),(2,8),(8,11),(11,15),(15,10),(10,9),(9,4),(4,12),(12,3),(3,0),) iterations = 100 max_number_perturbations = 4 file_path = 'results/multipartite_deconvolution_perturbation.txt' f=open(file_path,'w') for number_of_perturbations in range(1,max_number_perturbations+1): correct_count = run_simulation(n, A1,multipartite_graph_matrix,iterations, p, number_of_perturbations) f.write(str(number_of_perturbations) + ' ' + str(correct_count) +'\n') print(str(correct_count) + ' out of '+ str(iterations) + ' iterations') f.close()
from graphs.graph_visualiser import GraphVisualiser from deconvolve import GraphDeconvolver from eigenvalue_investigations.eigenvalue_permutations import matrix_permute if __name__ == '__main__': n = 45 # regular graph file_path = 'graphs/data/45_12_3_3.txt' graph_loader = GraphLoader(n, file_path) regular_graph = graph_loader.load() # randomly permute regular_matrix = matrix_permute(n, regular_graph.adjacency_matrix) A2 = Graph.create_adjacency_list( n, regular_matrix ) #need to do this as it hasn't updated the internal adjacency list representation #45 cycle A1 = ((0, 24), (0, 34), (1, 10), (1, 26), (2, 33), (2, 41), (3, 7), (3, 38), (4, 28), (4, 32), (5, 19), (5, 23), (6, 18), (6, 42), (7, 16), (8, 13), (8, 25), (9, 36), (9, 37), (10, 30), (11, 13), (11, 34), (12, 29), (12, 39), (14, 23), (14, 33), (15, 25), (15, 30), (16, 28), (17, 24), (17, 43), (18, 43), (19, 22), (20, 32), (20, 40), (21, 35), (21, 44), (22, 37), (26, 40), (27, 31), (27, 41), (29, 42), (31, 38), (35, 36), (39, 44)) A_matrix = Graph.create_adjacency_matrix( n, A1) + regular_matrix #convolve the components graph_deconvolver = GraphDeconvolver(n, A1, A2)
np.set_printoptions(suppress=True) print('Problem status: ', status) print('Norm value: ', problem_value) print('A: \n', A_matrix) print('A1: \n', A1_star) print('A2: \n', A2_star) print('A1+A2: \n ', np.add(A1_star, A2_star)) def set_visualiser_attributes(graph_visualiser): graph_visualiser.A.node_attr['shape'] = 'circle' graph_visualiser.A.node_attr['style'] = 'filled' graph_visualiser.A.node_attr['color'] = 'red' graph_visualiser.A.edge_attr['color'] = 'blue' graph_visualiser.A.edge_attr['penwidth'] = '2em' return graph_visualiser graph_visualiser = GraphVisualiser(Graph.create_adjacency_list( n, A_matrix)) graph_visualiser = set_visualiser_attributes(graph_visualiser) graph_visualiser.draw_png('figures/A_small.png') graph_visualiser = GraphVisualiser( Graph.create_adjacency_list(n, np.round(A1_star))) graph_visualiser = set_visualiser_attributes(graph_visualiser) graph_visualiser.draw_png('figures/A1_star_small.png') graph_visualiser = GraphVisualiser( Graph.create_adjacency_list(n, np.round(A2_star))) graph_visualiser = set_visualiser_attributes(graph_visualiser) graph_visualiser.draw_png('figures/A2_star_small.png')
limit_constraints = NodeLimitConstraint(graph_generator.A, lower_limit=0, upper_limit=1) # 2nd largest eigenvalue of the laplacian >= 4 laplacian_constraints = LaplacianLambdaSecondMinConstraint( graph_generator.A, 4) # diag(A) == 0 diagonal_constraints = DiagonalConstraint(n, graph_generator.A, 0) constraints = limit_constraints.constraint_list + diagonal_constraints.constraint_list + degree_constraints.constraint_list + laplacian_constraints.constraint_list status, problem_value, graph = graph_generator.generate_graph(constraints) np.set_printoptions(suppress=True) print('Problem status ', status) print('Adjacency matrix which satisfies constraints: ', np.round(graph, 5)) laplacian_of_adjacency_matrix = scipy.sparse.csgraph.laplacian(graph) sorted_eigen_values = np.linalg.eigvalsh(laplacian_of_adjacency_matrix) print('2nd smallest eigenvalue: ', sorted_eigen_values[1]) print('Degree of nodes : ', np.reshape(np.sum(graph, axis=1), n)) adjacency_list = Graph.create_adjacency_list(n, graph) graph_visualiser = GraphVisualiser(adjacency_list) graph_visualiser.A.node_attr['shape'] = 'circle' graph_visualiser.A.node_attr['style'] = 'filled' graph_visualiser.A.node_attr['color'] = 'red' graph_visualiser.draw_png('figures/generated_graph.png')
# add gaussian noise to cycle A_matrix = Graph.create_adjacency_matrix(n, A) A_matrix_noisy = perturb_matrix(A_matrix, 0.2) epsilon_vector = np.zeros(n) status, problem_value, A_recovered = graph_denoiser.denoise( A_matrix_noisy, epsilon_vector) np.set_printoptions(precision=1e-3, suppress=True) print('Problem status: ', status) print('Norm value: ', problem_value) print('Recovered A: \n', A_recovered) print('is cycle graph: ', is_cycle(Graph.create_adjacency_list(n, A_recovered))) def set_visualiser_attributes(graph_visualiser): graph_visualiser.A.node_attr['shape'] = 'circle' graph_visualiser.A.node_attr['style'] = 'filled' graph_visualiser.A.node_attr['color'] = 'red' graph_visualiser.A.edge_attr['color'] = 'blue' #graph_visualiser.A.edge_attr['penwidth']='2em' return graph_visualiser graph_visualiser = GraphVisualiser( Graph.create_weighted_adjacency_list(n, A_matrix_noisy, 1e-6)) # weighted adjacency list graph_visualiser = set_visualiser_attributes(graph_visualiser) graph_visualiser.draw_png_weighted_graph('figures/A_noisy.png')
(8, 13), (8, 15), (9, 13), (9, 14), (9, 15), (10, 12), (10, 13), (11, 13), (11, 14), (12, 14), ) A_matrix = Graph.create_adjacency_matrix(n, A) correct_count = 0 iterations = 100 graph_deconvolver = GraphDeconvolver(n, A1, A2) for i in range(1, iterations): status, problem_value, A1_star, A2_star = graph_deconvolver.deconvolve( A_matrix) print('Problem status: ', status) cycle = is_cycle(Graph.create_adjacency_list(n, A1_star)) print('Is cycle: ', cycle) if cycle: correct_count += 1 print( str(correct_count) + ' correct out of ' + str(iterations) + ' iterations')
if status == 'optimal': print('Problem status: ', status) cycle = is_cycle(Graph.create_adjacency_list(n, A_recovered)) print('Is cycle: ', cycle) if cycle: correct_count += 1 return correct_count if __name__ == '__main__': n = 16 #16 cycle A = Graph.create_adjacency_list(n, bicycle(n)) iterations = 100 graph_name = 'bicycle' perturbations = [0.05, 0.052, 0.055] epsilons = [ 0, 0.0001, 0.0002, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.12, 0.15, 0.2, 0.22, 0.25 ] file_path = '../results/' + graph_name + '_denoise_epsilon_05.txt' f = open(file_path, 'w') for perturbation in perturbations: print('Perturbation scale: ', perturbation) for epsilon in epsilons: print('epsilon: ', epsilon)
def bicycle(n): A=cycle(n) A[0,n//2]=A[n//2,0]=1 return A n=16 #16 cycle # A = ((0,5),(5,13),(13,14),(14,6),(6,1),(1,7),(7,2),(2,8),(8,11),(11,15),(15,10),(10,9),(9,4),(4,12),(12,3),(3,0),) #clebsch graph A=((0,1),(0,4),(0,7),(0,9),(0,10),(1,2),(1,5),(1,8),(1,11),(2,3),(2,6),(2,9),(2,12),(3,4),(3,5),(3,7),(3,13),(4,6),(4,8),(4,14),(5,10),(5,14),(5,15),(6,10),(6,11),(6,15),(7,11),(7,12),(7,15),(8,12),(8,13),(8,15),(9,13),(9,14),(9,15),(10,12),(10,13),(11,13),(11,14),(12,14),) A = Graph.create_adjacency_list(n,bicycle(n)) graph_denoiser = GraphDenoiser(n,A) # add gaussian noise to cycle A_matrix = Graph.create_adjacency_matrix(n,A) A_matrix_noisy = perturb_matrix(A_matrix,0.4) epsilon_vector = np.zeros(n) status,problem_value,A_recovered= graph_denoiser.denoise(A_matrix_noisy, epsilon_vector) np.set_printoptions(suppress=True) print('Problem status: ',status) print('Norm value: ',problem_value)