% runcode for D_criterion

randn('seed',4);
rand('seed',4);

% simulated data

path('./D_criterion_with_K',path);

fid = fopen('D_sim.txt','w');
fclose(fid);

repeats = 30;
maxiter = 10000;

feas = [];

k = 3;

for design = 1:4
  
  if design ~= 3
    
    for m = [10000 50000 100000]
      
      Table = zeros(repeats,6);
      
      for repeat = 1:repeats
        
        [M, K, w0] = real_data(design,m);
        n = sqrt(size(M,1));
      
        K = randn(n,k);
      
        tic
        [w fval2] = multiplicatived(M,K,w0,10000,2e-4,maxiter);
        time2 = toc;
        
        feas = [feas; sum(w) min(w) fval2];
      
        tic
        [w fval3] = IPM_DK(M,K,1e-10,inf);
        time3 = toc;
        
        feas = [feas; sum(w) min(w) fval3];
        
        [w fval4 time4] = D_criterion_SDPT3_K(M,K);
        
        feas = [feas; sum(w) min(w) fval4];
      
        Table(repeat,:) = [time2 time3 time4 fval2 fval3 fval4];
        
      end
      
      Table = sum(Table,1)/repeats;
      
      fid = fopen('D_sim.txt','a');
      fprintf(fid, '%3d & %3d &  %5.2f  &  %5.2f  &  %5.2f  &  %13.6g  &  %13.6g  &  %13.6g\n', design, m, Table);
      fclose(fid);
      
    end
    
  else 
    
    for m = [100 200 300]
      
      Table = zeros(repeats,6);
      
      for repeat = 1:repeats
        
        [M, K, w0] = real_data(design,m);
        n = sqrt(size(M,1));
      
        K = randn(n,k);
      
        tic
        [w fval2] = multiplicatived(M,K,w0,10000,2e-4,maxiter);
        time2 = toc;
        
        feas = [feas; sum(w) min(w) fval2];
       
        tic
        [w fval3] = IPM_DK(M,K,1e-10,inf);
        time3 = toc;
        
        feas = [feas; sum(w) min(w) fval3];
        
        [w fval4 time4] = D_criterion_SDPT3_K(M,K);
        
        feas = [feas; sum(w) min(w) fval4];
      
        Table(repeat,:) = [time2 time3 time4 fval2 fval3 fval4];
        
      end
      
      Table = sum(Table,1)/repeats;
      
      fid = fopen('D_sim.txt','a');
      fprintf(fid, '%3d & %3d &  %5.2f  &  %5.2f  &  %5.2f  &  %13.6g  &  %13.6g  &  %13.6g\n', design, m, Table);
      fclose(fid);
      
    end
    
  end
   
end