clear;
clc;

% read original graph + missing node indexes
% loop 
%       read predict graph + restore graph which is the partal graph
%       read the nodes permutation 
%       calc GED vice the orignal graph
% end
%graphDist = [];
rootDir = 'C:\Users\Iraj\Desktop\DML\' ;
datasetDir = strcat(rootDir,'produced_graphs/'); %' '; % sigal 28.8.12 
resultsDir = strcat(rootDir,'results_ged/');
file1Iter = []; % networks options [1:1:10] - selection iter
file2Iter = [0]; % networks options [1:1:10] - network iter
missIter = [5]; % missing info options [1:1:5] - miss perc iter
parmIter = ['a' 'b' 'c']; %run parameters options ['a' 'b' 'c']
numNodes = 450;
run=0;
for f1 = file1Iter        % loop over networks
    for f2 = file2Iter        % loop over networks
        filePrefix = sprintf('testgraph_%d_%d',numNodes, f2);
        origFile = sprintf('%s.mat_%d_orig_edges.txt',filePrefix, f1);
        for i = missIter        % loop over missing info options 
            for j = parmIter         % loop over run parameters

                run = run + 1;
                
                missFile = sprintf('%s%s.mat_%d_%d_miss.txt', datasetDir, filePrefix, f1, i);

                permFile = sprintf('%s%s_%d_%d%s.perm.orig', resultsDir, filePrefix, f1, i, j);

                restoreFile = sprintf('%s_%d_%d%s_restored.txt', filePrefix, f1, i, j);
                predictFile = sprintf('%s_%d_%d%s_predicted.txt', filePrefix, f1, i, j);


                graph0 = LoadAsciiGraph(datasetDir, origFile, numNodes);
                graph1 = LoadAsciiGraph(resultsDir, restoreFile, numNodes);
                graph2 = LoadAsciiGraph(resultsDir, predictFile, numNodes);

                permNodes = load(permFile);
                missNodes = load(missFile)'; % indexes already from 1

                permNodes = permNodes + 1; % fix indexes for matlab (add 1 to all entries)
                
                origNodes = size(permNodes,1);
                numMissingNodes = size(missNodes,2);
                numNgrNodes = numNodes - origNodes - numMissingNodes;
                ngrNodesLastInx = origNodes; 
                missNodeLastInx = ngrNodesLastInx + numNgrNodes;
                
                nodes_to_keep = zeros(1, numNodes);
                
                % add original missing nodes and edges to restore graph
                for node = missNodes
                    temp = graph0(node,:); % get row from original graph
                    ind = find(temp); % get edges indexes
                    missNodeLastInx = missNodeLastInx + 1; % next node index (append as last)
                    nodeInx = missNodeLastInx;
                    
                    for other = ind
                        [otherInx, permNodes] = GetNodeInxPermutation(other, permNodes);
                        
                        % add both edges
                        graph1(nodeInx,otherInx) = 1;
                        graph1(otherInx,nodeInx) = 1;

                        % kep indexes
                        nodes_to_keep(otherInx) = 1;
                        nodes_to_keep(nodeInx) = 1;
                    end
                end
                
                graph3 = graph2;
                % delete predict edges between ALL predict nodes in the predict graph
                for n1 = origNodes+1:numNodes
                    for n2 = n1:numNodes
                        graph3(n1,n2) = 0;
                        graph3(n2,n1) = 0;
                    end
                end
                
                graph4 = graph3;
                % duplicate edges to make graph undirected
                for n1 = origNodes+1:numNodes
                    for n2 = 1:origNodes
                        if graph4(n1,n2) == 1
                            graph4(n2,n1) = 1;
                        elseif graph4(n2,n1) == 1
                            graph4(n1,n2) = 1;
                        end
                    end
                end

%                 save(strcat(resultsDir,'graph1'),'graph1');
%                 save(strcat(resultsDir,'graph2'),'graph2');
%                 graph1 = sparse(graph1);
%                 graph2 = sparse(graph2);
                 
                nodes_to_remove = ~nodes_to_keep;
                indices_to_remove = find(nodes_to_remove);
                indices_to_keep = find(nodes_to_keep);

                graph1(indices_to_remove,:) = [];
                graph1(:,indices_to_remove) = [];
                graph2(indices_to_remove,:) = [];
                graph2(:,indices_to_remove) = [];
                graph3(indices_to_remove,:) = [];
                graph3(:,indices_to_remove) = [];
                graph4(indices_to_remove,:) = [];
                graph4(:,indices_to_remove) = [];
                
                fprintf('Starting GraphEditDistance (%d %d %d %s) %d ...\n',f2,f1,i,j,rem(now,1));               
                bestDist1 = 3333; %GraphEditDistance(graph1, graph2, numMissingNodes+numNgrNodes);
                bestDist2 = 3333; %GraphEditDistance(graph1, graph3, numMissingNodes+numNgrNodes);
                bestDist3 = GraphEditDistance(graph1, graph4, numMissingNodes+numNgrNodes);
                fprintf('Completed GraphEditDistance (%d %d %d %s) %d.\n',f2,f1,i,j,rem(now,1));               
                
                graphDist(run).file = strcat(filePrefix,'.mat');
                graphDist(run).f1 = f1;
                graphDist(run).f2 = f2;
                graphDist(run).miss = i;
                graphDist(run).missNum = numMissingNodes;
                graphDist(run).parms = j;
                graphDist(run).bestDist1 = bestDist1;
                graphDist(run).bestDist2 = bestDist2;
                graphDist(run).bestDist3 = bestDist3;
                
            end
        end
    end
end

dateNow = clock;
dateNow = strcat(num2str(dateNow(1)),'_',num2str(dateNow(2)),'_', num2str(dateNow(3)),'_', num2str(dateNow(4)), num2str(dateNow(5)),'_', num2str(dateNow(6)));

% dump graphDist as text
outFile = sprintf('%sDumpGraphDist_1.txt',resultsDir);
fileID = fopen(outFile,'w');
fprintf('%d', fileID);
for r = graphDist
    fprintf(fileID,'\t%s',r.file);
    fprintf(fileID,'\t%d',r.f2);
    fprintf(fileID,'\t%d',r.f1);
    fprintf(fileID,'\t%d',r.miss);
    fprintf(fileID,'\t%d',r.missNum);
    fprintf(fileID,'\t%s',r.parms);
    fprintf(fileID,'\t%3.1f',r.bestDist1);
    fprintf(fileID,'\t%3.1f',r.bestDist2);
    fprintf(fileID,'\t%3.1f',r.bestDist3);
    fprintf(fileID,'\n');
end
fclose(fileID);

% dump all parameters
outFile = sprintf('%sGraphDist_%s.mat',resultsDir, dateNow);
save(outFile);

fprintf('Completed RunExperiment.\n');
