-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut_dist_fortran.f90
More file actions
306 lines (269 loc) · 7.7 KB
/
Copy pathcut_dist_fortran.f90
File metadata and controls
306 lines (269 loc) · 7.7 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
module cutnorm_mod
implicit none
private
public :: cut_distance
contains
function cut_distance(G, H) result(sdp)
implicit none
real(8), intent(in) :: G(:,:), H(:,:)
real(8) :: sdp
integer :: n
real(8), allocatable :: diff(:,:)
n = size(G,1)
allocate(diff(n,n))
diff = (G - H) / dble(n*n)
sdp = cut_norm(diff)
deallocate(diff)
end function cut_distance
function cut_norm(A) result(sdp_result)
implicit none
real(8), intent(in) :: A(:,:)
real(8) :: sdp_result
integer :: n1, N, n2, p, temp_int, i, j
real(8) :: temp
real(8), allocatable :: A_col_sum(:), A_row_sum(:)
real(8), allocatable :: A_ext(:,:), A_final(:,:)
real(8), allocatable :: x0(:,:), x(:,:)
real(8), allocatable :: nrmx0(:)
real(8), allocatable :: U(:,:), V(:,:), M(:,:)
n1 = size(A,1)
allocate(A_col_sum(n1), A_row_sum(n1))
do j = 1, n1
A_col_sum(j) = sum(A(:,j))
end do
do i = 1, n1
A_row_sum(i) = sum(A(i,:))
end do
temp = sum(A_col_sum)
allocate(A_ext(n1, n1+1))
A_ext(:,1:n1) = A
do i = 1, n1
A_ext(i, n1+1) = -A_row_sum(i)
end do
N = n1 + 1
allocate(A_final(N,N))
A_final(1:n1,:) = A_ext
do j = 1, n1
A_final(N,j) = -A_col_sum(j)
end do
A_final(N,N) = temp
temp = sqrt(2.0d0*dble(n1)) / 2.0d0
temp_int = nint(temp)
if (temp_int > 100) then
p = 100
else if (temp_int < 1) then
p = 1
else
p = temp_int
end if
n2 = 2 * N
allocate(x0(p,n2))
call random_normal(x0)
allocate(nrmx0(n2))
do j = 1, n2
nrmx0(j) = sqrt(sum(x0(:,j)**2))
if (nrmx0(j) > 1.0d-12) x0(:,j) = x0(:,j) / nrmx0(j)
end do
deallocate(nrmx0)
call optimize(x0, A_final, x)
allocate(U(p,N), V(p,N))
U = x(:,1:N)
V = x(:,N+1:n2)
allocate(M(N,N))
M = matmul(transpose(U), V)
sdp_result = abs(sum(A_final * M)) / 4.0d0
deallocate(A_col_sum, A_row_sum, A_ext, A_final, x0, x, U, V, M)
end function cut_norm
subroutine cut_norm_quad(V, A, f, g)
implicit none
real(8), intent(in) :: V(:,:), A(:,:)
real(8), intent(out) :: f
real(8), allocatable, intent(out) :: g(:,:)
integer :: p, n
real(8), allocatable :: Us(:,:), Vs(:,:)
p = size(V,1)
n = size(A,1)
allocate(Us(p,n), Vs(p,n))
Us = V(:,1:n)
Vs = V(:,n+1:2*n)
allocate(g(p,2*n))
g(:,1:n) = 2.0d0 * matmul(Vs, transpose(A))
g(:,n+1:2*n) = 2.0d0 * matmul(Us, A)
f = ( sum(g(:,1:n) * Us) + sum(g(:,n+1:2*n) * Vs) ) / 2.0d0
deallocate(Us, Vs)
end subroutine cut_norm_quad
subroutine optimize(x, A, x_out)
implicit none
real(8), intent(inout) :: x(:,:)
real(8), intent(in) :: A(:,:)
real(8), allocatable, intent(out) :: x_out(:,:)
integer :: p, n2, itr, mxitr, nt, n_iter_range, ls_iter, i
real(8) :: xtol, ftol, gtol, rho, eta, gamma, tau, tau_orig
real(8), allocatable :: nrmx(:)
real(8) :: f, fp, deriv, XDiff, FDiff, sy
real(8) :: Q, Qp, Cval, temp_tau
real(8), allocatable :: g(:,:), gp(:,:), dtX(:,:), dtXP(:,:), s(:,:), y(:,:)
real(8), allocatable :: xtg(:), gg(:), xx(:), xxgg(:)
real(8), allocatable :: a1(:), a2(:), xp(:,:)
real(8), allocatable :: crit(:,:), mcrit(:)
real(8) :: nrmG
mxitr = 600
xtol = 1.0d-8; ftol = 1.0d-10; gtol = 1.0d-8
rho = 1.0d-4; eta = 0.1d0; gamma = 0.85d0
tau = 1.0d-3; tau_orig = tau
nt = 5
p = size(x,1)
n2 = size(x,2)
allocate(nrmx(n2))
do ls_iter = 1, n2
nrmx(ls_iter) = sqrt(sum(x(:,ls_iter)**2))
if (nrmx(ls_iter) > 1.0d-12) x(:,ls_iter) = x(:,ls_iter) / nrmx(ls_iter)
end do
deallocate(nrmx)
call cut_norm_quad(x, A, f, g)
allocate(xtg(n2), gg(n2), xx(n2), xxgg(n2))
do ls_iter = 1, n2
xtg(ls_iter) = sum(x(:,ls_iter) * g(:,ls_iter))
gg(ls_iter) = sum(g(:,ls_iter)**2)
xx(ls_iter) = sum(x(:,ls_iter)**2)
xxgg(ls_iter) = xx(ls_iter) * gg(ls_iter)
end do
allocate(dtX(p,n2))
do ls_iter = 1, n2
dtX(:,ls_iter) = x(:,ls_iter)*xtg(ls_iter) - g(:,ls_iter)
end do
nrmG = sqrt(sum(dtX**2))
allocate(crit(mxitr,3))
crit = 0.0d0
Q = 1.0d0
Cval = f
do itr = 1, mxitr
dtXP = dtX; xp = x; gp = g; fp = f
deriv = rho * nrmG**2
ls_iter = 0
do
ls_iter = ls_iter + 1
temp_tau = tau / 2.0d0
allocate(a1(n2), a2(n2))
do i = 1, n2
a1(i) = ((1.0d0 + temp_tau*xtg(i))**2 - temp_tau**2*xxgg(i)) &
/ (1.0d0 + temp_tau**2*(-xtg(i)**2 + xxgg(i)))
a2(i) = -tau * xx(i) / (1.0d0 + temp_tau**2*(-xtg(i)**2 + xxgg(i)))
end do
do i = 1, n2
x(:,i) = xp(:,i)*a1(i) + gp(:,i)*a2(i)
end do
deallocate(a1, a2)
call cut_norm_quad(x, A, f, g)
if (f <= Cval - tau*deriv .or. ls_iter >= 5) exit
tau = eta * tau
end do
do i = 1, n2
xtg(i) = sum(x(:,i) * g(:,i))
gg(i) = sum(g(:,i)**2)
xx(i) = sum(x(:,i)**2)
xxgg(i) = xx(i) * gg(i)
end do
do i = 1, n2
dtX(:,i) = x(:,i)*xtg(i) - g(:,i)
end do
nrmG = sqrt(sum(dtX**2))
s = x - xp
XDiff = sqrt(sum(s**2)) / sqrt(dble(p))
FDiff = abs(fp - f) / (abs(fp) + 1.0d0)
crit(itr,1) = nrmG
crit(itr,2) = XDiff
crit(itr,3) = FDiff
allocate(mcrit(3))
mcrit = 0.0d0
n_iter_range = min(nt, itr)
do i = itr-n_iter_range+1, itr
mcrit = mcrit + crit(i,:)
end do
mcrit = mcrit / dble(n_iter_range)
if ((XDiff < xtol .and. FDiff < ftol) .or. &
(nrmG < gtol) .or. &
(mcrit(2) < 10.0d0*xtol .and. mcrit(3) < 10.0d0*ftol)) then
deallocate(mcrit)
exit
end if
deallocate(mcrit)
y = dtX - dtXP
sy = abs(sum(s * y))
tau = tau_orig
if (sy > 0.0d0) then
if (mod(itr,2) == 0) then
tau = sum(s**2) / sy
else
tau = sy / sum(y**2)
end if
tau = max(min(tau,1.0d20),1.0d-20)
end if
Qp = Q
Q = gamma * Qp + 1.0d0
Cval = (gamma*Qp*Cval + f) / Q
end do
deallocate(xtg, gg, xx, xxgg, dtX, crit, g, gp)
x_out = x
end subroutine optimize
subroutine random_normal(mat)
implicit none
real(8), intent(out) :: mat(:,:)
integer :: m, n, total, k, row, col
real(8) :: u1, u2, z, pi, eps
m = size(mat,1)
n = size(mat,2)
total = m*n
pi = 4.0d0 * atan(1.0d0)
eps = 1.0d-12
k = 1
do while (k <= total)
call random_number(u1)
call random_number(u2)
if (u1 < eps) u1 = eps
if (k < total) then
z = sqrt(-2.0d0*log(u1)) * cos(2.0d0*pi*u2)
row = mod(k-1,m)+1; col = ((k-1)/m)+1
mat(row,col) = z; k = k+1
z = sqrt(-2.0d0*log(u1)) * sin(2.0d0*pi*u2)
row = mod(k-1,m)+1; col = ((k-1)/m)+1
mat(row,col) = z; k = k+1
else
z = sqrt(-2.0d0*log(u1)) * cos(2.0d0*pi*u2)
row = mod(k-1,m)+1; col = ((k-1)/m)+1
mat(row,col) = z; k = k+1
end if
end do
end subroutine random_normal
end module cutnorm_mod
program main
use cutnorm_mod
implicit none
integer, parameter :: n = 400
real(8), dimension(n,n) :: A, B
integer :: i, j
real(8) :: s, r, p1, p2
call random_seed()
p1 = 0.2d0
p2 = 0.5d0
A = 0.0d0
do i = 1, n
do j = i+1, n
call random_number(r)
if (r < p1) then
A(i,j) = 1.0d0; A(j,i) = 1.0d0
end if
end do
end do
B = 0.0d0
do i = 1, n
do j = i+1, n
call random_number(r)
if (r < p2) then
B(i,j) = 1.0d0; B(j,i) = 1.0d0
end if
end do
end do
s = cut_distance(A, B)
print *, "The cut distance between A and B: ", s
end program main