% Run realdata for the min 0.5*\|Ax - b\|^2 subject to ||x||_0 <= k
clear all
clc
path('./Real_data',path);
%parameters
tol = 1e-5;
maxiter = inf;
% for DR
ratio = 10; 
ratio_1 = 30;
ratio_2 = 50;
% for PR
beta = 2.2; 
eta = 0.93;  
klist = [10 20 30];


fid = fopen('realdata.txt','w');
emptystring = '        ';


for cases = 1:4
  
  if cases == 1
    TrainFileName = 'leukemiatrain.txt'; TestFileName = 'leukemiatest.txt'; 
    traindata = load(TrainFileName); testdata = load(TestFileName);
    alldata = [traindata; testdata];
    string = 'Leukemia ';
  elseif cases == 2
    alldata = load('lymph.dat');
    string = 'Lymph ';
  elseif cases == 3
    alldata = load('scaledbctrain.txt');
    string = 'Breast';
  elseif cases == 4
    X = load('I2000.txt')'; yt = sign(load('I2000b.txt')); alldata = [X yt];
    string = 'Colon ';
  end
  
  % standardize data
  nn = size(alldata,2);
  alldata = zscore(alldata);
  for i = 1:nn-1
    a = alldata(:,i);
    alldata(:,i) = a/norm(a);
  end
  
  A = alldata(:,1:nn-1);
  b = alldata(:,nn);
 
  for i = 1:3
      k = klist(i);
      fprintf(' Start DR 10 \n')
      tDR = tic;
      [fval,z,x,y,iter] = DR_ls(A,b,k,tol,maxiter,ratio);
      time = toc(tDR);
      fprintf(' iter = %5d  time = %5.1f  fval = %8.7e\n', iter, time, fval);
      
      fprintf(' Start DR 30 \n')
      tDR1 = tic;
      [fval1,z1,x1,y1,iter1] = DR_ls(A,b,k,tol,maxiter,ratio_1);
      time1 = toc(tDR1);
      fprintf(' iter = %5d  time = %5.1f  fval = %8.7e\n', iter1, time1, fval1);
      
      fprintf(' Start DR 50\n')
      tDR2 = tic;
      [fval2,z2,x2,y2,iter2] = DR_ls(A,b,k,tol,maxiter,ratio_2);
      time2 = toc(tDR2);
      fprintf(' iter = %5d  time = %5.1f  fval = %8.7e\n', iter2, time2, fval2);

      fprintf(' Start PR \n')
      tPR = tic;
      [fvalPR,zPR,xPR,yPR,iterPR] = PR_ls(A,b,k,tol,maxiter,beta,eta); 
      timePR = toc(tPR);
      fprintf(' iter = %5d  time = %5.1f  fval = %8.7e\n', iterPR, timePR, fvalPR);
      
      if i == 2
        fprintf(fid,' %8s & %2d & %5.0f & %4.2e & %5.0f & %4.2e & %5.0f & %4.2e & %5.0f & %4.2e\n',...
            string, k, iter, fval, iter1, fval1, iter2, fval2, iterPR, fvalPR);
      else
        fprintf(fid,' %8s & %2d & %5.0f & %4.2e & %5.0f & %4.2e & %5.0f & %4.2e & %5.0f & %4.2e\n',...
            emptystring, k, iter, fval, iter1, fval1, iter2, fval2, iterPR, fvalPR);
      end
  end
end
  
fclose(fid);