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")
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)
def run_single_sim(self): """ Run the defense simulation """ for step in range(self.prm['steps']): if step < len(self.protected) and len(self.protected) > 0 and get_defense_category(self.prm['defense']) == 'edge': self.track_simulation(step) u, v = self.protected['added'][step] self.graph_.add_edge(u, v) if 'removed' in self.protected[step]: u, v = self.protected['removed'][step] self.graph.remove_edge(u, v) else: self.track_simulation(step) print("Ending defense simulation early, not an 'edge' defense or out of {}s".format(get_attack_category(self.prm['defense']))) results = [v['measure'] if v['measure'] is not None else 0 for k, v in self.sim_info.items()] return results
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)