diff --git a/mesa/examples/advanced/wolf_sheep/agents.py b/mesa/examples/advanced/wolf_sheep/agents.py index e262996fcff..256e7e62c80 100644 --- a/mesa/examples/advanced/wolf_sheep/agents.py +++ b/mesa/examples/advanced/wolf_sheep/agents.py @@ -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 diff --git a/mesa/examples/advanced/wolf_sheep/model.py b/mesa/examples/advanced/wolf_sheep/model.py index 0a89a5a47a9..df12f112c92 100644 --- a/mesa/examples/advanced/wolf_sheep/model.py +++ b/mesa/examples/advanced/wolf_sheep/model.py @@ -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: