% 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'
    };
datasetSize = size(myDatasets,2);

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

% 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 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

% write current dataset and error measure to output files
pred50Values=fopen('pred50Values.txt','w' );
fprintf(pred50Values, 'Pred50 Values','%s');

% write the selected methods
for runIndex = 1:randomRunSize
    % write pre-processor
    fprintf(pred50Values, [',' char(fixedPreProcessors(runIndex))]);
    % write algorithm
    fprintf(pred50Values, ['-' char(fixedAlgorithms(runIndex))]);
end
 fprintf(pred50Values, '\n');
 fclose(pred50Values);
 
% 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));
    Pred50Results = -1 * ones(size(algorithms,1),1);
    
    pred50Values=fopen('pred50Values.txt','a' );
    fprintf(pred50Values, char(myDatasets(datasetCounter)),'%s');
    % process each algorithm's results to find the MRE then Pred50 values
    for i = 1:size(algorithms,1)
       MREResults(i,:) = abs(actuals' - predictions(i,:))./actuals';
       Pred50Results(i) = (size(find(MREResults(i,:)<=0.5),2)*100)/size(actuals,1);
       fprintf(pred50Values, [',' num2str(Pred50Results(i))]');
    end
    fprintf(pred50Values,'\n');
    fclose(pred50Values);
    
end
