%----------------------------- example_2 ---------------------------------%
%
% This script runs three trust-region subproblem solvers, Alg. 3, Alg. 4,
% and the external solver "obs.m" (L2 norm) from within LSR1_SC
%
% The subproblem solvers are implemented in:
%
% sc_sr1_infty.m    - Shape-changing infinity norm (Alg. 3)
% sc_sr1_2.m        - Shape-changing 2 norm (Alg. 4)
% 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 in [500, 1000, 5000, 10000, 50000, 100000, 300000]
%
% This script includes the option to print the results to the
% file example_2.txt in the "DATA/" folder, with format for LaTeX
%
%-------------------------------------------------------------------------%
% 04/27/21, J.B., Preparation for release
% 06/21/22, J.B., Edit defaults and preparation for release

clc;
clear;
warning('off','MATLAB:nearlySingularMatrix');

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

printFile   = 1;
fname       = '../DATA/example_2.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];

% Setting some trust-region algorithm parameters
pars.tol    = 1e-4;
pars.print  = 0;
pars.maxiter= 200;
pars.m      = 5;

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

fprintf('Example 2 ##########################################\n');    
fprintf('Rosenbrock objective: f(x)                          \n');
fprintf('n = [500, 1000, 5000, 10000, 50000, 100000, 300000] \n');
fprintf('\n');
fprintf('Sub. Algorithms: TR:SC-INF, TR:SC-L2, TR:L2        \n');
fprintf('####################################################\n');
fprintf('\n');
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; % 30, 2
    
    % Using sc_sr1_infty.m (Shape-changing infinity norm solver, Alg. 3)
    pars.whichSub       = 1;
    [xk1,gk1,fk1,out1]  = LSR1_SC(x0,func,grad,pars);
    
    % Using sc_sr1_2.m (Shape-changing 2 norm solver, Alg. 4)
    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.numf,out1.ctime,out1.ng,...
                       out2.numiter,out2.numf,out2.ctime,out2.ng,...
                       out3.numiter,out3.numf,out3.ctime,out3.ng);
    end    
end

% Save file
if printFile == 1
    fclose(fres);
end

warning('on','MATLAB:nearlySingularMatrix');




