Skip to content

Commit

Permalink
bleh
Browse files Browse the repository at this point in the history
  • Loading branch information
Fur0rem committed Feb 6, 2024
1 parent 46ae4b8 commit f9f2dcf
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 153 deletions.
52 changes: 51 additions & 1 deletion scripts/Strategies.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import random
import networkx as nx

from CityGraph import CityGraph
from Attack import Attack, print_suppression_edge


# Juste pour test pour l'instant
ville="Saints"

Expand Down Expand Up @@ -98,6 +100,12 @@ def moving_attack(graph : CityGraph, nbTimes : int, budget : int) -> Attack :
for edge_idx in range(graph.nb_edges()) :
if nb_people_on_edges[edge_idx] > 0 :
# Move the people

# If there are no neighbours, stay on the edge
if len(neighbours[edge_idx]) == 0 :
attacks.add_attack(time, edge_idx)
continue

edge_idx2 = random.choice(list(neighbours[edge_idx]))
nb_people_on_edges[edge_idx] -= graph.get_nb_lanes_of_edge(edge_idx)
nb_people_on_edges[edge_idx2] += graph.get_nb_lanes_of_edge(edge_idx)
Expand All @@ -117,6 +125,46 @@ def moving_attack(graph : CityGraph, nbTimes : int, budget : int) -> Attack :

return attacks

def is_bridge(graph : CityGraph, node0 : int, node1 : int) -> bool :
"""Returns True if the edge is a bridge"""
# TODO : ca marche pas pour les graphes orientes car networkx veut me rendre malheureux
# Remove the edge
graph.graph.remove_edge(node0, node1)
# Check if the graph is still connected
is_connected = nx.is_connected(graph.graph)
# Add the edge back
graph.graph.add_edge(node0, node1)
return not is_connected


def connex_attack(graph : CityGraph, nbTimes : int, budget : int) -> Attack :
"""We remove edges which cut the graph into two parts recursively
Algorithm reference : https://stackoverflow.com/questions/11218746/bridges-in-a-connected-graph"""

attacks = Attack(graph)
edges = list(graph.edges())

# Find the bridge with the highest betweenness centrality
# Then remove it
for time in range(nbTimes) :
# Find the bridge with the highest betweenness centrality
bridge = None
max_betweenness = 0
for edge_idx in range(graph.nb_edges()) :
edge = graph.get_edge_at(edge_idx)
if is_bridge(graph, edge[0], edge[1]) :
betweenness = graph.get_edge_betweenness_centrality(edge_idx)
if betweenness > max_betweenness :
bridge = edge_idx
max_betweenness = betweenness
if bridge is not None :
attacks.add_attack(time, bridge)
else :
break

print_suppression_edge(graph, bridge)

return attacks

graph = CityGraph(ville)
graph.print_stats()
Expand All @@ -131,4 +179,6 @@ def moving_attack(graph : CityGraph, nbTimes : int, budget : int) -> Attack :
# a = betweenness_centralities_attack(graph, 10, 10)
# a.write_to_file("test_betweenness_centralities_attack.txt")
a = moving_attack(graph, 10, 100)
a.write_to_file("test_moving_attack.txt")
a.write_to_file("test_moving_attack.txt")
a = connex_attack(graph, 10, 10)
a.write_to_file("test_connex_attack.txt")
1 change: 1 addition & 0 deletions test_connex_attack.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
300 changes: 148 additions & 152 deletions test_moving_attack.txt
Original file line number Diff line number Diff line change
@@ -1,153 +1,149 @@
152
1 20 2 4 9
5 1 3 0 6 10
5 30 9 0 1 2 3 5 6 7 8 10
148
0 1 2 0 10
0 5 3 4 5 9
1 20 9 1 2 3 4 5 6 7 8 9
2 3 2 0 10
2 7 6 1 2 5 6 8 9
3 4 11 0 1 2 3 4 5 6 7 8 9 10
5 1 3 0 4 10
5 30 4 1 2 6 9
6 63 2 0 10
8 15 3 2 3 5
8 26 8 0 1 3 4 7 8 9 10
8 31 3 5 6 9
9 26 4 0 3 9 10
9 60 4 1 3 4 6
9 61 3 4 5 8
10 23 3 0 1 10
10 33 4 2 4 6 7
10 42 7 0 1 3 4 5 8 10
11 12 7 0 1 3 4 6 7 10
11 50 3 2 8 10
12 11 6 0 2 5 6 9 10
12 13 8 0 1 2 3 4 5 7 10
12 55 8 0 3 4 6 7 8 9 10
13 12 6 0 1 5 6 9 10
13 14 7 0 3 4 6 7 8 10
13 55 11 0 1 2 3 4 5 6 7 8 9 10
14 13 4 1 2 3 7
14 51 5 3 4 5 8 9
14 56 5 0 2 5 7 10
15 8 1 3
15 16 4 0 2 4 10
15 82 4 2 7 8 9
16 15 6 0 1 5 6 9 10
16 17 8 0 2 3 4 5 7 8 10
16 57 7 0 1 3 4 6 8 10
17 16 5 0 4 7 9 10
17 26 7 0 1 3 5 6 7 10
17 61 7 1 2 3 4 6 7 9
18 19 7 0 1 2 4 7 8 10
18 36 5 0 1 2 5 10
19 18 6 1 5 6 7 8 9
19 75 6 0 3 4 7 9 10
20 19 5 1 2 5 7 8
20 75 2 0 10
21 48 2 0 10
21 50 2 0 10
22 23 5 1 3 6 7 10
22 39 5 0 5 8 9 10
22 45 4 3 4 9 10
23 10 4 1 2 6 9
23 22 3 7 8 9
23 60 7 0 2 3 4 5 7 10
24 25 1 9
24 40 2 1 6
25 24 3 0 3 10
25 37 5 2 5 7 8 9
25 81 5 3 4 5 7 8
26 8 7 0 1 2 3 6 7 10
26 9 6 0 5 7 8 9 10
26 17 9 1 2 3 4 5 6 7 8 9
27 28 2 7 9
27 67 4 1 3 4 6
28 27 2 0 10
28 29 1 2
28 73 2 5 8
29 28 1 9
29 30 1 4
30 5 5 4 5 6 8 9
30 29 6 0 1 3 8 9 10
30 63 8 0 1 2 3 5 7 8 10
31 8 4 0 4 8 10
31 32 5 0 2 6 7 10
31 37 3 0 2 10
32 31 8 1 2 3 4 5 6 7 9
32 34 8 0 3 4 5 7 8 9 10
32 62 6 0 1 2 5 8 10
34 32 4 0 2 7 10
34 35 5 3 4 5 6 9
35 34 3 0 9 10
35 36 4 1 4 8 9
35 65 4 3 4 7 8
36 18 4 0 1 2 10
36 35 8 0 3 4 5 6 7 8 10
36 70 4 3 5 6 9
37 25 6 0 3 5 7 9 10
37 31 4 1 2 6 8
38 39 4 1 5 6 10
38 43 7 0 1 2 3 6 9 10
38 47 7 2 3 4 5 8 9 10
39 22 8 0 1 4 6 7 8 9 10
39 38 6 0 2 3 4 8 9
39 76 4 0 3 5 10
42 10 9 1 2 3 4 5 6 7 8 9
42 42 4 0 3 8 10
43 38 3 6 7 8
44 46 8 0 1 2 3 5 6 8 9
44 48 6 1 2 3 5 7 10
44 56 7 0 3 5 6 7 8 10
45 22 5 0 1 2 8 9
45 46 6 0 1 2 5 7 10
45 53 5 1 5 6 8 10
46 44 4 0 3 4 6
46 45 4 2 4 7 9
46 53 8 0 2 3 4 7 8 9 10
47 38 3 3 4 7
48 21 5 2 4 7 8 10
48 44 6 0 1 3 5 6 10
48 51 6 4 5 6 8 9 10
49 51 1 7
50 11 7 3 4 5 6 7 9 10
50 21 3 2 3 8
50 55 3 1 9 10
51 14 6 0 5 7 8 9 10
51 48 5 1 3 4 6 9
51 49 9 0 2 3 4 5 6 8 9 10
51 52 5 1 2 4 5 6
52 51 2 0 10
53 45 4 1 3 4 8
53 46 2 2 9
53 54 3 4 5 7
55 12 4 1 2 6 9
55 13 4 1 4 7 8
55 50 2 0 5
56 14 4 1 4 8 9
56 44 2 6 9
56 80 2 2 6
57 16 1 7
57 59 7 0 4 5 6 8 9 10
57 68 4 1 2 6 9
59 57 4 0 5 7 10
59 58 7 1 3 4 6 7 8 9
59 60 3 1 5 6
60 9 3 3 5 8
60 23 3 2 3 4
60 59 3 0 5 10
61 9 1 6
61 17 2 0 10
61 69 3 4 5 8
62 32 4 2 4 7 9
63 6 6 2 3 4 6 8 9
63 30 6 0 4 5 8 9 10
63 71 6 1 2 3 4 6 7
64 65 2 0 10
65 35 3 2 3 5
65 64 6 1 2 4 6 7 9
65 66 5 1 5 6 8 9
66 65 2 0 10
71 63 2 4 6
71 72 1 3
71 73 2 2 8
72 71 3 0 4 10
72 73 7 1 2 5 6 7 8 9
72 74 3 1 3 5
74 72 2 0 10
75 2 6 2 3 4 6 8 9
76 39 3 2 6 7
76 77 1 8
7 3 2 0 10
8 15 7 0 1 4 6 7 9 10
8 26 3 1 6 8
8 31 6 0 3 4 5 9 10
9 26 7 0 2 3 5 6 7 10
9 60 9 0 1 2 4 5 7 8 9 10
9 61 9 0 2 4 5 6 7 8 9 10
10 23 6 0 1 2 8 9 10
10 33 6 0 3 6 7 9 10
10 42 7 1 2 3 4 5 6 7
11 12 1 4
11 50 1 10
12 11 5 0 1 3 5 7
12 13 4 0 2 3 10
12 55 4 1 2 4 9
13 12 2 4 6
13 14 5 1 2 5 7 8
13 55 2 2 8
14 13 6 0 1 3 4 8 10
14 51 3 3 5 9
14 56 6 0 4 6 7 9 10
15 8 5 0 2 3 6 10
15 16 5 1 2 7 8 9
15 82 9 0 1 2 3 4 5 6 7 10
16 15 2 0 10
16 17 1 9
16 57 2 5 7
17 16 4 3 4 7 8
17 26 4 4 6 8 9
17 61 1 1
18 19 8 1 2 3 4 6 7 8 9
18 36 7 0 1 3 6 7 9 10
19 18 8 0 1 2 3 5 6 9 10
19 75 7 0 3 4 5 7 9 10
20 19 7 0 2 4 5 6 7 10
20 75 3 0 8 10
22 23 5 0 4 8 9 10
22 39 4 2 3 5 10
22 45 6 0 2 4 6 7 10
23 10 2 1 3
23 22 6 0 1 2 3 4 8
23 60 4 2 5 7 8
25 24 3 1 3 9
25 37 2 0 10
25 81 3 2 4 5
26 8 3 0 7 10
26 9 8 0 1 2 3 4 5 9 10
26 17 6 0 5 6 7 8 10
28 27 7 1 2 4 5 7 8 9
28 29 7 0 3 5 7 8 9 10
28 73 5 0 2 3 4 10
29 28 5 1 3 4 7 9
29 30 3 5 6 8
30 5 4 0 1 2 10
30 29 6 0 4 5 6 8 10
30 63 7 1 2 3 6 7 8 9
31 8 3 1 3 8
31 32 6 1 3 4 5 7 9
31 37 7 0 2 3 5 7 9 10
32 31 4 3 5 8 9
32 34 4 0 5 6 10
32 62 4 1 2 4 7
33 10 10 0 1 2 3 4 6 7 8 9 10
33 41 9 0 2 3 4 5 7 8 9 10
34 32 7 0 1 3 6 7 8 10
34 35 5 0 1 2 4 10
35 34 5 1 3 6 7 9
35 36 8 1 2 3 4 5 6 7 8
35 65 5 0 2 5 9 10
36 18 9 0 1 2 3 5 6 8 9 10
36 35 8 0 2 4 5 6 8 9 10
36 70 6 1 2 4 5 7 8
37 25 3 6 7 8
37 31 4 0 2 6 10
38 39 6 0 2 4 5 6 9
38 43 7 1 2 3 4 6 8 10
38 47 7 0 1 3 4 7 8 10
39 22 6 2 4 5 8 9 10
39 38 5 0 5 6 9 10
39 76 7 0 2 3 6 8 9 10
41 33 6 0 3 5 6 9 10
42 10 11 0 1 2 3 4 5 6 7 8 9 10
42 42 3 0 4 10
43 38 5 0 1 5 7 9
44 46 5 1 2 3 4 9
44 48 6 0 2 3 6 9 10
44 56 9 0 1 3 5 6 7 8 9 10
45 22 2 4 10
45 46 2 4 6
45 53 10 0 1 2 3 4 5 7 8 9 10
46 44 5 0 3 6 7 10
46 45 3 2 4 10
46 53 6 4 5 6 8 9 10
47 38 3 1 5 7
48 21 4 2 3 5 9
48 44 4 0 3 4 10
48 51 5 0 6 7 8 10
49 51 2 0 10
50 11 2 6 9
50 21 1 7
50 55 2 8 10
51 14 9 0 1 2 3 4 6 7 8 10
51 48 2 1 2
51 49 5 2 5 7 8 9
51 52 6 1 4 5 6 8 9
53 45 4 3 4 5 8
53 54 3 6 7 9
55 12 2 0 10
55 13 2 7 8
55 50 4 3 6 8 9
56 14 4 1 3 5 9
56 44 5 2 4 5 6 8
56 80 6 0 1 3 4 7 10
57 16 3 2 7 8
57 59 7 1 2 3 4 5 7 9
57 68 5 0 6 8 9 10
60 9 3 0 9 10
60 23 4 3 4 6 8
60 59 4 1 4 6 8
61 9 8 0 2 3 5 6 7 8 10
61 17 5 0 1 4 8 10
61 69 3 1 6 7
63 6 4 3 5 6 8
63 30 5 0 1 4 7 10
63 71 4 2 3 5 9
65 35 2 2 5
65 66 2 4 9
68 57 5 0 4 5 6 10
70 36 2 0 10
73 28 4 0 6 9 10
73 71 2 2 8
73 72 3 1 5 6
75 2 7 1 3 4 6 7 8 9
76 39 3 1 5 7
76 77 1 3
80 56 2 2 4
80 82 2 5 7
82 15 1 4
82 81 2 5 6

0 comments on commit f9f2dcf

Please sign in to comment.