% min_X 0.5*mdot(X,C*X) + lambda*||X(:)||_0
clc;clear all;close all;
maxNumCompThreads(1);
addpath('util','solver','../data');
rand('seed',0); randn('seed',0);

data_list   = [1:10];
r = 20;
lambda = 10;
time_limits = [30;30;30;30;30;30;30;30;30;30];
result_fig = [];
for i_data_list = 1:length(data_list)
    
    idat = data_list(i_data_list);
    dat_x = GetData(idat);
    [m,n] = size(dat_x);
    time_limit = time_limits(i_data_list);
    C = - dat_x'*dat_x;
    fprintf('================= start =================\n');
    fprintf(' time_limit: %d\n',time_limit);
    fprintf('       data: %d\n',idat);
    fprintf('          r: %d\n',r);
    fprintf('     lambda: %d\n',lambda);
    fprintf('          m: %d\n',size(dat_x,1));
    fprintf('          n: %d\n',size(dat_x,2));
    fprintf('sparsity(I): %f\n',(r)/(n*r));
    X0 = eye(n,r);
    [X00, ~] = qr(randn(n,r), 0);

    HandleTrueObj = @(X)ComputeTrueObjL0PCA(X,C,lambda,1e-5);
    [X1,his1,ts1] = ADMM_L0PCA_for_plot(0.5*C,lambda,r,X0,1e7,time_limit,HandleTrueObj);
    [X2,his2,ts2] = PADM_L0PCA_for_plot(0.5*C,lambda,r,X0,1e7,time_limit,HandleTrueObj);
    [X3,his3,ts3] = ADMM_L0PCA_for_plot(0.5*C,lambda,r,X00,1e7,time_limit,HandleTrueObj);
    [X4,his4,ts4] = PADM_L0PCA_for_plot(0.5*C,lambda,r,X00,1e7,time_limit,HandleTrueObj);
    [X5,his5,ts5] = OBCD_for_plot(X0,C,lambda,HandleTrueObj(X1),1e7,1,time_limit,1e-8);
    
    
    %         figure;
%         plot(ts1,his1,'c*');hold on;
%         plot(ts2,his2,'g');hold on;
%         plot(ts3,his3,'y');hold on;
%         plot(ts4,his4,'k');hold on;
%         plot(ts5,his5,'r');hold on;

        fprintf('objective value:\n');
        fprintf('%.5f %.5f %.5f ',HandleTrueObj(X1),HandleTrueObj(X2),HandleTrueObj(X3));
        fprintf('%.5f %.5f %.5f ',HandleTrueObj(X4),HandleTrueObj(X5));
        fprintf('\n');
        
        fprintf('sparsity:\n');
        fprintf('%.5f %.5f %.5f ',sparsity(X1),sparsity(X2),sparsity(X3));
        fprintf('%.5f %.5f %.5f ',sparsity(X4),sparsity(X5));
        fprintf('\n');
        
        
    One=[];
    One.his1 = his1; One.ts1 = ts1;
    One.his2 = his2; One.ts2 = ts2;
    One.his3 = his3; One.ts3 = ts3;
    One.his4 = his4; One.ts4 = ts4;
    One.his5 = his5; One.ts5 = ts5;
    result_fig{i_data_list} = One;
    save result_fig_30s_10 result_fig;
    
end







