├── manpg_code_share ├── misc │ ├── setduplicat_pduplicat.m │ ├── Kmn.m │ ├── proximal_l1.m │ ├── Elimination_mat.m │ ├── proximal_l21.m │ ├── DuplicationM.m │ ├── Re_sub_grad_spca.m │ ├── itril.m │ └── itriu.m ├── compressed modes │ ├── positive_CMs.m │ ├── Sch_matrix.m │ ├── read me.txt │ ├── soc_CM.m │ ├── manpg_CMS_adap.m │ ├── manpg_CMS.m │ ├── PAMAL_CMs.m │ ├── demo.m │ └── demo_compare_CMS_dim_n.m ├── SPCA │ ├── read me.txt │ ├── Re_sub_grad_spca.m │ ├── soc_spca.m │ ├── manpg_orth_sparse.m │ ├── manpg_orth_sparse_adap.m │ ├── PAMAL_spca.m │ └── demo_compare_SPCA_dimension_n.m └── SSN_subproblem │ ├── conjgrad.m │ ├── Semi_newton_matrix_l21.m │ └── Semi_newton_matrix.m └── README.md /manpg_code_share/misc/setduplicat_pduplicat.m: -------------------------------------------------------------------------------- 1 | function setduplicat_pduplicat(n) 2 | global Dn pDn; 3 | Dn=sparse(DuplicationM(n)); 4 | pDn=(Dn'*Dn)\Dn'; 5 | end -------------------------------------------------------------------------------- /manpg_code_share/misc/Kmn.m: -------------------------------------------------------------------------------- 1 | function K = Kmn(m,n) 2 | 3 | K = zeros(m*n, m*n); 4 | m0 = 1:(m*n); 5 | 6 | N = reshape(m0, m,n)'; 7 | n0 = N(:); 8 | 9 | for i = 1:(m*n) 10 | K(m0(i), n0(i)) = 1; 11 | end -------------------------------------------------------------------------------- /manpg_code_share/compressed modes/positive_CMs.m: -------------------------------------------------------------------------------- 1 | function X = positive_CMs(X,r) 2 | %flip the negative columns of CMs to be positive 3 | 4 | for i = 1:r 5 | [~,I] = max(abs(X(:,i))); 6 | if X(I,i) < 0 7 | X(:,i) = - X(:,i); 8 | end 9 | end 10 | end 11 | 12 | -------------------------------------------------------------------------------- /manpg_code_share/misc/proximal_l1.m: -------------------------------------------------------------------------------- 1 | function [ x_prox,Act_set ,Inact_set] = prox_l1( b ,lambda,r ) 2 | 3 | a = abs(b)-lambda; 4 | if r < 15 5 | Act_set = double( a > 0); 6 | else 7 | Act_set = ( a > 0); 8 | end 9 | x_prox = (Act_set.*sign(b)).*a; 10 | if nargout==3 11 | Inact_set= (a <= 0); 12 | end 13 | 14 | end 15 | 16 | -------------------------------------------------------------------------------- /manpg_code_share/misc/Elimination_mat.m: -------------------------------------------------------------------------------- 1 | function [ Em ] = Elimination_mat( m ) 2 | %m Size of E 3 | T = tril(ones(m)); % Lower triangle of 1's 4 | f = find(T(:)); % Get linear indexes of 1's 5 | k = m*(m+1)/2; % Row size of L 6 | m2 = m*m; % Colunm size of L 7 | L = zeros(m2,k); % Start with L' 8 | x = f + m2*(0:k-1)'; % Linear indexes of the 1's within L' 9 | L(x) = 1; % Put the 1's in place 10 | Em = L'; % Now transpose to actual L 11 | 12 | 13 | end 14 | 15 | -------------------------------------------------------------------------------- /manpg_code_share/misc/proximal_l21.m: -------------------------------------------------------------------------------- 1 | function [ x_prox, delta ,Inact_set] = prox_l21( b ,lambda,r ) 2 | %proximal mapping of L_21 norm 3 | [n,r] = size(b); delta = cell(n,1); 4 | nr = vecnorm(b,2,2) ; 5 | a = nr - lambda; 6 | if r < 15 7 | Act_set = double( a > 0); 8 | else 9 | Act_set = ( a > 0); 10 | end 11 | 12 | x_prox = Act_set.*(1- lambda./nr).*b; 13 | for i = 1:n 14 | delta{i} = (eye(r) - lambda./nr(i).*(eye(r) - b(i,:)'*b(i,:)./nr(i)))*Act_set(i); 15 | end 16 | % diag = 1 - lambda*ones(size(b))./nr + lambda.*b.^2./(nr.^3); 17 | % diag = Act_set.*diag; 18 | if nargout==3 19 | Inact_set= (a <= 0); 20 | end 21 | 22 | end 23 | 24 | -------------------------------------------------------------------------------- /manpg_code_share/compressed modes/Sch_matrix.m: -------------------------------------------------------------------------------- 1 | function [H] = Sch_matrix(a,b,l1) 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 | %Schrodinger operator 4 | % Input: 5 | % a, b: Two end points. % 6 | % n: number of grid points. % 7 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 | 9 | % h = (b-a)/(n); h1=h*h; 10 | % H = zeros(n,n); 11 | % for i=1:n-1 12 | % H(i,i) = -2/h1; H(i+1,i) = 1/h1; H(i,i+1)= 1/h1; 13 | % end 14 | % H(n,n) = -2/h1; 15 | % H(1,n)= 1/h1; H(n,1)=1/h1; 16 | 17 | dx = (b-a)/l1; 18 | Lap_1D = -2*speye(l1,l1) + spdiags(ones(l1-1,1),-1,l1,l1) + spdiags(ones(l1,1),1,l1,l1); 19 | Lap_1D(1,l1) = 1; Lap_1D(l1,1) = 1; 20 | H = -1/2*Lap_1D/dx/dx ; 21 | 22 | -------------------------------------------------------------------------------- /manpg_code_share/SPCA/read me.txt: -------------------------------------------------------------------------------- 1 | documentation by: 2 | Shixiang Chen 3 | chenshxiang@gmail.com 4 | The Chinese University of Hong Kong 5 | 2019 6 | 7 | ---------------------------------- 8 | 9 | This package contains the code used in the paper "Proximal Gradient Method for Nonsmooth Optimization over the Stiefel Manifold 10 | ". 11 | 12 | This code has been tested to run in MATLAB R2018b. 13 | 14 | If you use this code in an academic paper, please cite our paper: 15 | Shixiang Chen, Shiqian Ma, Anthony Man-Cho So and Tong Zhang. Proximal Gradient Method for Nonsmooth Optimization over the Stiefel Manifold. arXiv preprint arXiv:1811.00980, 2018 16 | 17 | 18 | ---------------------------------- 19 | - demo_compare_SPCA_dimension_n : compares 4 algorithms ManPG, PAMAL, SOC and Riemannian subgradient method for solving spare PCA problem with orthogonal constraint 20 | ---------------------------------- 21 | 22 | If you have any questions or find any bugs, feel free to contact Shixiang Chen . -------------------------------------------------------------------------------- /manpg_code_share/SSN_subproblem/conjgrad.m: -------------------------------------------------------------------------------- 1 | function [x, k,res] = conjgrad(A,b,tol) 2 | % CONJGRAD Conjugate Gradient Method. 3 | % solving A*x = b; A is linear operator, b is n*n matrix 4 | % A(x) is column wise mapping A: x(:,i) -> b(:,i) with A = bldiag{A1,A2,..An} 5 | k = 0; 6 | if nargin<3 7 | tol=1e-7; 8 | end 9 | tol = max(1e-7, tol); 10 | 11 | x = 0; 12 | r = b ;%- A(x); 13 | r_norm = norm(r,'fro')^2; 14 | if r_norm < tol^2 15 | res = sqrt(r_norm); 16 | return 17 | end 18 | p = r; 19 | 20 | Ap = A(p); 21 | %s = dot(y,z); 22 | s = sum(sum(p.*Ap)); 23 | %t = dot(r,y)./s; 24 | alpha = r_norm/s; 25 | x = x + alpha*p; 26 | r_norm_prev = r_norm; 27 | 28 | for k = 1:numel(b) 29 | r = r - alpha*Ap; 30 | r_norm = norm(r,'fro')^2; 31 | if r_norm . 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ManPG 2 | MATLAB implementation of ManPG 3 | 4 | documentation by: 5 | Shixiang Chen 6 | chenshxiang@gmail.com 7 | 8 | The Chinese University of Hong Kong 9 | 2019 10 | 11 | ---------------------------------- 12 | Citation 13 | ---------------------------------- 14 | This package contains the code used in the paper "Proximal Gradient Method for Nonsmooth Optimization over the Stiefel Manifold 15 | ". 16 | 17 | This code has been tested to run in MATLAB R2018b. 18 | 19 | If you use this code in an academic paper, please cite our paper: 20 | Shixiang Chen, Shiqian Ma, Anthony Man-Cho So and Tong Zhang. Proximal Gradient Method for Nonsmooth Optimization over the Stiefel Manifold. arXiv preprint arXiv:1811.00980, 2018 21 | 22 | 23 | ---------------------------------- 24 | - SPCA\demo_compare_SPCA_dimension_n : compares 4 algorithms ManPG, PAMAL, SOC and Riemannian subgradient method for solving spare PCA problem with orthogonal constraint 25 | - compressed modes\demo_compare_CMS_dim_n: compares 4 algorithms ManPG, PAMAL, SOC and Riemannian subgradient method for solving compressed modes problem 26 | ---------------------------------- 27 | 28 | ---------------- 29 | License 30 | --------------- 31 | MIT 32 | 33 | If you have any questions or find any bugs, feel free to contact Shixiang Chen . 34 | -------------------------------------------------------------------------------- /manpg_code_share/misc/DuplicationM.m: -------------------------------------------------------------------------------- 1 | function M = DuplicationM(n, option) 2 | % function M = DuplicationM(n) 3 | % M = DuplicationM(n, 'lo') % (default) OR 4 | % M = DuplicationM(n, 'up') % 5 | % Return duplication matrix order n 6 | % 7 | % It is always assumed Duplication arount main diagonal (k=0) 8 | % 9 | % Ouput are sparse 10 | % 11 | % DuplicationM(size(A),'lo')*A(itril(size(A))) == A(:) %true for lower half 12 | % DuplicationM(size(A),'up')*A(itriu(size(A))) == A(:) %true for upper half 13 | % 14 | % Author: Bruno Luong 15 | % Date: 21/March/2009 16 | % 17 | % Ref : Magnus, Jan R.; Neudecker, Heinz (1980), "The elimination matrix: 18 | % some lemmas and applications", Society for Industrial and Applied Mathematics. 19 | % Journal on Algebraic and Discrete Methods 1 (4): 422449, 20 | % doi:10.1137/0601049, ISSN 0196-5212. 21 | 22 | if nargin<2 23 | option = 'lo'; % default 24 | end 25 | 26 | if isscalar(n) 27 | n = [n n]; 28 | end 29 | 30 | switch lower(option(1)) 31 | case 'l' % u, lo, LO, LOWER ... 32 | [I J] = itril(n); 33 | case 'u' % u, up, UP, UPPER ... 34 | [I J] = itriu(n); 35 | otherwise 36 | error('option must be ''lo'' or ''up'''); 37 | end 38 | 39 | % Find the sub/sup diagonal part that can flip to other side 40 | loctri = find(I~=J & J<=n(1) & I<=n(2)); 41 | % Indices of the flipped part 42 | Itransposed = sub2ind(n, J(loctri), I(loctri)); 43 | 44 | % Convert to linear indice 45 | I = sub2ind(n, I, J); 46 | 47 | % Result 48 | M = sparse([I; Itransposed], ... 49 | [(1:length(I))'; loctri], 1, prod(n), length(I)); 50 | 51 | end -------------------------------------------------------------------------------- /manpg_code_share/SPCA/Re_sub_grad_spca.m: -------------------------------------------------------------------------------- 1 | function [X_Rsub,F_Sub,sparsity,time_Rsub,i,succ_flag] = Re_sub_grad_spca(B,option) 2 | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% remannian subgradient; 3 | %min -Tr(X'*B*X)+ mu*norm(X,1) s.t. X'*X=Ir. X \in R^{p*r} 4 | tic; 5 | r=option.r; 6 | n=option.n; 7 | mu = option.mu; 8 | maxiter =option.maxiter + 1; 9 | tol = option.tol; 10 | 11 | X = option.phi_init; 12 | 13 | if option.type == 1 14 | AX = B*X; 15 | else 16 | AX = B'*(B*X); 17 | end 18 | 19 | h=@(X) mu*sum(sum(abs(X))); 20 | 21 | f_re_sub = zeros(maxiter,1); 22 | succ_flag = 0; 23 | f_re_sub(1) = -sum(sum(X.*(AX))) + h(X); 24 | for i = 2:maxiter 25 | gx = 2*AX - mu*sign(X) ; %negative Euclidean gradient 26 | xgx = X'*gx; 27 | pgx = gx - 0.5*X*(xgx+xgx'); %negative Riemannian gradient using Euclidean metric 28 | %pgx = gx; 29 | %eta = 0.6*0.99^i; 30 | eta = 1/i^(3/4); 31 | %eta = 1/i; 32 | X = X + eta * pgx; % Riemannian step 33 | %[q,~] = qr(q); % retraction 34 | [U, SIGMA, S] = svd(X'*X); SIGMA =diag(SIGMA); X = X*(U*diag(sqrt(1./SIGMA))*S'); 35 | if option.type == 1 36 | AX = B*X; 37 | else 38 | AX = B'*(B*X); 39 | end 40 | f_re_sub(i) = -sum(sum(X.*(AX))) + h(X); 41 | if f_re_sub(i) < option.F_manpg + option.tol 42 | succ_flag = 1; 43 | break; 44 | end 45 | 46 | end 47 | X((abs(X)<=1e-5))=0; 48 | X_Rsub = X; 49 | time_Rsub = toc; 50 | sparsity= sum(sum(X_Rsub==0))/(n*r); 51 | F_Sub = f_re_sub(i-1); 52 | 53 | %plot(f_re_sub); 54 | %hold on; 55 | %plot(norm_grad); 56 | fprintf('Rsub:Iter *** Fval *** CPU **** sparsity \n'); 57 | 58 | print_format = ' %i %1.5e %1.2f %1.2f \n'; 59 | fprintf(1,print_format, i-1, F_Sub, time_Rsub, sparsity); 60 | end 61 | 62 | -------------------------------------------------------------------------------- /manpg_code_share/misc/Re_sub_grad_spca.m: -------------------------------------------------------------------------------- 1 | function [X_Rsub,F_Sub,sparsity,time_Rsub,i,succ_flag] = Re_sub_grad_spca(B,option) 2 | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% remannian subgradient; 3 | %min -Tr(X'*B*X)+ mu*norm(X,1) s.t. X'*X=Ir. X \in R^{p*r} 4 | tic; 5 | r=option.r; 6 | n=option.n; 7 | mu = option.mu; 8 | maxiter =option.maxiter + 1; 9 | tol = option.tol; 10 | 11 | X = option.phi_init; 12 | 13 | if option.type == 1 14 | AX = B*X; 15 | else 16 | AX = B'*(B*X); 17 | end 18 | 19 | h=@(X) mu*sum(sum(abs(X))); 20 | 21 | f_re_sub = zeros(maxiter,1); 22 | succ_flag = 0; 23 | f_re_sub(1) = -sum(sum(X.*(AX))) + h(X); 24 | for i = 2:maxiter 25 | gx = 2*AX - mu*sign(X) ; %negative Euclidean gradient 26 | xgx = X'*gx; 27 | pgx = gx - 0.5*X*(xgx+xgx'); %negative Riemannian gradient using Euclidean metric 28 | %pgx = gx; 29 | %eta = 0.6*0.99^i; 30 | eta = 1/i^(3/4); 31 | %eta = 1/i; 32 | X = X + eta * pgx; % Riemannian step 33 | %[q,~] = qr(q); % retraction 34 | [U, SIGMA, S] = svd(X'*X); SIGMA =diag(SIGMA); X = X*(U*diag(sqrt(1./SIGMA))*S'); 35 | if option.type == 1 36 | AX = B*X; 37 | else 38 | AX = B'*(B*X); 39 | end 40 | f_re_sub(i) = -sum(sum(X.*(AX))) + h(X); 41 | if f_re_sub(i) < option.F_manpg + option.tol 42 | succ_flag = 1; 43 | break; 44 | end 45 | 46 | end 47 | X((abs(X)<=1e-5))=0; 48 | X_Rsub = X; 49 | time_Rsub = toc; 50 | sparsity= sum(sum(X_Rsub==0))/(n*r); 51 | F_Sub = f_re_sub(i-1); 52 | 53 | %plot(f_re_sub); 54 | %hold on; 55 | %plot(norm_grad); 56 | fprintf('Rsub:Iter *** Fval *** CPU **** sparsity \n'); 57 | 58 | print_format = ' %i %1.5e %1.2f %1.2f \n'; 59 | fprintf(1,print_format, i-1, F_Sub, time_Rsub, sparsity); 60 | end 61 | 62 | -------------------------------------------------------------------------------- /manpg_code_share/misc/itril.m: -------------------------------------------------------------------------------- 1 | function [I J] = itril(sz, k) 2 | % function [I J] = itril(sz, k) % OR 3 | % I = itril(sz, k) 4 | % 5 | % Return the subindices [I J] (or linear indices I if single output call) 6 | % in the purpose of extracting an lower triangular part of the matrix of 7 | % the size SZ. Input k is optional shifting. For k=0, extract from the main 8 | % diagonal. For k>0 -> above the diagonal, k<0 -> below the diagonal 9 | % 10 | % This returnd same as [...] = find(tril(ones(sz),k)) 11 | % - Output is a column and sorted with respect to linear indice 12 | % - No intermediate matrix is generated, that could be useful for large 13 | % size problem 14 | % - Mathematically, A(itril(size(A)) is called (lower) "half-vectorization" 15 | % of A 16 | % 17 | % Example: 18 | % 19 | % A = [ 7 5 4 20 | % 4 2 3 21 | % 9 1 9 22 | % 3 5 7 ] 23 | % 24 | % I = itril(size(A)) % gives [1 2 3 4 6 7 8 11 12]' 25 | % A(I) % gives [7 4 9 3 2 1 5 9 7]' OR A(tril(A)>0) 26 | % 27 | % Author: Bruno Luong 28 | % Date: 21/March/2009 29 | 30 | if isscalar(sz) 31 | sz = [sz sz]; 32 | end 33 | m=sz(1); 34 | n=sz(2); 35 | 36 | % Main diagonal by default 37 | if nargin<2 38 | k=0; 39 | end 40 | 41 | nc = min(n,m+k); % number of columns of the triangular part 42 | lo = max((1:nc).'-k,1); % lower row indice for each column 43 | hi = m + zeros(nc,1); % upper row indice for each column 44 | 45 | if isempty(lo) 46 | I = zeros(0,1); 47 | J = zeros(0,1); 48 | else 49 | c=cumsum([0; hi-lo]+1); % cumsum of the length 50 | I = accumarray(c(1:end-1), (lo-[0; hi(1:end-1)]-1), ... 51 | [c(end)-1 1]); 52 | I = cumsum(I+1); % row indice 53 | J = cumsum(accumarray(c,1)); 54 | J = J(1:end-1); % column indice 55 | end 56 | 57 | if nargout<2 58 | % convert to linear indices 59 | I = sub2ind([m n], I, J); 60 | end 61 | 62 | end % itril 63 | -------------------------------------------------------------------------------- /manpg_code_share/misc/itriu.m: -------------------------------------------------------------------------------- 1 | function [I J] = itriu(sz, k) 2 | % function [I J] = itriu(sz) % OR 3 | % I = itriu(sz) OR 4 | % 5 | % Return the subindices [I J] (or linear indices I if single output call) 6 | % in the purpose of extracting an upper triangular part of the matrix of 7 | % the size SZ. Input k is optional shifting. For k=0, extract from the main 8 | % diagonal. For k>0 -> above the diagonal, k<0 -> below the diagonal 9 | % 10 | % This returnd same as [...] = find(triu(ones(sz),k)) 11 | % - Output is a column and sorted with respect to linear indice 12 | % - No intermediate matrix is generated, that could be useful for large 13 | % size problem 14 | % - Mathematically, A(itriu(size(A)) is called (upper) "half-vectorization" 15 | % of A 16 | % 17 | % Example: 18 | % 19 | % A = [ 7 5 4 20 | % 4 2 3 21 | % 9 1 9 22 | % 3 5 7 ] 23 | % 24 | % I = itriu(size(A)) % gives [1 5 6 9 10 11]' 25 | % A(I) % gives [7 5 2 4 3 9]' OR A(triu(A)>0) 26 | % 27 | % Author: Bruno Luong 28 | % Date: 21/March/2009 29 | 30 | if isscalar(sz) 31 | sz = [sz sz]; 32 | end 33 | m=sz(1); 34 | n=sz(2); 35 | 36 | % Main diagonal by default 37 | if nargin<2 38 | k=0; 39 | end 40 | 41 | nc = n-max(k,0); % number of columns of the triangular part 42 | lo = ones(nc,1); % lower row indice for each column 43 | hi = min((1:nc).'-min(k,0),m); % upper row indice for each column 44 | 45 | if isempty(lo) 46 | I = zeros(0,1); 47 | J = zeros(0,1); 48 | else 49 | c=cumsum([0; hi-lo]+1); % cumsum of the length 50 | I = accumarray(c(1:end-1), (lo-[0; hi(1:end-1)]-1), ... 51 | [c(end)-1 1]); 52 | I = cumsum(I+1); % row indice 53 | J = accumarray(c,1); 54 | J(1) = 1 + max(k,0); % The row indices starts from this value 55 | J = cumsum(J(1:end-1)); % column indice 56 | end 57 | 58 | if nargout<2 59 | % convert to linear indices 60 | I = sub2ind([m n], I, J); 61 | end 62 | 63 | end % itriu 64 | -------------------------------------------------------------------------------- /manpg_code_share/compressed modes/soc_CM.m: -------------------------------------------------------------------------------- 1 | function [X_Soc, F_SOC,sparsity_soc,time_soc,error_XPQ, iter_soc, flag_succ] =soc_CM(H,option) 2 | % min Tr(X'*A*X)+ mu*norm(X,1) s.t. X'*X=Ir. 3 | % A = -H 4 | tic; 5 | r = option.r; n = option.n; 6 | mu = option.mu; 7 | maxiter =option.maxiter; 8 | tol = option.tol; 9 | 10 | A = -H; 11 | h = @(X) mu*sum(sum(abs(X))); 12 | 13 | 14 | dx = option.L/n; 15 | LAM_A = - (cos(2*pi*(0:n-1)'/n)-1)/dx^2; 16 | %beta=1/mu*r/5; lambda=1/mu*r/20; % step size of compressed modes 17 | beta = n*r*mu/25 + 1; 18 | 19 | %beta = max(abs(LAM_A)); 20 | lambda = beta; 21 | 22 | LAM_SOC = 2*LAM_A + lambda + beta; 23 | flag = 0; 24 | P = option.phi_init; 25 | Q = P; 26 | Z = zeros(n,r); %dual variable 27 | b = Z; 28 | F_ad=zeros(maxiter,1); 29 | not_full_rank = 0; 30 | for itera=1:maxiter 31 | LZ = beta*(P-Z) + lambda*(Q-b); 32 | X = real(ifft(bsxfun(@rdivide,fft( LZ ),LAM_SOC))); 33 | 34 | %%%% shrinkage Q 35 | Q = sign(X+b).*max(0,abs(X+b)-mu/lambda); 36 | 37 | %%%% solve P 38 | 39 | Y = X + Z; 40 | [U,~,V] = svd(Y,0); 41 | P = U*V'; 42 | %%%%%%%%%%%%% svd Y'*Y 43 | % [U, D, S] = svd(Y'*Y); 44 | % D = diag(D); 45 | % if abs(prod(D))>0 46 | % P = Y*(U*diag(sqrt(1./D))*S'); 47 | % else 48 | % not_full_rank = not_full_rank+1; 49 | % end 50 | 51 | %%%%%%%%% 52 | Z = Z+X-P; 53 | b = b+X-Q; 54 | % AP = A*P; F_ad(itera)=sum(sum(P.*(AP)))+h(P); 55 | % AX = real(fft(bsxfun(@times,ifft( X ),LAM_A))); 56 | 57 | 58 | if itera>2 59 | normXQ = norm(X-Q,'fro'); 60 | normQ = norm(Q,'fro'); 61 | normX = norm(X,'fro'); 62 | normP = r; 63 | normXP = norm(X-P,'fro'); 64 | if normXQ/max(1,max(normQ,normX)) + normXP/max(1,max(normP,normX)) 0.1 103 | fprintf('SOC returns different point \n'); 104 | fprintf('Soc:Iter *** Fval *** CPU **** sparsity ********* err \n'); 105 | 106 | print_format = ' %i %1.5e %1.2f %1.2f %1.3e \n'; 107 | fprintf(1,print_format, itera, F_ad(itera), time_soc, sparsity_soc, error_XPQ); 108 | flag_succ = 2; % different point 109 | F_SOC = 0; 110 | sparsity_soc = 0; 111 | iter_soc = 0; 112 | 113 | time_soc = 0; 114 | else 115 | 116 | flag_succ = 1; % success 117 | F_SOC = F_ad(itera); 118 | iter_soc = itera; 119 | % residual_Q = norm(Q'*Q-eye(n),'fro')^2; 120 | fprintf('Soc:Iter *** Fval *** CPU **** sparsity ********* err \n'); 121 | 122 | print_format = ' %i %1.5e %1.2f %1.2f %1.3e \n'; 123 | fprintf(1,print_format, itera, F_ad(itera), time_soc, sparsity_soc, error_XPQ); 124 | end 125 | end 126 | -------------------------------------------------------------------------------- /manpg_code_share/SPCA/soc_spca.m: -------------------------------------------------------------------------------- 1 | function [X_Soc, F_SOC,sparsity_soc,time_soc,error_XPQ, iter_soc, flag_succ]=soc_spca(B,option) 2 | %min -Tr(X'*A*X)+ mu*norm(X,1) s.t. X'*X=Ir. 3 | % A = B'*B type = 0 or A = B type = 1 4 | tic; 5 | r = option.r; 6 | n = option.n; 7 | mu = option.mu; 8 | maxiter =option.maxiter; 9 | tol = option.tol; 10 | type = option.type; 11 | if type==0 % data matrix 12 | A = -B'*B; 13 | else 14 | A = -B; 15 | end 16 | 17 | h=@(X) mu*sum(sum(abs(X))); 18 | %rho = svds(B,1)^2 + r/2;% stepsize 19 | %rho = svds(B,1)^2 + n*r*mu/25 + 1; 20 | %rho = svds(B,1)^2 + n/50 ;% good for mu and r 21 | rho = 2* svds(B,1)^2 ;% n/30 not converge 1.9* sometimes not converge 22 | lambda = rho; 23 | P = option.phi_init; Q = P; 24 | Z = zeros(n,r); 25 | b=Z; 26 | F_ad=zeros(maxiter,1); 27 | not_full_rank = 0; 28 | 29 | %chA = chol( 2*A + (r+lambda)*eye(d)); 30 | Ainv = inv( 2*A + (rho+lambda)*eye(n)); 31 | flag_maxiter = 0; 32 | 33 | for itera=1:maxiter 34 | LZ = rho*(P-Z)+lambda*(Q-b); 35 | % X=A_bar\LB; 36 | % X = chA\(chA'\LZ); 37 | X = Ainv*LZ; 38 | %%%% shrinkage Q 39 | Q = sign(X+b).*max(0,abs(X+b)-mu/lambda); 40 | 41 | %%%% solve P 42 | 43 | Y = X + Z; 44 | %%%%%%%%%%%%% svd Y'*Y 45 | % [U, D, S] = svd(Y'*Y); 46 | % D = diag(D); 47 | % if abs(prod(D))>0 48 | % P = Y*(U*diag(sqrt(1./D))*S'); 49 | % else 50 | % not_full_rank = not_full_rank+1; 51 | % end 52 | [U,~,V]= svd(Y,0); 53 | P = U*V'; 54 | %%%%%%%%% 55 | Z = Z+X-P; 56 | b = b+X-Q; 57 | 58 | if itera>2 59 | normXQ = norm(X-Q,'fro'); 60 | normQ = norm(Q,'fro'); 61 | normX = norm(X,'fro'); 62 | normP = r; 63 | normXP = norm(X-P,'fro'); 64 | if normXQ/max(1,max(normQ,normX)) + normXP/max(1,max(normP,normX)) 0.1 105 | fprintf('SOC returns different point \n'); 106 | fprintf('Soc:Iter *** Fval *** CPU **** sparsity ********* err \n'); 107 | 108 | print_format = ' %i %1.5e %1.2f %1.2f %1.3e \n'; 109 | fprintf(1,print_format, itera, F_ad(itera), time_soc, sparsity_soc, error_XPQ); 110 | flag_succ = 2; % different point 111 | F_SOC = 0; 112 | sparsity_soc = 0; 113 | iter_soc = 0; 114 | 115 | time_soc = 0; 116 | else 117 | 118 | flag_succ = 1; % success 119 | F_SOC = F_ad(itera); 120 | iter_soc = itera; 121 | % residual_Q = norm(Q'*Q-eye(n),'fro')^2; 122 | fprintf('Soc:Iter *** Fval *** CPU **** sparsity ********* err \n'); 123 | 124 | print_format = ' %i %1.5e %1.2f %1.2f %1.3e \n'; 125 | fprintf(1,print_format, itera, F_ad(itera), time_soc, sparsity_soc, error_XPQ); 126 | end 127 | end 128 | -------------------------------------------------------------------------------- /manpg_code_share/SSN_subproblem/Semi_newton_matrix_l21.m: -------------------------------------------------------------------------------- 1 | function [Z,inner_iter,Lam,r_l,stop_flag] = Semi_newton_matrix_l21(n,r,X,t, B,mut,inner_tol,prox,Lam0) 2 | %find the zero of X'*Z(Lam)+ Z(Lam)'*X=2*I, where z(Lam) is the proximal 3 | %point of B+2*t*X*Lam %prox_number=0; 4 | XtX = eye(r); 5 | Xt = X'; 6 | global Dn pDn 7 | numb=0; stop_flag=0; 8 | % commat = Kmn(n,n); 9 | %[n,p]=size(X); 10 | if nargin==8 11 | Lam=zeros(r); 12 | else 13 | Lam=Lam0; 14 | end 15 | X_Lam_prod = B+ 2*t*X*Lam; 16 | [Z,delta,Inact_set] = prox(X_Lam_prod,mut,r); 17 | ZX = Z'*X; 18 | R_Lam= ZX+ZX'-2*eye(r); 19 | RE=pDn*R_Lam(:); 20 | % s = s - r_s/gd; 21 | r_l=norm(R_Lam,'fro'); 22 | 23 | %g=sparse(n^2,n^2); 24 | lambda=0.1; eta1=0.1; eta2=0.3; sigma=r_l; lambda0=0.001; 25 | inner_iter=1; 26 | while r_l^2 > inner_tol 27 | g=zeros(r^2); 28 | reg= lambda*max(min(r_l,0.2),1e-11); 29 | % nnzZ = nnz(Z); 30 | %if r < 15 31 | % if nnzZ > floor(r*(r+1)/2) % directly compute inverse of G 32 | for i=1:r 33 | for j = 1:r 34 | for k = 1:n 35 | g((i*r-r+1:i*r),(j*r-r+1:j*r)) =g((i*r-r+1:i*r),(j*r-r+1:j*r)) + X(k,i)*X(k,j)*delta{k}; 36 | end 37 | end 38 | end 39 | G = 4*t*pDn*(g*Dn); 40 | % new_d =-(G+ reg*eye(n*(n+1)/2))\RE; 41 | [LG,UG] = lu(G+ reg*eye(r*(r+1)/2));% use chol decompostion solve linear system 42 | %new_d2 = -(4*t*Dn*pDn* g+ reg*eye(r^2))\R_Lam(:); 43 | %new_d2 = reshape(new_d2,r,r); 44 | new_d = -(UG\(LG\RE)); 45 | % 46 | % else% Use SWM Formula dim: number of non zero of Z 47 | % Xstack = zeros(nnzZ,r^2); 48 | % dim =0; 49 | % for i = 1:r 50 | % row = find(delta(:,i)==1); 51 | % Xstack((dim+1:dim+length(row)) ,(i*r-r+1:i*r)) = X(row,:); 52 | % dim = length(row) + dim; 53 | % end 54 | % V = Xstack*Dn; U =4*t* pDn*Xstack'; 55 | % [LG,UG] = lu(eye(nnzZ) + 1/reg*V*U); 56 | % new_d = -(1/reg*RE - 1/reg^2 *U*(UG\(LG\(V*RE)))); 57 | % %new_d = -(1/reg*RE - 1/reg^2 *U*((eye(nnzZ) + 1/reg*V*U)\(V*RE))); 58 | % end 59 | new_d=Dn*new_d; 60 | norm_d=norm(new_d)^2; 61 | new_d=reshape(new_d,r,r); 62 | % else% implement cg 63 | % Blkd = cell(r,1); 64 | % for i=1:r 65 | % % Blkd{i} = X'*(Act_set(:,i).*X); 66 | % ind =delta(:,i); 67 | % if sum(ind) < n/2 68 | % X_ind = X(ind,:); 69 | % Blkd{i} = X_ind'*X_ind; 70 | % else 71 | % ind = Inact_set(:,i); 72 | % % X_ind = X(ind,:); 73 | % X_ind = Xt(:,ind); 74 | % Blkd{i} = XtX - X_ind*X_ind'; 75 | % end 76 | % end 77 | % fun =@(x) linop(Blkd,x,r,t,reg); 78 | % [new_d,cg_iter,res] = conjgrad(fun,-R_Lam,min(1e-4,1e-3*r_l)); 79 | % norm_d=norm(new_d)^2; 80 | % end 81 | 82 | X_d_prod=2*t*X*new_d; 83 | X_Lam_new_prod= X_Lam_prod + X_d_prod; 84 | 85 | Lam_new = Lam + new_d ; 86 | [Z, delta,Inact_set]=prox(X_Lam_new_prod,mut,r); 87 | ZX=Z'*X; 88 | R_Lam_new= ZX+ZX'-2*eye(r); 89 | numb=numb+1; 90 | r_l_new=norm(R_Lam_new,'fro'); 91 | % while r_l_new ^2 >= r_l^2 *(1- armjp *t_new) && t_new > 1e-3 92 | 93 | pho=-sum(sum(R_Lam_new.*new_d))/norm_d; 94 | if r_l_new<=0.99*sigma 95 | Lam = Lam_new; 96 | r_l=r_l_new; sigma=r_l; R_Lam=R_Lam_new; RE=pDn*R_Lam(:); 97 | X_Lam_prod = X_Lam_new_prod; 98 | else 99 | V=Lam- sum(sum(R_Lam_new.*(Lam-Lam_new)))/r_l_new^2* R_Lam_new; 100 | X_V_prod=B+2*t*X*V; 101 | [Z,delta,Inact_set]=prox(X_V_prod,mut,r); 102 | ZX=Z'*X; R_V= ZX+ZX'-2*eye(r); %RE=En*R_Lam(:); 103 | r_v=norm(R_V,'fro'); 104 | numb = numb+1; 105 | if pho>=eta1 && r_v<=r_l 106 | Lam=V; R_Lam=R_V; RE=pDn*R_Lam(:); X_Lam_prod=X_V_prod; r_l=r_v; 107 | if pho>=eta2 108 | lambda=(lambda0+lambda)/2; 109 | else 110 | lambda=min(2*lambda,10^5); 111 | end 112 | else 113 | lambda=min(lambda*4,10^5); 114 | end 115 | end 116 | 117 | if inner_iter>20 % return if inner loop > 100 118 | stop_flag=1; 119 | break; 120 | end 121 | % En(j)=r_l; 122 | inner_iter=inner_iter+1; 123 | end 124 | end 125 | 126 | function V = linop(Block_d,x,n,t,reg) 127 | V = zeros(size(x)); 128 | 129 | for i = 1:n 130 | V(:,i) = Block_d{i}*x(:,i); 131 | end 132 | V = 2*t*(V+V') + reg*x; 133 | end -------------------------------------------------------------------------------- /manpg_code_share/compressed modes/manpg_CMS_adap.m: -------------------------------------------------------------------------------- 1 | function [X_manpg, F_manpg,sparsity,time_manpg,iter_adap,flag_succ,num_linesearch,mean_ssn] =manpg_CMS_adap(H,option,d_l,V) 2 | %min -Tr(X'*H*X)+ mu*norm(X,1) s.t. X'*X=Ir. 3 | %% 4 | %parameters 5 | tic; 6 | r = option.r;%number of col 7 | n = option.n;%dim 8 | mu = option.mu; 9 | maxiter =option.maxiter+1; 10 | tol = option.tol; 11 | %inner_tol = option.inner_tol; 12 | inner_iter = option.inner_iter; 13 | h=@(X) sum(mu.*sum(abs(X))); 14 | prox_fun = @(b,lambda,r) proximal_l1(b,lambda,r); 15 | inner_flag = 0; 16 | %setduplicat_pduplicat(r); 17 | Dn = sparse(DuplicationM(r)); 18 | pDn = (Dn'*Dn)\Dn'; 19 | t_min=1e-4; % minimum stepsize 20 | %% 21 | %initial point 22 | X = option.phi_init; 23 | % L=2*abs(eigs(full(B),1)); 24 | % L=2*abs(eigs(B,1)); 25 | L = 8/d_l^2.*(sin(pi/4))^2 + V; 26 | %dx = option.L/n; 27 | %LAM_A = (cos(2*pi*[0:n-1]'/n)-1)/dx^2; 28 | %HX = real(fft(bsxfun(@times,ifft( X ),LAM_A))); 29 | HX = H*X; 30 | 31 | F(1) = -sum(sum(X.*(HX)))+h(X); 32 | num_inner = zeros(maxiter,1); 33 | opt_sub = num_inner; 34 | num_linesearch = 0; 35 | alpha = 1; 36 | t0 = 1/L; 37 | t =t0; 38 | linesearch_flag = 0; 39 | num_inexact = 0; 40 | for iter = 2:maxiter 41 | % fprintf('manpg__________________iter: %3d, \n', iter); 42 | ngx = HX; % negative gradient 2AX 43 | %xgx = X'*ngx; 44 | %pgx = ngx - 0.5*X*(xgx+xgx'); 45 | pgx= ngx; % grad or projected gradient both okay 46 | %% subproblem 47 | if alpha < t_min || num_inexact > 10 48 | inner_tol = max(1e-15, min(1e-13,1e-5*tol*t^2)); % subproblem inexact; 49 | else 50 | inner_tol = max(1e-13, min(1e-11,1e-3*tol*t^2)); 51 | end 52 | 53 | if iter == 2 54 | [ PY,num_inner(iter),Lam, opt_sub(iter),in_flag]=Semi_newton_matrix(n,r,X,t,X+2*t*pgx,mu*t,inner_tol,prox_fun,inner_iter,zeros(r),Dn,pDn); 55 | 56 | else 57 | [ PY,num_inner(iter),Lam, opt_sub(iter) ,in_flag]=Semi_newton_matrix(n,r,X,t,X+2*t*pgx,mu*t,inner_tol,prox_fun,inner_iter,Lam,Dn,pDn); 58 | 59 | end 60 | 61 | % if iter ==2 62 | % [Y,~, dual_y,dual_z, ~, ~, ~, ~, subiter, ls, cg_iter] =SSNAL_mat(eye(n),eye(n),X-t*pgx, X, eye(n), eye(n),X, X,zeros(r),zeros(n,r), eye(r), mu*t, 50, 1e-4) ; 63 | % else 64 | % [Y,~, dual_y,dual_z, ~, ~, ~, ~, subiter, ls, cg_iter] = SSNAL_mat(eye(n),eye(n),X-t*pgx, X, eye(n),eye(n),X, X, dual_y, dual_z, eye(r), mu*t, 50, 1e-3) ; 65 | % end 66 | if in_flag == 1 % subprolem total iteration. 67 | inner_flag = 1 + inner_flag; 68 | end 69 | alpha=1; 70 | D = PY-X; %descent direction D 71 | 72 | [U, SIGMA, S] = svd(PY'*PY); 73 | SIGMA =diag(SIGMA); 74 | Z = PY*(U*diag(sqrt(1./SIGMA))*S'); 75 | % [Z,R]=qr(PY,0); Z = Z*diag(sign(diag(R))); %old version need consider the sign 76 | 77 | HZ = H*Z; 78 | % HZ = real(fft(bsxfun(@times,ifft( Z ),LAM_A))); 79 | % AZ = real(ifft( LAM_manpg.*fft(Z) )); 80 | 81 | F_trial = -sum(sum(Z.*(HZ)))+h(Z); 82 | normDsquared = norm(D,'fro')^2; 83 | 84 | % if normDsquared/t^2 < tol 85 | % % if abs(F(iter)-F(iter-1))/(abs(F(iter))+1)= F(iter-1)-0.5/t*alpha*normDsquared 90 | alpha = 0.5*alpha; 91 | linesearch_flag = 1; 92 | num_linesearch = num_linesearch+1; 93 | if alpha 1e-1 125 | flag_succ = 0; 126 | sparsity = 0; 127 | F_manpg = 0; 128 | time_manpg = 0; 129 | iter_adap = 0; 130 | else 131 | flag_succ = 1; 132 | iter_adap = iter; 133 | sparsity= sum(sum(X_manpg==0))/(n*r); 134 | F_manpg = F(iter); 135 | 136 | fprintf('ManPG_adap:Iter *** Fval *** CPU **** sparsity ***inner_inexact&averge_No. ** opt_norm ** total_linsea \n'); 137 | print_format = ' %i %1.5e %1.2f %1.2f %4i %2.2f %1.3e %d \n'; 138 | fprintf(1,print_format, iter-1,min(F), time_manpg,sparsity, inner_flag,mean_ssn ,sqrt(normDsquared)/t,num_linesearch); 139 | end -------------------------------------------------------------------------------- /manpg_code_share/compressed modes/manpg_CMS.m: -------------------------------------------------------------------------------- 1 | function [X_manpg, F_manpg,sparsity,time_manpg,iter,flag_succ,num_linesearch,mean_ssn] =manpg_CMS(H,option,d_l,V) 2 | %min -Tr(X'*H*X)+ mu*norm(X,1) s.t. X'*X=Ir. 3 | %% 4 | %parameters 5 | tic; 6 | r = option.r;%number of col 7 | n = option.n;%dim 8 | mu = option.mu; 9 | maxiter =option.maxiter+1; 10 | tol = option.tol; 11 | %inner_tol = option.inner_tol; 12 | inner_iter = option.inner_iter; 13 | h=@(X) sum(mu.*sum(abs(X))); 14 | prox_fun = @(b,lambda,r) proximal_l1(b,lambda,r); 15 | inner_flag = 0; 16 | %setduplicat_pduplicat(r); 17 | Dn = sparse(DuplicationM(r)); 18 | pDn = (Dn'*Dn)\Dn'; 19 | t_min=1e-4; % minimum stepsize 20 | %% 21 | %initial point 22 | X = option.phi_init; 23 | % L=2*abs(eigs(full(B),1)); 24 | % L=2*abs(eigs(B,1)); 25 | L = 8/d_l^2.*(sin(pi/4))^2 + V; 26 | %dx = option.L/n; 27 | %LAM_A = (cos(2*pi*[0:n-1]'/n)-1)/dx^2; 28 | %HX = real(fft(bsxfun(@times,ifft( X ),LAM_A))); 29 | HX = H*X; 30 | 31 | F(1) = -sum(sum(X.*(HX)))+h(X); 32 | num_inner = zeros(maxiter,1); 33 | opt_sub = num_inner; 34 | num_linesearch = 0; 35 | alpha = 1; 36 | t0 = 1/L; t =t0; 37 | linesearch_flag = 0; 38 | num_inexact = 0; 39 | for iter = 2:maxiter 40 | % fprintf('manpg__________________iter: %3d, \n', iter); 41 | ngx = HX; % negative gradient 2AX 42 | %xgx = X'*ngx; 43 | %pgx = ngx - 0.5*X*(xgx+xgx'); 44 | pgx= ngx; % grad or projected gradient both okay 45 | %% subproblem 46 | if alpha < t_min || num_inexact > 10 47 | inner_tol = max(5e-16, min(1e-14,1e-5*tol*t^2)); % subproblem inexact; 48 | else 49 | inner_tol = max(1e-13, min(1e-11,1e-3*tol*t^2)); 50 | end 51 | 52 | if iter == 2 53 | [ PY,num_inner(iter),Lam, opt_sub(iter),in_flag]=Semi_newton_matrix(n,r,X,t,X+2*t*pgx,mu*t,inner_tol,prox_fun,inner_iter,zeros(r),Dn,pDn); 54 | 55 | else 56 | [ PY,num_inner(iter),Lam, opt_sub(iter) ,in_flag]=Semi_newton_matrix(n,r,X,t,X+2*t*pgx,mu*t,inner_tol,prox_fun,inner_iter,Lam,Dn,pDn); 57 | 58 | end 59 | 60 | % if iter ==2 61 | % [Y,~, dual_y,dual_z, ~, ~, ~, ~, subiter, ls, cg_iter] =SSNAL_mat(eye(n),eye(n),X-t*pgx, X, eye(n), eye(n),X, X,zeros(r),zeros(n,r), eye(r), mu*t, 50, 1e-4) ; 62 | % else 63 | % [Y,~, dual_y,dual_z, ~, ~, ~, ~, subiter, ls, cg_iter] = SSNAL_mat(eye(n),eye(n),X-t*pgx, X, eye(n),eye(n),X, X, dual_y, dual_z, eye(r), mu*t, 50, 1e-3) ; 64 | % end 65 | if in_flag == 1 % subprolem total iteration. 66 | inner_flag = 1 + inner_flag; 67 | end 68 | alpha=1; 69 | D = PY-X; %descent direction D 70 | 71 | [U, SIGMA, S] = svd(PY'*PY); 72 | SIGMA =diag(SIGMA); 73 | Z = PY*(U*diag(sqrt(1./SIGMA))*S'); 74 | % [Z,R]=qr(PY,0); Z = Z*diag(sign(diag(R))); %old version need consider the sign 75 | 76 | HZ = H*Z; 77 | % HZ = real(fft(bsxfun(@times,ifft( Z ),LAM_A))); 78 | % AZ = real(ifft( LAM_manpg.*fft(Z) )); 79 | 80 | F_trial = -sum(sum(Z.*(HZ)))+h(Z); 81 | normDsquared = norm(D,'fro')^2; 82 | 83 | if normDsquared/t^2 < tol 84 | % if abs(F(iter)-F(iter-1))/(abs(F(iter))+1)= F(iter-1)-0.5/t*alpha*normDsquared 89 | alpha = 0.5*alpha; 90 | linesearch_flag = 1; 91 | num_linesearch = num_linesearch+1; 92 | if alpha 1e-1 126 | flag_succ = 0; 127 | sparsity = 0; 128 | F_manpg = 0; 129 | time_manpg = 0; 130 | else 131 | flag_succ = 1; 132 | sparsity= sum(sum(X_manpg==0))/(n*r); 133 | F_manpg = F(iter-1); 134 | 135 | fprintf('ManPG:Iter *** Fval *** CPU **** sparsity ***inner_inexact&averge_No. ** opt_norm ** total_linsea \n'); 136 | print_format = ' %i %1.5e %1.2f %1.2f %4i %2.2f %1.3e %d \n'; 137 | fprintf(1,print_format, iter-1,min(F), time_manpg,sparsity, inner_flag, sum(num_inner)/(iter-1) ,sqrt(normDsquared)/t,num_linesearch); 138 | end -------------------------------------------------------------------------------- /manpg_code_share/SPCA/manpg_orth_sparse.m: -------------------------------------------------------------------------------- 1 | function [X_manpg, F_manpg,sparsity,time_manpg,iter,flag_succ,num_linesearch,mean_ssn] = manpg_orth_sparse(B,option) 2 | %min -Tr(X'*B*X)+ mu*norm(X,1) s.t. X'*X=Ir. X \in R^{p*r} 3 | % mu can be a vector with weighted parameter 4 | %parameters 5 | tic; 6 | r = option.r;%number of col 7 | n = option.n;%dim 8 | mu = option.mu; 9 | maxiter = option.maxiter; 10 | tol = option.tol; 11 | h = @(X) sum(mu.*sum(abs(X))); 12 | inner_iter = option.inner_iter; 13 | prox_fun = @(b,lambda,r) proximal_l1(b,lambda,r); 14 | inner_flag = 0; 15 | %setduplicat_pduplicat(r); 16 | Dn = sparse(DuplicationM(r)); 17 | pDn = (Dn'*Dn)\Dn'; 18 | type = option.type; 19 | t_min = 1e-4; % minimum stepsize 20 | %% 21 | %initial point 22 | X = option.phi_init; 23 | if type == 1 24 | L = 2*abs(eigs(full(B),1)); 25 | % L=2*abs(eigs(B,1)); 26 | else 27 | L = 2*(svds(full(B),1))^2; 28 | end 29 | 30 | % if mu > L/2 31 | % fprintf('Too large penalty parameter mu, trivial solution\n'); 32 | % X_manpg = 33 | % return; 34 | % end 35 | 36 | if type == 1 37 | AX = B*X; 38 | else 39 | AX = B'*(B*X); 40 | end 41 | F(1) = -sum(sum(X.*(AX)))+h(X); 42 | num_inner = zeros(maxiter,1); 43 | opt_sub = num_inner; 44 | num_linesearch = 0; 45 | num_inexact = 0; 46 | alpha =1; 47 | t = 1/L; 48 | %inner_tol = 0.1*tol^2*t^2; 49 | 50 | for iter = 2:maxiter 51 | % fprintf('manpg__________________iter: %3d, \n', iter); 52 | ngx = 2*AX; % negative gradient pgx=gx-X*xgx; %projected gradient 53 | neg_pgx = ngx; % grad or projected gradient both okay 54 | %% subproblem 55 | if alpha < t_min || num_inexact > 10 56 | inner_tol = max(5e-16, min(1e-14,1e-5*tol*t^2)); % subproblem inexact; 57 | else 58 | inner_tol = max(1e-13, min(1e-11,1e-3*tol*t^2)); 59 | end 60 | 61 | if iter == 2 62 | [ PY,num_inner(iter),Lam, opt_sub(iter),in_flag] = Semi_newton_matrix(n,r,X,t,X + t*neg_pgx,mu*t,inner_tol,prox_fun,inner_iter,zeros(r),Dn,pDn); 63 | % [ PY,num2(iter),r_norm(iter)]=fista(X,pgx,mu,t); 64 | else 65 | [ PY,num_inner(iter),Lam, opt_sub(iter),in_flag] = Semi_newton_matrix(n,r,X,t,X + t*neg_pgx,mu*t,inner_tol,prox_fun,inner_iter,Lam,Dn,pDn); 66 | % [ PY,num2(iter),r_norm(iter)]=fista(X,pgx,mu,t); 67 | end 68 | 69 | % if iter ==2 70 | % [Y,~, dual_y,dual_z, ~, ~, ~, ~, subiter, ls, cg_iter] =SSNAL_mat(eye(n),eye(n),X-t*pgx, X, eye(n), eye(n),X, X,zeros(r),zeros(n,r), eye(r), mu*t, 50, 1e-4) ; 71 | % else 72 | % [Y,~, dual_y,dual_z, ~, ~, ~, ~, subiter, ls, cg_iter] = SSNAL_mat(eye(n),eye(n),X-t*pgx, X, eye(n),eye(n),X, X, dual_y, dual_z, eye(r), mu*t, 50, 1e-3) ; 73 | % end 74 | if in_flag == 1 % subprolem not exact. 75 | inner_flag = 1 + inner_flag; 76 | end 77 | alpha = 1; 78 | D = PY-X; %descent direction D 79 | 80 | [U, SIGMA, S] = svd(PY'*PY); SIGMA =diag(SIGMA); Z = PY*(U*diag(sqrt(1./SIGMA))*S'); 81 | % [Z,R]=qr(PY,0); Z = Z*diag(sign(diag(R))); %old version need consider the sign 82 | 83 | if type == 1 84 | AZ = B*Z; 85 | else 86 | AZ = B'*(B*Z); 87 | end 88 | % AZ = real(ifft( LAM_manpg.*fft(Z) )); 89 | 90 | f_trial = -sum(sum(Z.*(AZ))); 91 | F_trial = f_trial+h(Z); normDsquared=norm(D,'fro')^2; 92 | 93 | 94 | if normDsquared/t^2 < tol 95 | % if abs(F(iter)-F(iter-1))/(abs(F(iter))+1)= F(iter-1)-0.5/t*alpha*normDsquared 100 | alpha = 0.5*alpha; 101 | linesearch_flag = 1; 102 | num_linesearch = num_linesearch+1; 103 | if alpha 1e-1 133 | flag_succ = 0; 134 | sparsity = 0; 135 | F_manpg = 0; 136 | time_manpg = 0; 137 | else 138 | flag_succ = 1; 139 | sparsity= sum(sum(X_manpg==0))/(n*r); 140 | F_manpg = F(iter-1); 141 | 142 | fprintf('ManPG:Iter *** Fval *** CPU **** sparsity ***inner_inexact&averge_No. ** opt_norm ** total_linsea \n'); 143 | print_format = ' %i %1.5e %1.2f %1.2f %4i %2.2f %1.3e %d \n'; 144 | fprintf(1,print_format, iter-1,min(F), time_manpg,sparsity, inner_flag, sum(num_inner)/(iter-1) ,sqrt(normDsquared)/t,num_linesearch); 145 | end -------------------------------------------------------------------------------- /manpg_code_share/SPCA/manpg_orth_sparse_adap.m: -------------------------------------------------------------------------------- 1 | function [X_manpg, F_manpg,sparsity,time_manpg,iter_adap,flag_succ,num_linesearch,mean_ssn] = manpg_orth_sparse_adap(B,option) 2 | %min -Tr(X'*A*X)+ mu*norm(X,1) s.t. X'*X=Ir. 3 | % A = B'*B type = 0 or A = B type = 1 4 | % mu can be a vector with weighted parameter 5 | %parameters 6 | tic; 7 | r = option.r;%number of col 8 | n = option.n;%dim 9 | mu = option.mu; 10 | maxiter = option.maxiter; 11 | tol = option.tol; 12 | h = @(X) sum(mu.*sum(abs(X))); 13 | inner_iter = option.inner_iter; 14 | prox_fun = @(b,lambda,r) proximal_l1(b,lambda,r); 15 | inner_flag = 0; 16 | %setduplicat_pduplicat(r); 17 | Dn = sparse(DuplicationM(r)); 18 | pDn = (Dn'*Dn)\Dn'; 19 | type = option.type; 20 | t_min = 1e-4; % minimum stepsize 21 | %% 22 | %initial point 23 | X = option.phi_init; 24 | if type == 1 25 | L = 2*abs(eigs(full(B),1)); 26 | % L=2*abs(eigs(B,1)); 27 | else 28 | L = 2*(svds(full(B),1))^2; 29 | end 30 | 31 | if type == 1 32 | AX = B*X; 33 | else 34 | AX = B'*(B*X); 35 | end 36 | F(1) = -sum(sum(X.*(AX)))+h(X); 37 | num_inner = zeros(maxiter,1); 38 | opt_sub = num_inner; 39 | num_linesearch = 0; 40 | num_inexact = 0; 41 | alpha =1; 42 | t0 = 1/L; 43 | t = t0; 44 | %inner_tol = 0.1*tol^2*t^2; 45 | linesearch_flag = 0; 46 | for iter = 2:maxiter 47 | % fprintf('manpg__________________iter: %3d, \n', iter); 48 | ngx = 2*AX; % negative gradient pgx=gx-X*xgx; %projected gradient 49 | neg_pgx = ngx; % grad or projected gradient both okay 50 | %% subproblem 51 | if alpha < t_min || num_inexact > 10 52 | inner_tol = max(1e-15, min(1e-13,1e-5*tol*t^2)); % subproblem inexact; 53 | else 54 | inner_tol = max(1e-13, min(1e-11,1e-3*tol*t^2)); 55 | end 56 | 57 | if iter == 2 58 | [ PY,num_inner(iter),Lam, opt_sub(iter),in_flag] = Semi_newton_matrix(n,r,X,t,X + t*neg_pgx,mu*t,inner_tol,prox_fun,inner_iter,zeros(r),Dn,pDn); 59 | % [ PY,num2(iter),r_norm(iter)]=fista(X,pgx,mu,t); 60 | else 61 | [ PY,num_inner(iter),Lam, opt_sub(iter),in_flag] = Semi_newton_matrix(n,r,X,t,X + t*neg_pgx,mu*t,inner_tol,prox_fun,inner_iter,Lam,Dn,pDn); 62 | % [ PY,num2(iter),r_norm(iter)]=fista(X,pgx,mu,t); 63 | end 64 | 65 | % if iter ==2 66 | % [Y,~, dual_y,dual_z, ~, ~, ~, ~, subiter, ls, cg_iter] =SSNAL_mat(eye(n),eye(n),X-t*pgx, X, eye(n), eye(n),X, X,zeros(r),zeros(n,r), eye(r), mu*t, 50, 1e-4) ; 67 | % else 68 | % [Y,~, dual_y,dual_z, ~, ~, ~, ~, subiter, ls, cg_iter] = SSNAL_mat(eye(n),eye(n),X-t*pgx, X, eye(n),eye(n),X, X, dual_y, dual_z, eye(r), mu*t, 50, 1e-3) ; 69 | % end 70 | if in_flag == 1 % subprolem not exact. 71 | inner_flag = 1 + inner_flag; 72 | end 73 | alpha = 1; 74 | D = PY-X; %descent direction D 75 | 76 | [U, SIGMA, S] = svd(PY'*PY); SIGMA =diag(SIGMA); Z = PY*(U*diag(sqrt(1./SIGMA))*S'); 77 | % [Z,R]=qr(PY,0); Z = Z*diag(sign(diag(R))); %old version need consider the sign 78 | 79 | if type == 1 80 | AZ = B*Z; 81 | else 82 | AZ = B'*(B*Z); 83 | end 84 | % AZ = real(ifft( LAM_manpg.*fft(Z) )); 85 | 86 | f_trial = -sum(sum(Z.*(AZ))); 87 | F_trial = f_trial+h(Z); normDsquared=norm(D,'fro')^2; 88 | 89 | %% linesearch 90 | while F_trial >= F(iter-1)-0.5/t*alpha*normDsquared 91 | alpha = 0.5*alpha; 92 | linesearch_flag = 1; 93 | num_linesearch = num_linesearch+1; 94 | if alpha 1e-1 132 | flag_succ = 0; 133 | sparsity = 0; 134 | F_manpg = 0; 135 | time_manpg = 0; 136 | iter_adap = 0; 137 | else 138 | flag_succ = 1; 139 | iter_adap = iter; 140 | sparsity= sum(sum(X_manpg==0))/(n*r); 141 | F_manpg = F(iter); 142 | 143 | fprintf('ManPG_adap:Iter *** Fval *** CPU **** sparsity ***inner_inexact&averge_No. ** opt_norm ** total_linsea \n'); 144 | print_format = ' %i %1.5e %1.2f %1.2f %4i %2.2f %1.3e %d \n'; 145 | fprintf(1,print_format, iter-1,min(F), time_manpg,sparsity, inner_flag, sum(num_inner)/(iter-1) ,sqrt(normDsquared)/t,num_linesearch); 146 | end -------------------------------------------------------------------------------- /manpg_code_share/SSN_subproblem/Semi_newton_matrix.m: -------------------------------------------------------------------------------- 1 | function [Z,j,Lam,r_l,stop_flag] = Semi_newton_matrix(n,r,X,t, B,mut,inner_tol,prox_fun,inner_max_iter,Lam0,Dn,pDn) 2 | %find the zero of X'*Z(Lam)+ Z(Lam)'*X=2*I, where z(Lam) is the proximal 3 | %point of B+2*t*X*Lam %prox_number=0; 4 | XtX = eye(r); 5 | Xt = X'; 6 | %global Dn pDn 7 | 8 | stop_flag = 0; 9 | Lam = Lam0; 10 | X_Lam_prod = B + 2*t*(X*Lam); 11 | [Z,Act_set,Inact_set] = prox_fun(X_Lam_prod,mut,r); 12 | ZX = Z'*X; 13 | R_Lam = ZX+ZX'-2*eye(r); 14 | RE = pDn*R_Lam(:); 15 | % s = s - r_s/gd; 16 | r_l = norm(R_Lam,'fro'); 17 | g=zeros(r^2); 18 | %g=sparse(n^2,n^2); 19 | lambda = 0.2; 20 | eta1 = 0.1; 21 | eta2 = 0.3; 22 | sigma = r_l; 23 | lambda0 = 1e-3; 24 | j = 0; 25 | while r_l^2 > inner_tol 26 | 27 | reg = lambda*max(min(r_l,0.1),1e-11); 28 | nnzZ = nnz(Z); 29 | if r < 15 30 | if nnzZ > floor(r*(r+1)/2) % directly compute inverse of G 31 | for i = 1:r 32 | g((i*r-r+1:i*r),(i*r-r+1:i*r)) = X'*(Act_set(:,i).*X); 33 | end 34 | G = 4*t*(pDn*(g*Dn)); 35 | [LG,UG] = lu(G+ reg*eye(r*(r+1)/2)); 36 | new_d = -(UG\(LG\RE)); % use lu decompostion solve linear system 37 | %new_d1 = -(G+ reg*eye(r*(r+1)/2))\RE; 38 | %LG = chol(G+ reg*eye(r*(r+1)/2),'lower'); %new_d = LG'\(LG\(-RE)); 39 | else% Use SWM Formula dim: number of non zero of Z 40 | Xstack = zeros(nnzZ,r^2); 41 | dim =0; 42 | for i = 1:r 43 | row = find(Act_set(:,i)==1); 44 | Xstack((dim+1:dim+length(row)) ,(i*r-r+1:i*r)) = X(row,:); 45 | dim = length(row) + dim; 46 | end 47 | V = Xstack*Dn; U = 4*t* (pDn*Xstack'); 48 | [LG,UG] = lu(eye(nnzZ) + 1/reg*(V*U)); 49 | new_d = -(1/reg*RE - 1/reg^2 *U*(UG\(LG\(V*RE)))); 50 | % LG = chol(eye(nnzZ) + 1/reg*(V*U),'lower'); 51 | % new_d = -(1/reg*RE - 1/reg^2 *U*(LG'\(LG\(V*RE)))); 52 | %new_d = -(1/reg*RE - 1/reg^2 *U*((eye(nnzZ) + 1/reg*V*U)\(V*RE))); 53 | end 54 | new_d = Dn*new_d; 55 | %norm_d = norm(new_d)^2; 56 | new_d = reshape(new_d,r,r); 57 | else% implement cg 58 | Blkd = cell(r,1); 59 | for i=1:r 60 | % Blkd{i} = X'*(Act_set(:,i).*X); 61 | ind =Act_set(:,i); 62 | if sum(ind) < n/2 63 | X_ind = X(ind,:); 64 | Blkd{i} = X_ind'*X_ind; 65 | else 66 | ind = Inact_set(:,i); 67 | % X_ind = X(ind,:); 68 | X_ind = Xt(:,ind); 69 | Blkd{i} = XtX - X_ind*X_ind'; 70 | end 71 | end 72 | fun =@(x) linop(Blkd,x,r,t,reg); 73 | [new_d,cg_iter,res] = conjgrad(fun,-R_Lam,min(1e-4,1e-3*r_l)); 74 | % norm_d = norm(new_d)^2; 75 | end 76 | t_new = 1; 77 | X_d_prod = 2*t*(X*new_d); 78 | X_Lam_new_prod = X_Lam_prod + t_new*X_d_prod; 79 | 80 | %Lam_new = Lam + new_d ; 81 | [Z, Act_set,Inact_set] = prox_fun(X_Lam_new_prod,mut,r); 82 | ZX=Z'*X; 83 | R_Lam_new = ZX+ZX'-2*eye(r); 84 | 85 | r_l_new = norm(R_Lam_new,'fro'); 86 | while r_l_new ^2 >= r_l^2 *(1- 0.001 *t_new) && t_new > 1e-3 87 | t_new = 0.5*t_new; 88 | %Lam_new = Lam + t_new* new_d ; 89 | X_Lam_new_prod = X_Lam_prod + t_new*X_d_prod; 90 | [Z, Act_set,Inact_set] = prox_fun(X_Lam_new_prod,mut,r); 91 | ZX = Z'*X; 92 | R_Lam_new = ZX+ZX'-2*eye(r); 93 | r_l_new = norm(R_Lam_new,'fro'); 94 | %pho = -sum(sum(R_Lam_new.*new_d))/norm_d; 95 | % if r_l_new <= sigma % New step 96 | 97 | end 98 | Lam = Lam + t_new* new_d ; 99 | r_l = r_l_new; 100 | % sigma = r_l; 101 | R_Lam = R_Lam_new; 102 | RE = pDn*R_Lam(:); 103 | X_Lam_prod = X_Lam_new_prod; 104 | % else 105 | % V = Lam - sum(sum(R_Lam_new.*(Lam-Lam_new)))/r_l_new^2* R_Lam_new; 106 | % X_V_prod = B+2*t*(X*V); 107 | % [Z,Act_set,Inact_set] = prox_fun(X_V_prod,mut,r); 108 | % ZX = Z'*X; 109 | % R_V = ZX+ZX'-2*eye(r); 110 | % r_v = norm(R_V,'fro'); 111 | % 112 | % if pho>=eta1 && r_v<=r_l %projection step 113 | % Lam = V; 114 | % R_Lam = R_V; 115 | % RE = pDn*R_Lam(:); 116 | % X_Lam_prod = X_V_prod; 117 | % r_l = r_v; 118 | % else 119 | % if pho>=eta1 && r_v > r_l % contraction step 120 | % Lam = Lam - 2*t*R_Lam; 121 | % X_Lam_prod = B + 2*t*(X*Lam); 122 | % [Z,Act_set,Inact_set] = prox_fun(X_Lam_prod,mut,r); 123 | % ZX = Z'*X; 124 | % R_Lam = ZX+ZX'-2*eye(r); 125 | % RE = pDn*R_Lam(:); 126 | % r_l = norm(R_Lam,'fro'); 127 | % end 128 | % end 129 | % end 130 | % 131 | % if pho >= eta2 132 | % lambda=(lambda0+lambda)/2; 133 | % else 134 | % if pho> eta1 135 | % lambda = min(2*lambda,10^4); 136 | % 137 | % else 138 | % lambda = min(lambda*4,10^4); 139 | % end 140 | % end 141 | 142 | if j>inner_max_iter % return if inner loop > 100 143 | stop_flag = 1; 144 | break; 145 | end 146 | % En(j)=r_l; 147 | j = j+1; 148 | end 149 | end 150 | 151 | function V = linop(Block_d,x,n,t,reg) 152 | V = zeros(size(x)); 153 | 154 | for i = 1:n 155 | V(:,i) = Block_d{i}*x(:,i); 156 | end 157 | V = 2*t*(V+V') + reg*x; 158 | end -------------------------------------------------------------------------------- /manpg_code_share/compressed modes/PAMAL_CMs.m: -------------------------------------------------------------------------------- 1 | function [X_pamal, F_pamal,sparsity_pamal,time_PAMAL, error_XPQ,iter_pamal,flag_succ]=PAMAL_CMs(H,option,V) 2 | % min Tr(X'*A*X)+ mu*norm(X,1) s.t. X'*X=Ir. 3 | % A = -H 4 | tic; 5 | r=option.r; n=option.n; 6 | mu = option.mu; 7 | maxiter =option.maxiter; 8 | tol = option.tol; 9 | mu2 = 1/mu; 10 | A = - H; 11 | 12 | % r = 2*(svds(B,1))^2+ 0.5*d;% ADMM stepsize 13 | norminf=@(z) max(max(abs(z))); 14 | lam_min=-1e2; lam_max=1e2; 15 | c1=0.5; c2=c1; c3=c1; 16 | %rng(40); 17 | %beta = V + r/5; %stepsize 18 | %beta = n*r*mu/1000 + 1; 19 | beta_0 = r/20 + 1; 20 | beta = beta_0*2; 21 | %beta = r*mu/2 + 1; 22 | 23 | %[U,~]=eigs(A); [~,r]=size(U); X0= U(:,(r-n+1:r)); 24 | X0 = option.phi_init; 25 | X_old = X0; 26 | P_old = X0; 27 | Q_old = X0; 28 | lambda1 = zeros(n,r); 29 | lambda2 =lambda1; 30 | lambda1_old = lambda1; 31 | lambda2_old =lambda2; 32 | R1_old = 0; R2_old=0; 33 | F_PAMAL = zeros(maxiter,1); 34 | %Z1 = 2*A +c1*eye(d); 35 | dx = option.L/n; % r = 4/dx^2.*(sin(pi/4))^2 + n/2; 36 | LAM_A = - (cos(2*pi*(0:n-1)'/n)-1)/dx^2; 37 | FFTZ1 = 2*LAM_A + c1; 38 | flag = 0; 39 | flag_time_out = 0; 40 | not_full_rank = 0; 41 | pamal_time = 0; 42 | pamal_time = pamal_time + toc; 43 | for iterp=1:maxiter 44 | tic; 45 | num(iterp)=1; 46 | % Z= Z1 + 2*r*eye(d) ; 47 | FFTZ = FFTZ1 + 2*beta; 48 | % X=Z\(lambda1_old+lambda2_old+r*Q_old+r*P_old+c1*X_old); 49 | X = real(ifft(bsxfun(@rdivide,fft( lambda1_old+lambda2_old+ beta*Q_old+ beta*P_old+c1*X_old ),FFTZ))); 50 | 51 | Q_m= (beta*X-lambda1_old+c2*Q_old)/(beta+c2); 52 | eta= 1/(mu2*(beta+c2)); 53 | Q=sign(Q_m).* max(0, abs(Q_m)-eta); 54 | P_m = (beta*X+c3*P_old-lambda2_old)/(beta+c3); 55 | 56 | [U,~,V] = svd(P_m,0); 57 | P = U*V'; 58 | %%%%%%%%%%%%% svd P_m'*P_m 59 | % [U, D, S] = svd(P_m'*P_m); 60 | % D = diag(D); 61 | % if abs(prod(D))>0 62 | % P = P_m*U*diag(sqrt(1./D))*S'; 63 | % else 64 | % not_full_rank = not_full_rank +1; 65 | % end 66 | 67 | Theta1=beta*(Q_old-Q+P_old-P)+c1*(X_old-X); 68 | Theta2=c2*(Q_old-Q); 69 | Theta3=c3*(P_old-P); 70 | Theta=[Theta1,Theta2,Theta3]; 71 | X_old = X; P_old = P; Q_old = Q; 72 | 73 | while norminf(Theta)> max(1e-6, (0.995)^iterp) % Ji-paper 0.999: not good enough to get exact solution 74 | if num(iterp)> 100 75 | break; 76 | end 77 | %Z=2*A+(r+r+c1)*eye(d); 78 | % X=Z\(lambda1_old+lambda2_old+r*Q_old+r*P_old+c1*X_old); 79 | X = real(ifft(bsxfun(@rdivide,fft( lambda1_old+lambda2_old+beta*Q_old+beta*P_old+c1*X_old ),FFTZ))); 80 | Q_m = (beta*X-lambda1_old+c2*Q_old)/(beta+c2); 81 | eta= 1/(mu2*(beta+c2)); 82 | Q = sign(Q_m).* max(0, abs(Q_m)-eta); 83 | P_m = (beta*X+c3*P_old-lambda2_old)/(beta+c3); 84 | 85 | [U,~,V]=svd(P_m,0); 86 | P = U*V'; 87 | % [U, D, S] = svd(P_m'*P_m); 88 | % D = diag(D); 89 | % if abs(prod(D))>0 90 | % P = P_m*(U*diag(sqrt(1./D))*S'); 91 | % end 92 | Theta1 = beta*(Q_old-Q+P_old-P)+c1*(X_old-X); 93 | Theta2 = c2*(Q_old-Q); 94 | Theta3 = c3*(P_old-P); 95 | Theta = [Theta1,Theta2,Theta3]; 96 | 97 | X_old = X; P_old = P; Q_old = Q; 98 | num(iterp) = 1+num(iterp); 99 | 100 | end 101 | lambda1 =lambda1_old +beta*(Q-X); lambda2=lambda2_old+beta*(P-X); 102 | lambda1_old= max(lam_min,lambda1); lambda1_old= min(lam_max,lambda1_old); 103 | lambda2_old= max(lam_min,lambda2); lambda2_old= min(lam_max,lambda2_old); 104 | R1 = Q-X; 105 | R2 = P-X; 106 | 107 | if norminf(R1)>norminf(R1_old)*0.99 || norminf(R2)>norminf(R2_old)*0.99 108 | %beta = min(beta_0*1000, beta*1.001); 109 | beta = beta*1.001; 110 | end 111 | %AP=-B'*(B*P); 112 | 113 | %AP = real(fft(bsxfun(@times,ifft( P ),LAM_A))); 114 | R1_old = R1; R2_old = R2; 115 | if iterp > 2 116 | % if abs(F_PALM(iterp)-F_PALM(iterp-1))/(abs(F_PALM(iterp))+1) r*60 % not over r minutes 138 | flag_time_out = 1; 139 | break; 140 | end 141 | end 142 | P((abs(P)<=1e-5))=0; 143 | X_pamal =P; 144 | time_PAMAL = pamal_time; 145 | inner_iterate = sum(num)/iterp; 146 | error_XPQ = norm(X-P,'fro') + norm(X-Q,'fro'); 147 | sparsity_pamal= sum(sum(P==0))/(n*r); 148 | X_manpg = option.X_manpg ; 149 | if iterp == maxiter || flag_time_out == 1 150 | fprintf('PAMAL fails to converge in %d iterations \n', maxiter); 151 | %fprintf('PAMAL:Iter *** Fval *** CPU **** sparsity *** iaverge_No. ** err *** inner_opt \n'); 152 | 153 | %print_format = ' %i %1.5e %1.2f %1.2f %2.2f %1.3e %1.3e \n'; 154 | %fprintf(1,print_format, iterp,F_PAMAL(iterp),time_PAMAL, sparsity_pamal, inner_iterate, error_XPQ,norminf(Theta)); 155 | flag_succ = 0; 156 | F_pamal = 0; 157 | sparsity_pamal = 0; 158 | iter_pamal = 0; 159 | time_PAMAL = 0; 160 | 161 | 162 | else 163 | if flag == 1 || norm(X_manpg*X_manpg'- X_pamal*X_pamal','fro')^2 > 0.1 164 | fprintf('PAMAL returns different point or fail to converge \n'); 165 | 166 | flag_succ = 2; % different point 167 | fprintf('PAMAL:Iter *** Fval *** CPU **** sparsity *** iaverge_No. ** err *** inner_opt \n'); 168 | 169 | print_format = ' %i %1.5e %1.2f %1.2f %2.2f %1.3e %1.3e \n'; 170 | fprintf(1,print_format, iterp,F_PAMAL(iterp),time_PAMAL, sparsity_pamal, inner_iterate, error_XPQ,norminf(Theta)); 171 | F_pamal = 0; 172 | sparsity_pamal = 0; 173 | iter_pamal = 0; 174 | time_PAMAL = 0; 175 | else 176 | flag_succ = 1; 177 | 178 | F_pamal = F_PAMAL(iterp); 179 | %Eigspalm=eig(P'*A*P); 180 | 181 | iter_pamal = iterp; 182 | % semilogy((1:iter),F(1:iter)-min(min(F),min(F2))); 183 | % hold on; 184 | % semilogy((1:i),F2(1:i)-min(min(F),min(F2))) 185 | fprintf('PAMAL:Iter *** Fval *** CPU **** sparsity *** iaverge_No. ** err *** inner_opt \n'); 186 | 187 | print_format = ' %i %1.5e %1.2f %1.2f %2.2f %1.3e %1.3e \n'; 188 | fprintf(1,print_format, iterp,F_PAMAL(iterp),time_PAMAL, sparsity_pamal, inner_iterate, error_XPQ,norminf(Theta)); 189 | end 190 | end -------------------------------------------------------------------------------- /manpg_code_share/SPCA/PAMAL_spca.m: -------------------------------------------------------------------------------- 1 | function [X_pamal, F_pamal,sparsity_pamal,time_PAMAL, error_XPQ,iter_pamal,flag_succ] =PAMAL_spca(B,option) 2 | 3 | %min -Tr(X'*A*X)+ mu*norm(X,1) s.t. X'*X=Ir. 4 | % A = B'*B type = 0 or A = B type = 1 5 | tic; 6 | r = option.r; 7 | n = option.n; 8 | mu = option.mu; 9 | maxiter =option.maxiter; 10 | tol = option.tol; 11 | type = option.type; 12 | mu2=1/mu; 13 | if type == 0 % data matrix 14 | A = -B'*B; 15 | else 16 | A = -B; 17 | end 18 | 19 | % r = 2*(svds(B,1))^2+ 0.5*d;% ADMM stepsize 20 | norminf=@(z) max(max(abs(z))); 21 | lam_min=-1e2; lam_max=1e2; c1=0.5; c2=c1; c3=c1; 22 | %rng(40); 23 | %beta = 2*svds(B,1)^2 + r/10+2; %stepsize 24 | % beta = 2*svds(B,1)^2 + n/25 ; % good for mu and r 25 | 26 | beta0 = 2*svds(B,1)^2 ; 27 | beta = 2.5*beta0 ; 28 | %[U,~]=eigs(A); [~,r]=size(U); X0= U(:,(r-n+1:r)); 29 | X0 = option.phi_init; 30 | X_old=X0; P_old=X0; Q_old=X0; 31 | lambda1=zeros(n,r); 32 | lambda2=lambda1; 33 | lambda1_old=lambda1; lambda2_old=lambda2; 34 | R1_old=0; R2_old=0; 35 | F_PAMAL = zeros(maxiter,1); 36 | Z1 = 2*A + c1*eye(n); 37 | %dx = option.L/d; 38 | %FFTZ1 = - 2*(cos(2*pi*[1:d]'/d)-1)/dx^2 + c1; 39 | 40 | flag = 0; 41 | r_old = beta; r0=beta; 42 | Z = Z1 + 2*beta*eye(n); %chZ= chol(Z); 43 | [UZ,Sigma] = svd(Z); 44 | UZ = real(UZ); 45 | Sigma = diag(real(Sigma)); 46 | flag_time_out = 0; 47 | not_full_rank = 0; 48 | pamal_time = 0; 49 | pamal_time = pamal_time + toc; 50 | 51 | for iterp = 1:maxiter 52 | tic; 53 | num(iterp)=1; 54 | % Z= Z1 + 2*r*eye(d) ; 55 | Sig_i = Sigma + 2*(beta-r0); 56 | 57 | % X=Z\(lambda1_old+lambda2_old+r*Q_old+r*P_old+c1*X_old); 58 | % X = chZ\(chZ'\(lambda1_old+lambda2_old+r*Q_old+r*P_old+c1*X_old)); 59 | % X = invZ*(lambda1_old+lambda2_old+r*Q_old+r*P_old+c1*X_old); 60 | % X = UZ*( diag(Sig_i)\(UZ'*(lambda1_old+lambda2_old+beta*Q_old+beta*P_old+c1*X_old))); 61 | X = UZ*bsxfun(@rdivide,UZ'*( lambda1_old+lambda2_old+ beta*Q_old+ beta*P_old+c1*X_old ),Sig_i); 62 | 63 | Q_m= (beta*X-lambda1_old+c2*Q_old)/(beta+c2); 64 | eta= 1/(mu2*(beta+c2)); 65 | Q=sign(Q_m).* max(0, abs(Q_m)-eta); 66 | P_m = (beta*X+c3*P_old-lambda2_old)/(beta+c3); 67 | 68 | [U,~,V]=svd(P_m,0); 69 | P = U*V'; 70 | %%%%%%%%%%%%% svd P_m'*P_m 71 | % [U, D, S] = svd(P_m'*P_m); 72 | % D = diag(D); 73 | % if abs(prod(D))>0 74 | % P = P_m*U*diag(sqrt(1./D))*S'; 75 | % else 76 | % not_full_rank = not_full_rank +1; 77 | % end 78 | 79 | Theta1 = beta*(Q_old-Q+P_old-P)+c1*(X_old-X); 80 | Theta2 = c2*(Q_old-Q); 81 | Theta3 = c3*(P_old-P); 82 | Theta=[Theta1,Theta2,Theta3]; 83 | X_old=X; P_old=P; Q_old=Q; 84 | 85 | while norminf(Theta)> max(1e-6,(0.996)^iterp) %PAM to solve inner problem 86 | if num(iterp) > 100 87 | break; 88 | end 89 | %X=Z\(lambda1_old+lambda2_old+r*Q_old+r*P_old+c1*X_old); 90 | % X = chZ\(chZ'\(lambda1_old+lambda2_old+r*Q_old+r*P_old+c1*X_old)); 91 | % X1 = invZ*(lambda1_old+lambda2_old+r*Q_old+r*P_old+c1*X_old); 92 | % X = UZ*( diag(Sig_i)\(UZ'*(lambda1_old+lambda2_old+beta*Q_old+beta*P_old+c1*X_old))); 93 | X = UZ*bsxfun(@rdivide,UZ'*( lambda1_old+lambda2_old+ beta*Q_old+ beta*P_old+c1*X_old ),Sig_i); 94 | % X = real(ifft(bsxfun(@rdivide,fft( lambda1_old+lambda2_old+r*Q_old+r*P_old+c1*X_old ),FFTZ))); 95 | Q_m= (beta*X-lambda1_old+c2*Q_old)/(beta+c2); 96 | eta= 1/(mu2*(beta+c2)); 97 | Q = sign(Q_m).* max(0, abs(Q_m)-eta); 98 | P_m = (beta*X+c3*P_old-lambda2_old)/(beta+c3); 99 | 100 | [U,~,V]= svd(P_m,0); 101 | P =U*V'; 102 | % [U, D, S] = svd(P_m'*P_m); 103 | % D = diag(D); 104 | % if abs(prod(D))>0 105 | % P = P_m*(U*diag(sqrt(1./D))*S'); 106 | % end 107 | Theta1 = beta*(Q_old-Q+P_old-P)+c1*(X_old-X); 108 | Theta2 = c2*(Q_old-Q); 109 | Theta3 = c3*(P_old-P); 110 | Theta = [Theta1,Theta2,Theta3]; 111 | 112 | X_old=X; P_old=P; Q_old=Q; 113 | num(iterp)=1+num(iterp); 114 | 115 | end 116 | lambda1=lambda1_old +beta*(Q-X); lambda2=lambda2_old+beta*(P-X); 117 | lambda1_old= max(lam_min,lambda1); lambda1_old= min(lam_max,lambda1_old); 118 | lambda2_old= max(lam_min,lambda2); lambda2_old= min(lam_max,lambda2_old); 119 | R1 = Q-X; R2 = P-X; 120 | % r_old =beta; 121 | if norminf(R1)> norminf(R1_old)*0.999 || norminf(R2) > norminf(R2_old)*0.999 122 | % if norminf(R1)>=norminf(R1_old) || norminf(R2)>=norminf(R2_old) 123 | beta=beta*1.001; 124 | %beta = beta*2; 125 | end 126 | %AP=-B'*(B*P); 127 | 128 | %AP = A*P; 129 | R1_old=R1; R2_old=R2; 130 | 131 | if iterp>2 132 | % if abs(F_PALM(iterp)-F_PALM(iterp-1))/(abs(F_PALM(iterp))+1) r*60 % not over r minutes 164 | flag_time_out = 1; 165 | break; 166 | end 167 | end 168 | P((abs(P)<=1e-5))=0; 169 | X_pamal =P; 170 | time_PAMAL = pamal_time; 171 | inner_iterate = sum(num)/iterp; 172 | error_XPQ = norm(X-P,'fro') + norm(X-Q,'fro'); 173 | sparsity_pamal= sum(sum(P==0))/(n*r); 174 | X_manpg = option.X_manpg ; 175 | if iterp == maxiter || flag_time_out == 1 176 | fprintf('PAMAL fails to converge in %d iterations \n', maxiter); 177 | %fprintf('PAMAL:Iter *** Fval *** CPU **** sparsity *** iaverge_No. ** err *** inner_opt \n'); 178 | 179 | %print_format = ' %i %1.5e %1.2f %1.2f %2.2f %1.3e %1.3e \n'; 180 | %fprintf(1,print_format, iterp,F_PAMAL(iterp),time_PAMAL, sparsity_pamal, inner_iterate, error_XPQ,norminf(Theta)); 181 | flag_succ = 0; 182 | F_pamal = 0; 183 | sparsity_pamal = 0; 184 | iter_pamal = 0; 185 | time_PAMAL = 0; 186 | 187 | 188 | else 189 | if flag == 1 || norm(X_manpg*X_manpg'- X_pamal*X_pamal','fro')^2 > 0.1 190 | fprintf('PAMAL returns different point or fail to converge \n'); 191 | 192 | flag_succ = 2; % different point 193 | fprintf('PAMAL:Iter *** Fval *** CPU **** sparsity *** iaverge_No. ** err *** inner_opt \n'); 194 | 195 | print_format = ' %i %1.5e %1.2f %1.2f %2.2f %1.3e %1.3e \n'; 196 | fprintf(1,print_format, iterp,F_PAMAL(iterp),time_PAMAL, sparsity_pamal, inner_iterate, error_XPQ,norminf(Theta)); 197 | F_pamal = 0; 198 | sparsity_pamal = 0; 199 | iter_pamal = 0; 200 | time_PAMAL = 0; 201 | else 202 | flag_succ = 1; 203 | 204 | F_pamal = F_PAMAL(iterp); 205 | %Eigspalm=eig(P'*A*P); 206 | 207 | iter_pamal = iterp; 208 | % semilogy((1:iter),F(1:iter)-min(min(F),min(F2))); 209 | % hold on; 210 | % semilogy((1:i),F2(1:i)-min(min(F),min(F2))) 211 | fprintf('PAMAL:Iter *** Fval *** CPU **** sparsity *** iaverge_No. ** err *** inner_opt \n'); 212 | 213 | print_format = ' %i %1.5e %1.2f %1.2f %2.2f %1.3e %1.3e \n'; 214 | fprintf(1,print_format, iterp,F_PAMAL(iterp),time_PAMAL, sparsity_pamal, inner_iterate, error_XPQ,norminf(Theta)); 215 | end 216 | end -------------------------------------------------------------------------------- /manpg_code_share/compressed modes/demo.m: -------------------------------------------------------------------------------- 1 | %function compare_spca 2 | 3 | clear all; 4 | close all; 5 | addpath ../misc 6 | addpath ../SSN_subproblem 7 | %n_set=[100; 500; 1000; 1500; 2000]; %dimension 8 | n_set = 128; 9 | r_set = 15; % rank 10 | mu_set = 1/30; 11 | index = 1; 12 | for id_n = 1:size(n_set,2) % n dimension 13 | n = n_set(id_n); 14 | fid =1; 15 | for id_r = 1%size(r_set,1) % r number of column 16 | 17 | for id_mu = 1%mu 18 | r = r_set(id_r); 19 | %mu = mu_set(id_mu); 20 | fprintf(fid,'===================================\n'); 21 | succ_no_manpg = 0; succ_no_manpg_BB = 0; succ_no_SOC = 0; succ_no_PAMAL = 0; succ_no_sub = 0; 22 | diff_no_SOC = 0; diff_no_PAMAL = 0; diff_no_sub = 0; 23 | fail_no_SOC = 0; fail_no_PAMAL = 0; fail_no_sub = 0; 24 | 25 | for test_random =1:1%times average. 26 | L = 50; dx = L/n; V = 0; 27 | % phi_init = CMs_1D_initial(d,n,dx); % initial point ---Guass kernel 28 | H = -Sch_matrix(0,L,n); % schrodinger operator 29 | %H = H/abs(eigs(H,1)); 30 | mu = mu_set(id_mu); 31 | fprintf(fid,'- n -- r -- mu --------\n'); 32 | fprintf(fid,'%4d %3d %3.3f \n',n,r,mu); 33 | rng('shuffle'); 34 | %rng(10); 35 | [phi_init,~] = svd(randn(n,r),0); % random intialization 36 | %[phi_init,~] = eigs(H,r); % singular value initialization 37 | option_Rsub.F_manpg = -1e10; 38 | option_Rsub.phi_init = phi_init; option_Rsub.maxiter = n*r; option_Rsub.tol = 5e-3; 39 | option_Rsub.r = r; option_Rsub.n= n; option_Rsub.mu=mu; option_Rsub.type = 1; 40 | 41 | [phi_init, F_Rsub(test_random),sparsity_Rsub(test_random),time_Rsub(test_random),... 42 | maxit_att_Rsub(test_random),succ_flag_sub]= Re_sub_grad_spca(H,option_Rsub); 43 | 44 | 45 | %%%%% manpg parameter 46 | %option_manpg.adap = 0; 47 | option_manpg.phi_init = phi_init; option_manpg.maxiter = 30000; option_manpg.tol =1e-8*n*r; 48 | option_manpg.r = r; option_manpg.n = n; option_manpg.mu = mu; 49 | option_manpg.L = L; 50 | %option_manpg.inner_tol =1e-11; 51 | option_manpg.inner_iter = 100; 52 | %%%%%% soc parameter 53 | option_soc.phi_init = phi_init; option_soc.maxiter = 30000; option_soc.tol =1e-4; 54 | option_soc.r = r; option_soc.n = n; option_soc.mu=mu; 55 | option_soc.L= L; 56 | %%%%%% PAMAL parameter 57 | option_PAMAL.phi_init = phi_init; option_PAMAL.maxiter = 30000; option_PAMAL.tol =1e-4; 58 | option_PAMAL.L = L; option_PAMAL.V = V; 59 | option_PAMAL.r = r; option_PAMAL.n = n; option_PAMAL.mu=mu; 60 | % B = randn(d,d)+eye(d,d); B = -B'*B; 61 | [X_manpg, F_manpg(test_random),sparsity_manpg(test_random),time_manpg(test_random),... 62 | maxit_att_manpg(test_random),succ_flag_manpg,lins(test_random),in_av(test_random)]= manpg_CMS(H,option_manpg,dx,V); 63 | if succ_flag_manpg == 1 64 | succ_no_manpg = succ_no_manpg + 1; 65 | end 66 | 67 | option_manpg.F_manpg = F_manpg(test_random); 68 | [X_manpg_adap, F_manpg_BB(test_random),sparsity_manpg_BB(test_random),time_manpg_BB(test_random),... 69 | maxit_att_manpg_BB(test_random),succ_flag_manpg_BB,lins_adap(test_random),in_av_adap(test_random)]= manpg_CMS_adap(H,option_manpg,dx,V); 70 | if succ_flag_manpg_BB == 1 71 | succ_no_manpg_BB = succ_no_manpg_BB + 1; 72 | end 73 | 74 | %%%%%% Riemannian subgradient parameter 75 | % option_Rsub.F_manpg = F_manpg(test_random); 76 | % option_Rsub.phi_init = phi_init; option_Rsub.maxiter = 1e1; option_Rsub.tol = 1e-3; 77 | % option_Rsub.r = r; option_Rsub.n= n; option_Rsub.mu=mu; option_Rsub.type = 1; 78 | % 79 | % [X_Rsub, F_Rsub(test_random),sparsity_Rsub(test_random),time_Rsub(test_random),... 80 | % maxit_att_Rsub(test_random),succ_flag_sub]= Re_sub_grad_spca(H,option_Rsub); 81 | % phi_init = X_Rsub; 82 | % if succ_flag_sub == 1 83 | % succ_no_sub = succ_no_sub + 1; 84 | % end 85 | 86 | 87 | 88 | option_soc.F_manpg = F_manpg(test_random); 89 | option_soc.X_manpg = X_manpg; 90 | option_PAMAL.F_manpg = F_manpg(test_random); 91 | option_PAMAL.X_manpg = X_manpg; 92 | 93 | [X_Soc, F_soc(test_random),sparsity_soc(test_random),time_soc(test_random),... 94 | soc_error_XPQ(test_random),maxit_att_soc(test_random),succ_flag_SOC]= soc_CM(H,option_soc); 95 | if succ_flag_SOC == 1 96 | succ_no_SOC = succ_no_SOC + 1; 97 | end 98 | 99 | 100 | [X_pamal, F_pamal(test_random),sparsity_pamal(test_random),time_pamal(test_random),... 101 | pam_error_XPQ(test_random), maxit_att_pamal(test_random),succ_flag_PAMAL]= PAMAL_CMs(H,option_PAMAL,V); 102 | if succ_flag_PAMAL ==1 103 | succ_no_PAMAL = succ_no_PAMAL + 1; 104 | end 105 | 106 | if succ_flag_sub == 0 107 | fail_no_sub = fail_no_sub + 1; 108 | end 109 | if succ_flag_sub == 2 110 | diff_no_sub = diff_no_sub + 1; 111 | end 112 | if succ_flag_SOC == 0 113 | fail_no_SOC = fail_no_SOC + 1; 114 | end 115 | if succ_flag_SOC == 2 116 | diff_no_SOC = diff_no_SOC + 1; 117 | end 118 | if succ_flag_PAMAL == 0 119 | fail_no_PAMAL = fail_no_PAMAL + 1; 120 | end 121 | if succ_flag_PAMAL == 2 122 | diff_no_PAMAL = diff_no_PAMAL + 1; 123 | end 124 | 125 | 126 | end 127 | 128 | end 129 | 130 | end 131 | 132 | 133 | 134 | Result(index,1) = sum(lins)/succ_no_manpg; Result(index,2) = sum(in_av)/succ_no_manpg; 135 | Result(index,3) = sum(lins_adap)/succ_no_manpg; Result(index,4) = sum(in_av_adap)/succ_no_manpg; 136 | 137 | Result(index,5) = succ_no_manpg_BB; Result(index,6) = succ_no_SOC; Result(index,7) = fail_no_SOC; Result(index,8) = diff_no_SOC; 138 | Result(index,9)= succ_no_PAMAL; Result(index,10)=fail_no_PAMAL; Result(index,11)= diff_no_PAMAL; 139 | index = index +1; 140 | 141 | iter.manpg(id_n) = sum(maxit_att_manpg)/succ_no_manpg; 142 | iter.manpg_BB(id_n) = sum(maxit_att_manpg_BB)/succ_no_manpg_BB; 143 | iter.soc(id_n) = sum(maxit_att_soc)/succ_no_SOC; 144 | iter.pamal(id_n) = sum(maxit_att_pamal)/succ_no_PAMAL; 145 | iter.Rsub(id_n) = sum(maxit_att_Rsub)/succ_no_sub; 146 | 147 | time.manpg(id_n) = sum(time_manpg)/succ_no_manpg; 148 | time.manpg_BB(id_n) = sum(time_manpg_BB)/succ_no_manpg_BB; 149 | time.soc(id_n) = sum(time_soc)/succ_no_SOC; 150 | time.pamal(id_n) = sum(time_pamal)/succ_no_PAMAL; 151 | time.Rsub(id_n) = sum(time_Rsub)/succ_no_sub; 152 | 153 | Fval.manpg(id_n) = sum(F_manpg)/succ_no_manpg; 154 | Fval.manpg_BB(id_n) = sum(F_manpg_BB)/succ_no_manpg_BB; 155 | Fval.soc(id_n) = sum(F_soc)/succ_no_SOC; 156 | Fval.pamal(id_n) = sum(F_pamal)/succ_no_PAMAL; 157 | Fval.Rsub(id_n) = sum(F_Rsub)/succ_no_sub; 158 | 159 | 160 | Sp.manpg(id_n) = sum(sparsity_manpg)/succ_no_manpg; 161 | Sp.manpg_BB(id_n) = sum(sparsity_manpg_BB)/succ_no_manpg_BB; 162 | Sp.soc(id_n) = sum(sparsity_soc)/succ_no_SOC; 163 | Sp.pamal(id_n) = sum(sparsity_pamal)/succ_no_PAMAL; 164 | Sp.Rsub(id_n) = sum(sparsity_Rsub)/succ_no_sub; 165 | %fprintf(fid,' Alg **** Iter ***** Fval *** sparsity ** cpu *** Error ***\n'); 166 | 167 | %print_format = 'ManPG: %1.3e %1.5e %1.2f %3.2f \n'; 168 | %fprintf(fid,print_format, iter.manpg(id_n), Fval.manpg(id_n), Sp.manpg(id_n),time.manpg(id_n)); 169 | %print_format = 'ManPG_adap: %1.3e %1.5e %1.2f %3.2f \n'; 170 | %fprintf(fid,print_format, iter.manpg_BB(id_n), Fval.manpg_BB(id_n), Sp.manpg_BB(id_n),time.manpg_BB(id_n)); 171 | %print_format = 'SOC: %1.3e %1.5e %1.2f %3.2f %1.3e\n'; 172 | %fprintf(fid,print_format,iter.soc(id_n) , Fval.soc(id_n), Sp.soc(id_n) ,time.soc(id_n),mean(soc_error_XPQ)); 173 | %print_format = 'PAMAL: %1.3e %1.5e %1.2f %3.2f %1.3e \n'; 174 | %fprintf(fid,print_format,iter.pamal(id_n) , Fval.pamal(id_n) ,Sp.pamal(id_n),time.pamal(id_n),mean(pam_error_XPQ)); 175 | %print_format = 'Rsub: %1.3e %1.5e %1.2f %3.2f \n'; 176 | %fprintf(fid,print_format,iter.Rsub(id_n) , Fval.Rsub(id_n) ,Sp.Rsub(id_n),time.Rsub(id_n)); 177 | %% plot 178 | fprintf(fid,' ---------------------------------------------------------\n'); 179 | H = - H; 180 | [VV,D] = eig(H+0); 181 | D = diag(D); 182 | [D I]= sort(D,'ascend'); 183 | VV = VV(:,I); 184 | Density_True = sum(VV(:,1:r).*VV(:,1:r),2); 185 | fprintf('True Totoal Energy %2.5f \n',trace(VV(:,1:r)'*H*VV(:,1:r))); 186 | fprintf('Total Energy from CMs %2.5f \n',trace(X_manpg_adap'*H*X_manpg_adap)); 187 | %flip the negative columns of CMs to be positive 188 | X_manpg = positive_CMs(X_manpg,r); 189 | X_manpg_adap = positive_CMs(X_manpg_adap,r); 190 | X_Soc = positive_CMs(X_Soc,r); 191 | X_pamal = positive_CMs(X_pamal,r); 192 | 193 | % CMs support - manpg-adap 194 | colormatrix = [0 0 0;0 0 1;0 1 0;0 1 1;1 0 0;1 0 1;1 .1 0.5;.1 .1 .6;1,0.5 0;0.5 0.5 0.5]; 195 | figure(1); 196 | set(gcf,'Position',[324 455 905 322]); 197 | set(gca, 'colororder',colormatrix, 'nextplot', 'replacechildren'); 198 | x = [0:dx:L-dx]'; 199 | plot(x,X_manpg_adap/sqrt(dx),'LineWidth',2); 200 | hlen = legend('\psi_1','\psi_2','\psi_3','\psi_4','\psi_5','Location','Bestoutside'); 201 | set(hlen,'FontSize',20); 202 | title([' \mu = ' num2str(mu) ', CMs \phi'],'FontSize',25); 203 | set(gca,'FontSize',20); 204 | set(gcf, 'PaperPositionMode', 'auto'); 205 | 206 | 207 | 208 | % CMs support - SOC 209 | colormatrix = [0 0 0;0 0 1;0 1 0;0 1 1;1 0 0;1 0 1;1 .1 0.5;.1 .1 .6;1,0.5 0;0.5 0.5 0.5]; 210 | figure(2); 211 | set(gcf,'Position',[324 455 905 322]); 212 | set(gca, 'colororder',colormatrix, 'nextplot', 'replacechildren'); 213 | x = [0:dx:L-dx]'; 214 | plot(x,X_Soc/sqrt(dx),'LineWidth',2); 215 | hlen = legend('\psi_1','\psi_2','\psi_3','\psi_4','\psi_5','Location','Bestoutside'); 216 | set(hlen,'FontSize',20); 217 | title([' \mu = ' num2str(mu) ', CMs \phi'],'FontSize',25); 218 | set(gca,'FontSize',20); 219 | set(gcf, 'PaperPositionMode', 'auto'); 220 | 221 | 222 | 223 | % CMs support - -PAMAL 224 | colormatrix = [0 0 0;0 0 1;0 1 0;0 1 1;1 0 0;1 0 1;1 .1 0.5;.1 .1 .6;1,0.5 0;0.5 0.5 0.5]; 225 | figure(3); 226 | set(gcf,'Position',[324 455 905 322]); 227 | set(gca, 'colororder',colormatrix, 'nextplot', 'replacechildren'); 228 | x = [0:dx:L-dx]'; 229 | plot(x,X_pamal/sqrt(dx),'LineWidth',2); 230 | hlen = legend('\psi_1','\psi_2','\psi_3','\psi_4','\psi_5','Location','Bestoutside'); 231 | set(hlen,'FontSize',20); 232 | title([' \mu = ' num2str(mu) ', CMs \phi'],'FontSize',25); 233 | set(gca,'FontSize',20); 234 | set(gcf, 'PaperPositionMode', 'auto'); 235 | 236 | 237 | % eigens of phi'*H*phi- manpg-adap 238 | 239 | [U1 D1] = eig(X_manpg_adap'*H*X_manpg_adap); 240 | D1 = diag(D1); 241 | [D1 ind1] = sort(D1,'ascend'); 242 | U1 = U1(:,ind1); 243 | [U2 D2] = eig(X_Soc'*H*X_Soc); 244 | D2 = diag(D2); 245 | [D2 ind2] = sort(D2,'ascend'); 246 | [U3 D3] = eig(X_pamal'*H*X_pamal); 247 | D3 = diag(D3); 248 | [D3 ind3] = sort(D3,'ascend'); 249 | figure(4); 250 | % set(gcf,'Position',[324 455 905 322]); 251 | plot(D(1:r),'g*','LineWidth',1); hold on; 252 | plot(D1(1:r),'ko','LineWidth',1,'MarkerSize',10); 253 | hold on; 254 | plot(D2(1:r),'bd','LineWidth',1) 255 | hold on; 256 | plot(D3(1:r),'c+','LineWidth',1) 257 | 258 | set(gcf, 'PaperPositionMode', 'auto'); 259 | hlen = legend('H','ManPG-Ada','SOC','PAMAL','Location','Best'); 260 | set(hlen,'FontSize',20); 261 | set(gca,'FontSize',15); 262 | 263 | 264 | end 265 | 266 | 267 | 268 | -------------------------------------------------------------------------------- /manpg_code_share/compressed modes/demo_compare_CMS_dim_n.m: -------------------------------------------------------------------------------- 1 | %function compare_spca 2 | clc 3 | clear; 4 | close all; 5 | addpath ../misc 6 | addpath ../SSN_subproblem 7 | %n_set=[100; 500; 1000; 1500; 2000]; %dimension 8 | n_set = 2.^(6:7); %dimension 9 | r_set = [1;2;4;]; % rank 10 | 11 | mu_set = (3:5)*0.1; 12 | index = 1; 13 | for id_n = 1:size(n_set,2) % n dimension 14 | n = n_set(id_n); 15 | fid =1; 16 | 17 | for id_r = 3 %size(r_set,1) % r number of column 18 | for id_mu = 1 %mu sparsity parameter 19 | r = r_set(id_r); 20 | %mu = mu_set(id_mu); 21 | succ_no_manpg = 0; succ_no_manpg_BB = 0; succ_no_SOC = 0; succ_no_PAMAL = 0; succ_no_sub = 0; 22 | diff_no_SOC = 0; diff_no_PAMAL = 0; diff_no_sub = 0; 23 | fail_no_SOC = 0; fail_no_PAMAL = 0; fail_no_sub = 0; 24 | for test_random =1:5 %times average. 25 | fprintf(fid,'==============================================================================================\n'); 26 | 27 | 28 | L = 50; dx = L/n; V = 0; 29 | % phi_init = CMs_1D_initial(d,n,dx); % initial point ---Guass kernel 30 | H = -Sch_matrix(0,L,n); % schrodinger operator 31 | %H = H/abs(eigs(H,1)); 32 | mu = mu_set(id_mu); 33 | fprintf(fid,'- n -- r -- mu --------\n'); 34 | fprintf(fid,'%4d %3d %3.3f \n',n,r,mu); 35 | fprintf(fid,'----------------------------------------------------------------------------------\n'); 36 | 37 | rng('shuffle'); 38 | %rng(174); 39 | [phi_init,~] = svd(randn(n,r),0); % random intialization 40 | %[phi_init,~] = eigs(H,r); % singular value initialization 41 | %% Remannian subgradient method 42 | option_Rsub.F_manpg = -1e10; 43 | option_Rsub.phi_init = phi_init; option_Rsub.maxiter = n*r; option_Rsub.tol = 5e-3; 44 | option_Rsub.r = r; option_Rsub.n= n; option_Rsub.mu=mu; option_Rsub.type = 1; 45 | 46 | [phi_init, F_Rsub(test_random),sparsity_Rsub(test_random),time_Rsub(test_random),... 47 | maxit_att_Rsub(test_random),succ_flag_sub]= Re_sub_grad_spca(H,option_Rsub); 48 | 49 | 50 | %% manpg 51 | %option_manpg.adap = 0; 52 | option_manpg.phi_init = phi_init; option_manpg.maxiter = 30000; option_manpg.tol =1e-8*n*r; 53 | option_manpg.r = r; option_manpg.n = n; option_manpg.mu = mu; 54 | option_manpg.L = L; 55 | %option_manpg.inner_tol =1e-11; 56 | option_manpg.inner_iter = 100; 57 | %% soc parameter 58 | option_soc.phi_init = phi_init; option_soc.maxiter = 30000; option_soc.tol =1e-4; 59 | option_soc.r = r; option_soc.n = n; option_soc.mu=mu; 60 | option_soc.L= L; 61 | %% PAMAL parameter 62 | option_PAMAL.phi_init = phi_init; option_PAMAL.maxiter = 30000; option_PAMAL.tol =1e-4; 63 | option_PAMAL.L = L; option_PAMAL.V = V; 64 | option_PAMAL.r = r; option_PAMAL.n = n; option_PAMAL.mu=mu; 65 | % B = randn(d,d)+eye(d,d); B = -B'*B; 66 | [X_manpg, F_manpg(test_random),sparsity_manpg(test_random),time_manpg(test_random),... 67 | maxit_att_manpg(test_random),succ_flag_manpg,lins(test_random),in_av(test_random)]= manpg_CMS(H,option_manpg,dx,V); 68 | if succ_flag_manpg == 1 69 | succ_no_manpg = succ_no_manpg + 1; 70 | end 71 | 72 | option_manpg.F_manpg = F_manpg(test_random); 73 | [X_manpg_BB, F_manpg_BB(test_random),sparsity_manpg_BB(test_random),time_manpg_BB(test_random),... 74 | maxit_att_manpg_BB(test_random),succ_flag_manpg_BB,lins_adap(test_random),in_av_adap(test_random)]= manpg_CMS_adap(H,option_manpg,dx,V); 75 | if succ_flag_manpg_BB == 1 76 | succ_no_manpg_BB = succ_no_manpg_BB + 1; 77 | end 78 | 79 | %% Riemannian subgradient parameter 80 | option_Rsub.F_manpg = F_manpg(test_random); 81 | option_Rsub.phi_init = phi_init; option_Rsub.maxiter = 1e1; option_Rsub.tol = 1e-3; 82 | option_Rsub.r = r; option_Rsub.n= n; option_Rsub.mu=mu; option_Rsub.type = 1; 83 | 84 | [X_Rsub, F_Rsub(test_random),sparsity_Rsub(test_random),time_Rsub(test_random),... 85 | maxit_att_Rsub(test_random),succ_flag_sub]= Re_sub_grad_spca(H,option_Rsub); 86 | %phi_init = X_Rsub; 87 | if succ_flag_sub == 1 88 | succ_no_sub = succ_no_sub + 1; 89 | end 90 | 91 | 92 | option_soc.F_manpg = F_manpg(test_random); 93 | option_soc.X_manpg = X_manpg; 94 | option_PAMAL.F_manpg = F_manpg(test_random); 95 | option_PAMAL.X_manpg = X_manpg; 96 | 97 | [X_Soc, F_soc(test_random),sparsity_soc(test_random),time_soc(test_random),... 98 | soc_error_XPQ(test_random),maxit_att_soc(test_random),succ_flag_SOC]= soc_CM(H,option_soc); 99 | if succ_flag_SOC == 1 100 | succ_no_SOC = succ_no_SOC + 1; 101 | end 102 | 103 | 104 | [X_pamal, F_pamal(test_random),sparsity_pamal(test_random),time_pamal(test_random),... 105 | pam_error_XPQ(test_random), maxit_att_pamal(test_random),succ_flag_PAMAL]= PAMAL_CMs(H,option_PAMAL,V); 106 | if succ_flag_PAMAL ==1 107 | succ_no_PAMAL = succ_no_PAMAL + 1; 108 | end 109 | 110 | if succ_flag_sub == 0 111 | fail_no_sub = fail_no_sub + 1; 112 | end 113 | if succ_flag_sub == 2 114 | diff_no_sub = diff_no_sub + 1; 115 | end 116 | if succ_flag_SOC == 0 117 | fail_no_SOC = fail_no_SOC + 1; 118 | end 119 | if succ_flag_SOC == 2 120 | diff_no_SOC = diff_no_SOC + 1; 121 | end 122 | if succ_flag_PAMAL == 0 123 | fail_no_PAMAL = fail_no_PAMAL + 1; 124 | end 125 | if succ_flag_PAMAL == 2 126 | diff_no_PAMAL = diff_no_PAMAL + 1; 127 | end 128 | if succ_flag_manpg == 1 129 | F_best(test_random) = F_manpg(test_random); 130 | end 131 | if succ_flag_manpg_BB == 1 132 | F_best(test_random) = min( F_best(test_random), F_manpg_BB(test_random)); 133 | end 134 | if succ_flag_SOC == 1 135 | F_best(test_random) = min( F_best(test_random), F_soc(test_random)); 136 | end 137 | if succ_flag_PAMAL == 1 138 | F_best(test_random) = min( F_best(test_random), F_pamal(test_random)); 139 | end 140 | 141 | end 142 | 143 | end 144 | 145 | end 146 | 147 | 148 | 149 | Result(index,1) = sum(lins)/succ_no_manpg; Result(index,2) = sum(in_av)/succ_no_manpg; 150 | Result(index,3) = sum(lins_adap)/succ_no_manpg; Result(index,4) = sum(in_av_adap)/succ_no_manpg; 151 | 152 | Result(index,5) = succ_no_manpg_BB; Result(index,6) = succ_no_SOC; Result(index,7) = fail_no_SOC; Result(index,8) = diff_no_SOC; 153 | Result(index,9)= succ_no_PAMAL; Result(index,10)=fail_no_PAMAL; Result(index,11)= diff_no_PAMAL; 154 | index = index +1; 155 | 156 | iter.manpg(id_n) = sum(maxit_att_manpg)/succ_no_manpg; 157 | iter.manpg_BB(id_n) = sum(maxit_att_manpg_BB)/succ_no_manpg_BB; 158 | iter.soc(id_n) = sum(maxit_att_soc)/succ_no_SOC; 159 | iter.pamal(id_n) = sum(maxit_att_pamal)/succ_no_PAMAL; 160 | iter.Rsub(id_n) = sum(maxit_att_Rsub)/succ_no_sub; 161 | 162 | time.manpg(id_n) = sum(time_manpg)/succ_no_manpg; 163 | time.manpg_BB(id_n) = sum(time_manpg_BB)/succ_no_manpg_BB; 164 | time.soc(id_n) = sum(time_soc)/succ_no_SOC; 165 | time.pamal(id_n) = sum(time_pamal)/succ_no_PAMAL; 166 | time.Rsub(id_n) = sum(time_Rsub)/succ_no_sub; 167 | 168 | Fval.manpg(id_n) = sum(F_manpg)/succ_no_manpg; 169 | Fval.manpg_BB(id_n) = sum(F_manpg_BB)/succ_no_manpg_BB; 170 | Fval.soc(id_n) = sum(F_soc)/succ_no_SOC; 171 | Fval.pamal(id_n) = sum(F_pamal)/succ_no_PAMAL; 172 | Fval.Rsub(id_n) = sum(F_Rsub)/succ_no_sub; 173 | Fval.best(id_n) = sum(F_best)/succ_no_manpg; 174 | 175 | Sp.manpg(id_n) = sum(sparsity_manpg)/succ_no_manpg; 176 | Sp.manpg_BB(id_n) = sum(sparsity_manpg_BB)/succ_no_manpg_BB; 177 | Sp.soc(id_n) = sum(sparsity_soc)/succ_no_SOC; 178 | Sp.pamal(id_n) = sum(sparsity_pamal)/succ_no_PAMAL; 179 | Sp.Rsub(id_n) = sum(sparsity_Rsub)/succ_no_sub; 180 | fprintf(fid,'==============================================================================================\n'); 181 | 182 | fprintf(fid,' Alg ***** Iter ***** Fval *** sparsity ** cpu *** Error ***\n'); 183 | 184 | print_format = 'ManPG: %1.3e %1.5e %1.2f %3.2f \n'; 185 | fprintf(fid,print_format, iter.manpg(id_n), Fval.manpg(id_n), Sp.manpg(id_n),time.manpg(id_n)); 186 | print_format = 'ManPG_adap: %1.3e %1.5e %1.2f %3.2f \n'; 187 | fprintf(fid,print_format, iter.manpg_BB(id_n), Fval.manpg_BB(id_n), Sp.manpg_BB(id_n),time.manpg_BB(id_n)); 188 | print_format = 'SOC: %1.3e %1.5e %1.2f %3.2f %1.3e\n'; 189 | fprintf(fid,print_format,iter.soc(id_n) , Fval.soc(id_n), Sp.soc(id_n) ,time.soc(id_n),mean(soc_error_XPQ)); 190 | print_format = 'PAMAL: %1.3e %1.5e %1.2f %3.2f %1.3e \n'; 191 | fprintf(fid,print_format,iter.pamal(id_n) , Fval.pamal(id_n) ,Sp.pamal(id_n),time.pamal(id_n),mean(pam_error_XPQ)); 192 | print_format = 'Rsub: %1.3e %1.5e %1.2f %3.2f \n'; 193 | fprintf(fid,print_format,iter.Rsub(id_n) , Fval.Rsub(id_n) ,Sp.Rsub(id_n),time.Rsub(id_n)); 194 | end 195 | 196 | %% plot 197 | 198 | % figure(1); 199 | % plot(n_set, time.manpg, 'r-','linewidth',1); hold on; 200 | % plot(n_set, time.manpg_BB, 'k-','linewidth',1); hold on; 201 | % plot(n_set, time.soc, 'b--','linewidth',1); hold on; 202 | % plot(n_set, time.pamal, 'c-.','linewidth',2); hold on; 203 | % %plot(n_set, time.Rsub, 'g-*','linewidth',2); 204 | % xlabel('dimenion-n'); ylabel('CPU'); 205 | % title(['comparison on CPU: different dimension',',r=',num2str(r),',\mu=',num2str(mu)]); 206 | % legend('ManPG','ManPG-adap','SOC','PAMAL','Location','NorthWest'); 207 | % %legend('ManPG','ManPG-BB','SOC','PAMAL','Rsub','Location','NorthWest'); 208 | % 209 | % filename_pic1 = ['CMS_CPU_n', '_' num2str(r) '_' num2str(mu) '.eps']; 210 | % saveas(gcf,filename_pic1,'epsc') 211 | % 212 | % 213 | % figure(2) 214 | % semilogy(n_set, Fval.manpg-Fval.best+1e-16, 'r-s','MarkerSize',10,'linewidth',1); hold on; 215 | % semilogy(n_set, Fval.manpg_BB-Fval.best+1e-16, 'k-o','MarkerSize',6,'linewidth',1); hold on; 216 | % semilogy(n_set, Fval.soc-Fval.best+1e-16, 'b-d','MarkerSize',8,'linewidth',1); hold on; 217 | % semilogy(n_set, Fval.pamal-Fval.best+1e-16, 'c-.','MarkerSize',20,'linewidth',1.5); hold on; 218 | % %semilogy(n_set, Fval.Rsub -Fval.best+1e-16, 'g-*','MarkerSize',10,'linewidth',1.5); 219 | % %legend('ManPG','ManPG-BB','SOC','PAMAL','Rsub','Location','NorthWest'); 220 | % legend('ManPG','ManPG-adap','SOC','PAMAL','Location','NorthWest'); 221 | % 222 | % xlabel('dimenion-n'); ylabel('fucntion value difference'); 223 | % title(['comparison on function value difference: different dimension',',r=',num2str(r),',\mu=',num2str(mu)]); 224 | % filename_pic2 = ['CMS_Fval_n', '_' num2str(r) '_' num2str(mu) '.eps']; 225 | % saveas(gcf,filename_pic2,'epsc') 226 | % 227 | % % 228 | % % 229 | % figure(3) 230 | % plot(n_set, Sp.manpg, 'r-s','MarkerSize',10,'linewidth',1); hold on; 231 | % plot(n_set, Sp.manpg_BB, 'k-o','MarkerSize',6,'linewidth',1); hold on; 232 | % plot(n_set, Sp.soc, 'b-d','MarkerSize',8,'linewidth',1); hold on; 233 | % plot(n_set, Sp.pamal, 'c-.','MarkerSize',20,'linewidth',1.5); hold on; 234 | % %plot(n_set, Sp.Rsub, 'g-*','MarkerSize',20,'linewidth',1.5); %hold on; 235 | % xlabel('dimenion-n'); ylabel('sparsity'); 236 | % legend('ManPG','ManPG-adap','SOC','PAMAL','Location','SouthEast'); 237 | % %legend('ManPG','ManPG-BB','SOC','PAMAL','Rsub','Location','NorthWest'); 238 | % 239 | % title(['comparison on sparsity: different dimension',',r=',num2str(r),',\mu=',num2str(mu)]); 240 | % filename_pic3 = ['CMS_Sparsity_n', '_' num2str(r) '_' num2str(mu) '.eps']; 241 | % saveas(gcf,filename_pic3,'epsc') 242 | % 243 | % 244 | % figure(4) 245 | % plot(n_set, iter.manpg, 'r-s','MarkerSize',10,'linewidth',1); hold on; 246 | % plot(n_set, iter.manpg_BB, 'k-o','MarkerSize',6,'linewidth',1); hold on; 247 | % plot(n_set, iter.soc, 'b-d','MarkerSize',8,'linewidth',1); hold on; 248 | % plot(n_set, iter.pamal, 'c-.','MarkerSize',20,'linewidth',1.5); %hold on; 249 | % %plot(n_set, iter.Rsub, 'g-*','MarkerSize',20,'linewidth',1.5); %hold on; 250 | % xlabel('dimenion-n'); ylabel('iter'); 251 | % legend('ManPG','ManPG-adap','SOC','PAMAL','Location','NorthWest'); 252 | % %legend('ManPG','ManPG-BB','SOC','PAMAL','Rsub','Location','NorthWest'); 253 | % 254 | % title(['comparison on iter: different dimension',',r=',num2str(r),',\mu=',num2str(mu)]); 255 | % filename_pic4= ['CMS_iter_n', '_' num2str(r) '_' num2str(mu) '.eps']; 256 | % saveas(gcf,filename_pic4,'epsc') 257 | % 258 | % close(figure(1)); 259 | % close(figure(2)); 260 | % close(figure(3)); 261 | % close(figure(4)); 262 | % filename = ['CMS_comparison_n_' num2str(r_set(id_r)) '_' num2str(mu_set(id_mu)) '.csv']; 263 | % csvwrite( filename, Result); 264 | 265 | 266 | 267 | -------------------------------------------------------------------------------- /manpg_code_share/SPCA/demo_compare_SPCA_dimension_n.m: -------------------------------------------------------------------------------- 1 | %function compare_spca 2 | 3 | clear; 4 | close all; 5 | addpath ../misc 6 | addpath ../SSN_subproblem 7 | n_set=[100; 200; 500; 800; 1000; 1500; ]; %dimension 8 | %n_set = 2.^(6:9); 9 | r_set = [1;2;5;10;]; % rank 10 | 11 | mu_set = 0.8; 12 | index = 1; 13 | for id_n = 1:1%size(n_set,1) % n dimension 14 | 15 | n = n_set(id_n); 16 | fid =1; 17 | 18 | for id_r = 3%size(r_set,1) % r number of column 19 | for id_mu = 1 % mu sparse parameter 20 | r = r_set(id_r); 21 | %mu = mu_set(id_mu); 22 | 23 | succ_no_manpg = 0; succ_no_manpg_BB = 0; succ_no_SOC = 0; succ_no_PAMAL = 0; succ_no_sub = 0; 24 | diff_no_SOC = 0; diff_no_PAMAL = 0; diff_no_sub = 0; 25 | fail_no_SOC = 0; fail_no_PAMAL = 0; fail_no_sub = 0; 26 | for test_random = 1:2 %times average. 27 | fprintf(fid,'==============================================================================================\n'); 28 | 29 | rng('shuffle'); 30 | %rng(70); 31 | m = 50; 32 | B = randn(m,n); 33 | type = 0; % random data matrix 34 | if (type == 1) %covariance matrix 35 | scale = max(diag(B)); % Sigma=A/scale; 36 | elseif (type == 0) %data matrix 37 | B = B - repmat(mean(B,1),m,1); 38 | % scale = []; 39 | % for id = 1:n 40 | % scale = [scale norm(B(:,id))]; 41 | % end 42 | % scale = max(scale); 43 | % B = B/scale; 44 | B = normc(B); 45 | % Sigma=A'*A; 46 | end 47 | %B(abs(B)<0.1) = 0; 48 | mu = mu_set(id_mu); 49 | fprintf(fid,'- n -- r -- mu --------\n'); 50 | fprintf(fid,'%4d %3d %3.3f \n',n,r,mu); 51 | fprintf(fid,'----------------------------------------------------------------------------------\n'); 52 | 53 | rng('shuffle'); 54 | %rng(177); 55 | [phi_init,~] = svd(randn(n,r),0); % random intialization 56 | %[phi_init,~] = eigs(H,r); % singular value initialization 57 | option_Rsub.F_manpg = -1e10; 58 | option_Rsub.phi_init = phi_init; option_Rsub.maxiter = 5e2; option_Rsub.tol = 5e-3; 59 | option_Rsub.r = r; option_Rsub.n= n; option_Rsub.mu=mu; option_Rsub.type = type; 60 | 61 | [phi_init, F_Rsub(test_random),sparsity_Rsub(test_random),time_Rsub(test_random),... 62 | maxit_att_Rsub(test_random),succ_flag_sub]= Re_sub_grad_spca(B,option_Rsub); 63 | 64 | 65 | %%%%% manpg parameter 66 | option_manpg.adap = 0; option_manpg.type =type; 67 | option_manpg.phi_init = phi_init; option_manpg.maxiter = 20000; option_manpg.tol =1e-8*n*r; 68 | option_manpg.r = r; option_manpg.n = n; option_manpg.mu = mu; 69 | %option_manpg.L = L; 70 | %option_manpg.inner_tol =1e-11; 71 | option_manpg.inner_iter = 100; 72 | %%%%%% soc parameter 73 | option_soc.phi_init = phi_init; option_soc.maxiter = 20000; option_soc.tol =1e-4; 74 | option_soc.r = r; option_soc.n = n; option_soc.mu=mu; 75 | %option_soc.L= L; 76 | option_soc.type = type; 77 | %%%%%% PAMAL parameter 78 | option_PAMAL.phi_init = phi_init; option_PAMAL.maxiter =20000; option_PAMAL.tol =1e-4; 79 | %option_PAMAL.L = L; option_PAMAL.V = V; 80 | option_PAMAL.r = r; option_PAMAL.n = n; option_PAMAL.mu=mu; option_PAMAL.type = type; 81 | % B = randn(d,d)+eye(d,d); B = -B'*B; 82 | [X_manpg, F_manpg(test_random),sparsity_manpg(test_random),time_manpg(test_random),... 83 | maxit_att_manpg(test_random),succ_flag_manpg, lins(test_random),in_av(test_random)]= manpg_orth_sparse(B,option_manpg); 84 | if succ_flag_manpg == 1 85 | succ_no_manpg = succ_no_manpg + 1; 86 | end 87 | 88 | option_manpg.F_manpg = F_manpg(test_random); 89 | [X_manpg_BB, F_manpg_BB(test_random),sparsity_manpg_BB(test_random),time_manpg_BB(test_random),... 90 | maxit_att_manpg_BB(test_random),succ_flag_manpg_BB,lins_adap(test_random),in_av_adap(test_random)]= manpg_orth_sparse_adap(B,option_manpg); 91 | if succ_flag_manpg_BB == 1 92 | succ_no_manpg_BB = succ_no_manpg_BB + 1; 93 | end 94 | 95 | %%%%%% Riemannian subgradient parameter 96 | option_Rsub.F_manpg = F_manpg(test_random); 97 | option_Rsub.phi_init = phi_init; option_Rsub.maxiter = 1e1; option_Rsub.tol = 5e-3; 98 | option_Rsub.r = r; option_Rsub.n= n; option_Rsub.mu=mu; option_Rsub.type = type; 99 | 100 | [X_Rsub, F_Rsub(test_random),sparsity_Rsub(test_random),time_Rsub(test_random),... 101 | maxit_att_Rsub(test_random),succ_flag_sub]= Re_sub_grad_spca(B,option_Rsub); 102 | %phi_init = X_Rsub; 103 | if succ_flag_sub == 1 104 | succ_no_sub = succ_no_sub + 1; 105 | end 106 | option_soc.F_manpg = F_manpg(test_random); 107 | option_soc.X_manpg = X_manpg; 108 | option_PAMAL.F_manpg = F_manpg(test_random); 109 | option_PAMAL.X_manpg = X_manpg; 110 | [X_Soc, F_soc(test_random),sparsity_soc(test_random),time_soc(test_random),... 111 | soc_error_XPQ(test_random),maxit_att_soc(test_random),succ_flag_SOC]= soc_spca(B,option_soc); 112 | if succ_flag_SOC == 1 113 | succ_no_SOC = succ_no_SOC + 1; 114 | end 115 | option_PAMAL.F_manpg = F_manpg(test_random); 116 | [X_pamal, F_pamal(test_random),sparsity_pamal(test_random),time_pamal(test_random),... 117 | pam_error_XPQ(test_random), maxit_att_pamal(test_random),succ_flag_PAMAL]= PAMAL_spca(B,option_PAMAL); 118 | if succ_flag_PAMAL ==1 119 | succ_no_PAMAL = succ_no_PAMAL + 1; 120 | end 121 | 122 | if succ_flag_sub == 0 123 | fail_no_sub = fail_no_sub + 1; 124 | end 125 | if succ_flag_sub == 2 126 | diff_no_sub = diff_no_sub + 1; 127 | end 128 | if succ_flag_SOC == 0 129 | fail_no_SOC = fail_no_SOC + 1; 130 | end 131 | if succ_flag_SOC == 2 132 | diff_no_SOC = diff_no_SOC + 1; 133 | end 134 | if succ_flag_PAMAL == 0 135 | fail_no_PAMAL = fail_no_PAMAL + 1; 136 | end 137 | if succ_flag_PAMAL == 2 138 | diff_no_PAMAL = diff_no_PAMAL + 1; 139 | end 140 | if succ_flag_manpg == 1 141 | F_best(test_random) = F_manpg(test_random); 142 | end 143 | if succ_flag_manpg_BB == 1 144 | F_best(test_random) = min( F_best(test_random), F_manpg_BB(test_random)); 145 | end 146 | if succ_flag_SOC == 1 147 | F_best(test_random) = min( F_best(test_random), F_soc(test_random)); 148 | end 149 | if succ_flag_PAMAL == 1 150 | F_best(test_random) = min( F_best(test_random), F_pamal(test_random)); 151 | end 152 | 153 | end 154 | 155 | end 156 | 157 | end 158 | Result(index,1) = sum(lins)/succ_no_manpg; Result(index,2) = sum(in_av)/succ_no_manpg; 159 | Result(index,3) = sum(lins_adap)/succ_no_manpg; Result(index,4) = sum(in_av_adap)/succ_no_manpg; 160 | 161 | %Result(index,5) = succ_no_manpg_BB; Result(index,6) = succ_no_SOC; Result(index,7)=succ_no_PAMAL; Result(index,8)=succ_no_sub; 162 | Result(index,5) = succ_no_manpg_BB; Result(index,6) = succ_no_SOC; Result(index,7) = fail_no_SOC; Result(index,8) = diff_no_SOC; 163 | Result(index,9)= succ_no_PAMAL; Result(index,10)=fail_no_PAMAL; Result(index,11)= diff_no_PAMAL; 164 | index = index +1; 165 | 166 | iter.manpg(id_n) = sum(maxit_att_manpg)/succ_no_manpg; 167 | iter.manpg_BB(id_n) = sum(maxit_att_manpg_BB)/succ_no_manpg_BB; 168 | iter.soc(id_n) = sum(maxit_att_soc)/succ_no_SOC; 169 | iter.pamal(id_n) = sum(maxit_att_pamal)/succ_no_PAMAL; 170 | iter.Rsub(id_n) = sum(maxit_att_Rsub)/succ_no_sub; 171 | 172 | time.manpg(id_n) = sum(time_manpg)/succ_no_manpg; 173 | time.manpg_BB(id_n) = sum(time_manpg_BB)/succ_no_manpg_BB; 174 | time.soc(id_n) = sum(time_soc)/succ_no_SOC; 175 | time.pamal(id_n) = sum(time_pamal)/succ_no_PAMAL; 176 | time.Rsub(id_n) = sum(time_Rsub)/succ_no_sub; 177 | 178 | Fval.manpg(id_n) = sum(F_manpg)/succ_no_manpg; 179 | Fval.manpg_BB(id_n) = sum(F_manpg_BB)/succ_no_manpg_BB; 180 | Fval.soc(id_n) = sum(F_soc)/succ_no_SOC; 181 | Fval.pamal(id_n) = sum(F_pamal)/succ_no_PAMAL; 182 | Fval.Rsub(id_n) = sum(F_Rsub)/succ_no_sub; 183 | Fval.best(id_n) = sum(F_best)/succ_no_manpg; 184 | 185 | Sp.manpg(id_n) = sum(sparsity_manpg)/succ_no_manpg; 186 | Sp.manpg_BB(id_n) = sum(sparsity_manpg_BB)/succ_no_manpg_BB; 187 | Sp.soc(id_n) = sum(sparsity_soc)/succ_no_SOC; 188 | Sp.pamal(id_n) = sum(sparsity_pamal)/succ_no_PAMAL; 189 | Sp.Rsub(id_n) = sum(sparsity_Rsub)/succ_no_sub; 190 | fprintf(fid,'==============================================================================================\n'); 191 | 192 | fprintf(fid,' Alg **** Iter ***** Fval *** sparsity ** cpu *** Error ***\n'); 193 | 194 | print_format = 'ManPG: %1.3e %1.5e %1.2f %3.2f \n'; 195 | fprintf(fid,print_format, iter.manpg(id_n), Fval.manpg(id_n), Sp.manpg(id_n),time.manpg(id_n)); 196 | print_format = 'ManPG_adap: %1.3e %1.5e %1.2f %3.2f \n'; 197 | fprintf(fid,print_format, iter.manpg_BB(id_n), Fval.manpg_BB(id_n), Sp.manpg_BB(id_n),time.manpg_BB(id_n)); 198 | print_format = 'SOC: %1.3e %1.5e %1.2f %3.2f %1.3e\n'; 199 | fprintf(fid,print_format,iter.soc(id_n) , Fval.soc(id_n), Sp.soc(id_n) ,time.soc(id_n),mean(soc_error_XPQ)); 200 | print_format = 'PAMAL: %1.3e %1.5e %1.2f %3.2f %1.3e \n'; 201 | fprintf(fid,print_format,iter.pamal(id_n) , Fval.pamal(id_n) ,Sp.pamal(id_n),time.pamal(id_n),mean(pam_error_XPQ)); 202 | print_format = 'Rsub: %1.3e %1.5e %1.2f %3.2f \n'; 203 | fprintf(fid,print_format,iter.Rsub(id_n) , Fval.Rsub(id_n) ,Sp.Rsub(id_n),time.Rsub(id_n)); 204 | end 205 | 206 | %% plot 207 | 208 | % figure(1); 209 | % plot(n_set, time.manpg, 'r-','linewidth',1); hold on; 210 | % plot(n_set, time.manpg_BB, 'k-','linewidth',1); hold on; 211 | % plot(n_set, time.soc, 'b--','linewidth',1); hold on; 212 | % plot(n_set, time.pamal, 'c-.','linewidth',2); hold on; 213 | % %plot(n_set, time.Rsub, 'g-.','linewidth',2); 214 | % xlabel('dimenion-n'); ylabel('CPU'); 215 | % title(['comparison on CPU: different dimension',',r=',num2str(r),',\mu=',num2str(mu)]); 216 | % legend('ManPG','ManPG-adap','SOC','PAMAL','Location','NorthWest'); 217 | % filename_pic1 = ['SPCA_CPU_n', '_' num2str(r) '_' num2str(mu) '.eps']; 218 | % saveas(gcf,filename_pic1,'epsc') 219 | % 220 | % 221 | % figure(2) 222 | % semilogy(n_set, Fval.manpg-Fval.best+1e-16, 'r-s','MarkerSize',10,'linewidth',1); hold on; 223 | % semilogy(n_set, Fval.manpg_BB-Fval.best+1e-16, 'k-o','MarkerSize',6,'linewidth',1); hold on; 224 | % semilogy(n_set, Fval.soc-Fval.best+1e-16, 'b-d','MarkerSize',8,'linewidth',1); hold on; 225 | % semilogy(n_set, Fval.pamal-Fval.best+1e-16, 'c-.','MarkerSize',20,'linewidth',1.5); hold on; 226 | % %semilogy(n_set, Fval.Rsub -Fval.best+1e-16, 'g-.','MarkerSize',20,'linewidth',1.5); 227 | % %legend('ManPG','ManPG-BB','SOC','PAMAL','Rsub','Location','NorthWest'); 228 | % legend('ManPG','ManPG-adap','SOC','PAMAL','Location','NorthWest'); 229 | % 230 | % xlabel('dimenion-n'); ylabel('fucntion value difference'); 231 | % title(['comparison on function value difference: different dimension',',r=',num2str(r),',\mu=',num2str(mu)]); 232 | % filename_pic2 = ['SPCA_Fval_n', '_' num2str(r) '_' num2str(mu) '.eps']; 233 | % saveas(gcf,filename_pic2,'epsc') 234 | % 235 | % % 236 | % % 237 | % figure(3) 238 | % plot(n_set, Sp.manpg, 'r-s','MarkerSize',10,'linewidth',1); hold on; 239 | % plot(n_set, Sp.manpg_BB, 'k-o','MarkerSize',6,'linewidth',1); hold on; 240 | % plot(n_set, Sp.soc, 'b-d','MarkerSize',8,'linewidth',1); hold on; 241 | % plot(n_set, Sp.pamal, 'c-.','MarkerSize',20,'linewidth',1.5); hold on; 242 | % %plot(n_set, Sp.Rsub, 'g-.','MarkerSize',20,'linewidth',1.5); %hold on; 243 | % xlabel('dimenion-n'); ylabel('sparsity'); 244 | % legend('ManPG','ManPG-adap','SOC','PAMAL','Location','SouthEast'); 245 | % title(['comparison on sparsity: different dimension',',r=',num2str(r),',\mu=',num2str(mu)]); 246 | % filename_pic3 = ['SPCA_Sparsity_n', '_' num2str(r) '_' num2str(mu) '.eps']; 247 | % saveas(gcf,filename_pic3,'epsc') 248 | % 249 | % 250 | % figure(4) 251 | % plot(n_set, iter.manpg, 'r-s','MarkerSize',10,'linewidth',1); hold on; 252 | % plot(n_set, iter.manpg_BB, 'k-o','MarkerSize',6,'linewidth',1); hold on; 253 | % plot(n_set, iter.soc, 'b-d','MarkerSize',8,'linewidth',1); hold on; 254 | % plot(n_set, iter.pamal, 'c-.','MarkerSize',20,'linewidth',1.5); %hold on; 255 | % %plot(n_set, iter.Rsub, 'g-.','MarkerSize',20,'linewidth',1.5); %hold on; 256 | % xlabel('dimenion-n'); ylabel('iter'); 257 | % legend('ManPG','ManPG-adap','SOC','PAMAL','Location','NorthWest'); 258 | % title(['comparison on iter: different dimension',',r=',num2str(r),',\mu=',num2str(mu)]); 259 | % filename_pic4= ['SPCA_iter_n', '_' num2str(r) '_' num2str(mu) '.eps']; 260 | % saveas(gcf,filename_pic4,'epsc') 261 | % 262 | % close(figure(1)); 263 | % close(figure(2)); 264 | % close(figure(3)); 265 | % close(figure(4)); 266 | % filename = ['SPCA_comparison_n_' num2str(r_set(id_r)) '_' num2str(mu_set(id_mu)) '.csv']; 267 | % csvwrite( filename, Result); 268 | 269 | 270 | 271 | --------------------------------------------------------------------------------