-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselfishherd.py
68 lines (51 loc) · 1.68 KB
/
selfishherd.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Pranav Minasandra and Cecilia Baldoni
# pminasandra.github.io
# Dec 10, 2024
"""
Provides class SelfishHerd, a parallelisable way to instantiate and run
simulations.
"""
import os.path
import pickle
import numpy as np
import movement
import voronoi
class SelfishHerd:
"""
Args:
n (int): number of selfish agents
depth_of_reasoning (int): how deep they should anticipate others'
behaviours.
init_locs (np.array, n*2): initial_locations of agents.
"""
def __init__(self,
n,
depth_of_reasoning,
init_locs):
self.n = n
self.depth = depth_of_reasoning
self.init_locs = init_locs.copy()
self.records = init_locs.copy()
self.records = self.records[:,:,np.newaxis] #make 3d array
def run(self, t):
"""
Run the model for time t.
Args:
t (int): how many iterations to update the model.
"""
for i in range(t):
locs = self.records.copy()[:,:,-1]
vor = voronoi.get_bounded_voronoi(locs)
next_locs = movement.recursive_reasoning(locs, vor, self.depth,
locs)
self.records = np.dstack((self.records, next_locs))
def savedata(self, filename):
"""
Pickle-dumps the records to given filename
"""
with open(filename, "wb") as file_obj:
pickle.dump(self.records, file_obj)
def __str__(self):
return f"SelfishHerd object with {self.n} individuals and {self.records.shape[2]} rows of data."
def __repr__(self):
return self.__str__()