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

% 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, (10*size(dataset,1)));
    actual10by10values = [];
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%% ALGORITHMS AND PRE-PROCESSORS RUN BELOW %%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for runIndex = 1:(randomRunSize)
        runIndex
        
        % pick up the pre-processor on the queue
        currentPreProcessor = char(fixedPreProcessors(runIndex));
        % run the next algorithm
        currentAlgorithm = char(fixedAlgorithms(runIndex));
                
        % below variable keeps track of all instances in a 10 by 3 way
        instanceTracker = 0;
        
        %%%%%%%%%%% START 10 BY 10
        
        if runIndex == 1
            % randomize dataset and divide into 3 datasets
            for timesCounter = 1:10
                [fold1, fold2, fold3, fold4, fold5, fold6, fold7, fold8, fold9, fold10] = divideInto10(dataset);
                for foldCol = 1:10
                    eval(['fold' num2str(timesCounter) num2str(foldCol) ' = fold' num2str(foldCol) ';']);
                    eval(['actual10by10values = [actual10by10values fold' num2str(foldCol) '(:,size(fold' num2str(foldCol) ',2))''];']); 
                end
            end
        end
        
        % store algorithm results temporarily
        myTempResults = [];
        
        for timesCounter = 1:10 % since we are doing a ten by 3 cross validation
            
            for foldCol = 1:10
                eval(['fold' num2str(foldCol) ' = fold' num2str(timesCounter) num2str(foldCol) ';']);
            end
            
            % apply the pre-processor and get the new folds
            processedFolds = applyPreProcess([fold1;fold2;fold3;fold4;fold5;fold6;fold7;fold8;fold9;fold10], currentPreProcessor);
            
            tmpStartIndex = 1;
            tmpEndIndex = size(fold1,1);
            for z = 1:10
                if z == 10
                    eval(['fold' num2str(z) '=processedFolds(' num2str(tmpStartIndex) ':' num2str(tmpEndIndex) ',:);']);
                else
                    eval(['fold' num2str(z) '=processedFolds(' num2str(tmpStartIndex) ':' num2str(tmpEndIndex) ',:);']);
                    tmpStartIndex = tmpEndIndex + 1;
                    eval(['tmpEndIndex = tmpEndIndex + size(fold' num2str(z+1) ',1);']);
                end
            end
                        
            % for each fold do the following
            for foldCounter = 1:10
                if foldCounter == 1
                    testInstances = fold1; trainSet10by10 = [fold2;fold3;fold4;fold5;fold6;fold7;fold8;fold9;fold10];
                elseif foldCounter == 2
                    testInstances = fold2; trainSet10by10 = [fold1;fold3;fold4;fold5;fold6;fold7;fold8;fold9;fold10];
                elseif foldCounter == 3
                    testInstances = fold3; trainSet10by10 = [fold1;fold2;fold4;fold5;fold6;fold7;fold8;fold9;fold10];
                elseif foldCounter == 4
                    testInstances = fold4; trainSet10by10 = [fold1;fold2;fold3;fold5;fold6;fold7;fold8;fold9;fold10];
                elseif foldCounter == 5
                    testInstances = fold5; trainSet10by10 = [fold1;fold2;fold3;fold4;fold6;fold7;fold8;fold9;fold10];
                elseif foldCounter == 6
                    testInstances = fold6; trainSet10by10 = [fold1;fold2;fold3;fold4;fold5;fold7;fold8;fold9;fold10];
                elseif foldCounter == 7
                    testInstances = fold7; trainSet10by10 = [fold1;fold2;fold3;fold4;fold5;fold6;fold8;fold9;fold10];
                elseif foldCounter == 8
                    testInstances = fold8; trainSet10by10 = [fold1;fold2;fold3;fold4;fold5;fold6;fold7;fold9;fold10];
                elseif foldCounter == 9
                    testInstances = fold9; trainSet10by10 = [fold1;fold2;fold3;fold4;fold5;fold6;fold7;fold8;fold10];
                else
                    testInstances = fold10; trainSet10by10 = [fold1;fold2;fold3;fold4;fold5;fold6;fold7;fold8;fold9];
                end
                
                myTempResults = [myTempResults algorithmRunner10By3(testInstances, trainSet10by10, currentAlgorithm, currentPreProcessor, 0)]; % 0 means without GAC pruning
                
            end
        end
        
%         % now record the temp results
%         algorithmResults(runIndex,:) = tmpAlgorithmResults;
    algorithmResults(runIndex,:) = myTempResults;
    end
    
    % 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,:),actual10by10values',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
    eval(['save(''workspaces\10Way\' char(myDatasets(datasetCounter)) '.mat'', ''algorithmResults'', ''actual10by10values'',''winValues'', ''lossValues'');']);
    
end


