%--------------------------- driver_EX_tbl_8 -----------------------------%
%
% This script tests three L-SR1 trust-region subproblem solvers
% implemented in LSR1_SC. This experiment generates the data for 
% Table 8 in the manuscript.
% The subproblem solvers are:
%
% sc_sr1_infty.m    - Shape-changing infinity norm
% sc_sr1_2.m        - Shape-changing 2 norm
% obs.m             - L2 norm (external solver)
%
% The objective function, f(x): R^n -> R, is the Rosenbrock function. The
% dimensions of the problems are: 
%
% n = [500, 1000, 5000, 10000, 50000, 100000, 300000];
%
% This script includes the option to print the results to the
% file table_8.txt
%-------------------------------------------------------------------------%
clc;
clear;
warning('off','MATLAB:nearlySingularMatrix');

addpath(genpath('../ALGS'));
addpath(genpath('../EXTERNAL'));
addpath(genpath('../AUXILIARY'));

printFile   = 1;
fname       = '../DATA/table_8.txt';

% Rosenbrock objective function and gradient
func = @(x)( rosen_obj(x) );
grad = @(x)( rosen_grad(x) );

% Problem dimensions
ns = [500, 1000, 5000, 10000, 50000, 100000, 300000];

% Trust-region algorithm parameters
% Detailed description of parameters in the comments of LSR1_SC.m

pars.c1     = 9.e-4; 
pars.c2     = 0.75;  
pars.tol    = 1e-4;
pars.print  = 0;
pars.maxiter= 500;
pars.m      = 5; % Limited memory (low memory) m = 3
pars.gammaInit = 10; % Gamma initialization gam = 10
pars.whichInit = 4; % Init. 2
pars.storePsiPsi = 1; % Update and store PsiPsi

% Open file
if printFile == 1
    fres = fopen(fname, 'w');
end

fprintf('----------- Running Table 8 ----------- \n'); % 43 chars
fprintf('n \t TR:SC-INF        \t TR:SC-L2         \t TR:L2 \n');
fprintf('- \t Time     norm(g) \t Time     norm(g) \t Time     norm(g) \n');

% Loop over problem dimensions
for i = 1:length(ns)
    
    % Initial point
    n       = ns(i);
    x0      = zeros(n,1);
    x0(1)   = 30; 
    
    % Using sc_sr1_infty.m (Shape-changing infinity norm solver)
    pars.whichSub       = 1;
    [xk1,gk1,fk1,out1]  = LSR1_SC(x0,func,grad,pars);
    
    % Using sc_sr1_2.m (Shape-changing 2 norm solver)
    pars.whichSub       = 2;
    [xk2,gk2,fk2,out2]  = LSR1_SC(x0,func,grad,pars);

    % Using obs.m (L2 norm solver)
    pars.whichSub       = 3;
    [xk3,gk3,fk3,out3]  = LSR1_SC(x0,func,grad,pars);
    
    fprintf('%i \t %3.1e  %3.1e \t %3.1e  %3.1e \t %3.1e  %3.1e \n',n,...
        out1.ctime,out1.ng,out2.ctime,out2.ng,out3.ctime,out3.ng);
    
    if printFile == 1
        fprintf(fres, [' \\texttt{%i} & \\texttt{%i} &  \\texttt{%3.2e} & \\texttt{%3.2e} &', ...
                       ' \\texttt{%i} & \\texttt{%i} &  \\texttt{%3.2e} & \\texttt{%3.2e} &', ...
                       ' \\texttt{%i} & \\texttt{%i} &  \\texttt{%3.2e} & \\texttt{%3.2e} \\\\ \n'],...
                       out1.numiter,out1.numAccept,out1.ctime,out1.ng,...
                       out2.numiter,out2.numAccept,out2.ctime,out2.ng,...
                       out3.numiter,out3.numAccept,out3.ctime,out3.ng);
    end    
end

% Save file
if printFile == 1
    fclose(fres);
end
warning('on','MATLAB:nearlySingularMatrix');




