1717class Animal (CellAgent ):
1818 """The base animal class."""
1919
20- def __init__ (self , model , energy , p_reproduce , energy_from_food ):
20+ def __init__ (self , model , energy , p_reproduce , energy_from_food , cell ):
2121 """Initializes an animal.
2222
2323 Args:
2424 model: a model instance
2525 energy: starting amount of energy
2626 p_reproduce: probability of sexless reproduction
2727 energy_from_food: energy obtained from 1 unit of food
28+ cell: the cell in which the animal starts
2829 """
2930 super ().__init__ (model )
3031 self .energy = energy
3132 self .p_reproduce = p_reproduce
3233 self .energy_from_food = energy_from_food
33-
34- def random_move (self ):
35- """Move to a random neighboring cell."""
36- self .move_to (self .cell .neighborhood .select_random_cell ())
34+ self .cell = cell
3735
3836 def spawn_offspring (self ):
3937 """Create offspring."""
4038 self .energy /= 2
41- offspring = self .__class__ (
39+ self .__class__ (
4240 self .model ,
4341 self .energy ,
4442 self .p_reproduce ,
4543 self .energy_from_food ,
44+ self .cell ,
4645 )
47- offspring .move_to (self .cell )
4846
4947 def feed (self ): ... # noqa: D102
5048
51- def die (self ):
52- """Die."""
53- self .cell .remove_agent (self )
54- self .remove ()
55-
5649 def step (self ):
5750 """One step of the agent."""
58- self .random_move ()
51+ self .cell = self . cell . neighborhood . select_random_cell ()
5952 self .energy -= 1
6053
6154 self .feed ()
6255
6356 if self .energy < 0 :
64- self .die ()
57+ self .remove ()
6558 elif self .random .random () < self .p_reproduce :
6659 self .spawn_offspring ()
6760
@@ -91,7 +84,7 @@ def feed(self):
9184 self .energy += self .energy_from_food
9285
9386 # Kill the sheep
94- sheep_to_eat .die ()
87+ sheep_to_eat .remove ()
9588
9689
9790class GrassPatch (CellAgent ):
@@ -112,19 +105,19 @@ def fully_grown(self, value: bool) -> None:
112105 function_args = [self , "fully_grown" , True ],
113106 )
114107
115- def __init__ (self , model , fully_grown , countdown , grass_regrowth_time ):
108+ def __init__ (self , model , countdown , grass_regrowth_time , cell ):
116109 """Creates a new patch of grass.
117110
118111 Args:
119112 model: a model instance
120- fully_grown: (boolean) Whether the patch of grass is fully grown or not
121113 countdown: Time for the patch of grass to be fully grown again
122114 grass_regrowth_time : time to fully regrow grass
115+ cell: the cell to which the patch of grass belongs
123116 """
124- # TODO:: fully grown can just be an int --> so one less param (i.e. countdown)
125117 super ().__init__ (model )
126- self ._fully_grown = fully_grown
118+ self ._fully_grown = True if countdown == 0 else False # Noqa: SIM210
127119 self .grass_regrowth_time = grass_regrowth_time
120+ self .cell = cell
128121
129122 if not self .fully_grown :
130123 self .model .simulator .schedule_event_relative (
@@ -191,13 +184,7 @@ def __init__(
191184 self .random .randrange (self .height ),
192185 )
193186 energy = self .random .randrange (2 * sheep_gain_from_food )
194- sheep = Sheep (
195- self ,
196- energy ,
197- sheep_reproduce ,
198- sheep_gain_from_food ,
199- )
200- sheep .move_to (self .grid [pos ])
187+ Sheep (self , energy , sheep_reproduce , sheep_gain_from_food , self .grid [pos ])
201188
202189 # Create wolves
203190 for _ in range (self .initial_wolves ):
@@ -206,24 +193,14 @@ def __init__(
206193 self .random .randrange (self .height ),
207194 )
208195 energy = self .random .randrange (2 * wolf_gain_from_food )
209- wolf = Wolf (
210- self ,
211- energy ,
212- wolf_reproduce ,
213- wolf_gain_from_food ,
214- )
215- wolf .move_to (self .grid [pos ])
196+ Wolf (self , energy , wolf_reproduce , wolf_gain_from_food , self .grid [pos ])
216197
217198 # Create grass patches
218199 possibly_fully_grown = [True , False ]
219200 for cell in self .grid :
220201 fully_grown = self .random .choice (possibly_fully_grown )
221- if fully_grown :
222- countdown = grass_regrowth_time
223- else :
224- countdown = self .random .randrange (grass_regrowth_time )
225- patch = GrassPatch (self , fully_grown , countdown , grass_regrowth_time )
226- patch .move_to (cell )
202+ countdown = 0 if fully_grown else self .random .randrange (grass_regrowth_time )
203+ GrassPatch (self , countdown , grass_regrowth_time , cell )
227204
228205 def step (self ):
229206 """Run one step of the model."""
0 commit comments