Commit a4ad13ec by mrebollo

Covid-19 risk map v1

parent a090f9e4
This source diff could not be displayed because it is too large. You can view the blob instead.
% Configuration of the problem: num. nodes, initial risk and node coordinates
clear;
n = 3000;
x0 = rand(n,1);
coor = rand(n,2);
% Random graph
% A = genrandom(n);
% RGG graph
A = genrgg(n,coor,0.05);
% RGG graph with long-range edge
% A = genrgg_long(n,coor,0.05);
% Preferential attachment
% A = genpref(n);
% plot the network
p = plot(graph(A),'XData',coor(:,1),'YData',coor(:,2),'NodeCData',x0,'EdgeColor','#cccccc');
colormap(flipud(hot))
colorbar
axis([0 1 0 1])
[r,c] = find(A);
csvwrite("covid.csv",[r c]);
% Creation of the grid with initial risk values
grid_size = 10;
loc = ceil(coor * grid_size);
status = zeros(n,grid_size,grid_size);
for i = 1:n
status(i,loc(i,2), loc(i,1)) = x0(i);
end
% Consensus configuration
L = diag(sum(A)) - A;
eps = 1 / max(max(diag(sum(A)))) * 0.99;
P = eye(n) - eps * L;
% Consensus process
iter = 1500;
x = reshape(status,[n,grid_size * grid_size]);
% just to show the progress of the consensus
% the lines marqued with (C) can be commented or removed
output = zeros(n,iter); % (C)
for i = 2:iter
x = P * x;
output(:,i) = sum(x,2); % (C)
end
plot(output'); % (C)
% Show the final map
map = reshape(x(1,:,:),[grid_size,grid_size]);
[gx,gy] = meshgrid(1:grid_size);
figure
subplot(1,2,1);
surface(gx,gy,map,'FaceColor','inter')
colormap(flipud(hot))
subplot(1,2,2);
surface(gx,gy,map)
colormap(flipud(hot))
colorbar
\ No newline at end of file
function A = genpref(n,d)
%PREF Generate adjacency matrix for a scale free random graph.
%
% Input n: dimension of matrix (number of nodes in graph).
% d: minimum row sum (minimum node degree). Defaults to 2.
%
% Output A: n by n symmetric matrix with the attribute sparse
%
%
% Description: Nodes are added successively. For each node, d edges
% are generated. The endpoints are selected from the
% nodes whose edges have already been created, with bias
% towards high degree nodes. This is a MATLAB
% implementation of Algorithm 5 in [2].
%
% References: [1] A.L. Barabasi, R. Albert,
% Emergence of scaling in random networks,
% Science Vol. 286, 15 (1999).
%
% [2] V. Batagelj, U. Brandes,
% Efficient generation of large random networks,
% Phys. Rev. E, 71 (2005).
%
% Example: A = pref(100,2);
if nargin == 1
d = 2;
end
I = [];
J = [];
for v = 1:n
for i = 1:d
M(2*((v-1)*d+i)-1) = v;
I = cat(1,I,v);
r = ceil(rand*(2*((v-1)*d+i)-1));
M(2*((v-1)*d+i))=M(r);
J = cat(1,J,M(r));
end
end
S = ones(length(I),1);
A = sign(sparse([I;J],[J;I],[S;S],n,n));
A = A - diag(diag(A));
\ No newline at end of file
function A = genrandom( n, p )
if nargin < 2 || isempty(p)
p = log(n) / n; %min density
end
A = spones(sprandsym(n,p));
A(1:1+n:n*n) = 0;
end
function [A, dmat ] = genrgg(n, coor, dist)
%GENRGG Generates a Random Geometric Graph
% A rangom reometric graph is a random graph in the unit square [0,1]
% where nodes are linked when they are closer that a minimum distance dist
dmat = squareform(pdist(coor)); %matrix with distances (think about handle it in a vector)
if nargin < 3
dist = min(max(dmat));
end
[row,col] = find( dmat < dist );
A = spones(sparse([row;col],[col;row],1,n,n));
A(1:n+1:n*n) = 0;
end
function [rgg] = genrgg_long(n, coor, dist)
%GENRGG Generates a Random Geometric Graph
% A rangom reometric graph is a random graph in the unit square [0,1]
% where nodes are linked when they are closer that a minimum distance dist
% One lomg-range link added with prob inverse to the distance
[A, dmat] = genrgg(n, coor, dist);
% add long-range links as in kleinberg model
for i = 1:n
j = randi(n);
if(dmat(i,j) < rand())
A(i,j) = 1;
A(j,i) = 1;
end
end
end
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment