%--------------------------- plot_Fig1.m ---------------------------------%
%
% Based on recommendation a side-by-side comparison of the constant
% and variable initialization for same memory parameter
%
% Script to replot experiments with data from the "\DATA" folder
% for each solver. This is used for comparing the best solvers,
% with constant initializations and corresponds to Figure 1 in the
% manuscript
%
%-------------------------------------------------------------------------%
% 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., Plots with low memory values
% 04/06/21, J.B., Preparation of plots with 4 different "m" values
% 04/08/21, J.B., Comparison C. Init
% 04/09/21, J.B., Extended performance plots
% 04/23/21, J.B., Re-naming of file and preparation for release
% 01/18/22, J.B., Modification of script to plot additional figures
%                   as part of the revision process
% 01/20/22, J.B., Update to include function evaluations
% 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. Selected memory values in "ms"
% Additional settings based on reviewer comments

setting = 3; % 0,1,2
ms = [24,14,14,24];
fignm1 = 'Fig1_CINIT';
fignm2 = 'Fig1_CINIT';
if setting == 1
    ms = [24,24,24,24];    
elseif setting == 2
    ms = [14,14,14,14];
elseif setting == 3
    ms = [7,7,7,7];
end
if setting > 0
    intername = '';
    if setting ~= 3; intername = ['_SET_',num2str(setting)]; end
    fignm1 =[fignm1,intername,'_TIME'];
    fignm2 =[fignm2,intername,'_FEVAL'];
end
    
indAlg = [1 2 3 4];
np = 62;
nms = length(ms);
ex = zeros(np,nms);
t_aver = zeros(np,nms);
numit = zeros(np,nms);
numf = zeros(np,nms);

% Plotting labels, and additional parameters
leg={   'TR:SC-INF',...
        'TR:SC-L2',...
        'TR:L2',...
        'trCG'};

mleg = cell(nms);
types.markers   = ['o' 'o' 'o' 'o' 'o' 'o' 'o']; %'s' 's'

% Initial line types
types.colors    = ['b' 'r' 'm' 'g' 'k' 'c' 'y']; %'k' 'y'
types.lines     = {'-', '-.', '-','-.','-','-.','-'}; %'-',   '-'

% Load data
typesp.colors = [];
typesp.lines = {'-.',':',':','-.','--'};
typesp.markers =['o' 'o' 'o' 'o' 'o'];
for j=1:nms
    mleg{j} = [leg{j},' ($m=',num2str(ms(j)),'$)'];
    name_EXPERIMENT = ['EXPERIMENT_FINAL_CONS_m',num2str(ms(j))];    
    data = load([datapath,name_EXPERIMENT],'ex','numit','t_aver','numf');
    ex(:,j) = data.ex(:,j);
    numit(:,j) = data.numit(:,j);
    t_aver(:,j) = data.t_aver(:,j);
    numf(:,j) = data.numf(:,j);
    typesp.colors = [typesp.colors,types.colors(j)];
end

% Specification of tick-marks for extended perf. profiles
ticks   = -2; 
ticke   = 5; 
XTick   = 2.^(ticks:ticke);
XLim    = [XTick(1),XTick(end)];
leglocation1 = 'SouthEast';
legFontSize1 = 7.5;
markerSize = 0;
lineWidths = [1.8, 1.6, 1.4, 1.2];
typesp.markers =['o' 'o' 'd' 'h' 'o'];

% Extended perf. profile
perf_ext_fnc(ex(:,indAlg),t_aver(:,indAlg),mleg(indAlg),1,typesp,...
    leglocation1,XTick,XLim,legFontSize1,markerSize,lineWidths);

% On first plot "grid on" does not produce desired result,
% thus toggle once more
grid on; grid off;
grid on;
title('$\textnormal{Time (C Init.)}$','Interpreter','latex');

% Modified printing
fig                     = gcf;
fig.PaperPositionMode   = 'auto';
fig_pos                 = fig.PaperPosition;
fig.PaperSize           = [fig_pos(3) fig_pos(4)];
figname = [fignm1,'.pdf'];

print(fig,'-dpdf',fullfile(figpath,figname));

%close ALL;

% Additional settings 
% Extended perf. profile
if setting > 0

    perf_ext_fnc(ex(:,indAlg),numf(:,indAlg),mleg(indAlg),1,typesp,...
        leglocation1,XTick,XLim,legFontSize1,markerSize,lineWidths);

    % On first plot "grid on" does not produce desired result,
    % thus toggle once more
    grid on; grid off;
    grid on;
    title('$\textnormal{Function Calls (C Init.)}$','Interpreter','latex');

    % Modified printing
    fig                     = gcf;
    fig.PaperPositionMode   = 'auto';
    fig_pos                 = fig.PaperPosition;
    fig.PaperSize           = [fig_pos(3) fig_pos(4)];
    figname = [fignm2,'.pdf'];

    print(fig,'-dpdf',fullfile(figpath,figname));
end

%close ALL;

% Second part of plots (Init. 2)
% Memory values from experiments (with non-constant initialization)
% 62 problems used
ms = [5,5,5,5];
qs = [7,7,7,5];

setting = 2; % 0,1,2
fignm1 = 'Fig1_INIT2';
fignm2 = 'Fig1_INIT2';
if setting == 1
    ms = [5,5,5,5];
    qs = [5,5,5,5];
elseif setting == 2
    ms = [7,7,7,7];
    qs = [7,7,7,7];
elseif setting == 3
    ms = [12,12,12,12];
    qs = [12,12,12,12];
end
if setting > 0
    if setting ~= 2; intername = ['_SET_',num2str(setting)]; end
    fignm1 =[fignm1,intername,'_TIME'];
    fignm2 =[fignm2,intername,'_FEVAL'];    
end

indAlg = [1 2 3 4];
np = 62;
nms = length(ms);
ex = zeros(np,nms);
t_aver = zeros(np,nms);
numit = zeros(np,nms);
numf = zeros(np,nms);
nsol = 4;

% Plotting labels, and additional parameters
leg={   'TR:SC-INF',...
    'TR:SC-L2',...
    'TR:L2',...
    'trCG'};

mleg = cell(nms);
types.markers   = ['o' 'o' 'o' 'o' 'o' 'o' 'o']; %'s' 's'

% Initial line types
types.colors    = ['b' 'r' 'm' 'g' 'k' 'c' 'y']; %'k' 'y'
types.lines     = {'-', '-.', '-','-.','-','-.','-'}; %'-',   '-'

leglocation = 'SouthEast';
isLog = 1;
legFontSize = 8;
scaleMaxRatio = 4.0;
typesp.colors = [];
typesp.lines = {'-','-','-','-','--'};
typesp.markers =['o' 'o' 'o' 'o' 'o'];

for j=1:nms
    mleg{j} = [leg{j},' ($m=',num2str(ms(j)),', q=',num2str(qs(j)),'$)'];    
    name_EXPERIMENT = ['EXPERIMENT_FINAL_VARINIT2_',...
        'm',num2str(ms(j)),'_','q',num2str(qs(j))];    
    data = load([datapath,name_EXPERIMENT],'ex','numit','t_aver','numf');
    ex(:,j) = data.ex(:,j);
    numit(:,j) = data.numit(:,j);
    t_aver(:,j) = data.t_aver(:,j);
    numf(:,j) = data.numf(:,j);
    typesp.colors = [typesp.colors,types.colors(j)];
end

% Additional plots possible, by un-commenting the next lines

% perf(ex(:,indAlg),t_aver(:,indAlg),mleg(indAlg),1,typesp,...
%     leglocation,isLog,legFontSize,scaleMaxRatio);
% box on; grid on;
% title('$\textnormal{Time (Init. 2)}$','Interpreter','latex');
% fig                     = gcf;
% fig.PaperPositionMode   = 'auto';
% fig_pos                 = fig.PaperPosition;
% fig.PaperSize           = [fig_pos(3) fig_pos(4)];
% 
% figname = ['time_I_SOLV_ALL_BEST','.pdf']; %'time_EX_CUTEST_INIT_1';
% print(fig,'-dpdf',fullfile(figpath,figname));
% 
% perf(ex(:,indAlg),numit(:,indAlg),mleg(indAlg),1,typesp,leglocation,isLog,...
%     legFontSize,scaleMaxRatio);
% box on; grid on;
% title('$\textnormal{Iter (Init. 2)}$','Interpreter','latex');
% fig                     = gcf;
% fig.PaperPositionMode   = 'auto';
% fig_pos                 = fig.PaperPosition;
% fig.PaperSize           = [fig_pos(3) fig_pos(4)];
% 
% figname = ['iter_I_SOLV_ALL_BEST','.pdf'];
% print(fig,'-dpdf',fullfile(figpath,figname));

% Extended performance profiles 
ticks   = -2; % -1
ticke   = 5; % 2
XTick   = 2.^(ticks:ticke);
XLim    = [XTick(1),XTick(end)];
leglocation1 = 'SouthEast';
legFontSize1 = 7.5;
markerSize = 0;
lineWidths = [1.8, 1.6, 1.4, 1.2];

typesp.markers =['o' 'o' 'd' 'h' 'o'];

perf_ext_fnc(ex(:,indAlg),t_aver(:,indAlg),mleg(indAlg),1,typesp,...
    leglocation1,XTick,XLim,legFontSize1,markerSize,lineWidths);

% On first plot "grid on" does not produce desired result,
% thus toggle once more
grid on; grid off;
grid on;
title('$\textnormal{Time (Init. 2)}$','Interpreter','latex');

% Modified printing
fig                     = gcf;
fig.PaperPositionMode   = 'auto';
fig_pos                 = fig.PaperPosition;
fig.PaperSize           = [fig_pos(3) fig_pos(4)];

figname = [fignm1,'.pdf']; %'time_EX_CUTEST_INIT_1';

print(fig,'-dpdf',fullfile(figpath,figname));

%close ALL;

% Additional settings 
% Extended perf. profile
if setting > 0    
    
    perf_ext_fnc(ex(:,indAlg),numf(:,indAlg),mleg(indAlg),1,typesp,...
        leglocation1,XTick,XLim,legFontSize1,markerSize,lineWidths);
    
    % On first plot "grid on" does not produce desired result,
    % thus toggle once more
    grid on; grid off;
    grid on;
    title('$\textnormal{Function Calls (Init. 2)}$','Interpreter','latex');

    % Modified printing
    fig                     = gcf;
    fig.PaperPositionMode   = 'auto';
    fig_pos                 = fig.PaperPosition;
    fig.PaperSize           = [fig_pos(3) fig_pos(4)];
    figname = [fignm2,'.pdf'];

    print(fig,'-dpdf',fullfile(figpath,figname));
end

warning('on');
%close ALL;



