% This code solves   
%  min mu*||H(x)*U||_*+1/2*||x-bx||^2
% where H(x) is a block Hankel matrix formed from x, and bx and U come from measurements: 
% U is a matrix such that U'U=I.
% x is p by N+1 matrix.
% U is N-r+1 by q matrix.
% H(x) is p(r+1) by N-r+1 Hankel matrix. 
%
% The dual of this problem is
%  min   0.5*||gamma||_2^2 - bx'*gamma
%  s.t.  E^*(lambda) + gamma = 0, sigma_max(lambda) <= mu
% where E(x) = H(x)*U.
%
% This code applies ADM to the dual problem, with proximal terms added to
% regularize the mapping E^*(x).
%
% Input:  
% U       - N-r+1 by q matrix such that U'*U=I
% bx      - a p by N+1 real matrix (measured observations)
% r       - parameter
% 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       - p by N+1 real 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 Dec 2, 2012

function [x,fval,dval,t,iter,gap] = sysHankelDADM2(U,bx,r,mu,tol,init,freq,maxiter)

fprintf(' start DADM\n')

tic

% parameters
meps = 1e-8; % machine epsilon
Ut = U';

q = size(U,2);
p = size(bx,1);
N = size(bx,2)-1;
m = (r+1)*p;

L = min(r+1,N-r+1);
beta = norm(bx,2)/(16*L*mu); 
taubeta = 1.61*beta;
sigma2 = 1/L; % so that sigma*||HH*||<=1

% Initialization

if init == 1
  x = zeros(p,N+1);
else
  x = bx;
end
gamma = zeros(p,N+1);
lambda = zeros(m,q);

Hankeltime = 0;
Hankeladjtime = 0;
svd_time = 0;

Hankeladjtime0 = clock;
HaU = HradjX(lambda*Ut,N,r,p);
Hankeladjtime = Hankeladjtime + etime(clock,Hankeladjtime0);

fbest = inf;
dbest = -inf;
xbest = x;

% Loop

iter = 0;

while iter < maxiter
  
  xold = x;
  gammaold = gamma;
  lambdaold = lambda;
  
  gamma = (bx - x - beta*HaU)/(1+beta);
  
  if mod(iter,freq) == 0 && iter >= freq
    g = (Hx/beta + Hx2)/((1 + beta)/sigma2); % bug fixed on Nov 25, 2012
  else
    HaUgx = HaU + gamma + x/beta;
    Hankeltime0 = clock;
    g = Hrx(sigma2*HaUgx,r)*U;
    Hankeltime = Hankeltime + etime(clock,Hankeltime0);
  end
  
  lsg = lambda - g;
  
  svd_time0 = clock;
  [R,S,T] = svd(lsg,'econ');
  svd_time = svd_time + etime(clock,svd_time0);
  
  lambda = R*min(S,mu)*T'; % hence, always dual feasible
  
  Hankeladjtime0 = clock;
  HaU = HradjX(lambda*Ut,N,r,p);
  Hankeladjtime = Hankeladjtime + etime(clock,Hankeladjtime0);
  
  x = x + taubeta*(HaU + gamma);
  
  iter = iter + 1;
  
  if max([norm(x-xold,'fro'),norm(lambda-lambdaold,'fro'),norm(gamma-gammaold,'fro')]) < meps % early termination
    
    Hankeltime0 = clock;
    Hx = Hrx(x,r)*U;
    Hankeltime = Hankeltime + etime(clock,Hankeltime0);
    
    x2 = HaU + bx;
    Hankeltime0 = clock;
    Hx2 = Hrx(x2,r)*U;
    Hankeltime = Hankeltime + etime(clock,Hankeltime0);
    
    nrm = 0.5*norm(HaU,'fro')^2;
    fval2 = nrm + mu*sum(svd(Hx2));    
    fval = 1/2*norm(x-bx,'fro')^2 + mu*sum(svd(Hx));
    dval = -nrm - mytrace(HaU,bx);
    gap = abs(min(fval,fval2)-dval)/max(abs(dval),1);
    fbest = min(fval,fval2);
    dbest = dval;
    if fval > fval2
      xbest = x2;
    else
      xbest = x;
    end
    fprintf(' early termination \n')
%    fprintf(' iter, fval, dval, time = %g, %g, %g, %g\n', iter, fval, dval, toc)
%    fprintf('.')
    break
    
  end 
  
  if mod(iter,freq) == 0
    
    Hankeltime0 = clock;
    Hx = Hrx(x,r)*U;
    Hankeltime = Hankeltime + etime(clock,Hankeltime0);
    
    x2 = HaU + bx;
    Hankeltime0 = clock;
    Hx2 = Hrx(x2,r)*U;
    Hankeltime = Hankeltime + etime(clock,Hankeltime0);
    
    nrm = 0.5*norm(HaU,'fro')^2;
    fval2 = nrm + mu*sum(svd(Hx2));    
    fval = 1/2*norm(x-bx,'fro')^2 + mu*sum(svd(Hx));
    dval = -nrm - mytrace(HaU,bx);
    
    if dbest < dval
      dbest = dval;
    end
    if fbest > fval
      fbest = fval;
      xbest = x;
    end
    if fbest > fval2
      fbest = fval2;
      xbest = x2;
    end
%    fprintf(' iter, fval, dval, time = %g, %g, %g, %g\n', iter, fval, dval, toc)
%    fprintf('.')
    gap = abs(fbest-dbest)/max(abs(dbest),1);
    if gap < tol
      break
    end
    
  end
end

x = xbest;
fval = fbest;
dval = dbest;

t = toc;

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

fprintf(' #iter= %g  dobj= %4.3e  pobj= %4.3e  time= %g \n',iter,dval,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);