forked from thoppe/tf_thomson_charges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thomson_problem_clean.py
442 lines (350 loc) · 13.3 KB
/
thomson_problem_clean.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# -*- coding: utf-8 -*-
"""
CS520 Spring 2020
Class Project
Authors: Marcelo Souza, Poyraz Bozkurt
"""
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pl
from matplotlib.patches import Circle
from mpl_toolkits.mplot3d import Axes3D
import tensorflow as tf
import time
from scipy.optimize import minimize
from scipy.optimize import check_grad
import cvxopt
from cvxopt import solvers, matrix
from numpy import array
#tf.compat.v1.disable_eager_execution()
# Energy
def point_energy(v1, v2, alpha = 2.):
return la.norm(v2 - v1) ** (-alpha)
def total_energy(points, alpha = 2.):
e = 0.
for i in range(len(points)):
for j in range(i + 1, len(points)):
e = e + point_energy(points[i], points[j], alpha)
return e
# Normalized energy - does not depend on vectors' lengths
def normalized_energy(v1, v2, alpha = 2.):
n1 = la.norm(v1)
n2 = la.norm(v2)
return (la.norm((v2 / n2) - (v1 / n1)) ** (-alpha))
def normalized_total_energy(points, alpha = 2.):
e = 0.
for i in range(len(points)):
for j in range(i + 1, len(points)):
e = e + normalized_energy(points[i], points[j], alpha)
return e
# Gradient
def gradient(v, u):
dif = u - v
return dif * 2. * la.norm(dif) ** (-4)
def total_gradient(points):
grads = []
for i, p in enumerate(points):
g = p - p # zero vector
for j, q in enumerate(points):
if i != j:
g = g + gradient(p, q)
grads.append(g)
return np.array(grads)
# Serialization
# Map a set of N points in S^3 to a point in R^3N
def serialize(points):
return np.hstack(points)
def deserialize(serial_points):
rows = []
for i in range(int(len(serial_points) / 3)):
rows.append(serial_points[3*i:3*i+3])
return np.vstack(rows)
def serial_total_energy(serial_points):
# to do: implement appropriate routine
return total_energy(deserialize(serial_points))
def serial_normalized_total_energy(serial_points):
# to do: implement appropriate routine
return normalized_total_energy(deserialize(serial_points))
def serial_gradient(serial_points):
grads = []
for i in range(int(len(serial_points) / 3)):
v = serial_points[3*i:3*i+3]
g = [0., 0., 0.]
for j in range(int(len(serial_points) / 3)):
if j != i:
u = serial_points[3*j:3*j+3]
g = g + gradient(v, u)
grads.append(g)
return np.hstack(grads)
def serial_gradient_2d(serial_points):
grads = []
for i in range(int(len(serial_points) / 2)):
v = serial_points[2*i:2*i+2]
g = [0., 0.]
for j in range(int(len(serial_points) / 2)):
if j != i:
u = serial_points[2*j:2*j+2]
g = g + gradient(v, u)
grads.append(g)
return np.hstack(grads)
# Hessian
def serial_hessian(serial_points, verbose=False):
H = []
for i in range(int(len(serial_points) / 3)):
p = serial_points[3*i:3*i+3]
for di in range(3):
# build one row of the Hessian
row = []
msg = []
for j in range(int(len(serial_points) / 3)):
if (i==j):
for dj in range(3):
# sum over all q != p [optimize: this is the negative of the sum of the else section]
s = 0.
for k in range(int(len(serial_points) / 3)):
if k != i:
q = serial_points[3*k:3*k+3]
dif = q - p
norm = la.norm(dif)
s = s + (16. * norm ** (-6)) * (q[dj] - p[dj]) * (q[di] - p[di])
if dj == di:
s = s - (2. * norm ** (-4))
row.append(s)
msg.append('({:1d},{:1d})x({:1d},{:1d})'.format(i,di,j,dj))
else:
q = serial_points[3*j:3*j+3]
dif = q - p
norm = la.norm(dif)
for dj in range(3):
s = (16. * norm ** (-6)) * (q[dj] - p[dj]) * (q[di] - p[di])
if dj == di:
s = s + (2. * norm ** (-4))
row.append(s)
msg.append('({:1d},{:1d})x({:1d},{:1d})'.format(i,di,j,dj))
H.append(row)
if verbose: print(msg)
return np.array(H)
# Hessian
def serial_truncated_hessian(serial_points, verbose=False):
# each point affects its own gradient
# use this simpler version to avoid rank-0 Hessian
H = []
for i in range(int(len(serial_points) / 3)):
p = serial_points[3*i:3*i+3]
for di in range(3):
# build one row of the Hessian
row = []
msg = []
for j in range(int(len(serial_points) / 3)):
if (i==j):
for dj in range(3):
# sum over all q != p [optimize: this is the negative of the sum of the else section]
s = 0.
for k in range(int(len(serial_points) / 3)):
if k != i:
q = serial_points[3*k:3*k+3]
dif = q - p
norm = la.norm(dif)
s = s + (16. * norm ** (-6)) * (q[dj] - p[dj]) * (q[di] - p[di])
if dj == di:
s = s - (2. * norm ** (-4))
row.append(s)
msg.append('({:1d},{:1d})x({:1d},{:1d})'.format(i,di,j,dj))
else:
for dj in range(3):
row.append(0.)
msg.append('({:1d},{:1d})x({:1d},{:1d})'.format(i,di,j,dj))
H.append(row)
if verbose: print(msg)
return np.array(H)
# Wse this version to avoid rank-0 Hessian
def serial_adjusted_hessian(serial_points, adjust = 0.001):
h = serial_hessian(serial_points)
return adjust * np.diag(np.diagonal(h)) + h
# Initial set of random points
def random_points(n, d = 3, normalize = True):
points = []
for i in range(n):
p = np.random.normal(size = d)
if normalize: p = p / la.norm(p)
points.append(p)
return np.array(points)
N = 100
points = random_points(N)
serial_points = serialize(points)
# Gradient check
check_grad(serial_total_energy, serial_gradient, serial_points) # eg. 3.37e-05
# Adam Optimization (using TensorFlow)
minimization_steps = 500
print_at_each = 10
energy_decay = []
# --- central loop ---
vertices = [tf.Variable(p) for p in points]
opt = tf.keras.optimizers.Adam(learning_rate=0.001)
print('\nprocess start')
print('\nmethod = adam')
print('#points = {:4d}'.format(N))
print('#steps = {:4d}'.format(minimization_steps))
print('initial energy {:1.4f}'.format(total_energy(points)))
start_time = time.time()
E = []
for step in range(minimization_steps):
with tf.GradientTape() as t:
edges = []
N = len(points)
for i in range(N):
for j in range(i + 1, N):
edges.append(tf.subtract(vertices[i], vertices[j]))
energy = tf.math.add_n([1. / tf.math.reduce_sum(tf.math.abs(e) ** 2.) for e in edges])
gradients = t.gradient(energy, vertices) # <-- choose TensorFlow gradients
#gradients = total_gradient(vertices) # <-- choose own gradients
opt.apply_gradients(zip(gradients, vertices))
vertices = [tf.Variable(v / la.norm(v.numpy())) for v in vertices]
e = energy.numpy()
#e = normalized_total_energy([v.numpy() for v in vertices])
E.append(e)
if print_at_each != 0:
if (step + 1) % print_at_each == 0:
print('step {:4d} energy {:1.4f}'.format(step + 1, e))
partial_time = time.time()
print('partial time {:8.0f} sec'.format(partial_time - start_time))
end_time = time.time()
energy_decay.append(E)
print('final energy {:1.4f}'.format(energy.numpy()))
print('execution time {:8.0f} sec'.format(end_time - start_time))
# --- end of central loop ---
# Show energy decay
for E in energy_decay:
pl.plot(E)
# Show sphere with random and optimized points
opt_points = [v.numpy() for v in vertices]
fig = pl.figure()
ax = fig.add_subplot(111, projection='3d')
for p in points:
ax.scatter(p[0], p[1], p[2], color='blue')
for p in opt_points:
ax.scatter(p[0], p[1], p[2], color='red')
# surface to help visualization
u = np.linspace(0, 2 * np.pi, 200)
v = np.linspace(0, np.pi, 1000)
x = 1 * np.outer(np.cos(u), np.sin(v))
y = 1 * np.outer(np.sin(u), np.sin(v))
z = 1 * np.outer(np.ones(np.size(u)), np.cos(v))
elev = 10.0
rot = 80.0 / 180 * np.pi
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='lightgrey', linewidth=0, alpha=0.5)
pl.show()
# Trust Region
'trust-ncg' # Newton conjugate gradient
'trust-krylov' # Krylov
#method = 'trust-krylov'
method = 'trust-ncg'
print('\nprocess start')
print('method = ' + method)
print('#points = {:4d}'.format(N))
print('#steps = {:4d}'.format(minimization_steps))
start_time = time.time()
opt = minimize(serial_normalized_total_energy, serial_points, method='trust-ncg', jac=serial_gradient, hess=serial_hessian)
end_time = time.time()
energy_decay.append(E)
print('execution time {:8.0f} sec'.format(end_time - start_time))
if opt['success']:
print('optimization ended with success')
else:
print('optimization ended with failure')
opt_points = deserialize(opt['x'] )
for i in range(opt_points.shape[0]):
opt_points[i] = opt_points[i] / la.norm(opt_points[i])
e0 = total_energy(points)
e1 = total_energy(opt_points)
print('initial points energy: {:1.4f}\nfinal energy: {:1.4f}'.format(e0, e1))
# Convex optimization
# generates the set S of n points with minimal energy
# conditional on sum(p in A)[ sum(q in S)[|<s,t>|] ] = constant
def cvx_min_energy(n, A, S0):
# CVXOPT format:
#
# f[0](x) = energy(x) (to be minized)
# f[1](x) = first constraint <= 0
# = CONST. - sum(p in A) sum(q in S) | < p , q > |
#
# Df[0] = gradient of energy wrt x
# Df[1] = gradient of constraint wrt x
#
# H = z[0] * Hessian of energy wrt x
# (gradient of constraint is zero)
def F(x=None, z=None):
# x is the new set to be generated
# x = min Energy
# st. sum(p in A) sum(q in S) | < p , q > | <= CONST.
# S = normalize(x)
if x is None:
return 1, matrix(S0) # cvxopt specification - 1 constraint, x0 = previous set
_A = deserialize(A)
_x = deserialize(array(x)[:,0]) # deserialize(array(x)[:,0]) # to numpy matrix
C = len(_A) * len(_A) # some constant - there will be a normalization in the end
f0 = total_energy(_x)
prod = np.dot(_A, _x.T)
prod_sign = np.sign(prod)
f1 = np.abs(prod).sum().sum() - C
if f1 > 0: return None # cvxopt specification
f = np.array([f0, f1]).T
Df0 = total_gradient(_x)
Df1 = prod_sign.dot(_A)
Df = matrix(np.array([np.hstack(Df0), np.hstack(Df1)]))
if z is None: return f, Df
H = matrix(z[0] * serial_adjusted_hessian(S, 10.))
return f, Df, H
S = solvers.cp(F)
S = deserialize(S)
S = S / la.norm(S)
return S
N = 20 # size of set of points
m = 1000 # size of matrix A
A = random_points(m, normalize = False)
S0 = random_points(N)
cvx_min_energy(N, A, S0)
# Visual check of the gradient function
p = np.random.normal(size = 2)
p = p / la.norm(p)
x1 = p
p = np.random.normal(size = 2)
p = p / la.norm(p)
x2 = p
G = gradient(x1, x2)
g1 = G
g2 = -G
_x1 = x1 - g1
_x1 = _x1 / la.norm(_x1)
_x2 = x2 - g2
_x2 = _x2 / la.norm(_x2)
G = gradient(_x1, _x2)
_g1 = G
_g2 = -G
__x1 = _x1 - _g1
__x1 = __x1 / la.norm(__x1)
__x2 = _x2 - _g2
__x2 = __x2 / la.norm(__x2)
G = gradient(_x1, _x2)
__g1 = G
__g2 = -G
print('distance between points {:1.2f}'.format(la.norm(x1 - x2)))
print('gradient norm {:1.2f}'.format(la.norm(g1)))
circle = Circle((0., 0.), radius = 1., fill = False)
fig = pl.figure()
ax = fig.add_subplot(111)
ax.axis('equal')
ax.add_patch(circle)
ax.scatter(0., 0., color='black')
ax.scatter(x1[0], x1[1], color='blue')
ax.scatter(x2[0], x2[1], color='red')
ax.arrow(x1[0], x1[1], g1[0], g1[1])
ax.arrow(x2[0], x2[1], g2[0], g2[1])
ax.scatter(_x1[0], _x1[1], marker='^', color='blue')
ax.scatter(_x2[0], _x2[1], marker='^', color='red')
ax.arrow(_x1[0], _x1[1], _g1[0], _g1[1])
ax.arrow(_x2[0], _x2[1], _g2[0], _g2[1])
ax.scatter(__x1[0], __x1[1], marker='s', color='blue')
ax.scatter(__x2[0], __x2[1], marker='s', color='red')
ax.arrow(__x1[0], __x1[1], __g1[0], __g1[1])
ax.arrow(__x2[0], __x2[1], __g2[0], __g2[1])