|
| 1 | +""" |
| 2 | +Wolf-Sheep Predation Model |
| 3 | +================================ |
| 4 | +
|
| 5 | +Replication of the model found in NetLogo: |
| 6 | + Wilensky, U. (1997). NetLogo Wolf Sheep Predation model. |
| 7 | + http://ccl.northwestern.edu/netlogo/models/WolfSheepPredation. |
| 8 | + Center for Connected Learning and Computer-Based Modeling, |
| 9 | + Northwestern University, Evanston, IL. |
| 10 | +""" |
| 11 | + |
| 12 | +import mesa |
| 13 | +from mesa.space import MultiGrid |
| 14 | +from mesa.time import RandomActivationByType |
| 15 | + |
| 16 | +from .agents import GrassPatch, Sheep, Wolf |
| 17 | + |
| 18 | + |
| 19 | +class WolfSheep(mesa.Model): |
| 20 | + """ |
| 21 | + Wolf-Sheep Predation Model |
| 22 | +
|
| 23 | + A model for simulating wolf and sheep (predator-prey) ecosystem modelling. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + seed, |
| 29 | + height, |
| 30 | + width, |
| 31 | + initial_sheep, |
| 32 | + initial_wolves, |
| 33 | + sheep_reproduce, |
| 34 | + wolf_reproduce, |
| 35 | + grass_regrowth_time, |
| 36 | + wolf_gain_from_food=13, |
| 37 | + sheep_gain_from_food=5, |
| 38 | + ): |
| 39 | + """ |
| 40 | + Create a new Wolf-Sheep model with the given parameters. |
| 41 | +
|
| 42 | + Args: |
| 43 | + initial_sheep: Number of sheep to start with |
| 44 | + initial_wolves: Number of wolves to start with |
| 45 | + sheep_reproduce: Probability of each sheep reproducing each step |
| 46 | + wolf_reproduce: Probability of each wolf reproducing each step |
| 47 | + wolf_gain_from_food: Energy a wolf gains from eating a sheep |
| 48 | + grass: Whether to have the sheep eat grass for energy |
| 49 | + grass_regrowth_time: How long it takes for a grass patch to regrow |
| 50 | + once it is eaten |
| 51 | + sheep_gain_from_food: Energy sheep gain from grass, if enabled. |
| 52 | + """ |
| 53 | + super().__init__(seed=seed) |
| 54 | + # Set parameters |
| 55 | + self.height = height |
| 56 | + self.width = width |
| 57 | + self.initial_sheep = initial_sheep |
| 58 | + self.initial_wolves = initial_wolves |
| 59 | + self.sheep_reproduce = sheep_reproduce |
| 60 | + self.wolf_reproduce = wolf_reproduce |
| 61 | + self.wolf_gain_from_food = wolf_gain_from_food |
| 62 | + self.grass_regrowth_time = grass_regrowth_time |
| 63 | + self.sheep_gain_from_food = sheep_gain_from_food |
| 64 | + |
| 65 | + self.schedule = RandomActivationByType(self) |
| 66 | + self.grid = MultiGrid(self.height, self.width, torus=False) |
| 67 | + |
| 68 | + # Create sheep: |
| 69 | + for _i in range(self.initial_sheep): |
| 70 | + pos = ( |
| 71 | + self.random.randrange(self.width), |
| 72 | + self.random.randrange(self.height), |
| 73 | + ) |
| 74 | + energy = self.random.randrange(2 * self.sheep_gain_from_food) |
| 75 | + sheep = Sheep(self.next_id(), pos, self, True, energy) |
| 76 | + self.grid.place_agent(sheep, pos) |
| 77 | + self.schedule.add(sheep) |
| 78 | + |
| 79 | + # Create wolves |
| 80 | + for _i in range(self.initial_wolves): |
| 81 | + pos = ( |
| 82 | + self.random.randrange(self.width), |
| 83 | + self.random.randrange(self.height), |
| 84 | + ) |
| 85 | + energy = self.random.randrange(2 * self.wolf_gain_from_food) |
| 86 | + wolf = Wolf(self.next_id(), pos, self, True, energy) |
| 87 | + self.grid.place_agent(wolf, pos) |
| 88 | + self.schedule.add(wolf) |
| 89 | + |
| 90 | + # Create grass patches |
| 91 | + possibly_fully_grown = [True, False] |
| 92 | + for _agent, pos in self.grid.coord_iter(): |
| 93 | + fully_grown = self.random.choice(possibly_fully_grown) |
| 94 | + if fully_grown: |
| 95 | + countdown = self.grass_regrowth_time |
| 96 | + else: |
| 97 | + countdown = self.random.randrange(self.grass_regrowth_time) |
| 98 | + patch = GrassPatch(self.next_id(), pos, self, fully_grown, countdown) |
| 99 | + self.grid.place_agent(patch, pos) |
| 100 | + self.schedule.add(patch) |
| 101 | + |
| 102 | + def step(self): |
| 103 | + self.schedule.step() |
0 commit comments