|
| 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 | +TODO: Implement grass |
| 12 | +
|
| 13 | +''' |
| 14 | + |
| 15 | + |
| 16 | +import random |
| 17 | + |
| 18 | +from mesa import Model, Agent |
| 19 | +from mesa.space import MultiGrid |
| 20 | +from mesa.time import Random_Activation |
| 21 | + |
| 22 | +from RandomWalk import RandomWalker |
| 23 | + |
| 24 | +class WolfSheepPredation(Model): |
| 25 | + ''' |
| 26 | + Wolf-Sheep Predation Model |
| 27 | + ''' |
| 28 | + |
| 29 | + initial_sheep = 100 |
| 30 | + initial_wolves = 50 |
| 31 | + sheep_gain_from_food = 4 |
| 32 | + wolf_gain_from_food = 20 |
| 33 | + sheep_reproduce = 0.04 |
| 34 | + wolf_reproduce = 0.05 |
| 35 | + |
| 36 | + height = 20 |
| 37 | + width = 20 |
| 38 | + |
| 39 | + def __init__(self): |
| 40 | + ''' |
| 41 | + Create a new Wolf-Sheep model with the given parameters. |
| 42 | + ''' |
| 43 | + #TODO: Accept all other parameters |
| 44 | + |
| 45 | + self.schedule = Random_Activation(self) |
| 46 | + self.grid = MultiGrid(self.height, self.width, torus=True) |
| 47 | + |
| 48 | + # Create sheep: |
| 49 | + for i in range(self.initial_sheep): |
| 50 | + x = random.randrange(self.width) |
| 51 | + y = random.randrange(self.height) |
| 52 | + sheep = Sheep(self.grid, x, y, True) |
| 53 | + self.grid[y][x].add(sheep) |
| 54 | + self.schedule.add(sheep) |
| 55 | + |
| 56 | + # Create wolves |
| 57 | + for i in range(self.initial_wolves): |
| 58 | + x = random.randrange(self.width) |
| 59 | + y = random.randrange(self.height) |
| 60 | + energy = random.randrange(2 * self.wolf_gain_from_food) |
| 61 | + wolf = Wolf(self.grid, x, y, True, energy) |
| 62 | + self.grid[y][x].add(wolf) |
| 63 | + self.schedule.add(wolf) |
| 64 | + |
| 65 | + def step(self): |
| 66 | + self.schedule.step() |
| 67 | + |
| 68 | + |
| 69 | +class Sheep(RandomWalker, Agent): |
| 70 | + ''' |
| 71 | + A sheep that walks around, reproduces (asexually) and gets eaten. |
| 72 | +
|
| 73 | + The init is the same as the RandomWalker. |
| 74 | + ''' |
| 75 | + |
| 76 | + def step(self, model): |
| 77 | + ''' |
| 78 | + A model step. Move, then eat grass and reproduce. |
| 79 | + ''' |
| 80 | + self.random_move() |
| 81 | + if random.random() < model.sheep_reproduce: |
| 82 | + # Create a new sheep: |
| 83 | + lamb = Sheep(self.grid, self.x, self.y, self.moore) |
| 84 | + model.grid[self.y][self.x].add(lamb) |
| 85 | + model.schedule.add(lamb) |
| 86 | + |
| 87 | + |
| 88 | +class Wolf(RandomWalker, Agent): |
| 89 | + ''' |
| 90 | + A wolf that walks around, reproduces (asexually) and eats sheep. |
| 91 | + ''' |
| 92 | + |
| 93 | + energy = None |
| 94 | + |
| 95 | + def __init__(self, grid, x, y, moore, energy): |
| 96 | + super().__init__(grid, x, y, moore) |
| 97 | + self.energy = energy |
| 98 | + |
| 99 | + |
| 100 | + def step(self, model): |
| 101 | + self.random_move() |
| 102 | + self.energy -= 1 |
| 103 | + |
| 104 | + # If there are sheep present, eat one |
| 105 | + this_cell = model.grid[self.y][self.x] |
| 106 | + sheep = [obj for obj in this_cell if isinstance(obj, Sheep)] |
| 107 | + if len(sheep) > 0: |
| 108 | + sheep_to_eat = random.choice(sheep) |
| 109 | + self.energy += model.wolf_gain_from_food |
| 110 | + |
| 111 | + # Kill the sheep |
| 112 | + model.grid[self.y][self.x].remove(sheep_to_eat) |
| 113 | + model.schedule.remove(sheep_to_eat) |
| 114 | + |
| 115 | + # Reproduction: |
| 116 | + if random.random() < model.wolf_reproduce: |
| 117 | + # Create a new wolf cub |
| 118 | + cub = Wolf(self.grid, self.x, self.y, self.moore, self.energy/2) |
| 119 | + self.energy = self.energy/2 |
| 120 | + model.grid[self.y][self.x].add(cub) |
| 121 | + model.schedule.add(cub) |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
0 commit comments