-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrandom_walk.py
executable file
·36 lines (30 loc) · 1.12 KB
/
random_walk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Generalized behavior for random walking, one grid cell at a time.
"""
from mesa import Agent
class RandomWalker(Agent):
"""
Class implementing random walker methods in a generalized manner.
Not indended to be used on its own, but to inherit its methods to multiple
other agents.
"""
def __init__(self, unique_id, pos, model, moore=True):
"""
grid: The MultiGrid object in which the agent lives.
x: The agent's current x coordinate
y: The agent's current y coordinate
moore: If True, may move in all 8 directions.
Otherwise, only up, down, left, right.
"""
super().__init__(unique_id, model)
self.pos = pos
self.moore = moore
def random_move(self):
"""
Step one cell in any allowable direction.
"""
# Pick the next cell from the adjacent cells.
next_moves = self.model.grid.get_neighborhood(self.pos, self.moore, True)
next_move = self.random.choice(next_moves)
# Now move:
self.model.grid.move_agent(self, next_move)