% define datasets of experiment
myDatasets = {
%     'cocomo81',...
%     'nasa93',...s
    'desharnais',...
    'albrecht',...
    };

% below datasets are the 10% of max variance reduced datasets
% dont forget to comment out other dataset definitions when you want to use
% reduced datasets!!!
% myDatasets = {
%     'cocomo81_reduced','cocomo81o_reduced','cocomo81e_reduced','cocomo81s_reduced',...
%     'nasa93_reduced','nasa93_center_2_reduced','nasa93_center_5_reduced',...
%     'desharnais_reduced','desharnaisL1_reduced','desharnaisL2_reduced','desharnaisL3_reduced',...
%     'sdr_reduced'...
%     'albrecht_reduced','china_reduced','finnish_reduced','maxwell_reduced'
%     };

% delete pre-existing results file
delete 'RUN_RESULTS.txt';
delete 'SUM_RESULTS.txt';

% select if there is gac pruning or not
isThereGacPruning = 1;

% define pre-processors of experiment
myPreProcessors = {'none','log','disc3bin','disc5bin','norm',...
    'SWReg'};

% define algorithms of experiment
myAlgorithms = {'SWReg','RuleInd','1NN','NNet','ABE0'};

% define error-measures of experiment
myErrorMeasures = {'MMRE'};

% define how many random runs you want for each dataset
randomRunSize = 5;

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

% delete previous file
fid=fopen('RUN_RESULTS.txt','w');
fclose(fid);

% define variables for randomSize methods
fixedPreProcessors = cell(randomRunSize,1) ;
fixedAlgorithms = cell(randomRunSize,1);
% settle the ten different algorithm&pre-processor combinations
% randNum1 = randi([1 preProcessorSize],1,randomRunSize);   % random num. for pre-proc.
% randNum2 = randi([1 algorithmSize],1,randomRunSize);   % random num. for algorithms

% above code is for random mappings between methods and pre-processors
% below code is somewhat hard coded mappings
% randNum1 = [1 2 3 4 5 6 6 6 5];
% randNum2 = [2 2 1 4 5 4 5 3 4];

% below assignments are for test purpose only
randNum1 = [1 1 1 1 1];
randNum2 = [1 2 3 4 5];

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)),';']);
    myDatasets(datasetCounter)  % print out the dataset name
    
    % define a variable to keep the loss values
    lossValues = -1 * ones(errorMeasureSize,randomRunSize);
    
    % write current dataset and error measure to output file
    fid=fopen('RUN_RESULTS.txt','a' );
    fprintf(fid, ['Dataset: ' char(myDatasets(datasetCounter))],'%s');
    % write the selected methods
    for runIndex = 1:randomRunSize
        % write pre-processor
        fprintf(fid, [',' char(fixedPreProcessors(runIndex))]);
        % write algorithm
        fprintf(fid, ['-' char(fixedAlgorithms(runIndex))]);
    end
    fprintf(fid, ['\n']);    % append a new line at the end of the line
    % close file
    fclose(fid);
    
    % for each error measure
    for errorCounter = 1: size(myErrorMeasures,2)
        currentErrorMeasure = char(myErrorMeasures(errorCounter));
        % open file to append
        fid=fopen('RUN_RESULTS.txt','a' );        % write error measure to file
        fprintf(fid,[char(myErrorMeasures(errorCounter)) ',']);
        % close file
        fclose(fid);
        
        % define a variable to keep the results of each method
        algorithmResults = -1 * ones(size(algorithmSize,1), 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, currentErrorMeasure, currentPreProcessor, isThereGacPruning);
        end
        
        % at this point we have the predictions of all the algorithms
        % now we can compare these results and store their loss values
        lossValues(errorCounter,:) = orderAlgorithms(algorithmResults,dataset(:,size(dataset,2)),currentErrorMeasure,fixedAlgorithms, fixedPreProcessors, myDatasets(datasetCounter));
        
    end
    % open file to append
    fid=fopen('RUN_RESULTS.txt','a' );        % write sum to file
    fprintf(fid,'SUM');
    sums = sum(lossValues);
    for tempCounter = 1:randomRunSize
        fprintf(fid, [',' num2str(sums(tempCounter))]);
    end
    fprintf(fid,'\n');
    
    fprintf(fid,'MEDIAN');
    medians = median(lossValues);
    for tempCounter = 1:randomRunSize
        fprintf(fid, [',' num2str(medians(tempCounter))]);
    end
    fprintf(fid,'\n\n');  % put a blank line for the next dataset
    % close fileS
    fclose(fid);
end


