forked from jcollfont/inverseecg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inverseTikhonov_geometry_ADMM_proofOfConcept.m
144 lines (106 loc) · 2.78 KB
/
inverseTikhonov_geometry_ADMM_proofOfConcept.m
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
%% HELP:
%
% AUTHOR:
% Jaume Coll-Font <jcollfont@gmail.com>
%
%% main function
function [xk,zk,rho,lamk] = inverseTikhonov_geometry_ADMM_proofOfConcept(A,R,ECG,lambda,initialx,rho,min_r,min_s,verbose,zk,lamk)
% DEFINE
[N, M] =size(A);
[M,T,K] = size(initialx);
% matrices for rapid computations
revisit_rho = 1000;
% SET UP PROBLEM
U = zeros(M,M,K);
c = zeros(M,T,K);
Q = zeros(M,M,K);
for ii = 1:K
Q(:,:,ii) = squeeze(A(:,:,ii))'*squeeze(A(:,:,ii)) + lambda*R'*R;
c(:,:,ii) = -2*squeeze(A(:,:,ii))'*ECG(:,:,ii);
[U(:,:,ii)] = chol( 2*Q(:,:,ii) + rho*eye(M) );
end
% INITIALIZE with WARM START
xk = initialx;
if exist('zk') && exist('lamk') && ( numel(zk)*numel(lamk)~=0 )
[zk] = min_L_z(xk,lamk,rho);
else
zk = mean(xk,3);
end
[rk,sk] = residuals(xk,zk,zk,rho);
if ~exist('lamk') || numel(lamk)==0
lamk = rho*rk;
end
k = 1;
% ADMM
while true
% min f(x)
[xk] = min_L_x_overdet(U,c,xk,zk,lamk,rho);
% min g(z)
zk1 = zk;
[zk] = min_L_z(xk,lamk,rho);
% compute residuals
[rk,sk] = residuals(xk,zk,zk1,rho);
% min lam
lamk = lamk + rho*rk;
% primal and dual residual norms
nrk = norm(rk(:),2);
nsk = norm(sk(:),2);
% verbose and stopping criteria
if verbose; fprintf('Iter: %d. Primal residual: %0.6f. Dual residual %0.6f.\n',k,nrk,nsk);end
k = k+1;
if ( nrk < min_r )&&( nsk < min_s )
if verbose;fprintf('GatoDominguez!\n');end
return;
end
% update adaptive rho
if mod(k,revisit_rho) == 0
[rho, U] = new_rho(nrk,nsk,rho,Q,U);
end
end
end
%% min f(x) --- actual objective function (LSQ)
% Optimize over the fitting error function. This is the Least Squares
% problem.
%
function [xk] = min_L_x_overdet(R,c,xk,zk,lamk,rho)
[K] = size(xk,3);
for ii = 1:K
xk(:,:,ii) = squeeze(R(:,:,ii))\squeeze(R(:,:,ii)'\( -c(:,:,ii) + rho*zk - lamk(:,:,ii) ));
end
end
%% min g(x) --- constraints
% Optimizes over the constraint functions.
%
function [zk] = min_L_z(xk,lamk,rho)
zk = mean(rho*xk + lamk ,3)/(rho);
end
%% compute residuals
% Computes the new residuals (primal and dual) at each iteration.
%
function [rk,sk] = residuals(xk,zk,zk1,rho)
rk = xk - repmat(zk,[1,1,size(xk,3)]);
sk = -rho*(zk1 - zk);
end
%% update rho
% every revisit_rho iterations checks the difference between residuals and
% changes rho appropriately.
%
% If r > mu*s -> rho = tau*rho;
% elseif s > mu*r -> rho = 1/tau*rho;
%
function [rho,U] = new_rho(nrk,nsk,rho,Q,U)
mu = 10;
tau = 2;
M = size(Q,1);
if (nrk > mu*nsk)
rho = tau*rho;
for ii = 1:size(U,3)
[U(:,:,ii)] = chol(2*squeeze(Q(:,:,ii)) + rho*eye(M));
end
elseif (nsk > mu*nrk)
rho = rho/tau;
for ii = 1:size(U,3)
[U(:,:,ii)] = chol(2*squeeze(Q(:,:,ii)) + rho*eye(M));
end
end
end