%set parameters
randn('seed',2); % 2 for the experiments in the paper
rand('seed',2); % 2 for the experiments in the paper

fid = fopen('log_05.txt','w');
for index = 1:10
    
    m = 720*index;
    n = 2560*index;
    k = 80*index;
    maxiter = inf;
    nf = 0.01;
    tol = 1e-4; %terminate criterion
    M2 = 5;

    %parameter for log
    lambda = 5e-4;
    epsilon = 0.5; 

    fprintf(' *********************************************\n')
    fprintf(' index %2.0f, lambda %2.1e, epsilon %2.1e, \n', index, lambda, epsilon)
    fprintf(' *********************************************\n')
     
    % generate problems
    progress_r = [];
    for repeats = 1:20
        A = randn(m,n);
        for j = 1:n
            A(:,j) = A(:,j)/norm(A(:,j));%normalize A
        end
        w = zeros(n,1);
        I = randperm(n);
        w(I(1:k)) = randn(k,1);
        b = A*w + nf*randn(m,1);
        
        % compute the L
        if m < 2000
            tstart = tic;
            L = norm(A*A');
            teig = toc(tstart);
        else
            clear opts
            opts.issym = 1;
            tstart = tic;
            L = eigs(A*A',1,'LM',opts);
            teig = toc(tstart);
        end
        fprintf(' time for eigenvalues %g\n', teig)
        
        %GIST %% NPG
        clear opts
        opts.x0 = zeros(n,1);
        opts.maxiter = inf;
        opts.M = M2;
        opts.tol = tol;
        fprintf(' ********** Comparison starts **********\n') % this line is edited on Oct 20, 2017
        
        [x00,iter00,Fval_log00,time00] = GIST(A,b,lambda,epsilon,opts);
        fprintf(' reweighted NPG terminated: iter %d, time %5.1f, fval %6.5e\n', iter00, time00, Fval_log00)
        
        % reweighted NPG
        [x0,iter0,Fval_log0,time0] = ReNPG(A,b,epsilon,lambda, tol,maxiter,5);
        fprintf(' reweighted NPG_1 terminated: iter %d, time %5.1f, fval %6.5e\n', iter0, time0, Fval_log0)
        
        % RPGe1
        %         profile on
        [x1,iter1,time1] = IRL1e1(A,b,epsilon,lambda, L, tol,maxiter,200);
        Fval_log1 = 0.5*norm(A*x1-b)^2 + lambda*sum(log(abs(x1)/epsilon + 1));
        fprintf(' RPGe1 terminated: iter %d, time %5.1f, fval %6.5e\n', iter1, time1, Fval_log1)
        
        % RPGe2
        [x2,iter2,time2] = IRL1e2(A,b,epsilon,lambda, L, tol,maxiter,50);
        Fval_log2 = 0.5*norm(A*x2-b)^2 + lambda*sum(log(abs(x2)/epsilon + 1));
        fprintf(' RPGe2 terminated: iter %d, time %5.1f, fval %6.5e\n', iter2, time2, Fval_log2)
        %         profile off
        
        % RPGe3
        [x3,iter3,time3] = IRL1e3(A,b,epsilon,lambda, L, tol,maxiter,50);
        Fval_log3 = 0.5*norm(A*x3-b)^2 + lambda*sum(log(abs(x3)/epsilon + 1));
        fprintf(' RPGe3 terminated: iter %d, time %5.1f, fval %6.5e\n', iter3, time3, Fval_log3)
        
        progress_r = [progress_r; teig iter00 iter0 iter1 iter2 iter3 time00 time0 time1 time2 time3 Fval_log00 Fval_log0 Fval_log1 Fval_log2 Fval_log3];        
    end
    fprintf(fid,' %5.0f & %5.0f & %5.1f & %5.0f & %5.0f &%5.0f &%5.0f &%5.0f &%6.1f&%6.1f&%6.1f&%6.1f&%6.1f & %5.4e& %5.4e& %5.4e& %5.4e& %5.4e\n',  m, n, mean(progress_r));
end
fclose(fid);