%------------------------------ plot_Fig8.m ------------------------------%
%
% Script to replot experiments with data from the "\DATA" folder
% for each solver
%
% This compares the solvers in order to determine optimal default values,
% and corresponds to Figure 8 in the manuscript
%
% The values of m are: m = [5,7,12]
% The values of q are: q = [ceil(2/3*m),m,floor(3/2*m)]
%                        = [4,5,8,5,7,12,7,10,18]
%-------------------------------------------------------------------------%
% 03/03/21, J.B., Initial version
% 03/05/21, J.B.,
% 03/08/21, J.B., Plot memory outcomes per solver
% 03/10/21, J.B., Replotting lower "m" values
% 04/06/21, J.B., Preparation of experiments with varying "m" and "q".
%                   In order to swap between these values, "ins" and "outs"
%                   are defined.
% 04/07/21, J.B., Comparison using all combinations
% 04/08/21, J.B., Modification to use "results" from last run
% 04/23/21, J.B., Re-naming of file and preparation for release
% 01/28/22, J.B., Updating of figure names after revision
% 06/20/22, J.B., Toggle warning and keep figures on

clc;
clear;

addpath(genpath('../ALGS'));
figpath = fullfile(pwd,'..','/FIGS/');
datapath = fullfile(pwd,'..','/DATA/');

warning('off');

% Memory values from experiments (with constant initialization)
% 62 problems used
outs= [5,7,12];
ins = [ceil((2/3)*outs)',outs',floor((3/2)*outs)'];

inLab = 'q'; % Inner label
outLab = 'm'; % Outer label
indAlg = [1 2 3]; % Columns of data (e.g., [1,2,3])
np = 62;
nins = size(ins,2);%length(ins);
nouts = length(outs);
ex = zeros(np,nins);
t_aver = zeros(np,nins);
numit = zeros(np,nins);
nsol = 4;

% Plotting details
leg={   'TR:SC-INF (Init. 2)',...
    'TR:SC-L2 (Init. 2)',...
    'TR:L2 (Init. 2)',...
    'trCG (Init. 2)'};

mleg = cell(nins);
types.markers   = ['o' 'o' 'o' 'o' 'o' 'o' 'o']; %'s' 's'
types.colors    = ['b' 'r' 'm' 'g' 'k' 'c' 'y']; %'k' 'y'
types.lines     = {'-', '-.', '-','-.','-','-.','-'}; %'-',   '-'
leglocation = 'SouthEast';
isLog = 0;

for i=1:nsol
    for k=1:nouts
        
        % Load data
        typesp.colors = [];
        typesp.lines = {'-','--',':','-.'};
        typesp.markers =['o' 'o' 'o' 'o'];
        for j=1:nins
            mleg{j} = ['$',inLab,'=',num2str(ins(k,j)),'$'];
            if strcmp(inLab,'q')
                suffix = [outLab,num2str(outs(k)),'_',inLab,num2str(ins(k,j))];
            else
                suffix = [inLab,num2str(ins(k,j)),'_',outLab,num2str(outs(k))];
            end
            name_EXPERIMENT = ['EXPERIMENT_FINAL_VARINIT2_',suffix];
            data = load([datapath,name_EXPERIMENT],'ex','numit','t_aver');
            ex(:,j) = data.ex(:,i);
            numit(:,j) = data.numit(:,i);
            t_aver(:,j) = data.t_aver(:,i);
            typesp.colors = [typesp.colors,types.colors(i)];
        end
        
        perf(ex(:,indAlg),t_aver(:,indAlg),mleg(indAlg),1,typesp,leglocation,isLog);
        box on; grid on;
        title(['$\textnormal{Time (',leg{i},')},',outLab,'=',...
            num2str(outs(k)),'$'],'Interpreter','latex');
        fig                     = gcf;
        fig.PaperPositionMode   = 'auto';
        fig_pos                 = fig.PaperPosition;
        fig.PaperSize           = [fig_pos(3) fig_pos(4)];
        
        figname = ['Fig8_',outLab,num2str(outs(k)),'_',num2str(i),...
            'inLab_',inLab,'.pdf']; %'time_EX_CUTEST_INIT_1';
        print(fig,'-dpdf',fullfile(figpath,figname));
        
        % Additional comparisons on iterations possible by un-commenting
        % the next lines
        
%         perf(ex(:,indAlg),numit(:,indAlg),mleg(indAlg),1,typesp,leglocation,isLog);
%         box on; grid on;
%         title(['$\textnormal{Iter (',leg{i},')},',outLab,'=',...
%             num2str(outs(k)),'$'],'Interpreter','latex');
%         fig                     = gcf;
%         fig.PaperPositionMode   = 'auto';
%         fig_pos                 = fig.PaperPosition;
%         fig.PaperSize           = [fig_pos(3) fig_pos(4)];
%         figname = ['iter_I_SOLV_ALL_COMB_',outLab,num2str(outs(k)),'_',num2str(i),...
%             'inLab_',inLab,'.pdf'];
%         
%         print(fig,'-dpdf',fullfile(figpath,figname));
        
    end
    
end

%close ALL;

warning('on');
