def test_deep_equals(self): A_matrix = Graph.create_adjacency_matrix(self.n, self.A) A1_matrix = Graph.create_adjacency_matrix(self.n, self.A1) A2_matrix = Graph.create_adjacency_matrix(self.n, self.A2) result = Utils.deep_equals(A_matrix, A1_matrix + A2_matrix, 1e-4) self.assertTrue(result)
def test_solution_checker(self): A_matrix = Graph.create_adjacency_matrix(self.n,self.A) A1_matrix = Graph.create_adjacency_matrix(self.n,self.A1) A2_matrix = Graph.create_adjacency_matrix(self.n,self.A2) perturbed_A1 = A1_matrix - self.pertubation_matrix perturbed_A2 = A2_matrix + self.pertubation_matrix graph_deconvolver = GraphDeconvolver(self.n,self.A1,self.A2) result = graph_deconvolver.check_solution(A_matrix,perturbed_A1,perturbed_A2,self.tolerance) self.assertTrue(result)
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, 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, 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, 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, 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 generate_cycle_family_constraints(n, M): # 16-node cycles # all values between 0 and 1 limit_constraints = NodeLimitConstraint(M, lower_limit=0, upper_limit=1) # diag(A) == 0 diagonal_constraints = DiagonalConstraint(n, M, 0) # all nodes have degree 2 degree_constraints = DegreeConstraint(n, M, 2) # convex hull of 16 node cycles A = ( (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10), (10, 11), (11, 12), (12, 13), (13, 14), (14, 15), (15, 0), ) A_matrix = Graph.create_adjacency_matrix(n, A) spectral_hull_constraint = SpectralHullConstraint(A_matrix, M) return limit_constraints.constraint_list + diagonal_constraints.constraint_list + degree_constraints.constraint_list + spectral_hull_constraint.constraint_list
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) status, problem_value, A1_star, A2_star = graph_deconvolver.deconvolve( A_matrix) 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):
# sparse well-connected graphs on 16 nodes # all values between 0 and 1 limit_constraints = NodeLimitConstraint(M, lower_limit=0, upper_limit=1) # diag(M) == 0 diagonal_constraints = DiagonalConstraint(n, M, 0) # (A*ones)_i <= 2.5 degree_constraints = MaxWeightedDegreeConstraint(n, M, 2.5) # 2nd smalled eigenvalue of the laplacian >= 1.1 laplacian_constraints = LaplacianLambdaSecondMinConstraint(M, 1.1) return limit_constraints.constraint_list + diagonal_constraints.constraint_list + degree_constraints.constraint_list + laplacian_constraints.constraint_list if __name__ == '__main__': # a 16 node path (cycle with vertex removed n = 16 A = ((0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10), (10, 11), (11, 12), (12, 13), (13, 14), (14, 15)) A_matrix = Graph.create_adjacency_matrix(n, A) M = cvx.Symmetric(n) family_1_constraints = generate_cycle_family_constraints(n, M) family_2_constraints = generate_sparse_well_connected_constraints(n, M) test_families(A_matrix, M, family_1_constraints, family_2_constraints)
def __init__(self, n, A1, A2): self.n = n self.A1 = Graph.create_adjacency_matrix( n, A1 ) #could accept either matrix or tuple and create matrix if needed? self.A2 = Graph.create_adjacency_matrix(n, A2)
else: return problem.status, np.nan, np.nan if __name__ == '__main__': nk4 = 4 K4_A = ( (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3), ) K4_matrix = Graph.create_adjacency_matrix(nk4, K4_A) #Generate large graph with erdos renyi model n = 40 p = 0.5 er_graph = ErdosRenyiGraph(n, p) network = er_graph.generator() #need to rename doesn't make much sense A0 = Graph.create_adjacency_matrix(n, network) #embed subgraph (should randomise the location) n_2 = 20 #n/2 A0[n_2:n_2 + nk4, n_2:n_2 + nk4] = K4_matrix # put in the middle status, value, sub_graph_location = sub_graph_finder(K4_matrix, A0) np.set_printoptions(suppress=True)