-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellObjectTest.py
More file actions
1777 lines (1338 loc) · 51.5 KB
/
cellObjectTest.py
File metadata and controls
1777 lines (1338 loc) · 51.5 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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
import random
from mesa import Agent, Model
from mesa.space import MultiGrid
from mesa.time import RandomActivation
from mesa.datacollection import DataCollector
from collections import namedtuple
import typing
Point = namedtuple('Point', 'x y')
PotentialMove = namedtuple("PotentialMove", ["pos", "content"])
Cost = namedtuple('Cost', ['food', 'oxygen'])
Reward = namedtuple('Reward', ['food', 'oxygen'])
Move = namedtuple(
"Move", ["pos", "action", "location", "cost", "reward", "value"])
RankedMove = namedtuple("Rank", ["move", "rank"])
Params = namedtuple("Parameters", ["breathe_amount", "eat_amount", "eat_efficiency", "max_food", "max_oxy",
"low_food", "low_oxy"] )
class LarvaParams():
def __init__(self,dict):
'''self.EAT_AMOUNT = 0
self.EAT_EFFICIENCY = 0
self.BREATHE_AMOUNT = 0
self.MAX_FOOD = 0 #6
self.MAX_OXY = 0 #6
self.LOW_FOOD = 0
self.LOW_OXY = 0'''
#self.POOP_THRESHOLD = 5
self.PARAMETERS = {}
for param in dict:
#print(param)
setattr(self, param, dict[param])
class NeighborHoodObject():
def __init__(self,model:Model,pos:tuple,center=False,moore=False):
self.model = model
self.pos = pos
self.neighbor_pos = self.neighbors(center,moore)
#List of moves where actions can take place as prerequiste conditions are met
self.edible_list = list()
self.breatheable_list = list()
self.moveable_list = list()
self.swapable_list = list()
# make this a dictionary acessable by pos
self.neighbors = {}
for pos in self.neighbor_pos:
cell = CellObject(self.model,pos)
self.neighbors[pos] = cell
def update(self, center=False,moore=False):
self.get_neighbors(center,moore)
self.get_edible()
self.get_breatheable()
self.get_moveable()
def get_neighbors(self, center, moore):
self.neighbor_pos = self.neighbors(center, moore)
self.neighbors = list()
for neighbor in self.neighbor_pos:
cell = CellObject(self.model,neighbor)
self.neighbors.append(cell)
def get_cell(self,pos):
return self.neighbors[pos]
def get_edible(self):
self.edible_list = [self.neighbors[neighbor]
for neighbor in self.neighbors if self.neighbors[neighbor].is_edible(self.pos)]
return self.edible_list
def get_diffuse_targets(self,type="Food"):
return [self.neighbors[neighbor]
for neighbor in self.neighbors if self.neighbors[neighbor].is_diffusible(type)]
def get_breatheable(self):
#Need to do this as it iterates through keys not values
self.breatheable_list = [
self.neighbors[neighbor] for neighbor in self.neighbors if self.neighbors[neighbor].is_breatheable(self.pos)]
return self.breatheable_list
def get_moveable(self,direction="all"):
#four direction: up, down, all, side
check_pos = self.pos[1]
if direction == "up":
check_pos += 1
elif direction == "down":
check_pos -= 1
if direction=="all":
self.moveable_list = [self.neighbors[neighbor]
for neighbor in self.neighbors if self.neighbors[neighbor].is_moveable()]
else:
self.moveable_list = [
self.neighbors[neighbor] for neighbor in self.neighbors if self.neighbors[neighbor].is_moveable() and neighbor[1] == check_pos]
#neighbor.pos[1] == check_pos]
'''elif direction == "up":
self.moveable_list = [
neighbor for neighbor in self.neighbors if neighbor.is_moveable() and neighbor.pos[1] == self.pos[1] + 1]
elif direction == "down":
self.moveable_list = [
neighbor for neighbor in self.neighbors if neighbor.is_moveable() and neighbor.pos[1] == self.pos[1] - 1]
elif direction == "side":
self.moveable_list = [
neighbor for neighbor in self.neighbors if neighbor.is_moveable() and neighbor.pos[1] == self.pos[1]]
'''
return self.moveable_list
pass
def get_swapable(self,direction="all"):
if direction == "all":
self.swapable_list = [self.neighbors[neighbor]
for neighbor in self.neighbors if self.neighbors[neighbor].Food is not None or self.neighbors[neighbor].Poop is not None]
else:
check_pos = self.pos[1]
if direction == "up":
check_pos += 1
elif direction == "down":
check_pos -= 1
self.swapable_list = [self.neighbors[neighbor]
for neighbor in self.neighbors if neighbor[1] == check_pos and (self.neighbors[neighbor].Food is not None or self.neighbors[neighbor].Poop is not None)]
#let poop and food be moved through by swapping positions, let poop be swapped at any side but food can only be swapped from behind
#just let it swap with whatever it wants
return self.swapable_list
def neighbors(self, center=False, moore=True):
pos = self.pos
neighbors = self.model.grid.get_neighborhood(
pos,
moore=moore,
include_center=center)
return neighbors
class CellObject():
#store these all in a dictionary??
def __init__(self,model: Model, pos):
self.model = model
self.pos = pos
self.LarvaAgent = None
self.Food = None
self.Poop = None
self.Oxy = None
self.LarvaTail = None
contents = self.__cell_contents()
for agent in contents:
classname = agent.__class__.__name__
#classstring = str(classname)
#setattr(agent,classname)
setattr(self,classname,agent)
self.is_solid = False
if self.LarvaAgent is not None or self.LarvaTail is not None or self.Food is not None or self.Poop is not None:
self.is_solid = True
def is_edible(self,agent_pos):
if self.Food is None:
return False
if self.pos[1] == agent_pos[1] - 1:
if self.LarvaAgent is None and self.LarvaTail is None:
return True
return False
def is_breatheable(self,agent_pos):
if self.pos[1] == agent_pos[1] + 1:
if not self.is_solid:
return True
return False
def is_moveable(self):
if not self.is_solid:
return True
return False
def is_swapable(self,agent_pos):
#if cell contains food or poop and no larva, let larva swap position to here
pass
def is_diffusible(self,type):
if getattr(self,type) is not None:
return True
return False
def __cell_contents(self):
return self.model.grid.get_cell_list_contents(self.pos)
class ExtendedAgent(Agent):
def __init__(self, unique_id: int, model: Model):
super().__init__(unique_id, model)
self.kill_flag = False
self.type = None
self.enter_count = 0
def randindex(self,length=1):
#returns a rand index of a list of given size
return random.randint(0,length-1)
def get_neighborhood(self,pos=None,center=False,moore=False):
if pos is None:
pos = self.pos
neighborhood = self.__get_neighborhood_object(pos,center,moore)
return neighborhood
def __get_neighborhood_object(self,pos,center,moore):
#do checks in dictionary using pos tuple as key
#or maybe only keep cellObj in dictionary
return NeighborHoodObject(self.model,pos,center,moore)
def get_position(self):
return self.pos
def neighbors(self, pos=None, center=False, moore=True):
if (pos == None):
pos = self.pos
if isinstance(pos, (ExtendedAgent, LarvaAgent, Agent, LarvaTail, Oxygen, Food)):
pos = pos.pos
neighbors = self.model.grid.get_neighborhood(
pos,
moore=moore,
include_center=center)
return neighbors
def neighbor_contents(self, pos=None, neighbors=None, center=False, moore=True):
if pos == None and neighbors == None:
return list()
if pos == None:
pos = self.pos
if neighbors == None:
neighbors = self.neighbors(pos, center, moore)
return self.model.grid.get_cell_list_contents(neighbors)
def cell_contents(self, pos=None):
if pos == None:
pos = self.pos
return self.model.grid.get_cell_list_contents(pos)
def is_solid(self, pos):
contents = self.cell_contents(pos)
bool = False
for agent in contents:
if isinstance(agent, Food):
bool = True
break
if isinstance(agent, LarvaAgent):
bool = True
break
if isinstance(agent, LarvaTail):
bool = True
break
return bool
def is_food(self,pos):
contents = self.cell_contents(pos)
for agent in contents:
if isinstance(agent, Food):
return True
def is_poop(self, pos):
contents = self.cell_contents(pos)
for agent in contents:
if isinstance(agent, Poop):
return True
def get_food2(self,pos):
pass
def get_food(self, pos):
contents = self.cell_contents(pos)
for agent in contents:
if isinstance(agent, Food) and not hasattr(agent,'is_poop'):
return agent
return None
#def get_food(self,cell_obj: CellObject):
# return cell_obj.Food
def get_poop(self, pos):
contents = self.cell_contents(pos)
for agent in contents:
if isinstance(agent, Poop):
return agent
return None
def _remove_agent(self, agent):
agent.die()
def die(self):
#print(str(self), "running self.die")
if self.kill_flag == False:
self.kill_flag = True
self.model.kill_agents.append(self)
return True
return False
def step(self):
pass
class LarvaAgent(ExtendedAgent):
def __init__(self,
unique_id: int,
model: Model,
type=None,
next=None,
pos=None):
super().__init__(unique_id, model)
self.LARVA_SIZE = 1
self.EAT_AMOUNT = self.model.larva_params["eat_amount"]
self.EAT_EFFICIENCY = self.model.larva_params["eat_efficiency"]
self.BREATHE_AMOUNT = self.model.larva_params["breathe_amount"]
self.MAX_FOOD = 6 #6
self.MAX_OXY = 12 #6
self.LOW_FOOD = 0
self.LOW_OXY = 0
self.POOP_THRESHOLD = 5
for param in self.model.larva_params:
setattr(self, param.upper(), self.model.larva_params[param])
self.type = type
self.food = 3.0
self.oxy = 2.0
self.staged_move = None
self.eating = None
self.times_pooped = 0
self.poop = 0
self.end = False
self.previous = None
self.next = None
#the agent that the larva is currently watching, if any
self.watching = None
#create tail agents here
previous_agent = self
for i in range(self.LARVA_SIZE-1):
endv = False
if i+2 == self.LARVA_SIZE:
endv = True
id = str(self.unique_id) + str(i*5) + str(1000) + str(i+1)
agent = LarvaTail(id, self.model, self.type, self,
previous_agent, next=None, end=endv)
previous_agent.next = agent
previous_agent = agent
#stage moves for all elements in the larva then exucute those moves using a different function
def stage(self, cell, location):
if not cell == None:
#self.staged_move = cell
if location == 0:
#go forward
if (not (self.next == None)):
move = self.pos
self.next.stage(move, location)
#self.staged_move = cell
else:
if not self.previous == None:
#self.staged_move = cell
move = self.pos
self.previous.stage(move, location)
#self.staged_move = cell
self.staged_move = cell
"""
Stages changes for tail agents so that they can be executed at once and move with the head without detachment
"""
def execute(self, direction=0):
#print("execute running with: ", self.pos,"direction = ",direction)
#print("staged_move is: " + str(self.staged_move))
cell = self.staged_move
#print("executing move from",self.pos,"to",cell)
#if isinstance(self, LarvaTail) and self.end == False:
#self.move_to(cell) # ,suppress_restrictions=True)
#else:
self.move_to(cell) # ,suppress_restrictions=True)
if direction == 0 and (not self.next == None):
self.next.execute(direction)
elif direction == 1 and (not self.previous == None):
self.previous.execute(direction)
def sense(self):
#senses nearby agents and chooses a nearby agent to watch
#given the set of neighbors, determine nearby agents
pass
def __get_food_reward(self, amount, food_agent_amount):
r = 0
eat_amount = min(amount, food_agent_amount)
r = round(min(self.MAX_FOOD, eat_amount), 4)
return r
def __get_oxy_reward(self, amount):
r = 0
r = round(min(self.MAX_OXY, amount), 4)
return r
def add_food(self, amount):
self.food = min(self.food+amount, self.MAX_FOOD)
#print("Food is now:", str(self.food))
def add_oxy(self, amount):
self.oxy = min(self.oxy+amount, self.MAX_OXY)
def __validate_cell(self, cell):
#Validates moves to cells
contents = self.model.grid.get_cell_list_contents(cell)
if (contents == None or len(contents) == 0 or self.model.grid.is_cell_empty(cell)):
return True
if len(contents) > 1:
for i in range(len(contents)):
if self.is_solid(contents[i]):
return False
def get_reward(self, content, action):
#given a move, determine the reward for the move
reward_food = 0
reward_oxy = 0
if action == "move":
reward_food = 0
reward_oxy = 0
elif action == "action:breathe" or action == "action:breathe+move":
reward_food = 0
reward_oxy = self.__get_oxy_reward(self.BREATHE_AMOUNT)
elif action == "action:eat":
reward_food = self.__get_food_reward(self.EAT_AMOUNT, content.amount)
reward_oxy = 0
elif action == "wait":
reward_food = 0
reward_oxy = 0
return Reward(reward_food, reward_oxy)
def get_possible_steps(self, location=None):
#first find the pos to use based on location.
# location == None or location == 0 imply using self.pos
# location == 1 should use the tail pos
pos = None
agent = self.get_agent(location)
agent_pos = self.get_agent_pos(location)
#Now get the neighborhood around pos
neighbors = self.neighbors(agent_pos)
neighbors_contents = self.neighbor_contents(neighbors=neighbors)
#define the empty list that will hold all possible moves
possible_steps = list()
def create_move(self, content, pos, action, location):
#find cost, reward, and value
cost = self.get_cost(action, location)
reward = self.get_reward(content, action)
value = self.__get_value(cost, reward)
#Create the move using the above values and the move namedtuple
move = Move(pos, action, location, cost, reward, value)
return move
def is_actionable(self, pos, content):
# finds if a given position is actionable by iterating through its contents and returing false if any
# contents prevent movement.
def guard_conditions(self, pos, content):
# if this conditon returns false then the cell contains invalid contents
def conditions(self, pos, content):
#the conditions to check for
if (content == None or self.model.grid.is_cell_empty(pos)):
return False
if isinstance(content, (LarvaAgent, LarvaTail)):
return False
else:
return True
if isinstance(content, list):
actionable = True
for element in content:
if conditions(self, pos, element) == False:
actionable = False
return actionable
else:
return conditions(self, pos, content)
if guard_conditions(self, pos, content) == False:
return False
else:
return True
def get_action(self, pos, content, location, agent_pos):
#create an empty list to append poterntial actions to
action_list = list()
if isinstance(content, Oxygen) and content.get_traversable():
action = "move"
action_list.append(action)
# append another for breathing at tail
if location == 1 and content.depleted == False and pos[1] >= agent_pos[1]:
action = "action:breathe"
#If the position differs
if not pos == agent_pos:
action = "action:breathe+move"
action_list.append(action)
if isinstance(content, Food) and location == 0 and pos[1] <= agent_pos[1]:
action = "action:eat"
action_list.append(action)
return action_list
move_list = list()
for cell, contents in zip(neighbors, neighbors_contents):
if is_actionable(self, cell, contents):
#go through each elements in contents and create a move for it
if isinstance(contents, list):
for element in contents:
action_list = get_action(
self, cell, element, location, agent_pos)
if (not action_list == None) and len(action_list) >= 1:
for action in action_list:
move = create_move(
self, element, cell, action, location)
move_list.append(move)
else:
action_list = get_action(
self, cell, contents, location, agent_pos)
if (not action_list == None) and len(action_list) >= 1:
for action in action_list:
move = create_move(self, contents, cell, action, location)
move_list.append(move)
if not move_list == None:
possible_steps.extend(move_list)
return possible_steps
def __get_value(self, cost, reward):
def greedy_func(move_cost, move_reward, current_amount, buffer):
food_cost = move_cost[0]
food_reward = move_reward[0]
food_amount = current_amount[0]
food_buffer = buffer[0]
food_value = (food_amount + (food_reward - food_cost)) / food_amount
oxy_cost = move_cost[1]
oxy_reward = move_reward[1]
oxy_amount = current_amount[1]
oxy_buffer = buffer[1]
oxy_value = (oxy_amount + (oxy_reward - oxy_cost)) / oxy_amount
value = round((food_value + oxy_value)/2, 4)
return value
current_amount = (self.food, self.oxy)
buffer = (0.2, 0.2)
return greedy_func(cost, reward, current_amount, buffer)
def rank(self, head_moves=None, tail_moves=None):
#ranks the moves according to their value, computed by the value function
move_list = list()
if head_moves == None and tail_moves == None:
return None
if not tail_moves == None:
for move in tail_moves:
move_list.append(move)
if not head_moves == None:
for move in head_moves:
move_list.append(move)
move_list.sort(reverse=True, key=lambda move: move.value)
return move_list
def update_values(self):
self.BREATHE_AMT = self.model.larva_params.breathe_amount
self.EAT_AMOUNT = self.model.larva_params.eat_amount
pass
def __get_tail_pos(self):
agent = self
while not agent.next == None:
#Returns the tail as agent
agent = agent.next
return agent.pos
def get_cost(self, action, location=None):
#use location to make moves going up cost more
OXY_COST = 0
FOOD_COST = 0
if location == None:
location = 0
if action == "move":
OXY_COST = 0.3
FOOD_COST = 0.4
elif action == "action:eat":
OXY_COST = 0.3
FOOD_COST = 0.5
elif action == "action:breathe":
OXY_COST = 0.1
FOOD_COST = 0.2
elif action == "action:breathe+move":
OXY_COST = 0.1 + 0.3 # +1.0
FOOD_COST = 0.2 + 0.4 # +1.0
elif action == "wait":
OXY_COST = 0.1
FOOD_COST = 0.1
#also do sqrt(2) (apprx) multiplication to moves that are diagonal
if location == 1 and not action == "action:breathe":
#to simulate gravity
OXY_COST *= 1.2
FOOD_COST *= 1.2
return Cost(FOOD_COST, OXY_COST)
def breathe(self):
agent = self
if isinstance(self, LarvaTail):
agent = self.agent
#agent.oxy = self.add_oxy(self.BREATHE_AMOUNT)
self.add_oxy(self.BREATHE_AMOUNT)
def get_movement_cost(self):
#gets the cost to move from the current position to pos
OXY_COST = 0.5
FOOD_COST = 1
return (FOOD_COST, OXY_COST)
def apply_cost(self, action):
agent = self
if isinstance(self, LarvaTail):
agent = self.agent
cost = agent.get_cost(action)
agent.food -= cost[0]
agent.oxy -= cost[1]
def do_action2(self, move):
agent = self
if move.location == 1:
agent = self.get_agent(1)
elif move.location == 0:
agent = self.get_agent(0)
if not agent == self:
agent.do_action2(move)
return
pass
def do_action(self, action, cell, location, agent=None):
#give content with the move
#just give this the move
success = False
#direction = False
#if location == 0:
#direction = True
if not agent == None:
#then go to that agent to start
agent.do_action(action, cell, location)
return
#agent_pos = self.pos
#if location == 1:
# agent_pos = self.__get_tail_pos()
#send content of cell and agent pos
if action == "move": # ACCOUNT FOR BACKWARDS MOVEMENT???
#if height does not chnage then simply shift the head
testagent = self
if location == 1:
testagent = self.get_agent(location)
testagent.stage(cell, location)
testagent.execute(location)
pass
elif action == "action:eat" and location == 0:
contents = self.cell_contents(cell)
if len(contents) >= 1: # Need to deal with contents not being a list
for agent in contents:
if isinstance(agent, Food):
self.eat(agent)
success = True
if agent.amount <= 0:
self.stage(cell, location)
self.execute(location)
break
elif action == "action:breathe":
if location == 1:
self.breathe()
success = True
elif action == "action:breathe+move":
if location == 1:
self.stage(cell, location)
self.execute(location)
self.breathe()
success = True
elif action == "wait":
success = True
pass
if success:
self.apply_cost(action)
def die(self):
#print(str(self), "running self.die")
if not self.next == None:
self.next.die()
#overriding the overriden method
#ExtendedAgent.die()
#print("DIEDIE")
if self.kill_flag == False:
self.kill_flag = True
self.model.kill_agents.append(self)
return True
return False
def __check_conditions(self):
if self.food <= 0 or self.oxy <= 0:
return True
return False
def step(self):
if self.__check_conditions():
self.die()
return
if self.poop > self.POOP_THRESHOLD:
self.create_poop(self.poop)
self.poop = 0
neighborhood_object = self.get_neighborhood(self.pos,center=False,moore=True)
pos = None
move = None
if self.oxy <= 3.0:
#Low oxy
#change to returing breathible rather than storing it?
oxy_moves = neighborhood_object.get_breatheable()#breatheable_list
if len(oxy_moves) > 0:
self.breathe()
self.food -= 0.1
return
else:
#if we are here then there are no empty spaces above the agent so we must try and swap positions
swap_moves = neighborhood_object.get_swapable(direction="up")
if len(swap_moves) > 0:
#choose one randomly and start eating
index = self.randindex(len(swap_moves))
#pos = swap_moves[index].pos
cellObj = swap_moves[index]
#print(str(cellObj))
if cellObj.Poop is not None:
self.swap_position(cellObj.Poop)
elif cellObj.Food is not None:
self.swap_position(cellObj.Food)
self.oxy -= 0.3
self.food -= 0.3
return
pass
else:
#try to eat
if not self.eating is None and self.eating.amount > 0:
self.eat(self.eating)
#need to also do a check if eating is in the neighborhood ****************************
#self.oxy -=1 #masybe remove this to have it simulate enzymes
return
else:
eat_moves = neighborhood_object.get_edible()
if len(eat_moves) > 0:
#choose one randomly and start eating
index = self.randindex(len(eat_moves))
food_agent = eat_moves[index].Food
self.eating = food_agent
self.eat(food_agent)
self.oxy -= 1
return
else:
#try and go down
down_moves = neighborhood_object.get_moveable(direction="down")
if len(down_moves) > 0:
index = self.randindex(len(down_moves))
pos = down_moves[index].pos
self.oxy -= 0.1
self.food -= 0.1
else:
swap_moves = neighborhood_object.get_swapable(direction="down")
if len(swap_moves) > 0:
index = self.randindex(len(swap_moves))
cellObj = swap_moves[index]
print(str(cellObj))
if cellObj.Poop is not None:
self.swap_position(cellObj.Poop)
elif cellObj.Food is not None:
self.swap_position(cellObj.Food)
self.oxy -= 0.2
self.food -= 0.2
return
else:
pass
if (not pos == None) and (not self.model.grid.out_of_bounds(pos)):
move = pos
if move is None or self.is_solid(move):
move_list = neighborhood_object.get_moveable()
if len(move_list) > 0:
index = self.randindex(len(move_list))
move = move_list[index].pos
self.oxy -= 0.1
self.food -= 0.1
self.move_to(move)
def stepbac(self):
#first check if death conditions are valid, if so add to kill list and early return
self.update_values()
#print(self.BREATHE_AMOUNT)
#print(self.food)
#print(self.oxy)
if self.__check_conditions():
self.die()
return
if self.poop > self.POOP_THRESHOLD:
self.create_poop(self.poop)
self.poop = 0
neighbors = self.neighbors()#self.neighbor_contents()
contents = self.neighbor_contents(neighbors = neighbors)
move_list = list()
for pos,content in zip(neighbors,contents):
move_list.append(PotentialMove(pos,content))
#if isinstance(neighbor,list()):
#for element in neighbor:
#if self.oxy < self.LOW_OXY:
#index = random.randint(0, len(move_list) - 1)
#move = move_list[index]
#self.move_to(move)
#Now search through the list according to parameters
move = None
pos = None
if self.oxy < 3.0:
#print("LowO")
#pos = (self.pos[0], self.pos[1] + 1)
neighbors = self.neighbors(moore=False)
contents = self.neighbor_contents(neighbors = neighbors)
#check that there is oxygen to breathe
contains_oxy = False
for neighbor,contents in zip(neighbors,contents):
if contains_oxy is True:
break
if self.is_solid(neighbor):
continue
if isinstance(contents, list):
for element in contents:
if isinstance(element,Oxygen):
contains_oxy = True
break
if isinstance(contents, Oxygen):
contains_oxy = True
break