-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
318 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import osmnx as ox | ||
import networkx as nx | ||
import random | ||
|
||
from CityGraph import CityGraph | ||
|
||
# Juste pour test pour l'instant | ||
ville="Saints" | ||
|
||
class Attack : | ||
""" | ||
Represents an attack on a graph | ||
--- Attributes --- | ||
graph : the graph to attack (READ-ONLY) | ||
attacks : the attacks to perform | ||
""" | ||
|
||
graph : CityGraph | ||
attacks : list[int] # Format : [time, edge_idx] | ||
|
||
def __init__(self, graph : CityGraph) : | ||
self.graph = graph | ||
self.attacks = [] | ||
|
||
def add_attack(self, time : int, edge_idx : int) -> None : | ||
"""Adds an attack to the list""" | ||
self.attacks.append((time, edge_idx)) | ||
|
||
def write_to_file(self, file_path : str) -> None : | ||
"""Writes the attacks to a file""" | ||
|
||
# Conversion | ||
edges_deleted : dict[int, set[int]] = {} | ||
for time, edge_idx in self.attacks : | ||
node0 = self.graph.get_node_index(self.graph.get_edge_index(edge_idx)[0]) | ||
node1 = self.graph.get_node_index(self.graph.get_edge_index(edge_idx)[1]) | ||
if (node0, node1) in edges_deleted : | ||
edges_deleted[(node0, node1)].add(time) | ||
else : | ||
edges_deleted[(node0, node1)] = {time} | ||
|
||
# Sorting for easier parsing in the future | ||
for edge in edges_deleted : | ||
edges_deleted[edge] = list(edges_deleted[edge]) | ||
edges_deleted[edge].sort() | ||
|
||
edges_deleted : list[tuple[tuple[int, int], list[int]]] = [((edge[0], edge[1]), edges_deleted[edge]) for edge in edges_deleted] | ||
edges_deleted.sort(key = lambda x : (x[0][0], x[0][1])) | ||
|
||
# Writing | ||
with open(file_path, "w") as file : | ||
file.write(f"{len(edges_deleted)}\n") | ||
for edge, times in edges_deleted : | ||
file.write(f"{edge[0]} {edge[1]} {len(times)} {' '.join(map(str, times))}\n") | ||
|
||
print(f"Attacks have been written to {file_path}") | ||
|
||
def print_suppression_edge(graph : CityGraph, edge_idx : int, time : int): | ||
edge = graph.get_edge_index(edge_idx) | ||
noeud0 = graph.get_node_index(edge[0]) | ||
noeud1 = graph.get_node_index(edge[1]) | ||
print(f"T={time}, suppression de l'arete {edge_idx} entre les noeuds {noeud0} et {noeud1}") |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import osmnx as ox | ||
import networkx as nx | ||
import random | ||
|
||
from CityGraph import CityGraph | ||
from Attack import Attack, print_suppression_edge | ||
|
||
# Juste pour test pour l'instant | ||
ville="Saints" | ||
|
||
def random_attack(graph : CityGraph, nbTemps : int, budget : int) -> Attack : | ||
"""A chaque temps, on supprime budget liens choisis aleatoirement""" | ||
|
||
attacks = Attack(graph) | ||
edges = list(graph.edges()) | ||
|
||
for i in range(nbTemps) : | ||
edges_t = edges.copy() | ||
budget_used : int = 0 | ||
nb_retires : int = 0 | ||
while budget_used < budget : | ||
edge_idx = random.randint(0, graph.nb_edges() - 1 - nb_retires) | ||
# print_suppression_edge(graph, edge_idx, i) | ||
attacks.add_attack(i, edge_idx) | ||
edges_t.pop(edge_idx) | ||
nb_retires += 1 | ||
budget_used += 1 #graph.get_nb_lanes_of_edge(edge_idx) | ||
|
||
return attacks | ||
|
||
|
||
graph = CityGraph(ville) | ||
graph.print_stats() | ||
graph.show() | ||
|
||
a = random_attack(graph, 50, 50) | ||
a.write_to_file("test_attack.txt") |
Oops, something went wrong.