Skip to content

Commit 28d80c8

Browse files
committed
2 more examples moved
1 parent ecedb8f commit 28d80c8

File tree

4 files changed

+39
-46
lines changed

4 files changed

+39
-46
lines changed
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
import mesa
1+
from mesa.experimental.cell_space import FixedAgent
22

33

4-
class TreeCell(mesa.Agent):
4+
class TreeCell(FixedAgent):
55
"""
66
A tree cell.
77
88
Attributes:
9-
x, y: Grid coordinates
109
condition: Can be "Fine", "On Fire", or "Burned Out"
11-
unique_id: int
1210
1311
"""
1412

15-
def __init__(self, model):
13+
def __init__(self, model, cell):
1614
"""
1715
Create a new tree.
1816
Args:
1917
model: standard model reference for agent.
2018
"""
2119
super().__init__(model)
2220
self.condition = "Fine"
21+
self.cell = cell
2322

2423
def step(self):
2524
"""
2625
If the tree is on fire, spread it to fine trees nearby.
2726
"""
2827
if self.condition == "On Fire":
29-
for neighbor in self.model.grid.iter_neighbors(self.pos, True):
28+
for neighbor in self.cell.neighborhood.agents:
3029
if neighbor.condition == "Fine":
3130
neighbor.condition = "On Fire"
3231
self.condition = "Burned Out"

examples/forest_fire/forest_fire/model.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import mesa
22

3+
from mesa.experimental.cell_space import OrthogonalMooreGrid
4+
35
from .agent import TreeCell
46

57

@@ -8,19 +10,19 @@ class ForestFire(mesa.Model):
810
Simple Forest Fire model.
911
"""
1012

11-
def __init__(self, width=100, height=100, density=0.65):
13+
def __init__(self, width=100, height=100, density=0.65, seed=None):
1214
"""
1315
Create a new forest fire model.
1416
1517
Args:
1618
width, height: The size of the grid to model
1719
density: What fraction of grid cells have a tree in them.
1820
"""
19-
super().__init__()
20-
# Set up model objects
21+
super().__init__(seed=seed)
2122

22-
self.grid = mesa.space.SingleGrid(width, height, torus=False)
23+
# Set up model objects
2324

25+
self.grid = OrthogonalMooreGrid((width, height), capacity=1)
2426
self.datacollector = mesa.DataCollector(
2527
{
2628
"Fine": lambda m: self.count_type(m, "Fine"),
@@ -30,14 +32,13 @@ def __init__(self, width=100, height=100, density=0.65):
3032
)
3133

3234
# Place a tree in each cell with Prob = density
33-
for contents, (x, y) in self.grid.coord_iter():
35+
for cell in self.grid.all_cells:
3436
if self.random.random() < density:
3537
# Create a tree
36-
new_tree = TreeCell(self)
38+
new_tree = TreeCell(self, cell)
3739
# Set all trees in the first column on fire.
38-
if x == 0:
40+
if cell.coordinate[0] == 0:
3941
new_tree.condition = "On Fire"
40-
self.grid.place_agent(new_tree, (x, y))
4142

4243
self.running = True
4344
self.datacollector.collect(self)
@@ -59,8 +60,4 @@ def count_type(model, tree_condition):
5960
"""
6061
Helper method to count trees in a given condition in a given model.
6162
"""
62-
count = 0
63-
for tree in model.agents:
64-
if tree.condition == tree_condition:
65-
count += 1
66-
return count
63+
return len(model.agents.select(lambda x: x.condition == tree_condition))
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,58 @@
11
import mesa
22

3+
from mesa.experimental.cell_space import FixedAgent
34

4-
class Cell(mesa.Agent):
5+
6+
class Cell(FixedAgent):
57
"""Represents a single ALIVE or DEAD cell in the simulation."""
68

79
DEAD = 0
810
ALIVE = 1
911

10-
def __init__(self, pos, model, init_state=DEAD):
12+
def __init__(self, cell, model, init_state=DEAD):
1113
"""
1214
Create a cell, in the given state, at the given x, y position.
1315
"""
1416
super().__init__(model)
15-
self.x, self.y = pos
17+
self.cell = cell
1618
self.state = init_state
17-
self._nextState = None
18-
self.isConsidered = False
19+
self._next_state = None
20+
self.is_considered = False
1921

2022
@property
2123
def is_alive(self):
2224
return self.state == self.ALIVE
2325

24-
@property
25-
def neighbors(self):
26-
return self.model.grid.iter_neighbors((self.x, self.y))
27-
2826
@property
2927
def considered(self):
30-
return self.isConsidered is True
28+
return self.is_considered is True
3129

3230
def determine_state(self):
3331
"""
3432
Compute if the cell will be dead or alive at the next tick. A dead
3533
cell will become alive if it has only one neighbor. The state is not
36-
changed here, but is just computed and stored in self._nextState,
34+
changed here, but is just computed and stored in self._next_state,
3735
because our current state may still be necessary for our neighbors
3836
to calculate their next state.
3937
When a cell is made alive, its neighbors are able to be considered
4038
in the next step. Only cells that are considered check their neighbors
4139
for performance reasons.
4240
"""
4341
# assume no state change
44-
self._nextState = self.state
42+
self._next_state = self.state
4543

46-
if not self.is_alive and self.isConsidered:
44+
if not self.is_alive and self.is_considered:
4745
# Get the neighbors and apply the rules on whether to be alive or dead
4846
# at the next tick.
49-
live_neighbors = sum(neighbor.is_alive for neighbor in self.neighbors)
47+
live_neighbors = sum(neighbor.is_alive for neighbor in self.cell.neighborhood.agents)
5048

5149
if live_neighbors == 1:
52-
self._nextState = self.ALIVE
53-
for a in self.neighbors:
54-
a.isConsidered = True
50+
self._next_state = self.ALIVE
51+
for a in self.cell.neighborhood.agents:
52+
a.is_considered = True
5553

5654
def assume_state(self):
5755
"""
5856
Set the state to the new computed state
5957
"""
60-
self.state = self._nextState
58+
self.state = self._next_state

examples/hex_snowflake/hex_snowflake/model.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mesa
2+
from mesa.experimental.cell_space import HexGrid
23

34
from .cell import Cell
45

@@ -15,19 +16,17 @@ def __init__(self, width=50, height=50):
1516
"""
1617
super().__init__()
1718
# Use a hexagonal grid, where edges wrap around.
18-
self.grid = mesa.space.HexSingleGrid(width, height, torus=True)
19+
self.grid = HexGrid((width, height), capacity=1, torus=True)
1920

2021
# Place a dead cell at each location.
21-
for contents, pos in self.grid.coord_iter():
22-
cell = Cell(pos, self)
23-
self.grid.place_agent(cell, pos)
22+
for entry in self.grid.all_cells:
23+
cell = Cell(entry, self)
2424

2525
# activate the center(ish) cell.
26-
centerishCell = self.grid[width // 2][height // 2]
27-
28-
centerishCell.state = 1
29-
for a in centerishCell.neighbors:
30-
a.isConsidered = True
26+
centerish_cell = self.grid[(width // 2, height // 2)]
27+
centerish_cell.agents[0].state = 1
28+
for a in centerish_cell.neighborhood.agents:
29+
a.is_considered = True
3130

3231
self.running = True
3332

0 commit comments

Comments
 (0)