Skip to content

Commit ee6b6fc

Browse files
committed
use PropertyLayer to store cliff cell information
1 parent 9e00299 commit ee6b6fc

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

mesa/examples/advanced/wolf_sheep/agents.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ def feed(self):
3939
def step(self):
4040
"""Execute one step of the animal's behavior."""
4141
# Move to random neighboring cell
42-
self.cell = self.cell.neighborhood.select_random_cell()
42+
self.cell = self.cell.neighborhood.select_random_cell()
43+
is_cliff = self.model.grid.cliff.data[self.cell.coordinate[0]][self.cell.coordinate[1]]
44+
if is_cliff: # if it is a cliff, then the animal dies
45+
self.remove()
46+
return
47+
4348
self.energy -= 1
4449

4550
# Try to feed

mesa/examples/advanced/wolf_sheep/model.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ def __init__(
9292

9393
self.datacollector = DataCollector(model_reporters)
9494

95-
obstructionArr = [[False]*self.width for i in range(self.height)]
95+
cliff_arr = [[False]*self.width for i in range(self.height)]
9696

97-
obstructionCoord = set([(random.randrange(self.height), random.randrange(self.width)) for i in range(10)] ) # set is used because the random number gen might return the same coordinate
98-
for i,j in obstructionCoord:
99-
obstructionArr[i][j] = True
97+
cliff_coord = set([(random.randrange(self.height), random.randrange(self.width)) for i in range((width*height)//3)] ) # set is used because the random number gen might return the same coordinate
98+
for i,j in cliff_coord:
99+
cliff_arr[i][j] = True
100100

101-
obstructionArr = np.array(obstructionArr)
101+
cliff_arr = np.array(cliff_arr)
102102

103-
self.grid.add_property_layer(PropertyLayer.from_data("obstruction", obstructionArr))
103+
self.grid.add_property_layer(PropertyLayer.from_data("cliff", cliff_arr))
104104

105105
possibleCells = []
106106
for cell in self.grid.all_cells.cells:
107-
if (cell.coordinate[0], cell.coordinate[1]) not in obstructionCoord: # so we don't create wolf or sheep on obstructed cells
107+
if (cell.coordinate[0], cell.coordinate[1]) not in cliff_coord: # so we don't create wolf or sheep on cliff cells
108108
possibleCells.append(cell)
109109

110110
# Create sheep:

0 commit comments

Comments
 (0)