def __createSyPyNetwork__(nodes_val, id_to_edges): """ Internal. Converts a given simulation into a SyPy-friendly 'network' object that allows utilizing other (GSD-based) detection algorithms. Finds the largest connected component, and then creates a graph out of the bidirectional edges and corresponding nodes. """ all_ids = {node.id for node in nodes_val} hon_ids = {node.id for node in nodes_val if node.type == "hon"} syb_ids = [node.id for node in nodes_val if node.type == "syb"] mal_ids = [node.id for node in nodes_val if node.type == "mal"] succ_edges = set() bidir_edges = [] for node_id in all_ids: for edge in id_to_edges[node_id]: if edge.successful: src_id = edge.node_src.id dst_id = edge.node_dst.id if (dst_id, src_id) in succ_edges: bidir_edges += [(dst_id, src_id)] else: succ_edges.add((src_id, dst_id)) nx_graph = nx.Graph() nx_graph.add_nodes_from(all_ids) nx_graph.add_edges_from(bidir_edges) cc_graph = max(nx.connected_component_subgraphs(nx_graph), key=len) cc_nodes = cc_graph.nodes() cc_edges = [] for edge in bidir_edges: if edge[0] in cc_nodes and edge[1] in cc_nodes: cc_edges += [edge] nx_graph = nx.Graph() nx_graph.add_nodes_from(cc_nodes) nx_graph.add_edges_from(cc_edges) initial_sybils = list(set(all_ids) - set(cc_nodes)) honest_edge_counts = {node_id: 0 for node_id in cc_nodes} for edge in cc_edges: if edge[0] in hon_ids and edge[1] in hon_ids: honest_edge_counts[edge[0]] += 1 honest_edge_counts[edge[1]] += 1 known_honests = [ sorted(honest_edge_counts.items(), key=lambda kv: kv[1], reverse=True)[0][0] ] network = sypy.Network(None, None, "test", custom_graph=sypy.CustomGraph(nx_graph)) network.set_data(original_nodes=all_ids, sybils=syb_ids, malicious=mal_ids, known_honests=known_honests, initial_sybils=initial_sybils) return network
#create honest region honest_region = sypy.Region(graph=sypy.PowerLawGraph(num_nodes=100, node_degree=2, prob_triad=0.03, seed=1), name="HonestPowerLawGraph") honest_region.pick_random_honest_nodes(num_nodes=10) nx.set_node_attributes(honest_region.graph.structure, 'Node_type', 'Honest_node') #print honest_region.graph.structure.nodes(data='True') #honest_region.visualize() #create online scial network social_network = sypy.Network(left_region=honest_region, right_region=sybil_region, name="OnlineSocialNetwork") social_network.random_pair_stitch(num_edges=10) social_network.visualize() G = social_network.graph.structure #print G.nodes(data='True') nx.draw(G) plt.show() #create edgelist nx.write_edgelist(G, "edgelistfinal.txt") #find the diameter and radius dia = nx.diameter(G, e=None) #print (dia)
for node in honest_region.graph.nodes(): degree = honest_region.graph.structure.degree(node) if degree > max_degree: max_degree = degree max_node = node honest_region.pick_random_honest_nodes(num_nodes=49) if max_node in honest_region.known_honests: raise Exception("Max degree node already exists") honest_region.known_honests += [max_node] honest_stats = honest_region.get_region_stats() assert honest_stats.is_connected == True social_network = sypy.Network(left_region=honest_region, right_region=sybil_region, name="OnlineSocialNetwork", seed=GLOBAL_SEED) multi_benchmark = sypy.MultipleDetectorsBenchmark( detectors=[sypy.SybilRankDetector, sypy.SybilPredictDetector], network=social_network, thresholds=["pivot", "pivot"], kwargs=[{ "total_trust": social_network.graph.order(), "num_iterations_scaler": 1.0, "seed": GLOBAL_SEED }, { "total_trust": social_network.graph.order(), "num_iterations_scaler": 1.0, "operation_mode": "best", "seed": GLOBAL_SEED