Skip to content

Commit 2ba3032

Browse files
Remove PropertyLayer usage from mesa-examples (mesa#334)
1 parent b9f68c8 commit 2ba3032

File tree

3 files changed

+17
-28
lines changed

3 files changed

+17
-28
lines changed
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import numpy as np
22
from mesa import Model
33
from mesa.datacollection import DataCollector
4-
from mesa.discrete_space import PropertyLayer
54
from scipy.signal import convolve2d
65

76

87
# fmt: off
98
class GameOfLifeModel(Model):
109
def __init__(self, width=10, height=10, alive_fraction=0.2):
1110
super().__init__()
12-
# Initialize the property layer for cell states
13-
self.cell_layer = PropertyLayer(
14-
"cells", (width, height), default_value=False, dtype=bool
11+
self.cell_layer_data = np.random.choice(
12+
[True, False],
13+
size=(width, height),
14+
p=[alive_fraction, 1 - alive_fraction],
1515
)
16-
# Randomly set cells to alive
17-
self.cell_layer.data = np.random.choice([True, False], size=(width, height), p=[alive_fraction, 1 - alive_fraction])
18-
1916
# Metrics and datacollector
20-
self.cells = width * height
17+
self.total_cells = width * height
2118
self.alive_count = 0
2219
self.alive_fraction = 0
2320
self.datacollector = DataCollector(
@@ -36,19 +33,19 @@ def step(self):
3633
# Count neighbors using convolution.
3734
# convolve2d applies the kernel to each cell of the grid, summing up the values of neighbors.
3835
# boundary="wrap" ensures that the grid wraps around, simulating a toroidal surface.
39-
neighbor_count = convolve2d(self.cell_layer.data, kernel, mode="same", boundary="wrap")
36+
neighbor_count = convolve2d(self.cell_layer_data, kernel, mode="same", boundary="wrap")
4037

4138
# Apply Game of Life rules:
4239
# 1. A live cell with 2 or 3 live neighbors survives, otherwise it dies.
4340
# 2. A dead cell with exactly 3 live neighbors becomes alive.
4441
# These rules are implemented using logical operations on the grid.
45-
self.cell_layer.data = np.logical_or(
46-
np.logical_and(self.cell_layer.data, np.logical_or(neighbor_count == 2, neighbor_count == 3)),
42+
self.cell_layer_data = np.logical_or(
43+
np.logical_and(self.cell_layer_data, np.logical_or(neighbor_count == 2, neighbor_count == 3)),
4744
# Rule for live cells
48-
np.logical_and(~self.cell_layer.data, neighbor_count == 3) # Rule for dead cells
45+
np.logical_and(~self.cell_layer_data, neighbor_count == 3) # Rule for dead cells
4946
)
5047

5148
# Metrics
52-
self.alive_count = np.sum(self.cell_layer.data)
53-
self.alive_fraction = self.alive_count / self.cells
49+
self.alive_count = np.sum(self.cell_layer_data)
50+
self.alive_fraction = self.alive_count / self.total_cells
5451
self.datacollector.collect(self)

examples/hex_ant/model.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def _init_environment(self):
5757
# Create the Nest in the center
5858
center = (self.grid.width // 2, self.grid.height // 2)
5959
# Spike the 'home' pheromone at the nest so ants can find it initially
60-
self.grid.pheromone_home.data[center] = 1.0
60+
self.grid.pheromone_home[center] = 1.0
6161
# Mark the home location
62-
self.grid.home.data[center] = 1
62+
self.grid.home[center] = 1
6363

6464
# Scatter some Food Sources
6565
# Create 3 big clusters of food
@@ -101,10 +101,7 @@ def _update_pheromone_layer(self, layer_name):
101101
"""
102102
Apply evaporation to a pheromone layer.
103103
"""
104-
layer = getattr(self.grid, layer_name)
105-
106-
# Evaporation
107-
np_layer = layer.data
104+
np_layer = self.grid.property_layers[layer_name]
108105
np_layer *= 1.0 - self.evaporation_rate
109106

110107
# Clamp to 0 to prevent negative values

examples/termites/termites/model.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from mesa import Model
2-
from mesa.discrete_space import OrthogonalMooreGrid, PropertyLayer
2+
from mesa.discrete_space import OrthogonalMooreGrid
33

44
from .agents import Termite
55

@@ -25,18 +25,13 @@ def __init__(
2525

2626
self.grid = OrthogonalMooreGrid((width, height), torus=True, random=self.random)
2727

28-
self.wood_chips_layer = PropertyLayer(
29-
"woodcell", (width, height), default_value=False, dtype=bool
30-
)
31-
32-
# Randomly distribute wood chips, by directly modifying the layer's underlying ndarray
33-
self.wood_chips_layer.data = self.rng.choice(
28+
wood_chips = self.rng.choice(
3429
[True, False],
3530
size=(width, height),
3631
p=[self.wood_chip_density, 1 - self.wood_chip_density],
3732
)
3833

39-
self.grid.add_property_layer(self.wood_chips_layer)
34+
self.grid.add_property_layer("woodcell", wood_chips)
4035

4136
# Create agents and randomly distribute them over the grid
4237
Termite.create_agents(

0 commit comments

Comments
 (0)