/
CollectiveMotion.py
175 lines (135 loc) · 6.59 KB
/
CollectiveMotion.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
import numpy as np
import theano
import theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
np.random.seed(1234)
rng = np.random.RandomState(1234)
trng = RandomStreams(rng.randint(999999))
class CollectiveMotion:
def __init__(self,
inf = 1e37):
pos, vel = T.fmatrices(['pos', 'vel'])
nc, N, n_steps = T.iscalars(['nc', 'N', 'n_steps'])
ra, rb, re, r0 = T.fscalars(['ra', 'rb', 're', 'r0'])
v0, j, b = T.fscalars(['v0', 'J', 'b'])
nu = trng.uniform(size=(N,2), low=0.0, high=3.14159, dtype='floatX')
def distance_tensor(X):
E = X.reshape((X.shape[0], 1, -1)) - X.reshape((1, X.shape[0], -1))
D = T.sqrt(T.sum(T.square(E), axis=2))
return D
def direction_tensor(X):
E = X.reshape((X.shape[0], 1, -1)) - X.reshape((1, X.shape[0], -1))
L = T.sqrt(T.sum(T.square(E), axis=2))
L = T.pow(L + T.identity_like(L), -1)
L = T.stack([L, L, L], axis=2)
return L * E
def neighbourhood(X):
D = distance_tensor(X)
N = T.argsort(D, axis=0)
mask = T.cast(T.lt(N,nc), 'float32')
return N[1:nc+1], mask
def alignment(X,Y):
n, d = neighbourhood(X)
return T.sum(Y[n], axis=0)
def cohesion(X, inf=100.0):
D = distance_tensor(X)
E = direction_tensor(X)
n, d = neighbourhood(X)
F = T.zeros_like(E)
D = T.stack([D, D, D], axis=2)
d = T.stack([d, d, d], axis=2)
c1 = T.lt(D, rb)
c2 = T.and_(T.gt(D, rb), T.lt(D, ra))
c3 = T.and_(T.gt(D, ra), T.lt(D, r0))
F = T.set_subtensor(F[c1], -E[c1])
F = T.set_subtensor(F[c2], 0.25 * (D[c2] - re) / (ra - re) * E[c2])
F = T.set_subtensor(F[c3], E[c3])
return T.sum(d * F, axis=0)
def perturbation(nu = nu):
phi = nu[:,0]
theta = 2.0 * nu[:,1]
return T.stack([T.sin(theta) * T.sin(phi), T.cos(theta) * T.sin(phi), T.cos(phi)], axis=1)
def step(X,dX):
X_ = X + dX
V_ = j * nc / v0 * (alignment(X, dX)) + b * (cohesion(X)) + nc * (perturbation())
dV = T.sqrt(T.sum(T.square(V_), axis=1)).reshape(V_.shape[0],1)
dV = T.stack([dV, dV, dV], axis=1)
V = v0 * V_ / dV
return T.cast(X_, 'float32'), T.cast(V, 'float32')
def probability(X,Y):
n, d = neighbourhood(X)
vDv = T.batched_dot(Y[n].swapaxes(0,1), Y)
p = T.exp((j / 2.0) * T.sum(vDv, axis=1))
return p / T.sum(p)
sim, update = theano.scan(step,
outputs_info=[pos,vel],
n_steps=n_steps)
pos_, vel_ = sim
mean_final_velocity = 1 / (N * v0) * T.sqrt(T.sum(T.square(T.sum(vel_[-1], axis=0))))
particle_probability = probability(pos_[-1], vel_[-1])
self.f = theano.function([pos, vel, nc, ra, rb, r0, re, j, v0, b, N, n_steps],
[pos_, vel_],
allow_input_downcast=True)
self.g = theano.function([pos, vel, nc, ra, rb, r0, re, j, v0, b, N, n_steps],
mean_final_velocity,
allow_input_downcast=True)
self.h = theano.function([pos, vel, nc, ra, rb, r0, re, j, v0, b, N, n_steps],
particle_probability,
allow_input_downcast=True)
def simulate_particles(self,
J = 0.02,
N = 256,
nc = 20,
ra = 0.8,
rb = 0.2,
re = 0.5,
r0 = 1.0,
v0 = 0.05,
b = 5.0,
n_steps = 500):
test_nu = np.random.uniform(0.0, np.pi, size=(N,2))
test_r = np.random.uniform(0.0, 1.0, size=(N,1))
test_pos = test_r * np.column_stack([np.sin(2.0*test_nu[:,0]) * np.sin(test_nu[:,1]),
np.cos(2.0*test_nu[:,0]) * np.sin(test_nu[:,1]),
np.cos(test_nu[:,1])])
test_vel = np.zeros((N,3))
x, v = self.f(test_pos, test_vel, nc, ra, rb, r0, re, J, v0, b, N, n_steps)
return x,v
def calculate_mean_velocity(self,
J = 0.02,
N = 256,
nc = 20,
ra = 0.8,
rb = 0.2,
re = 0.5,
r0 = 1.0,
v0 = 0.05,
b = 5.0,
n_steps = 500):
test_nu = np.random.uniform(0.0, np.pi, size=(N,2))
test_r = np.random.uniform(0.0, 1.0, size=(N,1))
test_pos = test_r * np.column_stack([np.sin(2.0*test_nu[:,0]) * np.sin(test_nu[:,1]),
np.cos(2.0*test_nu[:,0]) * np.sin(test_nu[:,1]),
np.cos(test_nu[:,1])])
test_vel = np.zeros((N,3))
mean_final_velocity = self.g(test_pos, test_vel, nc, ra, rb, r0, re, J, v0, b, N, n_steps)
return mean_final_velocity
def calculate_probability(self,
J = 0.02,
N = 256,
nc = 20,
ra = 0.8,
rb = 0.2,
re = 0.5,
r0 = 1.0,
v0 = 0.05,
b = 5.0,
n_steps = 500):
test_nu = np.random.uniform(0.0, np.pi, size=(N,2))
test_r = np.random.uniform(0.0, 1.0, size=(N,1))
test_pos = test_r * np.column_stack([np.sin(2.0*test_nu[:,0]) * np.sin(test_nu[:,1]),
np.cos(2.0*test_nu[:,0]) * np.sin(test_nu[:,1]),
np.cos(test_nu[:,1])])
test_vel = np.zeros((N,3))
probability = self.h(test_pos, test_vel, nc, ra, rb, r0, re, J, v0, b, N, n_steps)
return probability