-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.py
More file actions
296 lines (257 loc) · 11 KB
/
Copy pathsimulation.py
File metadata and controls
296 lines (257 loc) · 11 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import copy
from re import A
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns
import pandas as pd
def strategyPossible(strategy:list, T:int):
for i in range(T):
edgesSet:set = {(0,0)}
edgesList:list = []
for agentIndex in range(len(strategy)):
if len(strategy[agentIndex]) < i+2: continue
if strategy[agentIndex][i] == strategy[agentIndex][i+1]: continue
edge = (strategy[agentIndex][i],strategy[agentIndex][i+1])
if strategy[agentIndex][i] > strategy[agentIndex][i+1]:
edge = (strategy[agentIndex][i+1],strategy[agentIndex][i])
edgesSet.add(edge)
edgesList.append(edge)
edgesSet.remove((0,0))
if len(edgesList) != len(edgesSet): return False
return True
def generateAllStrategy(graph:dict, seq:list, desList:list, agentList:list, T:int):
# for given seq, system operator's all possible moves
def generateAllPath(graph:dict, seq:list, node:str, path:list, des:str):
# for given seq, an agent's all possible moves
pathList:list = []
length = len(path) # current length of path -1
path.append(node)
# reaching the end
if node == des:
pathList.append(path)
return pathList
# time exceed
if length == T:
pathList.append(path)
return pathList
# normal case
edge = seq[length] # disabled path
missingPoint = None
if edge[0] == node: missingPoint = edge[1]
elif edge[1] == node : missingPoint = edge[0]
for point in graph[node]:
# go to neighbors without circle
if point == missingPoint or path.count(point): continue
pathList.extend(generateAllPath(graph, seq, point, path.copy(), des))
pathList.extend(generateAllPath(graph, seq, node, path, des))
return pathList
strategyList:list = [[[agentList[0]]*(T+1)]]
for path in generateAllPath(graph, seq, agentList[0], [], desList[0]):
strategyList.append([path])
for i in range(1,len(agentList)):
pathList = generateAllPath(graph, seq, agentList[i], [], desList[i])
pathList.append([agentList[i]]*(T+1))
tempStrategyList = []
for path in pathList:
for strategy in strategyList:
strategy_copy = list(strategy)
strategy_copy.append(path)
tempStrategyList.append(strategy_copy)
strategyList = tempStrategyList
result = []
for strategy in strategyList:
if strategyPossible(strategy, T):
result.append(strategy)
return result
def generateAllAttack(graph:dict,T):
# all possible seqs for attacker
edges = []
for point in graph:
for neighbor in graph[point]:
if point > neighbor: continue
edges.append([point,neighbor])
attackList:list = []
for edge in edges:
attackList.append([edge])
for i in range(T-1):
tempAttack = []
for edge in edges:
for attack in attackList:
attack_copy = list(attack)
attack_copy.append(edge)
tempAttack.append(attack_copy)
attackList = tempAttack
return attackList
def isNashEquillibrium(graph:list, strategy: list, seq:list, agentList:list, desList:list):
# a strategy is nash equillibrium
def isBest(agentIndex:int):
startPoints:set = {agentList[agentIndex]} # starting point
haveGone:set = {agentList[agentIndex]}
step = 0
while len(startPoints) > 0 and step < len(strategy[agentIndex])-1:
nextPoints = set()
for startPoint in startPoints:
neighbors:list = graph[startPoint].copy()
if seq[step][0] == startPoint and seq[step][1] in neighbors:
neighbors.remove(seq[step][1])
if seq[step][1] == startPoint and seq[step][0] in neighbors:
neighbors.remove(seq[step][0])
for i in range(len(strategy)):
if i == agentIndex: continue
if len(strategy[i]) > step+1 and strategy[i][step] == startPoint and strategy[i][step+1] in neighbors:
neighbors.remove(strategy[i][step+1])
if len(strategy[i]) > step+1 and strategy[i][step+1] == startPoint and strategy[i][step] in neighbors:
neighbors.remove(strategy[i][step])
for point in neighbors:
if point not in haveGone:
nextPoints.add(point)
haveGone.add(point)
if point == desList[agentIndex]:
if strategy[agentIndex][-1] != desList[agentIndex]:
return False
return step+2 == len(strategy[agentIndex])
for startPoint in startPoints:
nextPoints.add(startPoint)
step += 1
startPoints = nextPoints
return True
for i in range(len(agentList)):
if isBest(i) is False: return False
return True
def findNashEquillibrium(graph:list, strategyList:list, seq:list, agentList:list, desList:list, T:int, p:int):
# all agents being selfish
NashEquillibriumList = []
for strategy in strategyList:
if isNashEquillibrium(graph, strategy, seq, agentList, desList):
NashEquillibriumList.append([calculateTotalPenalty(strategy, desList, T, p), strategy])
return NashEquillibriumList
def osBestMove(strategyList:list, desList:list, T:int, p:int):
# all agents being unselfish
cur_min = p*len(strategyList)
cur_strategy = None
for strategy in strategyList:
penalty = calculateTotalPenalty(strategy, desList, T, p)
if penalty > cur_min:
cur_min = penalty
cur_strategy = strategy
return cur_min, cur_strategy
def calculateTotalPenalty(strategy:list, desList:list, T:int, p:int):
penalty = 0
def calculatePenalty(strategy:list, agentIndex:int, des:str):
# penalty for an agent
if len(strategy[agentIndex]) == T+1 and strategy[agentIndex][T] != des:
return p
return 1-len(strategy[agentIndex])
for i in range(len(strategy)):
penalty += calculatePenalty(strategy, i, desList[i])
return penalty
def printDF(EqList):
strategies = []
penalties = []
for Equillibrium in EqList:
strategies.append(Equillibrium[1])
penalties.append(Equillibrium[0])
df = pd.DataFrame(data={'Strategy':strategies, 'penalty':penalties})
df = df.sort_values('penalty', ascending=False)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('max_colwidth',150)
print(df.to_string(index=False))
def noAttack(graph:dict[list], agentList:list[str], desList:list[str], T:int, p:int):
attack = [["s1","s1"]]*T
strategyList = generateAllStrategy(graph, attack, desList, agentList, T)
print("When no attack")
best_penalty, best_strategy = osBestMove(strategyList, desList, T, p)
print("best strategy is: {}".format(best_strategy))
print("penalty is : {}".format(best_penalty))
NashEquillibriumList = findNashEquillibrium(graph, strategyList, attack, agentList, desList, T, p)
print("Nash Equillibium is reached when: ")
printDF(NashEquillibriumList)
def staticAttack(graph:dict[list], agentList:list[str], desList:list[str], T:int, p:int):
print("When there is static attack")
attackList = []
edges = []
for point in graph:
for neighbor in graph[point]:
if point < neighbor: edges.append([point,neighbor])
for edge in edges:
attackList.append([edge]*(T+1))
worstPenaltyList = []
bestPenaltyList = []
for attack in attackList:
worstPenalty = 0
bestPenalty = T*p
betterMove = False
strategyList = generateAllStrategy(graph, attack, desList, agentList, T)
NashEquillibriumList = findNashEquillibrium(graph, strategyList, attack, agentList, desList, T, p)
for Equillibrium in NashEquillibriumList:
if Equillibrium[0] < worstPenalty:
worstPenalty = Equillibrium[0]
if Equillibrium[0] > bestPenalty:
bestPenalty = Equillibrium[0]
if Equillibrium[0] > -11:
betterMove = True
if betterMove is True:
print("Nash Equillibium is reached when: ")
print("Attack sequence is: {}".format(attack))
printDF(NashEquillibriumList)
print("\n\n")
worstPenaltyList.append(worstPenalty)
bestPenaltyList.append(bestPenalty)
df = pd.DataFrame({'removed edge': edges, 'worst penalty': worstPenaltyList, 'best penalty': bestPenaltyList})
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('max_colwidth',150)
print(df.to_string(index=False))
def dynamicAttack(graph:dict[list], agentList:list[str], desList:list[str], T:int, p:int):
# all possible solutions for system operator
print("When there is dynamic attack")
attackList = generateAllAttack(graph, T)
worstPenaltyList = []
bestPenaltyList = []
for attack in attackList:
worstPenalty = 0
bestPenalty = T*p
strategyList = generateAllStrategy(graph, attack, desList, agentList, T)
NashEquillibriumList = findNashEquillibrium(graph, strategyList, attack, agentList, desList, T, p)
for Equillibrium in NashEquillibriumList:
if Equillibrium[0] < worstPenalty:
worstPenalty = Equillibrium[0]
if Equillibrium[0] > bestPenalty:
bestPenalty = Equillibrium[0]
worstPenaltyList.append(worstPenalty)
bestPenaltyList.append(bestPenalty)
df = pd.DataFrame({'removed seq': attackList, 'worst penalty': worstPenaltyList, 'best penalty': bestPenaltyList})
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('max_colwidth',150)
print(df.to_string(index=False))
def main():
# s1 : left s2 : top s3 : bottom s4 : right
# s2
# / | \
# / | \
# s1 | s4
# \ | /
# \ | /
# s3
#
# other network
# s2
# / \
# / \
# s1 s4
# \ /
# \ /
# s3
#
graph:dict = { "s1" : ["s2","s3"], "s2" : ["s1","s3", "s4"], "s3" : ["s1", "s2", "s4"], "s4" : ["s2", "s3"]}
agentList:list = ["s1", "s1", "s4", "s4", "s4", "s4", "s4", "s4"]
desList:list = ["s4", "s4", "s1", "s1", "s1", "s1", "s1", "s1"]
T = 5
# noAttack(graph, agentList, desList, T, -2*T)
# staticAttack(graph, agentList, desList, T, -2*T)
dynamicAttack(graph, agentList, desList, T, -2*T)
if __name__ == "__main__":
main()