% 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'
    };

% define pre-processors of experiment
myPreProcessors = {'none','log',...
    'freq3bin','freq5bin','width3bin','width5bin',...
    'norm',...
    'SWReg','PCA','SFS'};

% define algorithms of experiment
myAlgorithms = {'SWReg','SLReg',...
    'CART-On','CART-Off',...
    'PLSR','PCR',...
    '1NN','ABE0',...
    'NNet'};

% define error-measures of experiment
myErrorMeasures = {'MAR','MMRE','MMER','MBRE','MIBRE','MDMRE','Pred25'};

% define how many random runs you want for each dataset
randomRunSize = size(myAlgorithms,2) * size(myPreProcessors,2);

% store size of each of the above variables
datasetSize = size(myDatasets,2);
preProcessorSize = size(myPreProcessors,2);
errorMeasureSize = size(myErrorMeasures,2);
algorithmSize = size(myAlgorithms,2);

for i = 1:size(myErrorMeasures,2)
    eval(['myErrorMeasure' num2str(i) 'LossValues = fopen(''myErrorMeasure' num2str(i) 'LossValues.txt'', ''w'');']);
    eval(['fclose(myErrorMeasure' num2str(i) 'LossValues);']);
    eval(['myErrorMeasure' num2str(i) 'WinValues = fopen(''myErrorMeasure' num2str(i) 'WinValues.txt'', ''w'');']);
    eval(['fclose(myErrorMeasure' num2str(i) 'WinValues);']);
end

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


% above code is for random mappings between methods and pre-processors
% below code is somewhat hard coded mappings
randNum1 = [];
randNum2 = [];
for i = 1:size(myPreProcessors,2)
    for j = 1:size(myAlgorithms,2)
        randNum1 = [randNum1,i];
        randNum2 = [randNum2,j];
    end
end


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
    
    eval(['load ',char(myDatasets(datasetCounter)),'.csv;']);
    eval(['dataset = ',char(myDatasets(datasetCounter)),';']);
    eval(['clear ',char(myDatasets(datasetCounter)),';']);
    clc;
    myDatasets(datasetCounter)  % print out the dataset name
    
    % define a variable to keep the results of each method
    algorithmResults = -1 * ones(randomRunSize, size(dataset,1));
    
    for runIndex = 1:(randomRunSize)
        % reload dataset to get rid of previous pre-processor effect
        eval(['load ',char(myDatasets(datasetCounter)),'.csv;']);
        eval(['dataset = ',char(myDatasets(datasetCounter)),';']);
        eval(['clear ',char(myDatasets(datasetCounter)),';']);
        
        % pick up the pre-processor on the queue
        currentPreProcessor = char(fixedPreProcessors(runIndex));
        % apply the pre-processor and get the new dataset
        dataset = applyPreProcess(dataset, currentPreProcessor);
        
        % run the next algorithm
        currentAlgorithm = char(fixedAlgorithms(runIndex));
        
        algorithmResults(runIndex,:) = algorithmRunner(dataset, currentAlgorithm, currentPreProcessor, 0); % 0 means without GAC pruning
        % algorithmResults(runIndex,:) = algorithmRunner(dataset, currentAlgorithm, currentPreProcessor, 1); % 1 means without GAC pruning
        
    end
    
    % before going to win-tie-loss calculation over all methods, we will 
    % calculate multi-methods. For that, we first find best solo-methods
    % then we take top 2,4,8,16 solo-methods and
    % take their mean, median and IRWM (by Mendes)
    % for each error measure
    winValues = [];
    lossValues = [];
    tieValues = [];
    for errorCounter = 1: size(myErrorMeasures,2) 
        tmpErrorMeasure = char(myErrorMeasures(errorCounter));
        % at this point we have the predictions of all the algorithms
        % now we can compare these results and store their loss values
        [tmpLoss tmpWin tmpTie] = orderAlgorithms(algorithmResults(1:randomRunSize,:),dataset(:,size(dataset,2)),tmpErrorMeasure,fixedAlgorithms(1:randomRunSize,:), fixedPreProcessors(1:randomRunSize,:), myDatasets(datasetCounter));
        winValues = [winValues,tmpWin];       
        lossValues = [lossValues,tmpLoss];
        tieValues = [tieValues,tmpTie];
    end
    
    
    % now save the workspace for this particular dataset
    actualsToSave = dataset(:,size(dataset,2));
    eval(['save(''workspaces\LOO\' char(myDatasets(datasetCounter)) '.mat'', ''algorithmResults'', ''actualsToSave'', ''winValues'', ''lossValues'');']);
    
end


