-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.py
executable file
·238 lines (176 loc) · 7.84 KB
/
simulation.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import time
import argparse
import numpy as np
from p5 import setup, draw, size, background, run, triangle, fill, rect, line, quad
from fish import Fish
class Simulation:
def __init__(self, fishes=4, replicas_top=0, replicas_bottom=0, follow_refugia_force=None,
free_run=False):
self.free_run = free_run
if not free_run:
self.width = 1500
self.height = 800
else:
self.width = 730
self.height = 730
# box parameters
self.box_width = 120
self.box_padding_left = 40
self.box_left = self.width - self.box_width - self.box_padding_left
self.box_top = 335
self.fishes = fishes
if free_run:
self.follow_refugia_force = 0
else:
self.follow_refugia_force = follow_refugia_force
self.replicas_top = replicas_top
# replica inside
self.replica_initial_y_top = self.box_top + 20
self.replica_initial_y_bottom = self.box_top + self.box_width - 20
self.replica_final_y_top = 80
self.replica_final_y_bottom = 720
# decision line position
if not self.free_run:
self.decision_x = 520
else:
# if will just never get there
self.decision_x = - 100
# shaded area position
if not self.free_run:
self.shaded_area_x = 280
else:
# if will just never get there
self.shaded_area_x = - 100
self.shoal = [Fish(self.get_starting_x(),
self.get_starting_y(),
self.width, self.height, self.shaded_area_x,
decision_x=self.decision_x,
follow_refugia_force=follow_refugia_force)
for _ in range(fishes)]
self.replica_y_start = self.box_top + self.box_width / 2
self.replica_x_start = self.width - self.box_padding_left - self.box_width / 2
self.replicas_coordinates = []
for i in range(replicas_top):
self.add_replica(i, 'top')
for i in range(replicas_bottom):
self.add_replica(i, 'bottom')
def add_replica(self, replica_id, position):
x = self.get_replica_x(replica_id)
y = self.get_replica_y(x, position)
final_y = self.get_replica_y(0, position)
self.replicas_coordinates.append((x, y, final_y))
replica = Fish(x, y, self.width, self.height, self.shaded_area_x,
replica=True, replica_final_y=final_y)
self.shoal.append(replica)
def get_replica_x(self, replica_id):
# inside
return self.width - self.box_padding_left - self.box_width / 2 - replica_id * 15
def get_replica_y(self, x, position):
# inside
x_initial = self.width - self.box_padding_left - self.box_width / 2
if position == 'top':
return (self.replica_initial_y_top - self.replica_final_y_top) * x \
/ x_initial + self.replica_final_y_top
else:
return (self.replica_initial_y_bottom - self.replica_final_y_bottom) * x \
/ x_initial + self.replica_final_y_bottom
def get_starting_x(self, position=None):
return self.box_left + np.random.random() * self.box_width
def get_starting_y(self, position=None):
# random position
return self.box_top + np.random.random() * self.box_width
def draw_objects(self):
# global simulation
# triangle((0, 0), (0, 200), (350, 100))
background(30, 30, 47)
# don't paint obstacles for free run
if self.free_run:
return
fill(50)
# shaded are upper quadrant
quad((0, 0), (0, 160), (280, 256), (self.shaded_area_x, 0))
quad((0, self.height), (0, 640), (280, 544), (self.shaded_area_x, self.height))
fill(102)
triangle((0, 160), (0, 640), (700, 400))
# fishes' start box
rect((self.box_left, 335), self.box_width, self.box_width)
# replica line
for rc in self.replicas_coordinates:
line((0, rc[2]), (rc[0], rc[1]))
# line((0, 720), (self.replica_x_start, self.replica_y_start))
# shaded area line
line((self.shaded_area_x, 0), (self.shaded_area_x, self.height))
# 544
# decision line
line((self.decision_x, 0), (self.decision_x, self.height))
def run_step(self, visualize=True):
# global simulation
if visualize:
self.draw_objects()
for fish in self.shoal:
fish.update(self.shoal)
# to test with one fish
# fish.update_one(fishes)
if visualize:
fish.show()
fishes_not_replica = list(filter(lambda f: not f.replica, self.shoal))
top = len(list(filter(lambda f: f.decision == 'top', fishes_not_replica)))
bottom = len(list(filter(lambda f: f.decision == 'bottom', fishes_not_replica)))
total = top + bottom
all_dicided = total == len(fishes_not_replica)
return all_dicided, top, bottom
def setup():
# global simulation
# this happens just once
size(simulation.width, simulation.height) # instead of create_canvas
def draw():
simulation.run_step()
def headless_simulation(fishes=2, replicas_top=0, replicas_bottom=0, follow_refugia_force=0.2):
start = time.time()
simulation = Simulation(fishes=fishes, replicas_top=replicas_top,
replicas_bottom=replicas_bottom, follow_refugia_force=follow_refugia_force)
all_dicided = False
step = 0
while not all_dicided:
step += 1
print(f'step: {step}', end='\r')
all_dicided, top, bottom = simulation.run_step(False)
print(f'Decided top: {top}, decided bottom: {bottom}, steps: {step}')
end = time.time()
print(f'total time: {end - start:.2f}')
return top, bottom
def headless_simulations(shoals=20, fishes=2, replicas_top=0, replicas_bottom=0, follow_refugia_force=0.2):
top_preference_proportions = []
for i in range(shoals):
print(f'\n\nShoal {i + 1} of {shoals}')
top, bottom = headless_simulation(fishes=fishes, replicas_top=replicas_top,
replicas_bottom=replicas_bottom,
follow_refugia_force=follow_refugia_force)
top_preference_proportion = top / fishes
top_preference_proportions.append(top_preference_proportion)
print(f'Proportions: {top_preference_proportions}')
return top_preference_proportions
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--fishes", dest="fishes",
nargs='?', const=2, type=int, default=2,
help="define how many fishes will be used in simulation (default = 2)")
parser.add_argument("-t", "--replicas-top", dest="replicas_top",
nargs='?', const=0, type=int, default=0,
help="define how many replicas_top will go top (default = 0)")
parser.add_argument("-b", "--replicas-bottom", dest="replicas_bottom",
nargs='?', const=0, type=int, default=0,
help="define how many replicas_bottom will go bottom (default = 0)")
parser.add_argument("-r", "--free-run", dest="free_run",
nargs='?', const=False, type=bool, default=False,
help="define if run in free mode (default=False)")
args = parser.parse_args()
print(args)
print(args.fishes)
simulation = Simulation(fishes=args.fishes,
replicas_top=args.replicas_top,
replicas_bottom=args.replicas_bottom,
free_run=args.free_run)
run()
# run(frame_rate=25)
# run(frame_rate=1000)