-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththomson.py
More file actions
executable file
·285 lines (204 loc) · 7.39 KB
/
thomson.py
File metadata and controls
executable file
·285 lines (204 loc) · 7.39 KB
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
import argparse
import logging
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(precision=5)
np.set_printoptions(suppress=True)
# Logging options
logging.basicConfig(
# filename=os.path.join(dir_path, 'thomson_problem.log'),
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# Define the parser used to process the command line input and then add a bunch of arguments
parser = argparse.ArgumentParser(
prog="python thomson.py",
description="Find approximate solutions to the Thomson problem in arbitrary dimension",
allow_abbrev=False,
)
parser.add_argument(
"-n",
metavar="dimension",
type=int,
required=False,
default=3,
help="the dimension of Euclidean space (default: 3)",
)
parser.add_argument(
"-m",
metavar="points",
type=int,
required=False,
default=8,
help="the number of point particles on the (n-1) sphere (default: 8)",
)
parser.add_argument(
"-p",
metavar="power",
type=int,
required=False,
default=1,
help="the power of the inverse radius in the potential energy function (default: 1)",
)
parser.add_argument(
"-eps",
metavar="epsilon",
type=float,
required=False,
default=0.1,
help="the gradient descent epsilon - i.e. the step size (default: 0.1)",
)
parser.add_argument(
"-max_iter",
metavar="max_iteration",
type=int,
required=False,
default=1e3,
help="the number of steps of gradient descent the program will take (default: 1000)",
)
parser.add_argument(
"--plot",
metavar="plot",
required=False,
default=False,
action=argparse.BooleanOptionalAction,
help="plot the found solution - only valid when n = 3 (default: false)",
)
def generate_random_vectors_of_unit_norm(n, m, fixed_seed=False):
if fixed_seed:
np.random.seed(0)
# List comprehensions are great
vector_lst = [generate_random_vector_of_unit_norm(n) for _ in range(m)]
return np.stack(vector_lst, axis=0)
def generate_random_vector_of_unit_norm(n):
x = np.random.standard_normal(n)
x = x / np.linalg.norm(x)
return x
def calculate_total_energy(array, p=1):
energy = 0
# Compute the pairwise differences
diff = array[:, None, :] - array[None, :, :]
dm = np.sum(diff**2, axis=-1) ** 0.5
mask = np.ones(dm.shape, dtype=bool)
np.fill_diagonal(mask, 0)
# The energy for the potential V(r) = r**(-p) is sum (dm[mask]^-p) / 2.0
energy = (dm[mask] ** (-p)).sum() / 2.0
return energy
# I'm leaving this here as a reminder of how I used to calculate this
def grad_func_old(array, p=1):
# Compute the pairwise differences
diff = array[:, None, :] - array[None, :, :]
# Compute the square of the distance matrix and add the unit matrix to
# avoid division by zero when calculating the inverse distances
dms = np.sum(diff**2, axis=-1) + np.eye(len(array))
# Inverse of the distance matrix squared
inv_distances = dms ** (-(p + 1) / 2)
# Matrix of gradients
grad_mtx = -2.0 * p * np.sum(diff * inv_distances[..., None], axis=1)
return grad_mtx
def grad_func(array, p=1):
# Compute pairwise squared differences
sq_diff = (
np.sum(array[:, None, :] ** 2, axis=-1)
+ np.sum(array[None, :, :] ** 2, axis=-1)
- 2.0 * np.dot(array, array.T)
)
# Add the unit matrix to avoid division by zero when calculating the
# inverse distance
sq_diff = sq_diff + np.eye(len(array))
# Inverse of the distance matrix squared
inv_distances = sq_diff ** (-(p + 1) / 2)
# Pairwise differences
diff = array[:, None, :] - array[None, :, :]
# Matrix of gradients
grad_mtx = -2.0 * p * np.einsum("ijk,ij->ik", diff, inv_distances)
return grad_mtx
# https://en.wikipedia.org/wiki/Barzilai%E2%80%93Borwein_method
def barzilai_borwein(v, v_prev, g, g_prev, eps):
s = v - v_prev # Displacement
y = g - g_prev # Change in gradient
s_dot_y = np.sum(s * y)
if s_dot_y > 0:
eps = np.sum(s * s) / s_dot_y
return eps
else:
return eps
def minimise_energy(w_init, max_iter=1e3, eps=0.01, p=1):
logging.debug("Starting energy minimisation")
v = w_init
# Pre-calculate the gradient for the first step
g = grad_func(v, p=p)
e = calculate_total_energy(w_init, p=p)
logging.debug("Energy of initial configuration is: {}".format(e))
# iterate over max_iter steps
for n in range(int(max_iter)):
logging.debug("{}/{}".format(n, int(max_iter)))
# Store the previous position and gradient
v_prev = v
g_prev = g
# Take one step of vanilla gradient descent using the current eps
v = v - eps * grad_func(v, p=p)
# Normalise each row so that the points are still on the unit sphere
norm_of_rows = np.linalg.norm(v, axis=1)
v = v / norm_of_rows[:, np.newaxis]
# Calculate the new gradient at the new position
g = grad_func(v, p=p)
# Calculate the next step-size using the Barzilar-Borwein method
eps = barzilai_borwein(v, v_prev, g, g_prev, eps)
logging.debug(f"Eps: {eps}")
energy = calculate_total_energy(v, p=p)
logging.debug(f"Energy: {energy}")
return v
def run_once(n, m, p, eps, max_iter):
w_init = generate_random_vectors_of_unit_norm(n=n, m=m)
logging.info("Initial energy: {}".format(calculate_total_energy(w_init, p=p)))
logging.debug("Initial configuration:")
logging.debug("{}".format(repr(w_init)))
# sanity check that the norms are close to unity
logging.debug("Norms of init vectors are:")
[logging.debug(np.linalg.norm(w_init[i])) for i in range(m)]
configuration = minimise_energy(w_init=w_init, eps=eps, max_iter=max_iter, p=p)
energy = calculate_total_energy(configuration, p=p)
np.savetxt(output_file_path(n, m, p), configuration, delimiter=",")
logging.info("Minimised energy: {}".format(energy))
logging.debug("Final configuration after energy minimisation:")
logging.debug(repr(configuration))
def plot_points_on_two_sphere(points):
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
# Generate spherical data
r = 1
theta = np.linspace(0, 2.0 * np.pi, 40)
phi = np.linspace(0, np.pi, 40)
# Convert to cartesian coordinates
x = r * np.outer(np.cos(theta), np.sin(phi))
y = r * np.outer(np.sin(theta), np.sin(phi))
z = r * np.outer(np.ones(np.size(theta)), np.cos(phi))
ax.plot_surface(x, y, z, rstride=1, cstride=1, color="c", alpha=0.3, linewidth=1)
ax.scatter(points[:, 0], points[:, 1], points[:, 2], color="r")
ax.set_xlim(left=-1, right=1)
ax.set_ylim(bottom=-1, top=1)
ax.set_aspect("equal")
plt.tight_layout()
plt.axis("off")
plt.show()
def output_file_path(n, m, p):
# TODO: return absolute instead of relative path
return f"./output/n={n}_m={m}_p={p}.csv"
def main():
args = parser.parse_args()
n = args.n
m = args.m
p = args.p
eps = args.eps
max_iter = args.max_iter
plot = args.plot
run_once(n=n, m=m, p=p, eps=eps, max_iter=max_iter)
if plot is True and n == 3:
points = np.genfromtxt(output_file_path(n, m, p), delimiter=",")
plot_points_on_two_sphere(points)
elif plot and n != 3:
logging.error("Can only plot solutions for n=3 (i.e. the 2-sphere)")
if __name__ == "__main__":
main()