

clearvars -except tstart
close all

for id_model = [3 4]
    switch id_model
        case 3
            ModelLabel = 'CB';
        case 4
            ModelLabel = 'FJ';
    end

    load([ ModelLabel '_InitialNetworks_100_agents.mat'], 'InteractionNetworks')

    overwrite_nertworks = 0;

    for id_network = 1:size(InteractionNetworks,2)
        InteractionNetwork = InteractionNetworks{id_network};
    
        if isequal(ModelLabel, 'CB')
            if sum(diag(double(InteractionNetwork<0))) > 0
                overwrite_nertworks = 1;
                % If one of the diagonal elements is negative
                disp([ num2str(id_network) ' Ups 1'])
                for id_agent = 1:size(InteractionNetwork,1)
                    InteractionNetwork(id_agent,id_agent) = 1;
                end
                disp([ num2str(id_network) '  modified'])
            end
            InteractionNetworks{id_network} = InteractionNetwork;
        end
    
        if isequal(ModelLabel, 'FJ')
            if sum(sum(InteractionNetwork,2)-ones(size(InteractionNetwork,1),1))>1e-10
                % If the adjacency matrix is not row-stochastic
                disp([ num2str(id_network) ' Ups 2'])
            end
            if sum(sum(double(InteractionNetwork<0))) > 0
                % If some of the elements is negative
                disp([ num2str(id_network) ' Ups 3'])
            end
        end
       
    end

    if overwrite_nertworks
        disp([ 9 'Networks of the ' ModelLabel ' model, overwritten' ])
        load([ ModelLabel '_InitialNetworks_100_agents.mat'], 'NetworkTopology', 'time_stamp')
        save([ ModelLabel '_InitialNetworks_100_agents.mat'], 'InteractionNetworks', 'NetworkTopology', 'time_stamp')
    end

end


% All these metrics are topological, and since all the topologies are the
% same for all models, it is independent of the model

% Color_1 = [1 1 0];
% Color_2 = [1 0 1];
% Color_3 = [0 1 1];
% Color_4 = [1 0.5 0.5];


Results = nan*ones(4,10);
row_counter = 0;

% 1 %  1,  2,  3,  4,  5 -> Topology num 1 ~ Complete graph
% 2 %  6,  7,  8,  9, 10 -> Topology num 2 ~ Directed ring 1
% 3 % 11, 12, 13, 14, 15 -> Topology num 3 ~ Directed ring 2
% 4 % 16, 17, 18, 19, 20 -> Topology num 4 ~ Undirected ring
% 5 % 21, 22, 23, 24, 25 -> Topology num 5 ~ Small-World
% 6 % 26, 27, 28, 29, 30 -> Topology num 6 ~ Random network
% 7 % 31, 32, 33, 34, 35 -> Topology num 7 ~ Torus lattice
% 8 % 36, 37, 38, 39, 40 -> Topology num 8 ~ Scale-Free
% 9 % 41, 42, 43, 44, 45 -> Topology num 9 ~ Small-World

LocalTopologies = [13 38 23 3];
for id_network = LocalTopologies
    row_counter = row_counter + 1;

    InteractionNetwork = InteractionNetworks{id_network};
    InteractionNetwork = double(InteractionNetwork>0); % Create the topology 

    % Average path length
    % diameter
    [Av_PathLength, num_cc, num_w_cc, diameter] = AveragePathLenght(InteractionNetwork);
    [ClusterCoeff, LocalCoeffs] = ClusterCoefficient(InteractionNetwork);

    clustering_variance = round(var(LocalCoeffs),10);

    num_ed = sum(sum(abs(InteractionNetwork)));

    out_degrees = sum(abs(InteractionNetwork),1);
    out_degree_var = round(var(out_degrees),20);
    out_degree_mean = mean(out_degrees);

    in_degrees = transpose(sum(abs(InteractionNetwork),2));
    in_degree_var = round(var(in_degrees),20);
    in_degree_mean = mean(in_degrees);
    
    BidirectionalIndex = BidirectionalCoefficientbyDefinition(InteractionNetwork);

    % Results= 

    Results(row_counter,1) = Av_PathLength; % Average path length
    Results(row_counter,2) = diameter; % Diameter

    Results(row_counter,3) = ClusterCoeff; % Clustering coefficient
    Results(row_counter,4) = clustering_variance; % Clustering variance

    Results(row_counter,5) = num_ed; % Number of edges

    Results(row_counter,7) = out_degree_var; % Out degree variance
    Results(row_counter,6) = out_degree_mean; % Out degree mean

    Results(row_counter,9) = in_degree_var; % In degree variance
    Results(row_counter,8) = in_degree_mean; % In degree mean

    Results(row_counter,10) = BidirectionalIndex; % Bidirectional index

end

MetricNames = {'Digraph', 'APL', 'D', '$\overline{CC}$', '$\sigma(CC)$', '\#E', ...
               '$\overline{\delta^{out}}$', '$\sigma(\delta^{out})$', ...
               '$\overline{\delta^{in}}$', '$\sigma(\delta^{in})$', 'BC'};

Explanations = ['Average Path Length (APL), Diameter (D), ' ...
                'Mean clustering ($\overline{CC}$), Clustering variance ($\sigma(CC)$), ', ...
                'Number of edges (\#E), Mean out-degree ($\overline{\delta^{out}}$), '...
                'Out-degree variance ($\sigma(\delta^{out})$), Mean in-degree ($\overline{\delta^{in}}$), ' ...
                'In-degree variance ($\sigma(\delta^{in})$), and Bidirectional Coefficient (BC).'];


% Topology_name = {'Directed ring', 'Scale-Free', 'Small-World', 'Complete'};

% 1 %  1,  2,  3,  4,  5 -> Topology num 1 ~ Complete graph
% 2 %  6,  7,  8,  9, 10 -> Topology num 2 ~ Directed ring 1
% 3 % 11, 12, 13, 14, 15 -> Topology num 3 ~ Directed ring 2
% 4 % 16, 17, 18, 19, 20 -> Topology num 4 ~ Undirected ring
% 5 % 21, 22, 23, 24, 25 -> Topology num 5 ~ Small-World
% 6 % 26, 27, 28, 29, 30 -> Topology num 6 ~ Random network
% 7 % 31, 32, 33, 34, 35 -> Topology num 7 ~ Torus lattice
% 8 % 36, 37, 38, 39, 40 -> Topology num 8 ~ Scale-Free
% 9 % 41, 42, 43, 44, 45 -> Topology num 9 ~ Small-World

Topology_name = cell(1,45);
for id_col = 1:45
    Topology_name{id_col} = DigraphLabel(id_col, 3); 
end


fileID = fopen('DigraphInformation.tex', 'w');
fprintf(fileID,'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');

fprintf(fileID,'%s','\begin{table}');fprintf(fileID,'\n');
fprintf(fileID,'%s','\centering');fprintf(fileID,'\n');
fprintf(fileID,'%s','\resizebox{\textwidth}{!}{%');fprintf(fileID,'\n');

cell_width = 'c';

cad = [];
for id_col = 1:size(Results,2)
    cad = [cad '| ' cell_width ' '];
end

fprintf(fileID,'%s',['	\begin{tabular}{| ' cell_width ' ' cad ' |}']);fprintf(fileID,'\n');

fprintf(fileID,'%s','\hline');fprintf(fileID,'\n');

line_text = [];
for id_col = 1:(size(Results,2)+1)

    aux_text = MetricNames{id_col};

    if id_col == (size(Results,2)+1)
        line_text = [line_text ' ' aux_text ' \\'];
    else
        line_text = [line_text ' ' aux_text ' & '];
    end
end

fprintf(fileID,'%s',line_text);fprintf(fileID,'\n');
fprintf(fileID,'%s','\hline\hline');fprintf(fileID,'\n');

for id_row = 1:size(Results,1)
    line_text = [ Topology_name{LocalTopologies(id_row)} ' & ' ];
    for id_col = 1:size(Results,2)

        Col_results = Results(:,id_col);
        [~, index] = sort(Col_results);
        select_row = find(index == id_row);

    
        aux_text = num2str(Results(id_row,id_col));

        if id_col == size(Results,2)
            line_text = [line_text ' \cellcolor{Color' num2str(select_row) '!25}' aux_text ' \\'];
        else
            line_text = [line_text ' \cellcolor{Color' num2str(select_row) '!25}' aux_text ' & '];
        end
        
           
    end

    fprintf(fileID,'%s',line_text);fprintf(fileID,'\n');
    fprintf(fileID,'%s','\hline');fprintf(fileID,'\n');

end

fprintf(fileID,'%s','	\end{tabular}}');fprintf(fileID,'\n');
fprintf(fileID,'%s',['	\caption{Digraph information for the digraph topologies considered in the agreement plot analysis.' ...
                     ' This information can also be found in Table \ref{tab:digraph_information_complete}.' ...
                     ' The metrics are ' Explanations '}']);fprintf(fileID,'\n');
fprintf(fileID,'%s','	\label{tab:digraph_information}	');fprintf(fileID,'\n');
fprintf(fileID,'%s','\end{table}');fprintf(fileID,'\n');

fprintf(fileID,'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');

fclose(fileID);


Color_1 = [1 1 0];
Color_2 = [1 0 1];
Color_3 = [0 1 1];
Color_4 = [1 0.5 0.5];


Results = nan*ones(4,10);
row_counter = 0;

% 1 %  1,  2,  3,  4,  5 -> Topology num 1 ~ Complete graph
% 2 %  6,  7,  8,  9, 10 -> Topology num 2 ~ Directed ring 1
% 3 % 11, 12, 13, 14, 15 -> Topology num 3 ~ Directed ring 2
% 4 % 16, 17, 18, 19, 20 -> Topology num 4 ~ Undirected ring
% 5 % 21, 22, 23, 24, 25 -> Topology num 5 ~ Small-World
% 6 % 26, 27, 28, 29, 30 -> Topology num 6 ~ Random network
% 7 % 31, 32, 33, 34, 35 -> Topology num 7 ~ Torus lattice
% 8 % 36, 37, 38, 39, 40 -> Topology num 8 ~ Scale-Free
% 9 % 41, 42, 43, 44, 45 -> Topology num 9 ~ Small-World

Fig = figure('Position',[288 84 838 680]);
id_model = 4;
NodeColor = (1/255)*[220,20,60];

LocalTopologies = [3 8 13 18 23 28 33 38 43];
for id_network = LocalTopologies % [13 38 23 3]
    row_counter = row_counter + 1;

    clf
    t = tiledlayout(1,1,'Padding','compact', 'TileSpacing','compact');

    InteractionNetwork = InteractionNetworks{id_network};
    InteractionNetwork = double(InteractionNetwork>0); % Create the topology 

    Adj_Mat = InteractionNetwork;
    Adj_Mat = Adj_Mat.*(ones(size(Adj_Mat)) - eye(size(Adj_Mat)));
    Digraph = digraph(transpose(Adj_Mat));
    [desLineWidth_Digraph, ArrowSize, desAlpha] = DigraphSettings(id_network, id_model);
    desLineWidth_Digraph = min([desLineWidth_Digraph*4, 2]);
    ArrowSize = max([ArrowSize*3, 7]);
    desAlpha = min([desAlpha*2.3, 1]);
    h = plot(Digraph, 'LineWidth', desLineWidth_Digraph, 'layout', 'force', 'NodeLabel',{}, 'NodeColor', NodeColor, 'EdgeAlpha',desAlpha, 'MarkerSize',7, 'ArrowSize', ArrowSize);
    border = 0.05;
    axis([min(h.XData)-(border*(max(h.XData) - min(h.XData))), max(h.XData)+(border*(max(h.XData) - min(h.XData))), min(h.YData)-(border*(max(h.YData) - min(h.YData))), max(h.YData)+(border*(max(h.YData) - min(h.YData)))])
    exportgraphics(Fig, ['Digraph_' num2str(id_network) '.png'], 'Resolution',300)


    % Average path length
    % diameter
    [Av_PathLength, num_cc, num_w_cc, diameter] = AveragePathLenght(InteractionNetwork);
    [ClusterCoeff, LocalCoeffs] = ClusterCoefficient(InteractionNetwork);

    clustering_variance = round(var(LocalCoeffs),10);

    num_ed = sum(sum(abs(InteractionNetwork)));

    out_degrees = sum(abs(InteractionNetwork),1);
    out_degree_var = round(var(out_degrees),10);
    out_degree_mean = mean(out_degrees);

    in_degrees = transpose(sum(abs(InteractionNetwork),2));
    in_degree_var = round(var(in_degrees),10);
    in_degree_mean = mean(in_degrees);


    BidirectionalIndex = BidirectionalCoefficientbyDefinition(InteractionNetwork);

    % Results= 

    Results(row_counter,1) = Av_PathLength; % Average path length
    Results(row_counter,2) = diameter; % Diameter

    Results(row_counter,3) = ClusterCoeff; % Clustering coefficient
    Results(row_counter,4) = clustering_variance; % Clustering variance

    Results(row_counter,5) = num_ed; % Number of edges

    Results(row_counter,7) = round(out_degree_var,10); % Out degree variance
    Results(row_counter,6) = out_degree_mean; % Out degree mean

    Results(row_counter,9) = round(in_degree_var,10); % In degree variance
    Results(row_counter,8) = in_degree_mean; % In degree mean

    Results(row_counter,10) = BidirectionalIndex; % Bidirectional index (coefficient)

end

clf
close all

MetricNames = {'Digraph', 'APL', 'D', '$\overline{CC}$', '$\sigma(CC)$', '\#E', ...
               '$\overline{\delta^{out}}$', '$\sigma(\delta^{out})$', ...
               '$\overline{\delta^{in}}$', '$\sigma(\delta^{in})$', 'BC'};


% Topology_name = {'Directed ring', 'Scale-Free', 'Small-World', 'Complete'};

% 1 %  1,  2,  3,  4,  5 -> Topology num 1 ~ Complete graph
% 2 %  6,  7,  8,  9, 10 -> Topology num 2 ~ Directed ring 1
% 3 % 11, 12, 13, 14, 15 -> Topology num 3 ~ Directed ring 2
% 4 % 16, 17, 18, 19, 20 -> Topology num 4 ~ Undirected ring
% 5 % 21, 22, 23, 24, 25 -> Topology num 5 ~ Small-World
% 6 % 26, 27, 28, 29, 30 -> Topology num 6 ~ Random network
% 7 % 31, 32, 33, 34, 35 -> Topology num 7 ~ Torus lattice
% 8 % 36, 37, 38, 39, 40 -> Topology num 8 ~ Scale-Free
% 9 % 41, 42, 43, 44, 45 -> Topology num 9 ~ Small-World

% Topology_name = {'Complete graph', ...
%                  'Directed ring 1', ...
%                  'Directed ring 2', ...
%                  'Undirected ring', ...
%                  'Small-World 1', ...
%                  'Random network', ...
%                  'Torus lattice', ...
%                  'Scale-Free', ...
%                  'Small-World 2'};

fileID = fopen('DigraphInformation_AllTopologies.tex', 'w');
fprintf(fileID,'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');

fprintf(fileID,'%s','\begin{table}');fprintf(fileID,'\n');
fprintf(fileID,'%s','\centering');fprintf(fileID,'\n');
fprintf(fileID,'%s','\resizebox{\textwidth}{!}{%');fprintf(fileID,'\n');


cell_width = 'c';

cad = [];
for id_col = 1:size(Results,2)
    cad = [cad '| ' cell_width ' '];
end

fprintf(fileID,'%s',['	\begin{tabular}{| ' cell_width ' ' cad ' |}']);fprintf(fileID,'\n');

fprintf(fileID,'%s','\hline');fprintf(fileID,'\n');

line_text = [];
for id_col = 1:(size(Results,2)+1)

    aux_text = MetricNames{id_col};

    if id_col == (size(Results,2)+1)
        line_text = [line_text ' ' aux_text ' \\'];
    else
        line_text = [line_text ' ' aux_text ' & '];
    end
end

fprintf(fileID,'%s',line_text);fprintf(fileID,'\n');
fprintf(fileID,'%s','\hline\hline');fprintf(fileID,'\n');


for id_row = 1:size(Results,1)
    line_text = [ Topology_name{LocalTopologies(id_row)} ' & ' ];
    for id_col = 1:size(Results,2)

        Col_results = Results(:,id_col);
        [~, index] = sort(Col_results);
        select_row = find(index == id_row);

    
        aux_text = num2str(Results(id_row,id_col));

        if id_col == size(Results,2)
            line_text = [line_text ' \cellcolor{Color' num2str(select_row) '!25}' aux_text ' \\'];
        else
            line_text = [line_text ' \cellcolor{Color' num2str(select_row) '!25}' aux_text ' & '];
        end
        
           
    end

    fprintf(fileID,'%s',line_text);fprintf(fileID,'\n');
    fprintf(fileID,'%s','\hline');fprintf(fileID,'\n');

end

fprintf(fileID,'%s','	\end{tabular}}');fprintf(fileID,'\n');
fprintf(fileID,'%s',['	\caption{Metrics for the different digraph topologies considered in Sections \ref{Sec:DigTop} and \ref{Sec:AgreementPlot},' ...
    ' and shown in Figure \ref{Fig:DigraphTopologyExamples}' ...
    '. The reported metrics are ' Explanations '}']);fprintf(fileID,'\n');
fprintf(fileID,'%s','	\label{tab:digraph_information_complete}	');fprintf(fileID,'\n');
fprintf(fileID,'%s','\end{table}');fprintf(fileID,'\n');

fprintf(fileID,'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');

fclose(fileID);


















