Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
dd54f21
Add draft implementation for CellSpace
Corvince Jan 19, 2024
7b58d8e
add features to cell
Corvince Jan 23, 2024
aef4a8b
remove create_neighborhood_getter
Corvince Jan 23, 2024
3240a2b
update benchmark models
Corvince Jan 23, 2024
d3df44e
try to import Grid directly from experimental
Corvince Jan 23, 2024
c80a0bd
adds a pythonic WolfSheep implementation
quaquel Jan 24, 2024
93d7db5
replace radius check with value error in neighborhood
quaquel Jan 24, 2024
be7b843
minor further code cleanup of wolfsheep
quaquel Jan 24, 2024
bd788ec
add HexGrid
quaquel Jan 24, 2024
cb0ef18
typo fix
quaquel Jan 24, 2024
94f06b1
various updates
quaquel Jan 27, 2024
be6c1af
Merge remote-tracking branch 'upstream/main' into experimental/cell-s…
quaquel Jan 27, 2024
875d0ed
add NetworkGrid
quaquel Jan 27, 2024
e4d121d
add NetworkGrid
quaquel Jan 27, 2024
5769a05
change how empties is handled
quaquel Jan 27, 2024
82a64d9
further cleanup of handling of empties
quaquel Jan 27, 2024
5fdc787
correct handling of radius in Schelling large
quaquel Jan 27, 2024
152995c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 27, 2024
c524034
Merge remote-tracking branch 'upstream/main' into experimental/cell-s…
quaquel Jan 28, 2024
972ecc4
Update cell_space.py
quaquel Jan 28, 2024
a656d93
change from Random to random
quaquel Jan 28, 2024
8df9276
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 28, 2024
f4a1089
small fixes and default capacity=None
Corvince Jan 28, 2024
1ebcf06
initial tests for cell_space
quaquel Jan 29, 2024
d362bae
some additional tests
quaquel Jan 29, 2024
c7d82d0
additional unit tests
quaquel Jan 30, 2024
b2842c0
restructure files and folders
Corvince Jan 31, 2024
740f003
various updates
quaquel Feb 2, 2024
2d8fcb5
additional tests and temporary fix for select_random_empty_cell
quaquel Feb 9, 2024
99ff668
Merge branch 'main' into experimental/cell-space
quaquel Feb 9, 2024
565ae0a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 9, 2024
ab87b70
improved annotations
quaquel Feb 9, 2024
ab13bbc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 9, 2024
8f2e3dd
Update discrete_space.py
quaquel Feb 9, 2024
ac8ff31
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel Feb 15, 2024
aa03962
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 15, 2024
9b28f9d
correct handling of seeds when running examples in issolation
quaquel Feb 16, 2024
bbca3ac
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel Feb 16, 2024
fe5a93f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 16, 2024
2c80330
added docstrings
quaquel Feb 17, 2024
c79aaf0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 17, 2024
8384cf1
reformating and minor update to tests
quaquel Feb 17, 2024
253531c
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel Feb 17, 2024
ebdee8e
Add optional neighborhood_func to Grid class
Corvince Feb 17, 2024
bb2fc52
allow n-dimensional grids
Corvince Feb 17, 2024
b3efdee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 17, 2024
8c7ad05
add validation, remove neighborhood_func, add tests
Corvince Feb 18, 2024
819e903
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 18, 2024
0f49dc7
Make Cell* generic
Corvince Feb 19, 2024
da94fb1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 19, 2024
b210686
Make Cell* generic
Corvince Feb 19, 2024
c35591c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 19, 2024
065e5ea
fixes to tests
quaquel Feb 19, 2024
1bfa009
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 19, 2024
6b1c454
type hint 3.9 fix in network.py
quaquel Feb 19, 2024
e0d5673
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel Feb 19, 2024
cf789f2
fix type checking
Corvince Feb 19, 2024
50666c1
update capacity, add annotations import
Corvince Feb 19, 2024
e520d3c
fix wolf_sheep
Corvince Feb 20, 2024
85251f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 20, 2024
3a247cb
fix wolf_sheep
Corvince Feb 20, 2024
86127f2
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel Feb 20, 2024
fb19879
seperate code path for 2d and nd grids when connecting cells
quaquel Feb 21, 2024
5ea5d35
seed as kwarg
quaquel Feb 21, 2024
b7ac86c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 21, 2024
76ca4a5
code formatting fix
quaquel Feb 21, 2024
45e3e9f
testing
quaquel Feb 22, 2024
3d8cdfd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 22, 2024
ce8ca0b
Merge branch 'main' into experimental/cell-space
quaquel Feb 22, 2024
18bd2a3
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel Feb 22, 2024
62c844e
minor docstring update
quaquel Feb 23, 2024
adff7aa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 23, 2024
7d5e6b0
Merge branch 'main' into experimental/cell-space
EwoutH Feb 27, 2024
b32af9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 27, 2024
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
12 changes: 5 additions & 7 deletions benchmarks/Schelling/schelling.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from mesa import Agent, Model
from mesa.space import SingleGrid
from mesa.experimental.cell_space import Grid
from mesa.time import RandomActivation


Expand All @@ -21,9 +21,7 @@ def __init__(self, unique_id, model, agent_type):

def step(self):
similar = 0
for neighbor in self.model.grid.iter_neighbors(
self.pos, moore=True, radius=self.model.radius
):
for neighbor in self.cell.neighborhood().agents:
if neighbor.type == self.type:
similar += 1

Expand Down Expand Up @@ -52,19 +50,19 @@ def __init__(
self.radius = radius

self.schedule = RandomActivation(self)
self.grid = SingleGrid(height, width, torus=True)
self.grid = Grid(height, width, torus=True)

self.happy = 0

# Set up agents
# We use a grid iterator that returns
# the coordinates of a cell as well as
# its contents. (coord_iter)
for _cont, pos in self.grid.coord_iter():
for cell in self.grid:
if self.random.random() < self.density:
agent_type = 1 if self.random.random() < self.minority_pc else 0
agent = SchellingAgent(self.next_id(), self, agent_type)
self.grid.place_agent(agent, pos)
cell.add_agent(agent)
self.schedule.add(agent)

def step(self):
Expand Down
25 changes: 11 additions & 14 deletions benchmarks/WolfSheep/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ def step(self):
self.energy -= 1

# If there is grass available, eat it
this_cell = self.model.grid.get_cell_list_contents(self.pos)
grass_patch = next(obj for obj in this_cell if isinstance(obj, GrassPatch))
grass_patch = next(
obj for obj in self.cell.agents if isinstance(obj, GrassPatch)
)
if grass_patch.fully_grown:
self.energy += self.model.sheep_gain_from_food
grass_patch.fully_grown = False

# Death
if self.energy < 0:
self.model.grid.remove_agent(self)
self.cell.remove_agent(self)
self.model.schedule.remove(self)
elif self.random.random() < self.model.sheep_reproduce:
# Create a new sheep:
self.energy /= 2
lamb = Sheep(
self.model.next_id(), self.pos, self.model, self.moore, self.energy
self.model.next_id(), None, self.model, self.moore, self.energy
)
self.model.grid.place_agent(lamb, self.pos)
self.cell.add_agent(lamb)
self.model.schedule.add(lamb)


Expand All @@ -58,28 +59,24 @@ def step(self):
self.energy -= 1

# If there are sheep present, eat one
x, y = self.pos
this_cell = self.model.grid.get_cell_list_contents([self.pos])
sheep = [obj for obj in this_cell if isinstance(obj, Sheep)]
sheep = [obj for obj in self.cell.agents if isinstance(obj, Sheep)]
if len(sheep) > 0:
sheep_to_eat = self.random.choice(sheep)
self.energy += self.model.wolf_gain_from_food

# Kill the sheep
self.model.grid.remove_agent(sheep_to_eat)
self.cell.remove_agent(sheep_to_eat)
self.model.schedule.remove(sheep_to_eat)

# Death or reproduction
if self.energy < 0:
self.model.grid.remove_agent(self)
self.cell.remove_agent(self)
self.model.schedule.remove(self)
elif self.random.random() < self.model.wolf_reproduce:
# Create a new wolf cub
self.energy /= 2
cub = Wolf(
self.model.next_id(), self.pos, self.model, self.moore, self.energy
)
self.model.grid.place_agent(cub, cub.pos)
cub = Wolf(self.model.next_id(), None, self.model, self.moore, self.energy)
self.cell.add_agent(cub)
self.model.schedule.add(cub)


Expand Down
7 changes: 3 additions & 4 deletions benchmarks/WolfSheep/random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"""

from mesa import Agent
from mesa.experimental.cell_space import Cell


class RandomWalker(Agent):
cell: Cell
"""
Class implementing random walker methods in a generalized manner.

Expand All @@ -31,7 +33,4 @@ def random_move(self):
Step one cell in any allowable direction.
"""
# Pick the next cell from the adjacent cells.
next_moves = self.model.grid.get_neighborhood(self.pos, self.moore, True)
next_move = self.random.choice(next_moves)
# Now move:
self.model.grid.move_agent(self, next_move)
self.cell.neighborhood().select_random_cell().add_agent(self)
28 changes: 11 additions & 17 deletions benchmarks/WolfSheep/wolf_sheep.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

import mesa
from mesa.space import MultiGrid
from mesa.experimental.cell_space import Grid
from mesa.time import RandomActivationByType

from .agents import GrassPatch, Sheep, Wolf
Expand Down Expand Up @@ -63,40 +63,34 @@ def __init__(
self.sheep_gain_from_food = sheep_gain_from_food

self.schedule = RandomActivationByType(self)
self.grid = MultiGrid(self.height, self.width, torus=False)
self.grid = Grid(self.height, self.width, torus=False, capacity=None)

# Create sheep:
for _i in range(self.initial_sheep):
pos = (
self.random.randrange(self.width),
self.random.randrange(self.height),
)
cell = self.grid.all_cells.select_random_cell()
energy = self.random.randrange(2 * self.sheep_gain_from_food)
sheep = Sheep(self.next_id(), pos, self, True, energy)
self.grid.place_agent(sheep, pos)
sheep = Sheep(self.next_id(), None, self, True, energy)
cell.add_agent(sheep)
self.schedule.add(sheep)

# Create wolves
for _i in range(self.initial_wolves):
pos = (
self.random.randrange(self.width),
self.random.randrange(self.height),
)
cell = self.grid.all_cells.select_random_cell()
energy = self.random.randrange(2 * self.wolf_gain_from_food)
wolf = Wolf(self.next_id(), pos, self, True, energy)
self.grid.place_agent(wolf, pos)
wolf = Wolf(self.next_id(), None, self, True, energy)
cell.add_agent(wolf)
self.schedule.add(wolf)

# Create grass patches
possibly_fully_grown = [True, False]
for _agent, pos in self.grid.coord_iter():
for cell in self.grid:
fully_grown = self.random.choice(possibly_fully_grown)
if fully_grown:
countdown = self.grass_regrowth_time
else:
countdown = self.random.randrange(self.grass_regrowth_time)
patch = GrassPatch(self.next_id(), pos, self, fully_grown, countdown)
self.grid.place_agent(patch, pos)
patch = GrassPatch(self.next_id(), None, self, fully_grown, countdown)
cell.add_agent(patch)
self.schedule.add(patch)

def step(self):
Expand Down
2 changes: 2 additions & 0 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
if TYPE_CHECKING:
# We ensure that these are not imported during runtime to prevent cyclic
# dependency.
from mesa.gridspace import Cell
from mesa.model import Model
from mesa.space import Position

Expand All @@ -46,6 +47,7 @@ def __init__(self, unique_id: int, model: Model) -> None:
"""
self.unique_id = unique_id
self.model = model
self.cell: Cell | None = None
self.pos: Position | None = None

# register agent
Expand Down
Loading