-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGWorld.py
724 lines (593 loc) · 35.2 KB
/
GWorld.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import numpy as np;
np.random.seed(0)
import json
import pprint
import Agent
VerboseFlag = False
# VerboseFlag = True
class GWorld:
def __init__(self, Map, Walls=[], OneWays=[]):
self.AgentList = []
self.AgentActiveStatus = []
self.AgentCrash = []
self.RestrictedMove = []
self.AgentLocations = []
self.MaxSteps = 4
self.PreviousAgentLocations = []
self.WorldMap = Map # Grid with 0 for inactive cells and 1 for active cells
self.WorldState = np.where(Map == 0, np.nan, Map) # Inactive Cells - NaN
self.WorldState = np.where(self.WorldState == 1, 0, self.WorldState) # Active Cells - 0 at the start
# Path restrictions = Walls and OneWays
self.RestrictedPaths = []
# Walls - Restricts movements in both directions
# Check and Add Walls
self.WorldWalls = []
for wall in Walls:
x1 = wall[0][0]
y1 = wall[0][1]
x2 = wall[1][0]
y2 = wall[1][1]
# Check whether the indices are within bounds
if (x1 < self.WorldMap.shape[0]) and (x2 < self.WorldMap.shape[0]) and \
(y1 < self.WorldMap.shape[1]) and (y2 < self.WorldMap.shape[1]):
# Check whether the indices are next to each other
# i.e. they have atleast one common index
# and the other index differs only by 1
if (x1 == x2):
if ((abs(y1 - y2)) == 1):
self.WorldWalls.append(wall)
# Add both directions to restricted paths
self.RestrictedPaths.append(wall)
self.RestrictedPaths.append([wall[1], wall[0]])
elif (y1 == y2):
if ((abs(x1 - x2)) == 1):
self.WorldWalls.append(wall)
# Add both directions to restricted paths
self.RestrictedPaths.append(wall)
self.RestrictedPaths.append([wall[1], wall[0]])
# OneWays - Restricts movement in the opposite direction
# Check and Add OneWays
self.WorldOneWays = []
for way in OneWays:
x1 = way[0][0]
y1 = way[0][1]
x2 = way[1][0]
y2 = way[1][1]
# Check whether the indices are within bounds
if (x1 < self.WorldMap.shape[0]) and (x2 < self.WorldMap.shape[0]) and \
(y1 < self.WorldMap.shape[1]) and (y2 < self.WorldMap.shape[1]):
# Check whether the indices are next to each other
# i.e. they have atleast one common index
# and the other index differs only by 1
if (x1 == x2):
if ((abs(y1 - y2)) == 1):
self.WorldOneWays.append(way)
# Add opposite direction to restricted paths
self.RestrictedPaths.append([way[1], way[0]])
elif (y1 == y2):
if ((abs(x1 - x2)) == 1):
self.WorldOneWays.append(way)
# Add opposite direction to restricted paths
self.RestrictedPaths.append([way[1], way[0]])
# # #--- For Visualisation----
# self.Gfig, self.Gax = plt.subplots()
# # line, = self.Gax.plot([]) # A tuple unpacking to unpack the only plot
# plt.clf()
# plt.axis('equal')
# #----
# self.AnimateGWorld()
def __eq__(self, other):
if not isinstance(other, GWorld):
return NotImplemented
return (self.AgentList == other.AgentList and
self.AgentActiveStatus == other.AgentActiveStatus and
self.AgentCrash == other.AgentCrash and
self.RestrictedMove == other.RestrictedMove and
self.AgentLocations == other.AgentLocations and
self.MaxSteps == other.MaxSteps and
self.PreviousAgentLocations == other.PreviousAgentLocations and
np.array_equal(self.WorldMap, other.WorldMap) and
np.array_equal(self.WorldState, other.WorldState) and
self.RestrictedPaths == other.RestrictedPaths and
self.WorldWalls == other.WorldWalls and
self.WorldOneWays == other.WorldOneWays)
def __hash__(self):
return hash((tuple(self.AgentList),
tuple(self.AgentActiveStatus),
tuple(self.AgentCrash),
tuple(self.RestrictedMove),
tuple(self.AgentLocations),
self.MaxSteps,
tuple(self.PreviousAgentLocations),
self.WorldMap.tobytes(),
self.WorldState.tobytes(),
tuple(map(tuple, self.RestrictedPaths)),
tuple(map(tuple, self.WorldWalls)),
tuple(map(tuple, self.WorldOneWays))))
# ----------------------------------------------------------------------------------------------- #
# Function to Add Agent : AddAgent( Agent , Location )
def AddAgent(self, Agent, Location, printStatus=True):
if (0 <= Location[0] < self.WorldState.shape[0]) and ((0 <= Location[1] < self.WorldState.shape[1])):
if self.WorldState[Location] == 0:
if printStatus:
print('Adding Agent to ', Location)
self.AgentList.append(Agent)
self.AgentActiveStatus.append(True)
self.AgentCrash.append(False)
self.RestrictedMove.append(False)
self.AgentLocations.append(Location)
self.WorldState[Location] = 0.5 # A Dummy Value to keep track of occupied cells
return True
elif self.WorldState[Location] > 0:
print('Adding Agent Aborted! Collision at spawn location : ', Location)
return False
else:
print('Adding Agent Aborted! Invalid Location! : ', Location)
return False
else:
print('Adding Agent Aborted! Location out of bounds ! : ', Location)
return False
# ----------------------------------------------------------------------------------------------- #
# Function get ActionSelection for Agents to be used in UpdateGWorld
def getActionSelection4UpdateGWorld(self, ActionID4Agents=[], VerboseFlag=False, defaultAction='random'):
# Storing the Actions received for each agent
ActionInputs = np.ones(len(self.AgentList)).astype(int) * -1
for AgentID, ActionID in ActionID4Agents:
if AgentID in np.arange(len(self.AgentList)):
ActionInputs[AgentID] = ActionID
# Getting Selected Actions from Agents
for idx, agent in enumerate(self.AgentList):
if self.AgentActiveStatus[idx] is True:
if VerboseFlag: print("Getting the selected action for Active Agent ", idx + 1)
# Action Selection
# If Agent was given an ActionID
if ActionInputs[idx] in np.arange(len(agent.Actions)):
if VerboseFlag: print('Selected ActionID :', ActionInputs[idx])
agent.ActionSelection(ActionID=ActionInputs[idx])
# Default Actions
else:
if defaultAction == 'random':
if VerboseFlag: print('Selecting Random Action')
agent.ActionSelection()
elif defaultAction == 'stay':
if VerboseFlag: print('Selected Action: Stay')
agent.ActionSelection(ActionID=0)
elif defaultAction in np.arange(len(agent.Actions)):
if VerboseFlag: print('Selected ActionID :', defaultAction)
agent.ActionSelection(ActionID=defaultAction)
else:
if VerboseFlag: print('Selecting Random Action')
agent.ActionSelection()
pass
# ----------------------------------------------------------------------------------------------- #
# Function to revert steps with collisions and update NewAgentLocations !
def revertStepsWithCollisions(self, step=None, NewAgentLocations_CurrentFloor=None):
if step is None:
print('Step not passed.')
return False
if NewAgentLocations_CurrentFloor is None:
print('NewAgentLocations_CurrentFloor not passed.')
return False
# Revert the Moves of Crashed Agents
for ii in np.arange(len(self.AgentList)):
if self.AgentCrash[ii] == True:
step_ii = (step + 1) * (len(self.AgentList[ii].SelectedAction)) / (self.MaxSteps)
step_ii_floor = np.floor(step_ii).astype(int)
# Make the agent stationary for all subsequent ActionSteps
for kk in np.arange(step_ii_floor, len(self.NewAgentLocations[ii])):
self.NewAgentLocations[ii][kk] = self.AgentLocations[ii]
# Make the floor location the last agent location.
NewAgentLocations_CurrentFloor[ii] = self.AgentLocations[ii]
return NewAgentLocations_CurrentFloor
# ----------------------------------------------------------------------------------------------- #
# Function to update AgentLocations and WorldLocations based on NewAgentLocations !
def update_agent_locations_2_world_state(self, NewAgentLocations_CurrentFloor=None, VerboseFlag=False):
if NewAgentLocations_CurrentFloor is None:
print('NewAgentLocations_CurrentFloor not passed.')
return False
for idx, agent in enumerate(self.AgentList):
# Update the Locations of Agents
self.AgentLocations[idx] = NewAgentLocations_CurrentFloor[idx]
if VerboseFlag: print('Updating AgentLocation from Last NewAgentLocation')
if VerboseFlag: print('NewAgentLocations', self.NewAgentLocations)
if VerboseFlag: print('AgentLocations', self.AgentLocations)
if self.AgentActiveStatus[idx] is True:
if VerboseFlag: print('Updating Location of Agent ', idx + 1, ' : ', self.AgentLocations[idx])
# Updating Positions of Agents in WorldState
self.WorldState[(self.AgentLocations[idx])] = idx + 1
pass
def collision_checks_and_resolution(self, step=None, NewAgentLocations_CurrentFloor=None, VerboseFlag=False):
if step is None:
print('Step not passed.')
return False
if NewAgentLocations_CurrentFloor is None:
print('NewAgentLocations_CurrentFloor not passed.')
return False
# ..........................................#
# Check for collisions in the NewAgentLocations
# And revert moves that lead to collisions
NumberOfAgents = len(self.AgentList)
CollisionCount = NumberOfAgents # No Agents => No Collisions
LoopCount = 0 # Fail Safe in case of Unresolved Collisions
while (CollisionCount > 0) and (LoopCount < 2 * NumberOfAgents):
LoopCount += 1
if VerboseFlag: print('Collisions : ', CollisionCount)
CollisionCount = 0 # Reset the counter
for ii in np.arange(NumberOfAgents - 1):
step_ii = (step + 1) * (len(self.AgentList[ii].SelectedAction)) / (self.MaxSteps)
step_ii_floor = np.floor(step_ii).astype(int)
step_ii_ceil = np.ceil(step_ii).astype(int)
NewAgentLocations_CurrentFloor[ii] = self.NewAgentLocations[ii][step_ii_floor]
for jj in np.arange(ii + 1, NumberOfAgents):
step_jj = (step + 1) * (len(self.AgentList[jj].SelectedAction)) / (self.MaxSteps)
step_jj_floor = np.floor(step_jj).astype(int)
step_jj_ceil = np.ceil(step_jj).astype(int)
NewAgentLocations_CurrentFloor[jj] = self.NewAgentLocations[jj][step_jj_floor]
if VerboseFlag:
print('ii,step_floor', (ii, step), 'self.NewAgentLocations[ii][step] :'
, self.NewAgentLocations[ii][step_ii_floor])
print('ii,step_ceil', (ii, step), 'self.NewAgentLocations[ii][step] :'
, self.NewAgentLocations[ii][step_ii_ceil])
print('jj.step_floor', (jj, step), 'self.NewAgentLocations[jj][step] :'
, self.NewAgentLocations[jj][step_jj_floor])
print('jj.step_ceil', (jj, step), 'self.NewAgentLocations[jj][step] :'
, self.NewAgentLocations[jj][step_jj_ceil])
if (self.NewAgentLocations[ii][step_ii_floor] == self.NewAgentLocations[jj][step_jj_floor]) or \
(self.NewAgentLocations[ii][step_ii_ceil] == self.NewAgentLocations[jj][
step_jj_ceil]):
# COLLISION !
if VerboseFlag:
print('Collision !')
self.print_collision_report(ii, jj, step_ii_ceil, step_ii_floor, step_jj_ceil,
step_jj_floor)
CollisionCount = self.record_collision(CollisionCount, ii, jj)
# ---------------------------------#
# ---------------------------------#
# Agent HEALTH UPDATES go here !! #
# ---------------------------------#
# ---------------------------------#
elif (self.NewAgentLocations[ii][step_ii_floor] == self.NewAgentLocations[jj][
step_jj_ceil]) and \
(self.NewAgentLocations[ii][step_ii_ceil] == self.NewAgentLocations[jj][
step_jj_floor]):
# CROSSOVER COLLISION ! in NewLocations
if VerboseFlag:
print('Crossover Collision !')
self.print_collision_report(ii, jj, step_ii_ceil, step_ii_floor, step_jj_ceil,
step_jj_floor)
CollisionCount = self.record_collision(CollisionCount, ii, jj)
# ---------------------------------#
# ---------------------------------#
# Agent HEALTH UPDATES go here !! #
# ---------------------------------#
# ---------------------------------#
elif self.NewAgentLocations[ii][step_ii_floor] == self.NewAgentLocations[jj][step_jj_ceil]:
# Possible Collision - if the hangovers don't match
overhang_floor = step_ii_ceil - step_ii
overhang_ceil = step_jj - step_jj_floor
overhang_condition = ((overhang_floor + overhang_ceil) <= 1)
direction_ii = np.array(self.NewAgentLocations[ii][step_ii_ceil]) \
- np.array(self.NewAgentLocations[ii][step_ii_floor])
direction_jj = np.array(self.NewAgentLocations[jj][step_jj_ceil]) \
- np.array(self.NewAgentLocations[jj][step_jj_floor])
same_direction_condition = np.array_equal(direction_ii, direction_jj)
if VerboseFlag:
print('same_direction_condition :', same_direction_condition)
print('overhang_condition :', overhang_condition)
# If the above 2 conditions are met, then the agents can slide along without hitting each other
if not (overhang_condition and same_direction_condition):
# Collision happens in the case where the above conditions are not met.
if VerboseFlag:
print('Collision !')
self.print_collision_report(ii, jj, step_ii_ceil, step_ii_floor, step_jj_ceil,
step_jj_floor)
CollisionCount = self.record_collision(CollisionCount, ii, jj)
# ---------------------------------#
# ---------------------------------#
# Agent HEALTH UPDATES go here !! #
# ---------------------------------#
# ---------------------------------#
elif self.NewAgentLocations[ii][step_ii_ceil] == self.NewAgentLocations[jj][step_jj_floor]:
# Possible Collision - if the hangovers don't match
overhang_floor = step_jj_ceil - step_jj
overhang_ceil = step_ii - step_ii_floor
overhang_condition = ((overhang_floor + overhang_ceil) <= 1)
direction_ii = np.array(self.NewAgentLocations[ii][step_ii_ceil]) \
- np.array(self.NewAgentLocations[ii][step_ii_floor])
direction_jj = np.array(self.NewAgentLocations[jj][step_jj_ceil]) \
- np.array(self.NewAgentLocations[jj][step_jj_floor])
same_direction_condition = np.array_equal(direction_ii, direction_jj)
if VerboseFlag:
print('same_direction_condition :', same_direction_condition)
print('overhang_condition :', overhang_condition)
# If the above 2 conditions are met, then the agents can slide along without hitting each other
if not (overhang_condition and same_direction_condition):
# Collision happens in the case where the above conditions are not met.
if VerboseFlag:
print('Collision !')
self.print_collision_report(ii, jj, step_ii_ceil, step_ii_floor, step_jj_ceil,
step_jj_floor)
CollisionCount = self.record_collision(CollisionCount, ii, jj)
# ---------------------------------#
# ---------------------------------#
# Agent HEALTH UPDATES go here !! #
# ---------------------------------#
# ---------------------------------#
elif (((self.NewAgentLocations[ii][step_ii_floor] == self.AgentLocations[jj]) and
(self.AgentLocations[ii] == self.NewAgentLocations[jj][step_jj_floor])) or
((self.NewAgentLocations[ii][step_ii_ceil] == self.AgentLocations[jj]) and
(self.AgentLocations[ii] == self.NewAgentLocations[jj][step_jj_ceil])) or
((self.NewAgentLocations[ii][step_ii_floor] == self.AgentLocations[jj]) and
(self.AgentLocations[ii] == self.NewAgentLocations[jj][step_jj_ceil])) or
((self.NewAgentLocations[ii][step_ii_ceil] == self.AgentLocations[jj]) and
(self.AgentLocations[ii] == self.NewAgentLocations[jj][step_jj_floor]))):
# CROSSOVER COLLISION ! - NewLocations with OldLocations
if VerboseFlag:
print('Crossover Collision !')
self.print_collision_report(ii, jj, step_ii_ceil, step_ii_floor, step_jj_ceil,
step_jj_floor)
CollisionCount = self.record_collision(CollisionCount, ii, jj)
# ---------------------------------#
# ---------------------------------#
# Agent HEALTH UPDATES go here !! #
# ---------------------------------#
# ---------------------------------#
########################################################################################################
NewAgentLocations_CurrentFloor = self.revertStepsWithCollisions(step=step, \
NewAgentLocations_CurrentFloor= \
NewAgentLocations_CurrentFloor)
########################################################################################################
# Fail Safe in case of Unresolved Collisions
if LoopCount >= 2 * NumberOfAgents:
print('Collisions Not Resolved !!!')
# ..........................................#
return NewAgentLocations_CurrentFloor
def record_collision(self, CollisionCount, ii, jj):
CollisionCount += 1
# Record Crash
self.AgentCrash[ii] = True
self.AgentCrash[jj] = True
return CollisionCount
def print_collision_report(self, ii, jj, step_ii_ceil, step_ii_floor, step_jj_ceil, step_jj_floor):
print('Agent ', ii + 1, ' : Old :', self.AgentLocations[ii])
print('Agent ', ii + 1, ' : New :', self.NewAgentLocations[ii][step_ii_floor])
print('Agent ', ii + 1, ' : New :', self.NewAgentLocations[ii][step_ii_ceil])
print('Agent ', jj + 1, ' : Old :', self.AgentLocations[jj])
print('Agent ', jj + 1, ' : New :', self.NewAgentLocations[jj][step_jj_floor])
print('Agent ', jj + 1, ' : New :', self.NewAgentLocations[jj][step_jj_ceil])
# ----------------------------------------------------------------------------------------------- #
# Function where the actions chosen by the agents are used to update the state of the world
def UpdateGWorld(self, defaultAction='random', ActionID4Agents=[],
apples=None, apple_eaters=None):
# Get Agent Actions
# Update AgentLocations and Check which agents are active
# Update WorldState
if VerboseFlag: print("Updating GWorld")
self.PreviousAgentLocations = self.AgentLocations.copy()
self.WorldState = np.where(self.WorldMap == 0, np.nan, self.WorldMap) # Inactive Cells - NaN
self.WorldState = np.where(self.WorldState == 1, 0, self.WorldState) # Active Cells - 0 at the start
# If nothing happens, the agents will be in the old positions
self.NewAgentLocations = []
for AgentLocation in self.AgentLocations:
self.NewAgentLocations.append([AgentLocation])
# self.NewAgentLocations = self.AgentLocations.copy()
##################################################################
self.getActionSelection4UpdateGWorld(ActionID4Agents, VerboseFlag=VerboseFlag, defaultAction=defaultAction)
##################################################################
for idx, agent in enumerate(self.AgentList):
if self.AgentActiveStatus[idx] is True:
# Resetting Crash Record for all the Agents
self.AgentCrash[idx] = False
# Resetting Restricted Moves Record for all Agents
self.RestrictedMove[idx] = False
apples_caught = []
# Getting (valid) NewLocations from action steps from Agents
for step in np.arange(self.MaxSteps):
NewAgentLocations_CurrentFloor = self.AgentLocations.copy()
for idx, agent in enumerate(self.AgentList):
if (self.AgentActiveStatus[idx] is True):
if step < len(agent.SelectedAction) and (self.AgentCrash[idx] is False):
ActionStep = agent.SelectedAction[step]
else:
ActionStep = (0, 0) # If no steps left in SelectedAction
if VerboseFlag:
print("Selected Action Name : ", agent.SelectedActionName)
print("Selected Action Move : ", ActionStep)
old_location = self.NewAgentLocations[idx][step]
# self.NewAgentLocations[idx][0] is the PreviousLocation (before any action step)
# self.NewAgentLocations[idx][step+1] = where the agent will be after "step"
if VerboseFlag:
print('Old Location: ', old_location)
print('idx,step:', (idx, step), 'ActionStep:', ActionStep)
new_location = (old_location[0] + ActionStep[0], old_location[1] + ActionStep[1])
if VerboseFlag: print('New Location (from step): ', new_location)
# Making sure that the agents are not pushed off the grid
new_location0 = np.clip(new_location[0], 0, self.WorldMap.shape[0] - 1)
new_location1 = np.clip(new_location[1], 0, self.WorldMap.shape[1] - 1)
# Restricted move if the new location is clipped.
if not new_location == (new_location0, new_location1):
self.RestrictedMove[idx] = True
new_location = (new_location0, new_location1)
AgentPath = [old_location, new_location]
# Checking if the new position is a valid location
if self.WorldState[new_location] >= 0:
# Checking is the move is along a restricted path
if AgentPath not in self.RestrictedPaths:
self.NewAgentLocations[idx].append(new_location)
# Print NewAgentLocation
if VerboseFlag: print('idx,NewAgentLocations[idx] : '
, (idx, self.NewAgentLocations[idx]))
else: # Setting OldLocation in case of Restricted Paths
self.NewAgentLocations[idx].append(old_location)
# Record Restricted Move
self.RestrictedMove[idx] = True
# Print NewAgentLocation
if VerboseFlag: print('idx,NewAgentLocations[idx] : '
, (idx, self.NewAgentLocations[idx]))
else: # Setting OldLocation in case of Invalid Location
self.NewAgentLocations[idx].append(old_location)
# Record Restricted Move
self.RestrictedMove[idx] = True
# Print NewAgentLocation
if VerboseFlag: print('idx,NewAgentLocations[idx] : '
, (idx, self.NewAgentLocations[idx]))
# Updating the NewAgentLocation when it is valid
#############################################
NewAgentLocations_CurrentFloor = self.collision_checks_and_resolution(step=step, \
NewAgentLocations_CurrentFloor= \
NewAgentLocations_CurrentFloor,
VerboseFlag=VerboseFlag)
#############################################
# Check for Apples
eaten_apple_ids = []
if apples is not None and apple_eaters is not None:
print(f'{apples=}')
for idx in apple_eaters:
for apple_id, apple in enumerate(apples):
print(f'{apple_id=}, {apple=}')
if NewAgentLocations_CurrentFloor[idx][0] == apple[0] and \
NewAgentLocations_CurrentFloor[idx][1] == apple[1]:
apples_caught.append([idx, apple_id])
eaten_apple_ids.append(apple_id)
apple_ids = list(range(len(apples)))
uneaten_apple_ids = [x for x in apple_ids if x not in eaten_apple_ids]
apples = apples[uneaten_apple_ids]
print(f'{apples =}')
print(f'{apples_caught=}')
#############################################
self.update_agent_locations_2_world_state(NewAgentLocations_CurrentFloor=NewAgentLocations_CurrentFloor,
VerboseFlag=VerboseFlag)
#############################################
agent_crashes = self.AgentCrash.copy()
restricted_moves = self.RestrictedMove.copy()
if apples is not None and apple_eaters is not None:
return agent_crashes, restricted_moves, apples, apples_caught
return agent_crashes, restricted_moves
def SelectActionsForAll(self, defaultAction='random', InputActionID4Agents=[]):
# Storing the Actions received for each agent
ActionInputs = np.ones(len(self.AgentList)).astype(int) * -1
for AgentID, ActionID in InputActionID4Agents:
if AgentID in np.arange(len(self.AgentList)):
ActionInputs[AgentID] = ActionID
ActionID4Agents = []
# Getting Selected Actions from Agents
for idx, agent in enumerate(self.AgentList):
if self.AgentActiveStatus[idx] is True:
if VerboseFlag: print("Getting the selected action for Active Agent ", idx + 1)
# Action Selection
# If Agent was given an ActionID
if ActionInputs[idx] in np.arange(len(agent.Actions)):
if VerboseFlag: print('Selected ActionID :', ActionInputs[idx])
SelectedActionID = agent.ActionSelection(ActionID=ActionInputs[idx])
# Default Actions
else:
if defaultAction == 'random':
if VerboseFlag: print('Selecting Random Action')
SelectedActionID = agent.ActionSelection()
elif defaultAction == 'stay':
if VerboseFlag: print('Selected Action: Stay')
SelectedActionID = agent.ActionSelection(ActionID=0)
elif defaultAction in np.arange(len(agent.Actions)):
if VerboseFlag: print('Selected ActionID :', defaultAction)
SelectedActionID = agent.ActionSelection(ActionID=defaultAction)
else:
if VerboseFlag: print('Selecting Random Action')
SelectedActionID = agent.ActionSelection()
ActionID4Agents.append((idx, SelectedActionID))
return ActionID4Agents
# ----------------------------------------------------------------------------------------------- #
# Function to remove an Agent - NOT IMPLEMENTED YET !
def RemoveAgent(self, Agent):
pass
# ----------------------------------------------------------------------------------------------- #
def GetAgentContext(self):
pass
# ----------------------------------------------------------------------------------------------- #
def SetAgentSensoryStates(self, Agent):
pass
# ----------------------------------------------------------------------------------------------- #
def LoadJsonScenario(json_filename='Scenarios.json', scenario_name='Base'):
# Reading Dictionary from JSON file
with open(json_filename) as json_file:
data = json.load(json_file)
Scenario = data[scenario_name]
if 'MdRs' in Scenario.keys():
for mdr_key in Scenario['MdRs']:
mdr = Scenario['MdRs'][mdr_key]
for slice_key in ['slicex', 'slicey']:
slice_arg = mdr[slice_key]
if slice_arg[2] == 0: # If slice_step is 0
slice_ = slice(slice_arg[0], slice_arg[1], None)
else:
slice_ = slice(slice_arg[0], slice_arg[1], slice_arg[2])
Scenario['MdRs'][mdr_key][slice_key] = slice_
if 'Policies' in Scenario.keys():
for policy_key in Scenario['Policies']:
policy = Scenario['Policies'][policy_key]
for slice_key in ['slicex', 'slicey']:
slice_arg = policy[slice_key]
if slice_arg[2] == 0: # If slice_step is 0
slice_ = slice(slice_arg[0], slice_arg[1], None)
else:
slice_ = slice(slice_arg[0], slice_arg[1], slice_arg[2])
Scenario['Policies'][policy_key][slice_key] = slice_
# Map_ = dict()
# Map_['Region'] = Scenario['Map']['Region']
# # Fixing the tuple format which is lost in JSON
# for listname in ['Walls', 'OneWays']:
# List_ = []
# for path in Scenario['Map'][listname]:
# path_ = []
# for location in path:
# path_.append(tuple(location))
# List_.append(path_)
# Map_[listname] = List_
# Scenario['Map'] = Map_
# # Fixing the tuple format for AgentLocations
# AgentLocations_ = []
# for location in Scenario['AgentLocations']:
# AgentLocations_.append(tuple(location))
# Scenario['AgentLocations'] = AgentLocations_
return Scenario
def AddJsonScenario(json_filename='Scenarios.json', new_scenario=None, new_scenario_name=None, Overwrite=False):
# Appending a new Scenario to the JSON file
# Reading Dictionary from JSON file
with open(json_filename) as json_file:
data = json.load(json_file)
# Adding new scenario
if new_scenario is None:
print('Error ! - No New Scenario Added')
return False
elif new_scenario_name is None:
print("Error - No Scenario Name - Aborted")
return False
elif new_scenario_name in data:
if Overwrite is True:
print('Overwriting Scenario: ', new_scenario_name)
data[new_scenario_name] = new_scenario
else:
print('Error - Scenario Exists - Overwrite is False !')
return False
else:
data[new_scenario_name] = new_scenario
pretty_print_json = pprint.pformat(data, width=150).replace("'", '"')
with open(json_filename, 'w') as f:
f.write(pretty_print_json)
return True
def SwapActionIDs4Agents(ActionID4Agents=None, agentIDs4swaps=[], actionIDs4swaps=[]):
if ActionID4Agents is None:
print('No ActionID4Agents Provided! Returning empty list!')
return []
elif not (len(agentIDs4swaps) == len(actionIDs4swaps)):
print('The lengths of agentIDs4swaps and actionIDs4swaps should match ! No Swaps Done!')
return ActionID4Agents
else:
new_ActionIDs4Agents = []
for agentID, actionID in ActionID4Agents:
if agentID in agentIDs4swaps:
idx = agentIDs4swaps.index(agentID)
new_actionID = actionIDs4swaps[idx]
new_ActionIDs4Agents.append((agentID, new_actionID))
else:
new_ActionIDs4Agents.append((agentID, actionID))
return new_ActionIDs4Agents