예제 #1
0
    def reset_simulation(self):
        """
        Resets the simulation between each run
        """

        self.graph = self.graph_og.copy()
        self.vaccinated = set()
        self.sim_info = defaultdict()

        self.infected = set(
            np.random.choice(list(self.graph.nodes),
                             size=int(self.prm['c'] * len(self.graph)),
                             replace=False).tolist())

        # decrease network diffusion
        if self.prm['diffusion'] == 'min' and self.prm['k'] > 0:

            if get_attack_category(self.prm['method']) == 'node':
                self.vaccinated = set(
                    run_attack_method(self.graph,
                                      self.prm['method'],
                                      self.prm['k'],
                                      seed=self.prm['seed']))
                self.infected = self.infected.difference(self.vaccinated)

            elif get_attack_category(self.prm['method']) == 'edge':
                edge_info = run_attack_method(self.graph,
                                              self.prm['method'],
                                              self.prm['k'],
                                              seed=self.prm['seed'])
                self.graph.remove_edges_from(edge_info)
            else:
                print(self.prm['method'], 'not available')

        # increase network diffusion
        elif self.prm['diffusion'] == 'max' and self.prm['k'] > 0:

            if get_defense_category(self.prm['method']) == 'edge':
                edge_info = run_defense_method(self.graph,
                                               self.prm['method'],
                                               self.prm['k'],
                                               seed=self.prm['seed'])

                self.graph.add_edges_from(edge_info['added'])
                self.graph.remove_edges_from(edge_info['removed'])
            else:
                print(self.prm['method'], 'not available')

        elif self.prm['diffusion'] is not None:
            print(self.prm['diffusion'], "not available or k <= 0")
예제 #2
0
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
예제 #3
0
    def reset_simulation(self):
        """
        Resets the simulation between each run
        """

        self.graph_ = self.graph.copy()
        self.attacked = []
        self.protected = []
        self.connectivity = []

        # attacked nodes or edges
        if self.prm['attack'] is not None and self.prm['k_a'] > 0:
            self.attacked = run_attack_method(self.graph_, self.prm['attack'], self.prm['k_a'], approx=self.prm['attack_approx'], seed=self.prm['seed'])

            if get_attack_category(self.prm['attack']) == 'edge':
                self.graph_.remove_nodes_from(self.attacked)

        elif self.prm['attack'] is not None:
            print(self.prm['attack'], "not available or k <= 0")

        # defended nodes or edges
        if self.prm['defense'] is not None and self.prm['steps'] > 0:

            if get_defense_category(self.prm['defense']) == 'node':
                self.protected = run_defense_method(self.graph_, self.prm['defense'], self.prm['steps'], seed=self.prm['seed'])

            elif get_defense_category(self.prm['defense']) == 'edge':
                self.protected = run_defense_method(self.graph_, self.prm['defense'], self.prm['steps'], seed=self.prm['seed'])

        elif self.prm['defense'] is not None:
            print(self.prm['defense'], "not available or k <= 0")

        # remove attacked nodes after checking that they are not defended
        if get_attack_category(self.prm['attack']) == 'node':
            if get_defense_category(self.prm['defense']) == 'node':
                diff = set(self.protected) - set(self.attacked)
                self.graph_.remove_nodes_from(diff)
            else:
                self.graph_.remove_nodes_from(self.attacked)
예제 #4
0
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]
예제 #5
0
    def reset_simulation(self):
        """
         Resets the simulation between each run
         """

        self.protected = set()
        self.failed = set()
        self.load = defaultdict()
        self.sim_info = defaultdict()
        self.capacity = self.capacity_og.copy()

        for n in self.graph.nodes:
            self.load[n] = self.capacity[n] * np.random.uniform(
                0, self.prm['l']
            )  # self.capacity[n] * np.random.uniform(0, self.prm['l'])  # self.capacity[n] *
            self.capacity[n] = self.capacity[n] * (1.0 + self.prm['r'])

        self.track_simulation(step=0)

        # attacked nodes or edges
        if self.prm['attack'] is not None and self.prm['k_a'] > 0:
            self.failed = set(
                run_attack_method(self.graph,
                                  self.prm['attack'],
                                  self.prm['k_a'],
                                  approx=self.prm['attack_approx'],
                                  seed=self.prm['seed']))

            if get_attack_category(self.prm['attack']) == 'node':
                for n in self.failed:
                    self.load[n] = 2 * self.load[
                        n]  # increase load by 2x when attacked

            elif get_attack_category(self.prm['attack']) == 'edge':
                self.graph.remove_edges_from(self.failed)

        # defended nodes or edges
        if self.prm['defense'] is not None and self.prm['k_d'] > 0:

            if get_defense_category(self.prm['defense']) == 'node':
                self.protected = run_defense_method(self.graph,
                                                    self.prm['defense'],
                                                    self.prm['k_d'],
                                                    seed=self.prm['seed'])
                for n in self.protected:
                    self.capacity[n] = 2 * self.capacity[
                        n]  # double the capacity when defended

            elif get_defense_category(self.prm['defense']) == 'edge':
                edge_info = run_defense_method(self.graph,
                                               self.prm['defense'],
                                               self.prm['k_d'],
                                               seed=self.prm['seed'])

                self.graph.add_edges_from(edge_info['added'])

                if 'removed' in edge_info:
                    self.graph.remove_edges_from(edge_info['removed'])

        elif self.prm['defense'] is not None:
            print(self.prm['defense'], "not available or k <= 0")

        self.track_simulation(step=1)