def test_attack_strength(): """ check that valid nodes are returned :return: """ graph = karate() methods = get_attack_methods() strength = list(range(1, 20)) for method in methods: for k in strength: nodes = run_attack_method(graph, method=method, k=k) assert len(nodes) == k
def test_defense_strength(): """ check that valid nodes are returned :return: """ graph = karate() methods = get_defense_methods() strength = list(range(1, 20)) for method in methods: if get_defense_category(method) == 'node': for k in strength: nodes = run_defense_method(graph, method=method, k=k) # print(method, k, nodes) assert len(nodes) == k
def test_cascading(): params = { 'runs': 10, 'steps': 30, 'l': 0.8, 'r': 0.5, 'capacity_approx': np.inf, 'k_a': 4, 'attack': 'rnd_node', 'k_d': 0, 'defense': None, 'robust_measure': 'largest_connected_component', 'seed': 1, 'plot_transition': False, 'gif_animation': False } graph = karate() cf = Cascading(graph, **params) attacked = cf.run_simulation() params['k_a'] = 0 params['attack'] = None cf = Cascading(graph, **params) baseline = cf.run_simulation() params['k_a'] = 4 params['attack'] = 'rnd_node' params['k_d'] = 4 params['defense'] = 'pr_node' cf = Cascading(graph, **params) defended = cf.run_simulation() assert sum(attacked) <= sum(defended) <= sum(baseline)
def test_method_selection(): """ check that valid nodes are returned :return: """ ground_truth = { # karate graph top 4 nodes to be removed 'ns_node': ([33, 0, 2, 32], [33, 2, 0, 32]), 'pr_node': [33, 0, 32, 2], 'eig_node': [33, 0, 2, 32], 'id_node': [33, 0, 32, 2], 'rd_node': [33, 0, 32, 1], 'ib_node': [0, 33, 32, 2], 'rb_node': [0, 33, 32, 2], 'ns_line_edge': [(32, 33), (8, 33), (31, 33), (13, 33)], 'pr_line_edge': [(32, 33), (0, 2), (0, 1), (0, 31)], 'eig_line_edge': [(32, 33), (8, 33), (31, 33), (13, 33)], 'deg_line_edge': [(32, 33), (0, 2), (0, 1), (31, 33)], 'id_edge': [(32, 33), (0, 2), (0, 1), (2, 32)], 'rd_edge': [(32, 33), (0, 2), (0, 1), (2, 32)], 'ib_edge': ([(0, 31), (0, 6), (0, 5), (0, 2)], [(0, 31), (0, 5), (0, 6), (0, 2)]), 'rb_edge': [(0, 31), (0, 2), (0, 8), (13, 33)], } graph = karate() k = 4 methods = get_attack_methods() for method in methods: values = run_attack_method(graph, method=method, k=k, seed=1) # print(method, values) if 'rnd' not in method and method != 'ib_edge' and method != 'ns_node': assert values == ground_truth[method] elif method == 'ib_edge' or method == 'ns_node': assert values == ground_truth[method][0] or values == ground_truth[ method][1]
def test_sis_model(): params = { 'model': 'SIS', 'b': 0.00208, 'd': 0.01, 'c': 1, 'runs': 10, 'steps': 5000, 'diffusion': 'max', 'method': 'add_edge_random', 'k': 15, 'seed': 1, 'plot_transition': False, 'gif_animation': False } graph = karate() ds = Diffusion(graph, **params) increased_diffusion = ds.run_simulation() params['diffusion'] = None params['method'] = None params['k'] = 0 ds = Diffusion(graph, **params) baseline_diffusion = ds.run_simulation() params['diffusion'] = 'min' params['method'] = 'ns_node' params['k'] = 4 ds = Diffusion(graph, **params) decreased_diffusion = ds.run_simulation() assert sum(decreased_diffusion) < sum(baseline_diffusion) < sum(increased_diffusion)
def test_method_selection(): """ check that valid nodes are returned :return: """ ground_truth = { # karate graph top 4 nodes to be monitored or top 4 edges to added/rewired 'ns_node': [33, 0, 2, 32], 'pr_node': [33, 0, 32, 2], 'eig_node': [33, 0, 2, 32], 'id_node': [33, 0, 32, 2], 'rd_node': [33, 0, 32, 1], 'ib_node': [0, 33, 32, 2], 'rb_node': [0, 33, 32, 2], 'rnd_node': [14, 19, 3, 27], 'add_edge_pr': { 'added': [(33, 0), (0, 32), (33, 2), (33, 1)] }, 'add_edge_eig': { 'added': [(33, 0), (33, 2), (0, 32), (33, 1)] }, 'add_edge_deg': { 'added': [(33, 0), (0, 32), (33, 2), (33, 1)] }, 'add_edge_random': { 'added': [(14, 19), (16, 22), (29, 20), (31, 15)] }, 'add_edge_preferential': { 'added': [(11, 9), (12, 14), (15, 16), (17, 18)] }, 'rewire_edge_random': { 'added': [(21, 26), (30, 31), (18, 26), (17, 29)], 'removed': [(27, 33), (32, 33), (9, 33), (2, 9)] }, 'rewire_edge_random_neighbor': { 'added': [(16, 22), (9, 12), (3, 23), (16, 30)], 'removed': [(14, 33), (19, 1), (3, 12), (27, 2)] }, 'rewire_edge_preferential': { 'added': [(18, 12), (10, 9), (28, 5), (1, 16)], 'removed': [(33, 18), (0, 10), (33, 28), (0, 1)] }, 'rewire_edge_preferential_random': { 'added': [(27, 19), (32, 8), (9, 32), (9, 10)], 'removed': [(27, 33), (32, 33), (9, 33), (2, 9)] } } graph = karate() k = 4 methods = get_defense_methods() for method in methods: values = run_defense_method(graph, method=method, k=k, seed=1) # print(method, values) if get_defense_category(method) == 'node': assert (values == ground_truth[method]) else: assert np.array_equal(values['added'], ground_truth[method]['added']) if 'removed' in values: assert (np.array_equal(values['removed'], ground_truth[method]['removed']))
def run_test(params): graph = karate() ds = Diffusion(graph, **params) ds.run_simulation()