-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNetGame2.py
226 lines (219 loc) · 9.36 KB
/
NetGame2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import networkx as nx
import numpy as np
import collections
import matplotlib.pyplot as plt
import random as rd
import statistics
def average_degree(G):
return np.mean(G.degree)
def average_degreesquare(G):
degree = G.degree
degree = np.array(degree)
egreesquare = (degree[:,1])**2
return np.mean(egreesquare)
def Kappa(G):
return average_degreesquare(G)/average_degree(G)
class Attacker:
remove_max = 10
Budget_attack = 0
Budget_oneturn = 0
Budget_exhaust = False
Budget_exhaust_thisturn = False
def __init__(self, m, b, o):
self.remove_max = m
self.Budget_attack = b
self.Budget_oneturn = o
def Set_remove_max(self,m):
self.remove_max = m
def Set_Budget_oneturn(self,o):
self.Budget_fraction = o
def Set_Budget_attack(self,b):
self.Budget_attack = b
def Set_flag(self, Bool):
self.Budget_exhaust = Bool
class Defender:
add_max = 10
Budget_defend = 0
Budget_oneturn = 0
Budget_exhaust = False
Budget_exhaust_thisturn = False
def __init__(self, m, b, o):
self.add_max = m
self.Budget_defend = b
self.Budget_oneturn = o
def Set_add_max(self,m):
self.add_max = m
def Set_Budget_oneturn(self,o):
self.Budget_fraction = o
def Set_Budget_defend(self,b):
self.Budget_defend = b
def Set_flag(self, Bool):
self.Budget_exhaust = Bool
class Game:
coff_1 = 100
coff_2 = 500
Kappa = 0 # molloy-reed criterion
Graph = [] # this network
Fraction = 0.7
Game_log = {}
attack = []
defender = []
def __init__(self, n,m,seed):
self.Graph = nx.barabasi_albert_graph(n,m,seed=seed)
self.Kappa = Kappa(self.Graph)
self.add_max = m
def Set_coff_1(self, c1):
self.coff_1 = c1
def Set_coff_2(self, c2):
self.coff_2 = c2
def Set_fraction(self,f):
self.Fraction = f
def Initialization(self):
print("initializing...")
Graph2 = self.Graph.copy()
BN = nx.edge_betweenness_centrality(Graph2)
BN_sorted = sorted(BN.items(), key=lambda item: item[1], reverse=True)
cost = 0
Kappa_0 = Kappa(Graph2)
while Kappa(Graph2) / Kappa_0 > self.Fraction:
remove_edge = BN_sorted[0][0]
cost = cost + BN_sorted[0][1]
BN_sorted.remove(BN_sorted[0])
a = remove_edge[0]
b = remove_edge[1]
Graph2.remove_edge(a,b)
Budget_attack = self.coff_1*cost*4
Budget_defend = Budget_attack
print("Attacker's budget is: ", Budget_attack)
print("Defender's budget is: ", Budget_defend)
self.attacker = Attacker(30,Budget_attack,0.3*Budget_attack)
self.defender = Defender(2,Budget_defend,0.05*Budget_defend)
def Attacker_win(self):
if Kappa(self.Graph)/self.Kappa < self.Fraction:
print("Kappa: ", Kappa(self.Graph) / self.Kappa)
return True
else:
print("Kappa: ", Kappa(self.Graph) / self.Kappa)
return False
def Defender_win(self, exhaust):
if Kappa(self.Graph)/self.Kappa >= self.Fraction and self.attacker.Budget_exhaust == True:
print("Kappa: ", Kappa(self.Graph) / self.Kappa)
return True
else:
return False
def Play_game(self):
print("Let's play a game.")
attack_turn = True
turn = 1
while not self.Attacker_win() or self.Defender_win():
if attack_turn == True:
print("Turn: ", turn)
self.attacker.Budget_exhaust_thisturn = False
Budget_oneturn = self.attacker.Budget_oneturn
print("This is attacker turn.")
print("Your budget", self.attacker.Budget_attack)
print("You can remove max edges: ", self.attacker.remove_max)
print("You can use max budget in one turn: ",Budget_oneturn)
print("Game log is as follow:")
print(self.Game_log)
BN = nx.edge_betweenness_centrality(self.Graph)
BN_sorted = sorted(BN.items(), key=lambda item: item[1], reverse=True)
remove_all = []
for i in range(self.attacker.remove_max):
remove = BN_sorted[0]
remove_edge = remove[0]
remove_cost = self.coff_1*remove[1]
while self.attacker.Budget_attack < remove_cost or Budget_oneturn < remove_cost:
if len(BN_sorted) == 1:
if self.attacker.Budget_attack < remove_cost:
print("Attacker's budget is exhausted. You can't afford a edge to remove.")
self.attacker.Budget_exhaust = True
else:
print("Attacker's budget is exhausted in this turn. You can't afford a edge to remove in this turn.")
self.attacker.Budget_exhaust_thisturn = True
break
BN_sorted.remove(remove)
remove = BN_sorted[0]
remove_edge = remove[0]
remove_cost = self.coff_1 * remove[1]
if self.attacker.Budget_exhaust == True or self.attacker.Budget_exhaust_thisturn == True:
break
v1 = remove_edge[0]
v2 = remove_edge[1]
remove_all.append(remove_edge)
self.Graph.remove_edge(v1,v2)
Budget_oneturn = Budget_oneturn - remove_cost
self.attacker.Budget_attack = self.attacker.Budget_attack - remove_cost
BN = nx.edge_betweenness_centrality(self.Graph)
BN_sorted = sorted(BN.items(), key=lambda item: item[1], reverse=True)
if self.Attacker_win():
break
if self.attacker.Budget_exhaust == True:
break
print("The attacker action in this turn: ", remove_all)
print("Kappa is: ", Kappa(self.Graph)/self.Kappa)
self.Game_log.update({-turn: remove_all})
attack_turn = False
turn = turn + 1
else:
print("Turn: ", turn)
self.defender.Budget_exhaust_thisturn = False
Budget_oneturn = self.defender.Budget_oneturn
print("This is defender turn.")
print("Your budget", self.defender.Budget_defend)
print("You can add max edges: ", self.defender.add_max)
print("You can use max budget in one turn: ", Budget_oneturn)
print("Game log is as follow:")
print(self.Game_log)
BN = nx.betweenness_centrality(self.Graph)
Nodes = self.Graph.nodes
add_all = []
if self.defender.Budget_exhaust == True:
print("Nothing you can do. Next turn")
self.Game_log.update({turn: add_all})
attack_turn = True
turn = turn + 1
continue
count = 0
pick_again1 = 0
pick_again2 = 0
while count < self.defender.add_max:
choice = rd.sample(Nodes,2)
v1 = choice[0]
v2 = choice[1]
if pick_again1 >= 10:
print("The budget of defender in this turn is exhausted. Next turn")
self.defender.Budget_exhaust_thisturn = True
break
if pick_again2 >= 10:
print("The budget of defender is exhausted. Next turn")
self.defender.Budget_exhaust_thisturn = True
break
if self.Graph.has_edge(v1, v2) == False:
add_cost = self.coff_2 * (BN[v1] + BN[v2]) * 0.5
if remove_cost > Budget_oneturn:
pick_again1 = pick_again1 + 1
continue
if remove_cost > self.defender.Budget_defend:
pick_again2 = pick_again2 + 1
continue
else:
self.Graph.add_edge(v1, v2)
Budget_oneturn = Budget_oneturn - add_cost
self.defender.Budget_defend = self.defender.Budget_defend - add_cost
add_all.append((v1, v2))
count = count + 1
else:
continue
print("The defender action in this turn: ", add_all)
print("Kappa is: ", Kappa(self.Graph) / self.Kappa)
self.Game_log.update({turn:add_all})
attack_turn = True
turn = turn + 1
if self.Attacker_win():
print("Attacker wins.")
else:
print("Defender wins.")
G = Game(50,18,6324)
G.Initialization()
G.Play_game()