From 05f18a1a16e0592196d13e85f5c7be715de765b2 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Tue, 10 Dec 2024 22:00:02 +0100 Subject: [PATCH 1/7] initial commit --- mesa/examples/advanced/wolf_sheep/app.py | 3 +++ mesa/examples/advanced/wolf_sheep/model.py | 26 +++++++++------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/mesa/examples/advanced/wolf_sheep/app.py b/mesa/examples/advanced/wolf_sheep/app.py index 878c6797bed..af226568f30 100644 --- a/mesa/examples/advanced/wolf_sheep/app.py +++ b/mesa/examples/advanced/wolf_sheep/app.py @@ -9,6 +9,9 @@ ) + + + def wolf_sheep_portrayal(agent): if agent is None: return diff --git a/mesa/examples/advanced/wolf_sheep/model.py b/mesa/examples/advanced/wolf_sheep/model.py index cc6ec6acc9f..c47fd480ba1 100644 --- a/mesa/examples/advanced/wolf_sheep/model.py +++ b/mesa/examples/advanced/wolf_sheep/model.py @@ -89,23 +89,17 @@ def __init__( self.datacollector = DataCollector(model_reporters) + # Create sheep: - for _ in range(initial_sheep): - pos = ( - self.random.randrange(width), - self.random.randrange(height), - ) - energy = self.random.randrange(2 * sheep_gain_from_food) - Sheep(self, energy, sheep_reproduce, sheep_gain_from_food, self.grid[pos]) - - # Create wolves - for _ in range(initial_wolves): - pos = ( - self.random.randrange(width), - self.random.randrange(height), - ) - energy = self.random.randrange(2 * wolf_gain_from_food) - Wolf(self, energy, wolf_reproduce, wolf_gain_from_food, self.grid[pos]) + Sheep.create_agents(self, initial_sheep, + self.rng.random((initial_sheep,))*2 * sheep_gain_from_food, + sheep_reproduce, sheep_gain_from_food, + self.random.choices(self.grid.all_cells, k=initial_sheep)) + # Create Wolves: + Wolf.create_agents(self, initial_wolves, + self.rng.random((initial_sheep,)) * 2 * wolf_gain_from_food, + wolf_reproduce, wolf_gain_from_food, + self.random.choices(self.grid.all_cells, k=initial_wolves)) # Create grass patches if enabled if grass: From bb4df2bc219199d1315eff085a0eb4f612ca84a5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:02:29 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/examples/advanced/wolf_sheep/app.py | 3 --- mesa/examples/advanced/wolf_sheep/model.py | 25 ++++++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/mesa/examples/advanced/wolf_sheep/app.py b/mesa/examples/advanced/wolf_sheep/app.py index af226568f30..878c6797bed 100644 --- a/mesa/examples/advanced/wolf_sheep/app.py +++ b/mesa/examples/advanced/wolf_sheep/app.py @@ -9,9 +9,6 @@ ) - - - def wolf_sheep_portrayal(agent): if agent is None: return diff --git a/mesa/examples/advanced/wolf_sheep/model.py b/mesa/examples/advanced/wolf_sheep/model.py index c47fd480ba1..0bf0b40918d 100644 --- a/mesa/examples/advanced/wolf_sheep/model.py +++ b/mesa/examples/advanced/wolf_sheep/model.py @@ -89,17 +89,24 @@ def __init__( self.datacollector = DataCollector(model_reporters) - # Create sheep: - Sheep.create_agents(self, initial_sheep, - self.rng.random((initial_sheep,))*2 * sheep_gain_from_food, - sheep_reproduce, sheep_gain_from_food, - self.random.choices(self.grid.all_cells, k=initial_sheep)) + Sheep.create_agents( + self, + initial_sheep, + self.rng.random((initial_sheep,)) * 2 * sheep_gain_from_food, + sheep_reproduce, + sheep_gain_from_food, + self.random.choices(self.grid.all_cells, k=initial_sheep), + ) # Create Wolves: - Wolf.create_agents(self, initial_wolves, - self.rng.random((initial_sheep,)) * 2 * wolf_gain_from_food, - wolf_reproduce, wolf_gain_from_food, - self.random.choices(self.grid.all_cells, k=initial_wolves)) + Wolf.create_agents( + self, + initial_wolves, + self.rng.random((initial_sheep,)) * 2 * wolf_gain_from_food, + wolf_reproduce, + wolf_gain_from_food, + self.random.choices(self.grid.all_cells, k=initial_wolves), + ) # Create grass patches if enabled if grass: From 3cb35c0c6781bec48d46554f6f01cc69e064b08d Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 11 Dec 2024 08:08:09 +0100 Subject: [PATCH 3/7] make it kwargs --- mesa/examples/advanced/wolf_sheep/agents.py | 2 +- mesa/examples/advanced/wolf_sheep/model.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/mesa/examples/advanced/wolf_sheep/agents.py b/mesa/examples/advanced/wolf_sheep/agents.py index e3f308aa479..7196a99a986 100644 --- a/mesa/examples/advanced/wolf_sheep/agents.py +++ b/mesa/examples/advanced/wolf_sheep/agents.py @@ -4,7 +4,7 @@ class Animal(CellAgent): """The base animal class.""" - def __init__(self, model, energy, p_reproduce, energy_from_food, cell): + def __init__(self, model, energy=0, p_reproduce=0.2, energy_from_food=1, cell=None): """Initialize an animal. Args: diff --git a/mesa/examples/advanced/wolf_sheep/model.py b/mesa/examples/advanced/wolf_sheep/model.py index 0bf0b40918d..327d18a006e 100644 --- a/mesa/examples/advanced/wolf_sheep/model.py +++ b/mesa/examples/advanced/wolf_sheep/model.py @@ -93,19 +93,19 @@ def __init__( Sheep.create_agents( self, initial_sheep, - self.rng.random((initial_sheep,)) * 2 * sheep_gain_from_food, - sheep_reproduce, - sheep_gain_from_food, - self.random.choices(self.grid.all_cells, k=initial_sheep), + energy=self.rng.random((initial_sheep,)) * 2 * sheep_gain_from_food, + p_reproduce=sheep_reproduce, + energy_from_food=sheep_gain_from_food, + cell=self.random.choices(self.grid.all_cells, k=initial_sheep), ) # Create Wolves: Wolf.create_agents( self, initial_wolves, - self.rng.random((initial_sheep,)) * 2 * wolf_gain_from_food, - wolf_reproduce, - wolf_gain_from_food, - self.random.choices(self.grid.all_cells, k=initial_wolves), + energy=self.rng.random((initial_sheep,)) * 2 * wolf_gain_from_food, + p_reproduce=wolf_reproduce, + energy_from_food=wolf_gain_from_food, + cell=self.random.choices(self.grid.all_cells, k=initial_wolves), ) # Create grass patches if enabled From d4bf0b7f04be9a105e4c7479c35d2e400960be15 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 11 Dec 2024 08:14:11 +0100 Subject: [PATCH 4/7] Update model.py --- mesa/examples/advanced/wolf_sheep/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mesa/examples/advanced/wolf_sheep/model.py b/mesa/examples/advanced/wolf_sheep/model.py index 327d18a006e..acb117d2ee6 100644 --- a/mesa/examples/advanced/wolf_sheep/model.py +++ b/mesa/examples/advanced/wolf_sheep/model.py @@ -96,7 +96,7 @@ def __init__( energy=self.rng.random((initial_sheep,)) * 2 * sheep_gain_from_food, p_reproduce=sheep_reproduce, energy_from_food=sheep_gain_from_food, - cell=self.random.choices(self.grid.all_cells, k=initial_sheep), + cell=self.random.choices(self.grid.all_cells.cells, k=initial_sheep), ) # Create Wolves: Wolf.create_agents( @@ -105,7 +105,7 @@ def __init__( energy=self.rng.random((initial_sheep,)) * 2 * wolf_gain_from_food, p_reproduce=wolf_reproduce, energy_from_food=wolf_gain_from_food, - cell=self.random.choices(self.grid.all_cells, k=initial_wolves), + cell=self.random.choices(self.grid.all_cells.cells, k=initial_wolves), ) # Create grass patches if enabled From 6697c12bb270fa6e003a580722b8e43c19f98fda Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 11 Dec 2024 08:31:03 +0100 Subject: [PATCH 5/7] fix error --- mesa/examples/advanced/wolf_sheep/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/examples/advanced/wolf_sheep/model.py b/mesa/examples/advanced/wolf_sheep/model.py index acb117d2ee6..73b9c26a833 100644 --- a/mesa/examples/advanced/wolf_sheep/model.py +++ b/mesa/examples/advanced/wolf_sheep/model.py @@ -102,7 +102,7 @@ def __init__( Wolf.create_agents( self, initial_wolves, - energy=self.rng.random((initial_sheep,)) * 2 * wolf_gain_from_food, + energy=self.rng.random((initial_wolves,)) * 2 * wolf_gain_from_food, p_reproduce=wolf_reproduce, energy_from_food=wolf_gain_from_food, cell=self.random.choices(self.grid.all_cells.cells, k=initial_wolves), From de77912162c3a38327911f15839166b14614f1fe Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Wed, 11 Dec 2024 13:56:02 +0100 Subject: [PATCH 6/7] small changes in default values to follow a sheep --- mesa/examples/advanced/wolf_sheep/agents.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mesa/examples/advanced/wolf_sheep/agents.py b/mesa/examples/advanced/wolf_sheep/agents.py index 7196a99a986..288ddb92ddf 100644 --- a/mesa/examples/advanced/wolf_sheep/agents.py +++ b/mesa/examples/advanced/wolf_sheep/agents.py @@ -4,7 +4,7 @@ class Animal(CellAgent): """The base animal class.""" - def __init__(self, model, energy=0, p_reproduce=0.2, energy_from_food=1, cell=None): + def __init__(self, model, energy=8, p_reproduce=0.04, energy_from_food=4, cell=None): """Initialize an animal. Args: From cc4b47856f462e422ada759b72ae0eb045aa6f0b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:56:12 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/examples/advanced/wolf_sheep/agents.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mesa/examples/advanced/wolf_sheep/agents.py b/mesa/examples/advanced/wolf_sheep/agents.py index 288ddb92ddf..e262996fcff 100644 --- a/mesa/examples/advanced/wolf_sheep/agents.py +++ b/mesa/examples/advanced/wolf_sheep/agents.py @@ -4,7 +4,9 @@ class Animal(CellAgent): """The base animal class.""" - def __init__(self, model, energy=8, p_reproduce=0.04, energy_from_food=4, cell=None): + def __init__( + self, model, energy=8, p_reproduce=0.04, energy_from_food=4, cell=None + ): """Initialize an animal. Args: