-
Notifications
You must be signed in to change notification settings - Fork 3
/
think_old.m
212 lines (198 loc) · 7.04 KB
/
think_old.m
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
%calculate distances between fish
%--------------------------------------------------------------------------
d = zeros(N,N); %pre-allocate
dp.x = zeros(N,N);
dp.y = zeros(N,N);
dp.theta = zeros(N,N);
for i = 1:N
for j = 1:N
dp.x(i,j) = p.x(j) - p.x(i);
dp.y(i,j) = p.y(j) - p.y(i);
dp.theta(i,j) = npi2pi(rad2deg(atan2(dp.y(i,j), dp.x(i,j))));
d(i,j) = sqrt( dp.x(i,j)^2 + dp.y(i,j)^2 );
end
end
%--------------------------------------------------------------------------
%calculate turning angles
%--------------------------------------------------------------------------
beta = zeros(N,N); %pre-allocate
for i = 1:N
for j = 1:N
angle = v.theta(j) - v.theta(i);
%repulsion zone (0 < d < r1)
if d(i,j) > 0 && d(i,j) < r1
plus90 = npi2pi(angle + 90);
minus90 = npi2pi(angle - 90);
if abs(plus90) < abs(minus90)
beta(i,j) = plus90;
else beta(i,j) = minus90;
end
end
%parallel orientation zone (r1 <= d < r2)
if d(i,j) >= r1 && d(i,j) < r2
beta(i,j) = npi2pi(angle);
end
%attraction zone (r2 <= d < r3)
if d(i,j) >= r2 && d(i,j) < r3
beta(i,j) = npi2pi(dp.theta(i,j) - v.theta(i));
end
% %searching zone (d >= r3)
% if d(i,j) >= r3
% beta(i,j) = npi2pi(360*rand);
% end
end
end
%--------------------------------------------------------------------------
%calculate resultant theta orientation
%--------------------------------------------------------------------------
%simple averaging model
% for i = 1:N
% v.theta(i) = npi2pi((sum(beta(i,:)) - beta(i,i)) / (N-1));
% end
%averaging (with no searching unless all neighbors farther than r3)
% for i = 1:N
% sum = 0;
% counter = 0;
% dmin = 2*T^2; %max distance in square tank of size TxT
%
% %find minumum neighbor distance for fish i
% for j = 1:N
% if i ~= j && d(i,j) < dmin
% dmin = d(i,j);
% end
% end
%
% for j = 1:N
% if i ~= j
% if dmin <= r3
% if d(i,j) <= r3 %only use neighbors closer than r3
% sum = sum + beta(i,j);
% counter = counter + 1;
% end
% else %if dmin > r3, use all neighbors (all searching zone)
% sum = sum + beta(i,j);
% counter = counter + 1;
% end
% end
% end
% v.theta(i) = npi2pi(v.theta(i) + npi2pi(sum/counter));
% end
%distance priority - average closest 4 neighbors (with dead zone)
% w = 30;
% closest4 = zeros(1,4); %pre-allocate for speed
% new_v.x = zeros(1,N); % temp vectors to hold new velocity
% new_v.y = zeros(1,N);
% for i = 1:N
% index = 0;
% sorted = sort(d(i,:));
% for j = 1:N
% %remove dead zone, searching zone, and self, and limit to 4
% for k = 1:N
% if index < 4 && k ~= i && abs(dp.theta(i,j)) < (180 - w)...
% && d(i,j) < r3 && d(i,j) == sorted(k)
% closest4(index + 1) = j;
% index = index + 1;
% end
% end
% end
%
% if index > 0 %at least 1 neighbor visible
% beta_avg = npi2pi(sum(beta(i,closest4(1:index)))/index);
% %add randomness (normal distribution)
% sigma = 15; %standard deviation = 15 degrees
% alpha = beta_avg + sigma*randn(1);
%
% %calculate velocity vector
% %should magnitude have some randomness too?
% v.theta(i) = npi2pi(v.theta(i) + alpha);
% % new_v.x(i) = sqrt(sum(v.x(closest4(1:index)).^2));
% % new_v.y(i) = sqrt(sum(v.y(closest4(1:index)).^2));
%
% else %no neighbors in sight -> searching
% beta_avg = npi2pi(360*rand);
% alpha = beta_avg; %no need to add normal dist.
% v.theta(i) = npi2pi(v.theta(i) + alpha);
% % vmag = gamrnd(4,1/3.3,1,1); %random gamma if no neighbors
% % new_v.x(i) = vmag.*cosd(v.theta(i));
% % new_v.y(i) = vmag.*sind(v.theta(i));
% end
% end
%front priority - average frontmost 4 neighbors (with dead zone)
% w = 30;
% front4 = zeros(1,4); %pre-allocate for speed
% for i = 1:N
% index = 0;
% sorted = sort(abs(dp.theta(i,:)));
% for j = 1:N
% %remove dead zone, searching zone, and self, and limit to 4
% for k = 1:N
% if index < 4 && k ~= i && abs(dp.theta(i,j)) < (180 - w)...
% && d(i,j) < r3 && abs(dp.theta(i,j)) == sorted(k)
% front4(index + 1) = j;
% index = index + 1;
% end
% end
% end
%
% if index > 0 %at least 1 neighbor visible
% beta_avg = npi2pi(sum(beta(i,front4(1:index)))/index);
% %add randomness (normal distribution)
% sigma = 15; %standard deviation = 15 degrees
% alpha = beta_avg + sigma*randn(1);
%
% %calculate velocity vector
% %should magnitude have some randomness too?
% v.theta(i) = npi2pi(v.theta(i) + alpha);
% % new_v.x(i) = sqrt(sum(v.x(closest4(1:index)).^2));
% % new_v.y(i) = sqrt(sum(v.y(closest4(1:index)).^2));
%
% else %no neighbors in sight -> searching
% beta_avg = npi2pi(360*rand);
% alpha = beta_avg; %no need to add normal dist.
% v.theta(i) = npi2pi(v.theta(i) + alpha);
% % vmag = gamrnd(4,1/3.3,1,1); %random gamma if no neighbors
% % new_v.x(i) = vmag.*cosd(v.theta(i));
% % new_v.y(i) = vmag.*sind(v.theta(i));
% end
% end
%d_priority;
front_priority;
%--------------------------------------------------------------------------
%assign new velocity and position
%--------------------------------------------------------------------------
%velocity - vmag random on gamma distribution
vmag = gamrnd(4,1/3.3,1,N);
v.x = vmag.*cosd(v.theta);
v.y = vmag.*sind(v.theta);
%velocity - average of neigbors based on priority rule / random searching
% v.x = new_v.x;
% v.y = new_v.y;
%position
p.x = p.x + v.x; %v = d/t, assume t = 1 iteration (timestep)
p.y = p.y + v.y;
%--------------------------------------------------------------------------
%border patrol
%--------------------------------------------------------------------------
for i = 1:N
if p.x(i) > T
v.x(i) = -v.x(i);
p.x(i) = T + v.x(i);
v.theta(i) = npi2pi(180 - v.theta(i)); %reflection
end
if p.x(i) < 0
v.x(i) = -v.x(i);
p.x(i) = 0 + v.x(i);
v.theta(i) = npi2pi(180 - v.theta(i));
end
if p.y(i) > T
v.y(i) = -v.y(i);
p.y(i) = T + v.y(i);
v.theta(i) = npi2pi(-v.theta(i));
end
if p.y(i) < 0
v.y(i) = -v.y(i);
p.y(i) = 0 + v.y(i);
v.theta(i) = npi2pi(-v.theta(i));
end
end
%--------------------------------------------------------------------------