clear;clc;

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


datasetSize = size(myDatasets,2);
for datasetCounter = 1:datasetSize
    % sum of win values for each error measure separately, over all datasets
    allWinValues3Way = -1 * ones(1,90);
    allLossValues3Way = -1 * ones(1,90);
    allWinMinusLossValues3Way = -1 * ones(1,90);
    allWinValuesLOO = -1 * ones(1,90);
    allLossValuesLOO = -1 * ones(1,90);
    allWinMinusLossValuesLOO = -1 * ones(1,90);
    
    stabilityLOOWin = -1 * ones(1,90);
    stabilityLOOLoss = -1 * ones(1,90);
    stabilityLOOWinLoss = -1 * ones(1,90);
    stability3WayWin = -1 * ones(1,90);
    stability3WayLoss = -1 * ones(1,90);
    stability3WayWinLoss = -1 * ones(1,90);
    
    % define theses variables
    orderInIcse = [57	58	3	4	12	13	66	67	84	85	70	16	71	89	77	69	5	88	78	6	75	76	35	68	83	61	7	87	86	30	31	48	49	62	73	8	64	82	17	55	1	26	80	39	40	81	44	9	46	52	2	53	90	59	34	72	65	56	25	21	22	79	37	50	10	15	14	41	43	51	60	42	33	28	38	24	47	23	32	11	19	29	54	63	45	18 27 36 20 74];
    myOrigin = [1:90];
    
    % load win and loss values for 3 way
    eval(['load(''workspaces\3Way\',char(myDatasets(datasetCounter)),'.mat'');']);
    winValues3Way = winValues';
    lossValues3Way = lossValues';
    winMinusLossValues3Way = (winValues-lossValues)';
    % then sum them up
    allWinValues3Way = sum(winValues3Way);
    allLossValues3Way = sum(lossValues3Way);
    allWinMinusLossValues3Way = sum(winMinusLossValues3Way);
    allWinValues3Way = (allWinValues3Way(orderInIcse));
    allLossValues3Way = (allLossValues3Way(orderInIcse));
    allWinMinusLossValues3Way = (allWinMinusLossValues3Way(orderInIcse));
    
    % now do the same for LOO
    % load win and loss values
    eval(['load(''workspaces\LOO\',char(myDatasets(datasetCounter)),'.mat'');']);
    winValuesLOO = winValues';
    lossValuesLOO = lossValues';
    winMinusLossValuesLOO = (winValues-lossValues)';
    % then sum them up
    allWinValuesLOO = sum(winValuesLOO);
    allLossValuesLOO = sum(lossValuesLOO);
    allWinMinusLossValuesLOO = sum(winMinusLossValuesLOO);
    allWinValuesLOO = (allWinValuesLOO(orderInIcse));
    allLossValuesLOO = (allLossValuesLOO(orderInIcse));
    allWinMinusLossValuesLOO = (allWinMinusLossValuesLOO(orderInIcse));
    
    % now start calculating the absolute rank deltas
    winIndices = []; lossIndices = []; winMinusLossIndices = [];
    [C winIndices] = sort(allWinValuesLOO, 'descend');
    stabilityLOOWin = abs(winIndices-myOrigin);
    [C lossIndices] = sort(allLossValuesLOO, 'ascend');
    stabilityLOOLoss = abs(lossIndices-myOrigin);
    [C winMinusLossIndices] = sort(allWinMinusLossValuesLOO, 'descend');
    stabilityLOOWinLoss = abs(winMinusLossIndices-myOrigin);
    
    winIndices = []; lossIndices = []; winMinusLossIndices = [];
    [C winIndices] = sort(allWinValues3Way, 'descend');
    stability3WayWin = abs(winIndices-myOrigin);
    [C lossIndices] = sort(allLossValues3Way, 'ascend');
    stability3WayLoss = abs(lossIndices-myOrigin);
    [C winMinusLossIndices] = sort(allWinMinusLossValues3Way, 'descend');
    stability3WayWinLoss = abs(winMinusLossIndices-myOrigin);
    
    h=figure;
    subplot(3,1,1); hold on; plot(stability3WayWin,'r');plot(stabilityLOOWin,'g'); hold off;
    title('Win');ylabel('Abs. Rank Delta');legend('3Way','LOO');
    subplot(3,1,2); hold on; plot(stability3WayLoss,'r');plot(stabilityLOOLoss,'g'); hold off;
    title('Loss');ylabel('Abs. Rank Delta');legend('3Way','LOO');
    subplot(3,1,3); hold on; plot(stability3WayWinLoss,'r');plot(stabilityLOOWinLoss,'g'); hold off;
    title('Win-Loss');ylabel('Abs. Rank Delta');legend('3Way','LOO');
    eval(['print(h,''-dpng'', ''plots\absRanks\perDataset\' char(char(myDatasets(datasetCounter))) '-Abs-Rank.png'');']);
    close all;
    
    
end
