% This code solves   
%  min mu*||H(x)||_*+1/2*||q.*(x-bx)||^2
% where H(x) is a block Hankel matrix formed from x,
% q is the weight matrix, and bx come from measurements: 
% x is m by n*(i+j-1) matrix.
% H(x) is m*i by n*j Hankel matrix. 
%
% The dual of this problem is
%  min   0.5*||gamma||_2^2 - (q.*bx)'*gamma
%  s.t.  H^*(Lambda) + q.*gamma = 0, sigma_max(Lambda) <= mu
%
% This code applies ADM to the primal problem, with proximal terms added to
% regularize both the q map in front of x and the mapping H(x).
%
% Input:  
% q       - m by n*(i+j-1) matrix of zero and ones
% bx      - a m by n*(i+j-1) matrix (measured observations)
% m,n,i,j - parameters
% mu      - regularization parameter
% tol     - termination tolerance 
% init    - 1 initialize all variables at zero
%           2 initialize x at bx
% freq    - frequency of checking termination criterion.
% maxiter - maximum number of iterations
%
% Output: 
% x       - m by n*(i+j-1) matrix, the approximate primal optimal solution
% fval    - approximate primal optimal value
% dval    - approximate dual optimal value
% t       - cputime
% iter    - number of iterations
% gap     - duality gap
%
% Written by Maryam Fazel, Ting Kei Pong and Defeng Sun
% Last modified Nov 27, 2012

function [x,fval,dval,t,iter,gap] = rcompleteADM(q,bx,m,n,i,j,mu,tol,init,freq,maxiter)

tic

% parameters

meps = 1e-8; % machine epsilon
beta = min(i,j)*mu/(2*norm(bx,2));
taubeta = 1.61*beta;
sigma = 1/(min(i,j)+1/beta); % sigma*(||H*H+Q*Q/beta||)<=1

% Initialization

if init == 1
  x = zeros(m,n*(i+j-1));
  X = zeros(m*i,n*j);
else
  x = q.*bx;
  X = Hkx(-x,m,n,i,j);
end
lambda = zeros(m*i,n*j);
qx = q.*bx;
tq = 1 - q;

Hankeltime = 0;
HankelAdjtime = 0;
svd_time = 0;

Hankeltime0 = clock;
Hx = Hkx(x,m,n,i,j);
Hankeltime = Hankeltime + etime(clock,Hankeltime0);

fbest = inf;
xbest = x;

% Loop

iter = 0;

while iter < maxiter
  
  xold = x;
  Xold = X;
  lambdaold = lambda;
  
  Hxl = lambda/beta - Hx;
  svd_time0 = clock;
  [R,S,T] = svd(Hxl,'econ');
  svd_time = svd_time + etime(clock,svd_time0);
  
  X = R*max(S - mu/beta,0)*T'; % matrix shrinkage
  
  Xl = X - lambda/beta;
  HankelAdjtime0 = clock;
  hxL = Hkxad(Xl,m,n,i,j);
  HankelAdjtime = HankelAdjtime + etime(clock,HankelAdjtime0);
  hxL = hxL + formap(x,m,n,i,j);
  
  G = hxL + (q.*x - qx)/beta;
  x = x - sigma*G;
  
  Hankeltime0 = clock;
  Hx = Hkx(x,m,n,i,j);
  Hankeltime = Hankeltime + etime(clock,Hankeltime0);
  
  lambda = lambda - taubeta*(Hx + X);
  
  iter = iter + 1;
  
  if max([norm(x-xold,'fro'),norm(lambda-lambdaold,'fro'),norm(X-Xold,'fro')]) < meps % early termination
  
    fval = 1/2*norm(q.*x-qx,'fro')^2 + mu*sum(svd(Hx));
    
    svd_time0 = clock;
    [R,S,T] = svd(lambda,'econ');
    svd_time = svd_time + etime(clock,svd_time0);
    
    lambda2 = R*min(S,mu)*T';
    HankelAdjtime0 = clock;
    hLL = Hkxad(lambda2,m,n,i,j);
    HankelAdjtime = HankelAdjtime + etime(clock,HankelAdjtime0);
    
    dfeas = norm(tq.*hLL,'fro')/max([1,norm(hLL,'fro')]);
    dval = -0.5*norm(q.*hLL,'fro')^2 - mytrace(qx,hLL);
    
%    fprintf(' iter, fval, dval, dfea, time = %g, %g, %g, %g, %g\n', iter, fval, dval, dfeas, toc)
    gap = abs(fval - dval)/max(abs(dval),1);
    fbest = fval;
    xbest = x;
    
    fprintf(' early termination \n')
%    fprintf(' iter, fval, dval, dfea, time = %g, %g, %g, %g, %g\n', iter, fval, dval, dfeas, toc)
    break
    
  end 
  
  if mod(iter,freq) == 0
    
    fval = 1/2*norm(q.*x-qx,'fro')^2 + mu*sum(svd(Hx));
    
    svd_time0 = clock;
    [R,S,T] = svd(lambda,'econ');
    svd_time = svd_time + etime(clock,svd_time0);
    
    lambda2 = R*min(S,mu)*T';
    
    HankelAdjtime0 = clock;
    hLL = Hkxad(lambda2,m,n,i,j);
    HankelAdjtime = HankelAdjtime + etime(clock,HankelAdjtime0);
    
    dfeas = norm(tq.*hLL,'fro')/max([1,norm(hLL,'fro')]);
    dval = -0.5*norm(q.*hLL,'fro')^2 - mytrace(qx,hLL);
    
    if fbest > fval
      fbest = fval;
      xbest = x;
    end
    
%    fprintf(' iter, fval, dval, dfea, time = %g, %g, %g, %g, %g, %g\n', iter, fval, dval, dfeas, toc)
    gap = abs(fbest - dval)/max(abs(dval),1);
    if max(gap,dfeas) < tol
      break
    end
    
  end
end

fval = fbest;
x = xbest;
t = toc;

fprintf('\n ADM terminates, init = %g \n', init);

fprintf(' #iter= %g  dobj= %4.3e  rel. dual feas= %g  pobj= %4.3e time= %g \n',iter,dval,dfeas,fval,t);

fprintf(' Total time for computing the the Hankel operator & its adjoint: %g  %g\n ', Hankeltime,HankelAdjtime);

fprintf('Total time for svd:  %g\n', svd_time);