Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions examples/conways_game_of_life_fast/model.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import numpy as np
from mesa import Model
from mesa.datacollection import DataCollector
from mesa.discrete_space import PropertyLayer
from scipy.signal import convolve2d


# fmt: off
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(
Expand All @@ -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)
9 changes: 3 additions & 6 deletions examples/hex_ant/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions examples/termites/termites/model.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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(
Expand Down
Loading