diff --git a/examples/conways_game_of_life_fast/model.py b/examples/conways_game_of_life_fast/model.py index bd7048378..c5320e1cd 100644 --- a/examples/conways_game_of_life_fast/model.py +++ b/examples/conways_game_of_life_fast/model.py @@ -1,7 +1,6 @@ import numpy as np from mesa import Model from mesa.datacollection import DataCollector -from mesa.discrete_space import PropertyLayer from scipy.signal import convolve2d @@ -9,15 +8,13 @@ class GameOfLifeModel(Model): def __init__(self, width=10, height=10, alive_fraction=0.2): super().__init__() - # Initialize the property layer for cell states - self.cell_layer = PropertyLayer( - "cells", (width, height), default_value=False, dtype=bool + self.cell_layer_data = np.random.choice( + [True, False], + size=(width, height), + p=[alive_fraction, 1 - alive_fraction], ) - # Randomly set cells to alive - self.cell_layer.data = np.random.choice([True, False], size=(width, height), p=[alive_fraction, 1 - alive_fraction]) - # Metrics and datacollector - self.cells = width * height + self.total_cells = width * height self.alive_count = 0 self.alive_fraction = 0 self.datacollector = DataCollector( @@ -36,19 +33,19 @@ def step(self): # Count neighbors using convolution. # convolve2d applies the kernel to each cell of the grid, summing up the values of neighbors. # boundary="wrap" ensures that the grid wraps around, simulating a toroidal surface. - neighbor_count = convolve2d(self.cell_layer.data, kernel, mode="same", boundary="wrap") + neighbor_count = convolve2d(self.cell_layer_data, kernel, mode="same", boundary="wrap") # Apply Game of Life rules: # 1. A live cell with 2 or 3 live neighbors survives, otherwise it dies. # 2. A dead cell with exactly 3 live neighbors becomes alive. # These rules are implemented using logical operations on the grid. - self.cell_layer.data = np.logical_or( - np.logical_and(self.cell_layer.data, np.logical_or(neighbor_count == 2, neighbor_count == 3)), + self.cell_layer_data = np.logical_or( + np.logical_and(self.cell_layer_data, np.logical_or(neighbor_count == 2, neighbor_count == 3)), # Rule for live cells - np.logical_and(~self.cell_layer.data, neighbor_count == 3) # Rule for dead cells + np.logical_and(~self.cell_layer_data, neighbor_count == 3) # Rule for dead cells ) # Metrics - self.alive_count = np.sum(self.cell_layer.data) - self.alive_fraction = self.alive_count / self.cells + self.alive_count = np.sum(self.cell_layer_data) + self.alive_fraction = self.alive_count / self.total_cells self.datacollector.collect(self) diff --git a/examples/hex_ant/model.py b/examples/hex_ant/model.py index 5fb769cea..96998d22b 100644 --- a/examples/hex_ant/model.py +++ b/examples/hex_ant/model.py @@ -57,9 +57,9 @@ def _init_environment(self): # Create the Nest in the center center = (self.grid.width // 2, self.grid.height // 2) # Spike the 'home' pheromone at the nest so ants can find it initially - self.grid.pheromone_home.data[center] = 1.0 + self.grid.pheromone_home[center] = 1.0 # Mark the home location - self.grid.home.data[center] = 1 + self.grid.home[center] = 1 # Scatter some Food Sources # Create 3 big clusters of food @@ -101,10 +101,7 @@ def _update_pheromone_layer(self, layer_name): """ Apply evaporation to a pheromone layer. """ - layer = getattr(self.grid, layer_name) - - # Evaporation - np_layer = layer.data + np_layer = self.grid.property_layers[layer_name] np_layer *= 1.0 - self.evaporation_rate # Clamp to 0 to prevent negative values diff --git a/examples/termites/termites/model.py b/examples/termites/termites/model.py index ae39e4861..1a43688fb 100644 --- a/examples/termites/termites/model.py +++ b/examples/termites/termites/model.py @@ -1,5 +1,5 @@ from mesa import Model -from mesa.discrete_space import OrthogonalMooreGrid, PropertyLayer +from mesa.discrete_space import OrthogonalMooreGrid from .agents import Termite @@ -25,18 +25,13 @@ def __init__( self.grid = OrthogonalMooreGrid((width, height), torus=True, random=self.random) - self.wood_chips_layer = PropertyLayer( - "woodcell", (width, height), default_value=False, dtype=bool - ) - - # Randomly distribute wood chips, by directly modifying the layer's underlying ndarray - self.wood_chips_layer.data = self.rng.choice( + wood_chips = self.rng.choice( [True, False], size=(width, height), p=[self.wood_chip_density, 1 - self.wood_chip_density], ) - self.grid.add_property_layer(self.wood_chips_layer) + self.grid.add_property_layer("woodcell", wood_chips) # Create agents and randomly distribute them over the grid Termite.create_agents(