% clearn workspace and command window
clear;clc;

% define datasets of experiment
myDatasets = {
    'cocomo81',...
    'desharnais',...
    'nasa93',...
    'sdr',...
    'albrecht',...
    'finnish',...
    'kemerer',...
    'maxwell',...
    'miyazaki94',...
    'telecom1',...
    'china'...
    };

% for each dataset in the study
datasetSize = size(myDatasets,2);
for datasetCounter = 1:datasetSize
    
    eval(['load ',char(myDatasets(datasetCounter)),'.csv;']);
    eval(['nowDataset = ',char(myDatasets(datasetCounter)),';']);
    eval(['clear ',char(myDatasets(datasetCounter)),';']);
    clc;
    myDatasets(datasetCounter)  % print out the dataset name
    
    % get the row and column number (without dependent variable)
    n = size(nowDataset,1);
    p = size(nowDataset,2) - 1; % exclude the effort value, i.e. dependent var.
    
    % log the dataset
    logNowDataset = [log(nowDataset(:,1:p)), nowDataset(:,(p+1))];
    
    % define test set size; the remaining instances will be the train
    testSize = ceil(n/10);
    
    % define overall actuals and predictions
    myOverallTestActuals = [];
    myOverallPassivePredictions = [];
    myOverallActivePredictions = [];
    myOverallRandomPredictions = [];
    myOverallCartPredictions = [];
    datasetActuals = logNowDataset(:,p+1);
    
    % calculate the index matrix that is the sorted distance matrix
    [tmpMatrix myDistMatrix] = calculateMyDistanceMatrix(logNowDataset);
        
    for experimentCounter = 1:10
        
        % pick up testSize-many instances at random
        trainIndices = 1:n;
        testIndices = [];
        for testPicker = 1:testSize
            tmpRnd = randi([1 size(trainIndices,2)],1,1);
            testIndices = [testIndices,trainIndices(tmpRnd)];
            trainIndices(tmpRnd) = [];
        end
        
        testActuals = logNowDataset(testIndices,p+1)'; % store actual effort values
        myOverallTestActuals = [myOverallTestActuals,testActuals];
        trainActuals = logNowDataset(trainIndices,p+1)';
        
        % now start getting the predictions from active and normal k
        % first get the predictions of k=1 with all effort known
        predictionsPassive = -1 * ones(1,testSize);
        predictionsRandom = -1 * ones(1,testSize);
        predictionsCart = -1 * ones(1,testSize);
        for i = 1:testSize
            relatedIndices = tmpMatrix(:,testIndices(i));
            relatedIndices = relatedIndices(trainIndices);
            [C closestGuyIndex] = min(relatedIndices);
            predictionsPassive(i) = trainActuals(closestGuyIndex);
            predictionsRandom(i) = trainActuals(randi([1 size(trainActuals,2)],1,1));
        end
        % store passive prediction
        myOverallPassivePredictions = [myOverallPassivePredictions,predictionsPassive];
        % store random precitons. random predictions will form the baseline
        myOverallRandomPredictions = [myOverallRandomPredictions,predictionsRandom];
        
        % now get the predictions for k=1, with different numbers of labeled
        % instances from 1 to n-1
        popIndex = findPopularity(tmpMatrix); % order instances w.r.t. popularity
        popIndex = popIndex(trainIndices);    % get pop indices of train instances
        predictionsActive = -1 * ones((n-testSize),testSize);
        for i = 1:(n-testSize)
            predictionsActive(i,:) = activePredict(datasetActuals,tmpMatrix,popIndex,i,testIndices,myDistMatrix);
        end
        myOverallActivePredictions = [myOverallActivePredictions,predictionsActive];
    end
    
    % now we will plot the results w.r.t. different error measures
    mdmrePassive = ones(1,(n-testSize)) * median(abs(myOverallTestActuals - myOverallPassivePredictions)./myOverallTestActuals);
    mdmreActive = -1 * ones(1,(n-testSize));
    for i = 1:(n-testSize)
        mdmreActive(i) = median(abs(myOverallTestActuals - myOverallActivePredictions(i,:))./myOverallTestActuals);
    end
    close all;
    h = figure;
    hold on;
    axis([0 (n-testSize) 0 max(mdmreActive) ]);
    plot(1:(n-testSize),mdmrePassive,'o-',1:(n-testSize),mdmreActive,'*-');
    legend('Passive 1NN','Active 1NN');
    xlabel('Number of labeled instances');ylabel('MdMRE');title(char(myDatasets(datasetCounter)));
    hold off;
    eval(['print(h,''-dpng'', ''lib\',char(myDatasets(datasetCounter)),'.png'');']);
    hold off;close all;
end