-
Notifications
You must be signed in to change notification settings - Fork 1k
Add cell-centric discrete spaces (experimental) #1994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
74 commits
Select commit
Hold shift + click to select a range
dd54f21
Add draft implementation for CellSpace
Corvince 7b58d8e
add features to cell
Corvince aef4a8b
remove create_neighborhood_getter
Corvince 3240a2b
update benchmark models
Corvince d3df44e
try to import Grid directly from experimental
Corvince c80a0bd
adds a pythonic WolfSheep implementation
quaquel 93d7db5
replace radius check with value error in neighborhood
quaquel be7b843
minor further code cleanup of wolfsheep
quaquel bd788ec
add HexGrid
quaquel cb0ef18
typo fix
quaquel 94f06b1
various updates
quaquel be6c1af
Merge remote-tracking branch 'upstream/main' into experimental/cell-s…
quaquel 875d0ed
add NetworkGrid
quaquel e4d121d
add NetworkGrid
quaquel 5769a05
change how empties is handled
quaquel 82a64d9
further cleanup of handling of empties
quaquel 5fdc787
correct handling of radius in Schelling large
quaquel 152995c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c524034
Merge remote-tracking branch 'upstream/main' into experimental/cell-s…
quaquel 972ecc4
Update cell_space.py
quaquel a656d93
change from Random to random
quaquel 8df9276
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f4a1089
small fixes and default capacity=None
Corvince 1ebcf06
initial tests for cell_space
quaquel d362bae
some additional tests
quaquel c7d82d0
additional unit tests
quaquel b2842c0
restructure files and folders
Corvince 740f003
various updates
quaquel 2d8fcb5
additional tests and temporary fix for select_random_empty_cell
quaquel 99ff668
Merge branch 'main' into experimental/cell-space
quaquel 565ae0a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ab87b70
improved annotations
quaquel ab13bbc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8f2e3dd
Update discrete_space.py
quaquel ac8ff31
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel aa03962
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9b28f9d
correct handling of seeds when running examples in issolation
quaquel bbca3ac
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel fe5a93f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2c80330
added docstrings
quaquel c79aaf0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8384cf1
reformating and minor update to tests
quaquel 253531c
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel ebdee8e
Add optional neighborhood_func to Grid class
Corvince bb2fc52
allow n-dimensional grids
Corvince b3efdee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8c7ad05
add validation, remove neighborhood_func, add tests
Corvince 819e903
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0f49dc7
Make Cell* generic
Corvince da94fb1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b210686
Make Cell* generic
Corvince c35591c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 065e5ea
fixes to tests
quaquel 1bfa009
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6b1c454
type hint 3.9 fix in network.py
quaquel e0d5673
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel cf789f2
fix type checking
Corvince 50666c1
update capacity, add annotations import
Corvince e520d3c
fix wolf_sheep
Corvince 85251f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3a247cb
fix wolf_sheep
Corvince 86127f2
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel fb19879
seperate code path for 2d and nd grids when connecting cells
quaquel 5ea5d35
seed as kwarg
quaquel b7ac86c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 76ca4a5
code formatting fix
quaquel 45e3e9f
testing
quaquel 3d8cdfd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ce8ca0b
Merge branch 'main' into experimental/cell-space
quaquel 18bd2a3
Merge branch 'experimental/cell-space' of https://github.com/Corvince…
quaquel 62c844e
minor docstring update
quaquel adff7aa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7d5e6b0
Merge branch 'main' into experimental/cell-space
EwoutH b32af9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,41 +9,40 @@ | |
Northwestern University, Evanston, IL. | ||
""" | ||
|
||
import mesa | ||
import math | ||
|
||
from mesa import Model | ||
from mesa.experimental.cell_space import CellAgent, OrthogonalVonNeumannGrid | ||
from mesa.time import RandomActivationByType | ||
|
||
class Animal(mesa.Agent): | ||
def __init__(self, unique_id, model, moore, energy, p_reproduce, energy_from_food): | ||
|
||
class Animal(CellAgent): | ||
def __init__(self, unique_id, model, energy, p_reproduce, energy_from_food): | ||
super().__init__(unique_id, model) | ||
self.energy = energy | ||
self.p_reproduce = p_reproduce | ||
self.energy_from_food = energy_from_food | ||
self.moore = moore | ||
|
||
def random_move(self): | ||
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.move_to(self.cell.neighborhood().select_random_cell()) | ||
|
||
def spawn_offspring(self): | ||
self.energy /= 2 | ||
offspring = self.__class__( | ||
self.model.next_id(), | ||
self.model, | ||
self.moore, | ||
self.energy, | ||
self.p_reproduce, | ||
self.energy_from_food, | ||
) | ||
self.model.grid.place_agent(offspring, self.pos) | ||
offspring.move_to(self.cell) | ||
self.model.schedule.add(offspring) | ||
|
||
def feed(self): | ||
... | ||
|
||
def die(self): | ||
self.model.grid.remove_agent(self) | ||
self.cell.remove_agent(self) | ||
self.remove() | ||
|
||
def step(self): | ||
|
@@ -67,8 +66,9 @@ class Sheep(Animal): | |
|
||
def feed(self): | ||
# If there is grass available, eat it | ||
agents = self.model.grid.get_cell_list_contents(self.pos) | ||
grass_patch = next(obj for obj in agents 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.energy_from_food | ||
grass_patch.fully_grown = False | ||
|
@@ -80,8 +80,7 @@ class Wolf(Animal): | |
""" | ||
|
||
def feed(self): | ||
agents = self.model.grid.get_cell_list_contents(self.pos) | ||
sheep = [obj for obj in agents 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.energy_from_food | ||
|
@@ -90,7 +89,7 @@ def feed(self): | |
sheep_to_eat.die() | ||
|
||
|
||
class GrassPatch(mesa.Agent): | ||
class GrassPatch(CellAgent): | ||
""" | ||
A patch of grass that grows at a fixed rate and it is eaten by sheep | ||
""" | ||
|
@@ -100,7 +99,7 @@ def __init__(self, unique_id, model, fully_grown, countdown): | |
Creates a new patch of grass | ||
|
||
Args: | ||
grown: (boolean) Whether the patch of grass is fully grown or not | ||
fully_grown: (boolean) Whether the patch of grass is fully grown or not | ||
countdown: Time for the patch of grass to be fully grown again | ||
""" | ||
super().__init__(unique_id, model) | ||
|
@@ -117,7 +116,7 @@ def step(self): | |
self.countdown -= 1 | ||
|
||
|
||
class WolfSheep(mesa.Model): | ||
class WolfSheep(Model): | ||
""" | ||
Wolf-Sheep Predation Model | ||
|
||
|
@@ -126,7 +125,6 @@ class WolfSheep(mesa.Model): | |
|
||
def __init__( | ||
self, | ||
seed, | ||
height, | ||
width, | ||
initial_sheep, | ||
|
@@ -136,7 +134,7 @@ def __init__( | |
grass_regrowth_time, | ||
wolf_gain_from_food=13, | ||
sheep_gain_from_food=5, | ||
moore=False, | ||
seed=None, | ||
): | ||
""" | ||
Create a new Wolf-Sheep model with the given parameters. | ||
|
@@ -152,6 +150,7 @@ def __init__( | |
once it is eaten | ||
sheep_gain_from_food: Energy sheep gain from grass, if enabled. | ||
moore: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing documentation. |
||
seed | ||
""" | ||
super().__init__(seed=seed) | ||
# Set parameters | ||
|
@@ -161,24 +160,25 @@ def __init__( | |
self.initial_wolves = initial_wolves | ||
self.grass_regrowth_time = grass_regrowth_time | ||
|
||
self.schedule = mesa.time.RandomActivationByType(self) | ||
self.grid = mesa.space.MultiGrid(self.height, self.width, torus=False) | ||
self.schedule = RandomActivationByType(self) | ||
self.grid = OrthogonalVonNeumannGrid( | ||
[self.height, self.width], | ||
torus=False, | ||
capacity=math.inf, | ||
random=self.random, | ||
) | ||
|
||
# Create sheep: | ||
for _ in range(self.initial_sheep): | ||
pos = ( | ||
self.random.randrange(self.width), | ||
self.random.randrange(self.height), | ||
) | ||
energy = self.random.randrange(2 * sheep_gain_from_food) | ||
sheep = Sheep( | ||
self.next_id(), | ||
self, | ||
moore, | ||
energy, | ||
sheep_reproduce, | ||
sheep_gain_from_food, | ||
self.next_id(), self, energy, sheep_reproduce, sheep_gain_from_food | ||
) | ||
self.grid.place_agent(sheep, pos) | ||
sheep.move_to(self.grid[pos]) | ||
self.schedule.add(sheep) | ||
|
||
# Create wolves | ||
|
@@ -189,21 +189,21 @@ def __init__( | |
) | ||
energy = self.random.randrange(2 * wolf_gain_from_food) | ||
wolf = Wolf( | ||
self.next_id(), self, moore, energy, wolf_reproduce, wolf_gain_from_food | ||
self.next_id(), self, energy, wolf_reproduce, wolf_gain_from_food | ||
) | ||
self.grid.place_agent(wolf, pos) | ||
wolf.move_to(self.grid[pos]) | ||
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(), self, fully_grown, countdown) | ||
self.grid.place_agent(patch, pos) | ||
patch.move_to(cell) | ||
self.schedule.add(patch) | ||
|
||
def step(self): | ||
|
@@ -213,7 +213,7 @@ def step(self): | |
if __name__ == "__main__": | ||
import time | ||
|
||
model = WolfSheep(15, 25, 25, 60, 40, 0.2, 0.1, 20) | ||
model = WolfSheep(25, 25, 60, 40, 0.2, 0.1, 20, seed=15) | ||
|
||
start_time = time.perf_counter() | ||
for _ in range(100): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
from .jupyter_viz import JupyterViz, make_text, Slider # noqa | ||
from mesa.experimental import cell_space | ||
|
||
|
||
__all__ = ["JupyterViz", "make_text", "Slider", "cell_space"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from mesa.experimental.cell_space.cell import Cell | ||
from mesa.experimental.cell_space.cell_agent import CellAgent | ||
from mesa.experimental.cell_space.cell_collection import CellCollection | ||
from mesa.experimental.cell_space.discrete_space import DiscreteSpace | ||
from mesa.experimental.cell_space.grid import ( | ||
Grid, | ||
HexGrid, | ||
OrthogonalMooreGrid, | ||
OrthogonalVonNeumannGrid, | ||
) | ||
from mesa.experimental.cell_space.network import Network | ||
|
||
__all__ = [ | ||
"CellCollection", | ||
"Cell", | ||
"CellAgent", | ||
"DiscreteSpace", | ||
"Grid", | ||
"HexGrid", | ||
"OrthogonalMooreGrid", | ||
"OrthogonalVonNeumannGrid", | ||
"Network", | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.