Skip to content

Commit

Permalink
use PropertyLayer to store cliff cell information
Browse files Browse the repository at this point in the history
  • Loading branch information
mgemaakbar committed Dec 22, 2024
1 parent 9e00299 commit ee6b6fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 6 additions & 1 deletion mesa/examples/advanced/wolf_sheep/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ def feed(self):
def step(self):
"""Execute one step of the animal's behavior."""
# Move to random neighboring cell
self.cell = self.cell.neighborhood.select_random_cell()
self.cell = self.cell.neighborhood.select_random_cell()
is_cliff = self.model.grid.cliff.data[self.cell.coordinate[0]][self.cell.coordinate[1]]
if is_cliff: # if it is a cliff, then the animal dies
self.remove()
return

self.energy -= 1

# Try to feed
Expand Down
14 changes: 7 additions & 7 deletions mesa/examples/advanced/wolf_sheep/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ def __init__(

self.datacollector = DataCollector(model_reporters)

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

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
for i,j in obstructionCoord:
obstructionArr[i][j] = True
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
for i,j in cliff_coord:
cliff_arr[i][j] = True

obstructionArr = np.array(obstructionArr)
cliff_arr = np.array(cliff_arr)

self.grid.add_property_layer(PropertyLayer.from_data("obstruction", obstructionArr))
self.grid.add_property_layer(PropertyLayer.from_data("cliff", cliff_arr))

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

# Create sheep:
Expand Down

0 comments on commit ee6b6fc

Please sign in to comment.