-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResponsibility.py
381 lines (290 loc) · 18.7 KB
/
Responsibility.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import numpy as np;
np.random.seed(0)
import copy
from tqdm import tqdm
import GWorld
import Agent
from functools import lru_cache
VerboseFlag = False
EPS = 0.000001
def CountValidMovesOfAffected(WorldIn, ActionID4Agents, AffectedID):
return CountValidMovesOfAffected_tuple(WorldIn, tuple(ActionID4Agents), AffectedID)
@lru_cache(maxsize=None)
def CountValidMovesOfAffected_tuple(WorldIn, ActionID4Agents, AffectedID):
# Counts ValidMoves for Affected Agent for the Actions Chosen by Others
ActionID4Agents = list(ActionID4Agents)
FuncWorld_outer = copy.deepcopy(WorldIn)
ActionID4Agents_outer = copy.deepcopy(ActionID4Agents)
Affected = FuncWorld_outer.AgentList[AffectedID]
ValidMovesCount = 0
validity_of_moves_of_affected = np.zeros(len(Affected.Actions))
for AffectedActionID in np.arange(len(Affected.Actions)):
agentIDs4swaps = [AffectedID]
actionIDs4swaps = [AffectedActionID]
# SwapActionID for Affected Agent
ActionID4Agents_inner = GWorld.SwapActionIDs4Agents(ActionID4Agents=ActionID4Agents_outer,
agentIDs4swaps=agentIDs4swaps,
actionIDs4swaps=actionIDs4swaps)
FuncWorld = copy.deepcopy(FuncWorld_outer)
AgentCrashes, RestrictedMoves = FuncWorld.UpdateGWorld(defaultAction='stay',
ActionID4Agents=ActionID4Agents_inner)
if (AgentCrashes[AffectedID] is False) and (RestrictedMoves[AffectedID] is False):
ValidMovesCount += 1
validity_of_moves_of_affected[AffectedActionID] = 1
# validity_of_moves_of_affected = 0 for crashes
del FuncWorld
del FuncWorld_outer
return ValidMovesCount, validity_of_moves_of_affected
def FeAR(WorldIn, ActionID4Agents, MovesDeRigueur4Agents=[]):
# Feasible Action-Space Reduction Metric
FuncWorld = copy.deepcopy(WorldIn)
# Storing the Actions received for each agent
ActionInputs = np.ones(len(FuncWorld.AgentList)).astype(int) * 0 # Default is Stay
for AgentID, ActionID in ActionID4Agents:
ActionInputs[AgentID] = ActionID
# Storing the Move de Rigueurs received for each agent
MovesDeRigueur = np.ones(len(FuncWorld.AgentList)).astype(int) * 0 # Default Move de Riguer is Stay
for AgentID, ActionID in MovesDeRigueur4Agents:
MovesDeRigueur[AgentID] = ActionID
Resp = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList)))
ValidMoves_moveDeRigueur = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList)))
ValidMoves_action = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList)))
list_of_actions_for_agents = []
for agentID in FuncWorld.AgentList:
list_of_actions_for_agents.append(len(agentID.Actions))
max_n_actions = max(list_of_actions_for_agents)
if VerboseFlag: print('max_n_actions : ', max_n_actions)
Validity_of_Moves_moveDeRigueur = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList), max_n_actions))
Validity_of_Moves_action = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList), max_n_actions))
for ii in tqdm(range(len(FuncWorld.AgentList)), colour="red", ncols=100): # Actors
for jj in np.arange(len(FuncWorld.AgentList)): # Affected
if not (ii == jj):
agentIDs4swaps = [ii]
# Actor - Move de Rigueur
actionIDs4swaps = [MovesDeRigueur[ii]]
ActionID4Agents_ActorMoveDeRigueur = GWorld.SwapActionIDs4Agents(ActionID4Agents=ActionID4Agents,
agentIDs4swaps=agentIDs4swaps,
actionIDs4swaps=actionIDs4swaps)
# Actor Moves
actionIDs4swaps = [ActionInputs[ii]]
ActionID4Agents_ActorMoves = GWorld.SwapActionIDs4Agents(ActionID4Agents=ActionID4Agents,
agentIDs4swaps=agentIDs4swaps,
actionIDs4swaps=actionIDs4swaps)
if VerboseFlag:
print('Actor {:02d} Moves'.format(ii + 1))
print('ActionIDs_ActorStays :', ActionID4Agents_ActorMoveDeRigueur)
print('ActionIDs_ActorMoves :', ActionID4Agents_ActorMoves)
ValidMoves_moveDeRigueur[ii][jj], Validity_of_Moves_moveDeRigueur[ii][jj] = \
CountValidMovesOfAffected(WorldIn=FuncWorld,
ActionID4Agents=ActionID4Agents_ActorMoveDeRigueur,
AffectedID=jj)
ValidMoves_action[ii][jj], Validity_of_Moves_action[ii][jj] = \
CountValidMovesOfAffected(WorldIn=FuncWorld,
ActionID4Agents=ActionID4Agents_ActorMoves,
AffectedID=jj)
Resp[ii][jj] = (ValidMoves_moveDeRigueur[ii][jj] - ValidMoves_action[ii][jj]) / \
(ValidMoves_moveDeRigueur[ii][jj] + EPS)
# 0.1 is added to the denominator to resolve cases when ValidMoves_stay is 0
Resp[ii][jj] = np.clip(Resp[ii][jj], -1, 1)
# Clipping Resp to the range [-1,1]
ValidMoves_moveDeRigueur = ValidMoves_moveDeRigueur.astype(int)
ValidMoves_action = ValidMoves_action.astype(int)
Validity_of_Moves_moveDeRigueur = Validity_of_Moves_moveDeRigueur.astype(int)
Validity_of_Moves_action = Validity_of_Moves_action.astype(int)
if VerboseFlag:
print('Validity_of_Moves_moveDeRigueur : ', Validity_of_Moves_moveDeRigueur)
print('Validity_of_Moves_action : ', Validity_of_Moves_action)
return Resp, ValidMoves_moveDeRigueur, ValidMoves_action, Validity_of_Moves_moveDeRigueur, Validity_of_Moves_action
def FeAR_4_one_actor(WorldIn, ActionID4Agents, MovesDeRigueur4Agents=[], actor_ii=0):
# Feasible Action-Space Reduction Metric
FuncWorld = copy.deepcopy(WorldIn)
# Storing the Actions received for each agent
ActionInputs = np.ones(len(FuncWorld.AgentList)).astype(int) * 0 # Default is Stay
for AgentID, ActionID in ActionID4Agents:
ActionInputs[AgentID] = ActionID
# Storing the Move de Rigueurs received for each agent
MovesDeRigueur = np.ones(len(FuncWorld.AgentList)).astype(int) * 0 # Default Move de Riguer is Stay
for AgentID, ActionID in MovesDeRigueur4Agents:
MovesDeRigueur[AgentID] = ActionID
Resp = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList)))
ValidMoves_moveDeRigueur = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList)))
ValidMoves_action = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList)))
list_of_actions_for_agents = []
for agentID in FuncWorld.AgentList:
list_of_actions_for_agents.append(len(agentID.Actions))
max_n_actions = max(list_of_actions_for_agents)
if VerboseFlag: print('max_n_actions : ', max_n_actions)
Validity_of_Moves_moveDeRigueur = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList), max_n_actions))
Validity_of_Moves_action = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList), max_n_actions))
ii = actor_ii
for jj in tqdm(range(len(FuncWorld.AgentList)), colour="red", ncols=100): # Affected
if not (ii == jj):
agentIDs4swaps = [ii]
# Actor - Move de Rigueur
actionIDs4swaps = [MovesDeRigueur[ii]]
ActionID4Agents_ActorMoveDeRigueur = GWorld.SwapActionIDs4Agents(ActionID4Agents=ActionID4Agents,
agentIDs4swaps=agentIDs4swaps,
actionIDs4swaps=actionIDs4swaps)
# Actor Moves
actionIDs4swaps = [ActionInputs[ii]]
ActionID4Agents_ActorMoves = GWorld.SwapActionIDs4Agents(ActionID4Agents=ActionID4Agents,
agentIDs4swaps=agentIDs4swaps,
actionIDs4swaps=actionIDs4swaps)
if VerboseFlag:
print('Actor {:02d} Moves'.format(ii + 1))
print('ActionIDs_ActorStays :', ActionID4Agents_ActorMoveDeRigueur)
print('ActionIDs_ActorMoves :', ActionID4Agents_ActorMoves)
ValidMoves_moveDeRigueur[ii][jj], Validity_of_Moves_moveDeRigueur[ii][jj] = \
CountValidMovesOfAffected(WorldIn=FuncWorld,
ActionID4Agents=ActionID4Agents_ActorMoveDeRigueur,
AffectedID=jj)
ValidMoves_action[ii][jj], Validity_of_Moves_action[ii][jj] = \
CountValidMovesOfAffected(WorldIn=FuncWorld,
ActionID4Agents=ActionID4Agents_ActorMoves,
AffectedID=jj)
Resp[ii][jj] = (ValidMoves_moveDeRigueur[ii][jj] - ValidMoves_action[ii][jj]) / \
(ValidMoves_moveDeRigueur[ii][jj] + EPS)
# 0.1 is added to the denominator to resolve cases when ValidMoves_stay is 0
Resp[ii][jj] = np.clip(Resp[ii][jj], -1, 1)
# Clipping Resp to the range [-1,1]
ValidMoves_moveDeRigueur = ValidMoves_moveDeRigueur.astype(int)
ValidMoves_action = ValidMoves_action.astype(int)
Validity_of_Moves_moveDeRigueur = Validity_of_Moves_moveDeRigueur.astype(int)
Validity_of_Moves_action = Validity_of_Moves_action.astype(int)
if VerboseFlag:
print('Validity_of_Moves_moveDeRigueur : ', Validity_of_Moves_moveDeRigueur)
print('Validity_of_Moves_action : ', Validity_of_Moves_action)
return Resp, ValidMoves_moveDeRigueur, ValidMoves_action, Validity_of_Moves_moveDeRigueur, Validity_of_Moves_action
def FeAR_4_affected_agents(WorldIn, ActionID4Agents, MovesDeRigueur4Agents=[], affected_jj=[0]):
# Feasible Action-Space Reduction Metric
FuncWorld = copy.deepcopy(WorldIn)
# Storing the Actions received for each agent
ActionInputs = np.ones(len(FuncWorld.AgentList)).astype(int) * 0 # Default is Stay
for AgentID, ActionID in ActionID4Agents:
ActionInputs[AgentID] = ActionID
# Storing the Move de Rigueurs received for each agent
MovesDeRigueur = np.ones(len(FuncWorld.AgentList)).astype(int) * 0 # Default Move de Riguer is Stay
for AgentID, ActionID in MovesDeRigueur4Agents:
MovesDeRigueur[AgentID] = ActionID
Resp = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList)))
ValidMoves_moveDeRigueur = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList)))
ValidMoves_action = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList)))
list_of_actions_for_agents = []
for agentID in FuncWorld.AgentList:
list_of_actions_for_agents.append(len(agentID.Actions))
max_n_actions = max(list_of_actions_for_agents)
if VerboseFlag: print('max_n_actions : ', max_n_actions)
Validity_of_Moves_moveDeRigueur = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList), max_n_actions))
Validity_of_Moves_action = np.zeros((len(FuncWorld.AgentList), len(FuncWorld.AgentList), max_n_actions))
for jj in affected_jj:
for ii in tqdm(range(len(FuncWorld.AgentList)), colour="red", ncols=100): # Actor
if not (ii == jj):
agentIDs4swaps = [ii]
# Actor - Move de Rigueur
actionIDs4swaps = [MovesDeRigueur[ii]]
ActionID4Agents_ActorMoveDeRigueur = GWorld.SwapActionIDs4Agents(ActionID4Agents=ActionID4Agents,
agentIDs4swaps=agentIDs4swaps,
actionIDs4swaps=actionIDs4swaps)
# Actor Moves
actionIDs4swaps = [ActionInputs[ii]]
ActionID4Agents_ActorMoves = GWorld.SwapActionIDs4Agents(ActionID4Agents=ActionID4Agents,
agentIDs4swaps=agentIDs4swaps,
actionIDs4swaps=actionIDs4swaps)
if VerboseFlag:
print('Actor {:02d} Moves'.format(ii + 1))
print('ActionIDs_ActorStays :', ActionID4Agents_ActorMoveDeRigueur)
print('ActionIDs_ActorMoves :', ActionID4Agents_ActorMoves)
ValidMoves_moveDeRigueur[ii][jj], Validity_of_Moves_moveDeRigueur[ii][jj] = \
CountValidMovesOfAffected(WorldIn=FuncWorld,
ActionID4Agents=ActionID4Agents_ActorMoveDeRigueur,
AffectedID=jj)
ValidMoves_action[ii][jj], Validity_of_Moves_action[ii][jj] = \
CountValidMovesOfAffected(WorldIn=FuncWorld,
ActionID4Agents=ActionID4Agents_ActorMoves,
AffectedID=jj)
Resp[ii][jj] = (ValidMoves_moveDeRigueur[ii][jj] - ValidMoves_action[ii][jj]) / \
(ValidMoves_moveDeRigueur[ii][jj] + EPS)
# 0.1 is added to the denominator to resolve cases when ValidMoves_stay is 0
Resp[ii][jj] = np.clip(Resp[ii][jj], -1, 1)
# Clipping Resp to the range [-1,1]
ValidMoves_moveDeRigueur = ValidMoves_moveDeRigueur.astype(int)
ValidMoves_action = ValidMoves_action.astype(int)
Validity_of_Moves_moveDeRigueur = Validity_of_Moves_moveDeRigueur.astype(int)
Validity_of_Moves_action = Validity_of_Moves_action.astype(int)
if VerboseFlag:
print('Validity_of_Moves_moveDeRigueur : ', Validity_of_Moves_moveDeRigueur)
print('Validity_of_Moves_action : ', Validity_of_Moves_action)
return Resp, ValidMoves_moveDeRigueur, ValidMoves_action, Validity_of_Moves_moveDeRigueur, Validity_of_Moves_action
def FeAL(WorldIn, ActionID4Agents, MovesDeRigueur4Agents=[]):
# Feasible Action-Space Left - for each agent
# A measure of the agency of each agent -
# - and thus an indicator of personal causal responsibility
FuncWorld = copy.deepcopy(WorldIn)
# Storing the Actions received for each agent
ActionInputs = np.ones(len(FuncWorld.AgentList)).astype(int) * 0 # Default is Stay
for AgentID, ActionID in ActionID4Agents:
ActionInputs[AgentID] = ActionID
# Storing the Move de Rigueurs received for each agent
MovesDeRigueur = np.ones(len(FuncWorld.AgentList)).astype(int) * 0 # Default Move de Riguer is Stay
for AgentID, ActionID in MovesDeRigueur4Agents:
MovesDeRigueur[AgentID] = ActionID
FeAL = np.zeros(len(FuncWorld.AgentList))
ValidMoves_moveDeRigueur_FeAL = np.zeros(len(FuncWorld.AgentList))
ValidMoves_action_FeAL = np.zeros(len(FuncWorld.AgentList))
list_of_actions_for_agents = []
for agentID in FuncWorld.AgentList:
list_of_actions_for_agents.append(len(agentID.Actions))
max_n_actions = max(list_of_actions_for_agents)
if VerboseFlag: print('max_n_actions : ', max_n_actions)
Validity_of_Moves_MdR_FeAL = np.zeros((len(FuncWorld.AgentList), max_n_actions))
Validity_of_Moves_action_FeAL = np.zeros((len(FuncWorld.AgentList), max_n_actions))
for ii in tqdm(range(len(FuncWorld.AgentList)), colour="red", ncols=100): # Affected
agentid_list = list(range(len(FuncWorld.AgentList)))
agentid_list.pop(ii)
agentIDs4swaps = agentid_list
# All agents but ego agent - Move de Rigueur
# action_mdr_list = MovesDeRigueur.copy()
# action_mdr_list.pop(ii)
actionIDs4swaps = np.delete(MovesDeRigueur, ii)
if VerboseFlag:
print("FuncWorld.AgentList,MovesDeRigueur",FuncWorld.AgentList,MovesDeRigueur)
print("agentIDs4swaps,actionIDs4swaps : ",agentIDs4swaps,actionIDs4swaps)
ActionID4Agents_OthersMoveDeRigueur = GWorld.SwapActionIDs4Agents(ActionID4Agents=ActionID4Agents,
agentIDs4swaps=agentIDs4swaps,
actionIDs4swaps=actionIDs4swaps)
# All agents but ego agent -Actor Moves
# action_move_list = ActionInputs
# action_move_list.pop(ii)
actionIDs4swaps = np.delete(ActionInputs, ii)
if VerboseFlag:
print("FuncWorld.AgentList,MovesDeRigueur",FuncWorld.AgentList,ActionInputs)
print("agentIDs4swaps,actionIDs4swaps : ",agentIDs4swaps,actionIDs4swaps)
ActionID4Agents_OthersMove = GWorld.SwapActionIDs4Agents(ActionID4Agents=ActionID4Agents,
agentIDs4swaps=agentIDs4swaps,
actionIDs4swaps=actionIDs4swaps)
if VerboseFlag:
print('Affected agent {:02d}!'.format(ii + 1))
print('ActionIDs_OthersMdR :', ActionID4Agents_OthersMoveDeRigueur)
print('ActionIDs_OthersMove :', ActionID4Agents_OthersMove)
ValidMoves_moveDeRigueur_FeAL[ii], Validity_of_Moves_MdR_FeAL[ii] = \
CountValidMovesOfAffected(WorldIn=FuncWorld,
ActionID4Agents=ActionID4Agents_OthersMoveDeRigueur,
AffectedID=ii)
ValidMoves_action_FeAL[ii], Validity_of_Moves_action_FeAL[ii] = \
CountValidMovesOfAffected(WorldIn=FuncWorld,
ActionID4Agents=ActionID4Agents_OthersMove,
AffectedID=ii)
FeAL[ii] = (ValidMoves_action_FeAL[ii]) / \
(ValidMoves_moveDeRigueur_FeAL[ii] + EPS)
# 0.1 is added to the denominator to resolve cases when ValidMoves_stay is 0
FeAL[ii] = np.clip(FeAL[ii], -1, 1)
# Clipping Resp to the range [-1,1]
ValidMoves_moveDeRigueur_FeAL = ValidMoves_moveDeRigueur_FeAL.astype(int)
ValidMoves_action_FeAL = ValidMoves_action_FeAL.astype(int)
Validity_of_Moves_MdR_FeAL = Validity_of_Moves_MdR_FeAL.astype(int)
Validity_of_Moves_action_FeAL = Validity_of_Moves_action_FeAL.astype(int)
if VerboseFlag:
print('Validity_of_Moves_moveDeRigueur : ', Validity_of_Moves_MdR_FeAL)
print('Validity_of_Moves_action : ', Validity_of_Moves_action_FeAL)
return FeAL, ValidMoves_moveDeRigueur_FeAL, ValidMoves_action_FeAL, \
Validity_of_Moves_MdR_FeAL, Validity_of_Moves_action_FeAL