% define datasets of experiment
myDatasets = {
    'desharnaisL1','desharnais','desharnaisL2','desharnaisL3',...
    'cocomo81s','cocomo81','cocomo81o','cocomo81e',...
    'nasa93','nasa93_center_1','nasa93_center_2','nasa93_center_5',...
    'sdr'...
    'albrecht',...
    'finnish','kemerer','maxwell','miyazaki94','telecom1','china'
    };

% define pre-processors of experiment
myPreProcessors = {'none','log','3bin','5bin','norm',...
    'SWR'};

% define algorithms of experiment
myAlgorithms = {'SWR','RuleInd','1NN','NNet','ABE0'};

% below code is somewhat hard coded mappings
randNum1 = [6 1	1	2	6	4	1	1	1	6	2	3	2	5	2	5	3	6	3 3];
randNum2 = [4 5	2	4	3	1	3	4	1	2	3	1	5	1	1	2	3   5	4 2];
% define how many random runs you want for each dataset
randomRunSize = size(randNum1,2);

% define variables for randomSize methods
fixedPreProcessors = cell(randomRunSize,1) ;
fixedAlgorithms = cell(randomRunSize,1);

for runIndex = 1:randomRunSize
    % randomly pick up pre-processor
    fixedPreProcessors(runIndex) = (myPreProcessors(randNum1(runIndex)));
    % randomly pick up an algorithm
    fixedAlgorithms(runIndex) = (myAlgorithms(randNum2(runIndex)));
end

% for each dataset in the study
for datasetCounter = 1:datasetSize
    datasetName = char(myDatasets(datasetCounter)); % define dataset name
    
    % load dataset
    eval(['load ',char(myDatasets(datasetCounter)),'.csv;']);
    eval(['dataset = ',char(myDatasets(datasetCounter)),';']);
    eval(['clear ',char(myDatasets(datasetCounter)),';']);
    
    actuals = dataset(:,size(dataset,2));  % get actual effort values
    eval(['predictions = ' char(myDatasets(datasetCounter)) 'Results;']);
    algorithms = fixedAlgorithms;
    preprocessors = fixedPreProcessors;
    % MRE = abs(actual - predicted)/actual and MMRE is the mean of all MREs
    
    % define a variable to keep the MMRE results for each algorithm
    MREResults = -1 * ones(size(algorithms,1),size(actuals,1));
    
    % process each algorithm's results to find the MRE values
    for i = 1:size(algorithms,1)
       MREResults(i,:) = abs(actuals' - predictions(i,:))./actuals';
    end
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % this part is for plotting purposes only
    
    MREResultsToPlot = MREResults;  % copy the MRE results to protect from changes
    % get the indices of the sorted MRE's for the method that had the lowest
    % sum MRE
    
    % then apply this indexing to all the methods
    toSave = figure('units','normalized','outerposition',[0 0 1 1],'visible','off');;
    set(gcf, 'Position', get(0,'Screensize')); 
    hold all;
    % axis([0 size(predictions,2) 0 10]);
    set(gca,'FontSize', 12,'FontName','Times New Roman');
    title(char(datasetName));
    for i = 1:size(algorithms,1)
        MREResultsToPlot(i,:) = MREResultsToPlot(i,:);
        hold all;
        subplot(5,4,i);
        eval(['h1' num2str(i) ' = semilogy(MREResultsToPlot(i,:));']);
        eval(['set(h1' num2str(i) ',  ''LineStyle'', ''-'', ''LineWidth'', 1.0, ''Color'', ''Black'');']);
        eval(['set(h1' num2str(i) ', ''Marker'', ''o'', ''MarkerFaceColor'', [0 0 0], ''MarkerEdgeColor'', [0 0 0], ''MarkerSize'', 3);']);
        legendStr = [ char(preprocessors(i)) '-' char(algorithms(i))];
%         legControl = legend(legendStr,'location', 'SouthWest');
        set(legControl,'FontSize', 8,'FontName','Times New Roman');
        hold off;
    end
    hold off;
    print('-dpng', [char(datasetName) '.png']);
    close(toSave);
    
    % at this point we drew and saved the mre plot, now we will plot 25%
    % and 75% percentile and medians
    boxPlotLabels = cell(randomRunSize,1);
    boxPlotLabels(1) = cellstr([char(preprocessors(i)) '-'  char(algorithms(i))]);
    for i = 2:size(algorithms,1)
        boxPlotLabels(i) = cellstr([char(preprocessors(i)) '-' char(algorithms(i))]);
    end
    set(gca,'FontSize',16,'FontName','Times New Roman');
    [percValues] = prctile(MREResultsToPlot', [25 75]);
    boxPlotToSave = boxplot(MREResultsToPlot',...
        'labels', boxPlotLabels,...
        'labelorientation','inline',...
        'datalim',[0 (max(percValues(2,:))+8)],...
        'symbol','');
    
    h = findobj(gca);
    children = get(h(2),'children');
    for i = 1:length(boxPlotLabels)
        set(children(i), 'FontSize', 10, 'FontName','Times New Roman');%To change label properties
    end
    saveas(h(2), [char(datasetName) 'BoxPlot.png']); 
    close all;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
end
