-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswarm.py
297 lines (190 loc) · 8.74 KB
/
swarm.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from typing import List
import numpy as np
import matplotlib.pyplot as plt
from particle import Particle
import random
class Swarm():
def __init__(self, particles : List[Particle], w_0 : float) -> None:
self.particles = particles
self.N = len(self.particles)
self.control = np.zeros((self.N, 1))
self.w_0 = w_0
self.del_U = np.zeros((self.N, 1))
self.R = 0
def compute_control_grad(self, K : float) -> None:
# Equation 8
for index, particle in enumerate(self.particles):
self.control[index] = self.w_0 - K/self.N * sum([np.sin(-part.theta + particle.theta).item(0) for part in self.particles])
def compute_control_phase_symmetry(self, kappa, K):
# Equation 19
self.find_center_of_mass()
for index, particle in enumerate(self.particles):
del_U_1 = 1/self.N*sum([np.sin(-part.theta + particle.theta).item(0) for part in self.particles])
del_U = -K*del_U_1
self.control[index] = self.w_0*(1+kappa*((np.exp(1j*particle.theta).real*(particle.pos - self.R).real)+(np.exp(1j*particle.theta).imag*(particle.pos - self.R).imag))) + kappa*del_U_1 + del_U
def parrallel_to_circular(self, kappa, K, R_0):
# Equation 63 (19 with beacon R_0)
for index, particle in enumerate(self.particles):
del_U_1 = 1/self.N*sum([np.sin(-part.theta + particle.theta).item(0) for part in self.particles])
del_U = -K*del_U_1
self.control[index] = self.w_0*(1+kappa*((np.exp(1j*particle.theta).real*(particle.pos - R_0).real)+(np.exp(1j*particle.theta).imag*(particle.pos - R_0).imag))) + del_U
def circular_to_circular(self, kappa, K, R_0):
# Equation 63 (19 with beacon R_0)
for index, particle in enumerate(self.particles):
del_U_1 = 1/self.N*sum([np.sin(-part.theta + particle.theta).item(0) for part in self.particles])
del_U = -K*del_U_1
self.control[index] = self.w_0*(1+kappa*((np.exp(1j*particle.theta).real*(particle.pos - R_0).real)+(np.exp(1j*particle.theta).imag*(particle.pos - R_0).imag))) + del_U
def circular_to_parrallel(self, theta_0, w_0, kappa, d, particle, K):
# Eq (66)
# K < 0, d > 0
self.find_center_of_mass()
r_tilde_N = particle.pos - self.R
del_U_1 = 1/self.N*sum([np.sin(part.theta - particle.theta).item(0) for part in self.particles])
theta_N = particle.theta
inner_product = r_tilde_N.real * particle.vel.real + r_tilde_N.imag * particle.vel.imag
u_N = w_0*(1+kappa*inner_product) + kappa*del_U_1 - K,del_U_1 + d*np.sin(theta_0 - theta_N)
return u_N
def compute_control_relative_equilibria(self, kappa, K_m):
# Equation 37
self.find_center_of_mass()
for index, particle in enumerate(self.particles):
del_U = self.calc_del_U(K_m, swarm.N, index)
del_U_1 = 1/self.N*sum([np.sin(part.theta - particle.theta).item(0) for part in self.particles])
self.control[index] = self.w_0*(1+kappa*((np.exp(1j*particle.theta).real*(particle.pos - self.R).real)+(np.exp(1j*particle.theta).imag*(particle.pos - self.R).imag))) - kappa*del_U_1 + del_U
def calc_del_U(self, K_m_0, M, k):
del_U = 0.
K_m = K_m_0
for j in range(self.N):
del_U_m = []
for m in range(1,M+1):
if m > swarm.N/2:
K_m = 0
del_U_m.append(K_m/float(m) * np.sin(m*(swarm.particles[k].theta-swarm.particles[j].theta)))
del_U += sum(del_U_m)
K_m = K_m_0
return del_U
def find_center_of_mass(self):
self.R = 0
for particle in self.particles:
self.R += particle.pos
self.R /= len(self.particles)
def euclid_propogate(self):
for control, particle in zip(self.control, self.particles):
particle.euclid_propogate(control)
if __name__ == "__main__":
K = -0.05 # 0.0125/4.0 # Use this for the splay state.
w_0 = 1/25.0 # 0.1
kappa = 0.1
particles = []
for _ in range(12):
particles.append(Particle(random.uniform(0,2*np.pi), 1, complex(random.uniform(0,10), random.uniform(0,10)), w_0))
swarm = Swarm(particles, w_0)
num_steps = 200 # must be larger than 100
swarm.w_0 = w_0
for t in range(num_steps):
# swarm.compute_control_grad(K)
swarm.compute_control_phase_symmetry(kappa, K)
# swarm.compute_control_relative_equilibria(kappa, K)
# swarm.circular_to_parrallel(90*np.pi/180.0, w_0, kappa, d, swarm.particles[-1],K) # K < 0, d >0, w_0 = kappa = 0
swarm.euclid_propogate()
theta_0 = 45*np.pi/180.0
# IMPULSE
for particle in swarm.particles:
particle.theta = theta_0
particle.vel = np.exp(1j*particle.theta)
# Circular-to-parrallel
K = -0.1 # 0.0125/4.0 # Use this for the splay state.
w_0 = 0
kappa = 0
d = 0.1
swarm.w_0 = w_0
for t in range(num_steps):
# swarm.compute_control_grad(K)
swarm.compute_control_phase_symmetry(kappa, K)
# swarm.compute_control_relative_equilibria(kappa, K)
swarm.circular_to_parrallel(theta_0, w_0, kappa, d, swarm.particles[-1],K) # K < 0, d > 0, w_0 = kappa = 0
swarm.euclid_propogate()
theta_0 = (15-90)*np.pi/180.0
# IMPULSE
for particle in swarm.particles:
particle.theta = theta_0
particle.vel = np.exp(1j*particle.theta)
# Parrallel-to-parrallel
K = -0.1 # 0.0125/4.0 # Use this for the splay state.
w_0 = 0
kappa = 0
d = 0.1
swarm.w_0 = w_0
for t in range(num_steps):
# swarm.compute_control_grad(K)
swarm.compute_control_phase_symmetry(kappa, K)
# swarm.compute_control_relative_equilibria(kappa, K)
swarm.circular_to_parrallel(theta_0, w_0, kappa, d, swarm.particles[-1],K) # K < 0, d > 0, w_0 = kappa = 0
swarm.euclid_propogate()
theta_0 = 45*np.pi/180.0
# IMPULSE
for particle in swarm.particles:
particle.theta = theta_0
particle.vel = np.exp(1j*particle.theta)
# Parrallel-to-parrallel
K = -0.1
w_0 = 0
kappa = 0
d = 0.1
swarm.w_0 = w_0
for t in range(num_steps):
# swarm.compute_control_grad(K)
swarm.compute_control_phase_symmetry(kappa, K)
# swarm.compute_control_relative_equilibria(kappa, K)
swarm.circular_to_parrallel(theta_0, w_0, kappa, d, swarm.particles[-1],K) # K < 0, d > 0, w_0 = kappa = 0
swarm.euclid_propogate()
# Parrallel-to-circular
K = -0.1
w_0 = 1/25.0
kappa = .1
d = 0.1
swarm.w_0 = w_0
# IMPULSE
swarm.find_center_of_mass()
R_0 = swarm.R
for particle in swarm.particles:
num = 1j*w_0*(particle.pos - R_0) # Find better explanation and physical meaning.
complex_arg = np.arctan2(num.imag,num.real)
particle.theta = complex_arg - particle.theta
particle.vel = np.exp(1j*particle.theta)
for t in range(num_steps):
# swarm.compute_control_grad(K)
# swarm.compute_control_phase_symmetry(kappa, K)
# swarm.compute_control_relative_equilibria(kappa, K)
# swarm.circular_to_parrallel(theta_2, w_0, kappa, d, swarm.particles[-1],K) # K < 0, d > 0, w_0 = kappa = 0
swarm.parrallel_to_circular(kappa,K,R_0)
swarm.euclid_propogate()
theta_0 = np.pi
# Circular-to-circular
K = -0.0125/4.0 # Use this for the splay state.
w_0 = 1/50.0
kappa = .2
swarm.w_0 = w_0
swarm.find_center_of_mass()
R_0 = swarm.R
for t in range(3*num_steps):
# swarm.compute_control_grad(K)
swarm.compute_control_phase_symmetry(kappa, K)
# swarm.compute_control_relative_equilibria(kappa, K)
# swarm.circular_to_parrallel(theta_0, w_0, kappa, d, swarm.particles[-1],K) # K < 0, d > 0, w_0 = kappa = 0
swarm.circular_to_circular(kappa,K,R_0)
swarm.euclid_propogate()
arrow_length = 10
head_width = 1
head_length = 2
for particle in swarm.particles:
plt.plot(particle.positions_real, particle.positions_imag)
plt.scatter(particle.positions_real[-1], particle.positions_imag[-1])
# plt.scatter(particle.circle_centers_real[-1], particle.circle_centers_imag[-1]) # Plot circle centers.
plt.arrow(particle.positions_real[-1], particle.positions_imag[-1], arrow_length*np.cos(particle.theta).item(0),
arrow_length*np.sin(particle.theta).item(0), head_width=head_width, head_length=head_length, fc='black', ec='black')
plt.axis('equal')
plt.title('Swarm Dynamics')
plt.xlabel('Re')
plt.ylabel('Im')
plt.show()