% 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 mixture of expert methods there will be
MoESize = 12;

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

% 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

% define variables for randomSize methods
fixedPreProcessors = cell(randomRunSize+MoESize,1) ;
fixedAlgorithms = cell(randomRunSize+MoESize,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
moePreProcessors = {'Top2','Top4','Top8','Top16'};
moeAlgorithms = {'MoEMean','MoEMedian','MoEIrwm'};
fixedPreProcessors(runIndex+1) = moePreProcessors(1);
fixedAlgorithms(runIndex+1) = moeAlgorithms(1);
fixedPreProcessors(runIndex+2)= moePreProcessors(2);
fixedAlgorithms(runIndex+2) = moeAlgorithms(1);
fixedPreProcessors(runIndex+3) = moePreProcessors(3);
fixedAlgorithms(runIndex+3) = moeAlgorithms(1);
fixedPreProcessors(runIndex+4) = moePreProcessors(4);
fixedAlgorithms(runIndex+4) = moeAlgorithms(1);
fixedPreProcessors(runIndex+5) = moePreProcessors(1);
fixedAlgorithms(runIndex+5) = moeAlgorithms(2);
fixedPreProcessors(runIndex+6)= moePreProcessors(2);
fixedAlgorithms(runIndex+6) = moeAlgorithms(2);
fixedPreProcessors(runIndex+7) = moePreProcessors(3);
fixedAlgorithms(runIndex+7) = moeAlgorithms(2);
fixedPreProcessors(runIndex+8) = moePreProcessors(4);
fixedAlgorithms(runIndex+8) = moeAlgorithms(2);
fixedPreProcessors(runIndex+9) = moePreProcessors(1);
fixedAlgorithms(runIndex+9) = moeAlgorithms(3);
fixedPreProcessors(runIndex+10)= moePreProcessors(2);
fixedAlgorithms(runIndex+10) = moeAlgorithms(3);
fixedPreProcessors(runIndex+11) = moePreProcessors(3);
fixedAlgorithms(runIndex+11) = moeAlgorithms(3);
fixedPreProcessors(runIndex+12) = moePreProcessors(4);
fixedAlgorithms(runIndex+12) = moeAlgorithms(3);


% for each dataset in the study
for datasetCounter = 1:datasetSize
    datasetName = char(myDatasets(datasetCounter)); % define dataset name
    
    % open and close file for current dataset
    mreValuesToWrite=fopen([datasetName 'mreValuesToWrite.txt'],'w' );
    fclose(mreValuesToWrite);
        
    % 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;']); % get predictions
    algorithms = fixedAlgorithms;
    preprocessors = fixedPreProcessors;
    
    % define a variable to keep the MMRE results for each algorithm
    MREResults = -1 * ones(size(algorithms,1),size(actuals,1));
    mreValuesToWrite=fopen([datasetName 'mreValuesToWrite.txt'],'a' );
    % process each algorithm's results to find the MRE values
    for i = 1:size(algorithms,1)
        
        % write pre-processor
        fprintf(mreValuesToWrite, char(fixedPreProcessors(i)));
        % write algorithm
        fprintf(mreValuesToWrite, ['-' char(fixedAlgorithms(i))]);
       
        fprintf(mreValuesToWrite, char(myDatasets(datasetCounter)),'%s');
        MREResults(i,:) = abs(actuals' - predictions(i,:))./actuals';
        for j = 1:size(predictions(i,:),2)
            fprintf(mreValuesToWrite, [',' num2str(MREResults(i,j))]);
        end
        fprintf(mreValuesToWrite,'\n');
    end
    
    fclose(mreValuesToWrite);
    
end