Skip to content

Commit b13748e

Browse files
committed
Adding in WolfSheepPredation
1 parent 1831aa2 commit b13748e

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

examples/WolfSheep/WolfSheep.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Visualization for the Wolf-Sheep Predation
3+
'''
4+
5+
from WolfSheep import Wolf, Sheep
6+
from mesa.visualization.TextVisualization import TextVisualization, TextData, TextGrid
7+
8+
class WolfSheepVisualization(TextVisualization):
9+
'''
10+
ASCII visualization of the WolfSheepPredation model.
11+
12+
Each cell displays S if only sheep, W if only wolves, or X if both.
13+
(blank if none)
14+
'''
15+
16+
def __init__(self, model):
17+
self.model = model
18+
grid_viz = TextGrid(self.model.grid, self.draw_cell)
19+
self.elements = [grid_viz]
20+
21+
@staticmethod
22+
def draw_cell(cell):
23+
if len(cell) == 0:
24+
return " "
25+
if len([obj for obj in cell if isinstance(obj, Sheep)]) == len(cell):
26+
return "S"
27+
elif len([obj for obj in cell if isinstance(obj, Wolf)]) == len(cell):
28+
return "W"
29+
else:
30+
return "X"
31+

examples/WolfSheep/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from WolfSheep import WolfSheepPredation
2+
from WolfSheepVisualization import WolfSheepVisualization
3+
4+
if __name__ == "__main__":
5+
model = WolfSheepPredation()
6+
viz = WolfSheepVisualization(model)

0 commit comments

Comments
 (0)