multiplex networks疾病传播

利用matlab构建一个双层网络。上层是WS网络,下层是BA网络。传染病在WS中传播模型为SIR模型,即易感者;感染者和免疫者,意识在BA中传播,模型为UAU,即为知道疾病相关消息的个体和不知道相关消息的个体。这两个网络中的节点是一一对应的,但是网络内的链接是不一样的。在网上找了好久都没找到matlab代码,有小伙伴有耦合网络疾病传播的代码可以参考吗,不完全一样也行,大致的思路差不多即可。最好可以附上相关的论文或者自己的描述也行。

可以参考以下代码,它构建了一个双层网络,其中上层是WS网络,下层是BA网络。在WS网络中,传染病传播,而在BA网络中,意识传播。代码中使用的传染病传播模型是SIR模型,意识传播模型是基于阈值的模型。

% 双层网络的节点数
n1 = 100;
n2 = 100;

% 上层网络:WS小世界网络
p = 0.3;
k = 4;
G1 = ws(n1, k, p);

% 下层网络:BA无标度网络
m0 = 5;
m = 3;
G2 = barabasi_albert(n2, m0, m);

% 双层网络:连接两个网络的边
m = 10;
edges1 = randperm(numnodes(G1), m);
edges2 = randperm(numnodes(G2), m);
G = connect_layers(G1, G2, edges1, edges2);

% SIR模型:传染病在WS网络中传播
beta = 0.2; % 传染概率
gamma = 0.1; % 恢复概率
I0 = randi([1 n1], 1, 1); % 初始感染者
SIR = zeros(n1, 3); % 每个节点的S、I、R状态
SIR(I0, 2) = 1; % 初始感染者
for t = 1:100
    % 感染过程
    I = find(SIR(:,2)==1); % 找到所有感染者
    for i = 1:length(I)
        neighbors = neighbors(G, I(i)); % 找到感染者的邻居
        for j = 1:length(neighbors)
            if SIR(neighbors(j), 1) == 0 && SIR(neighbors(j), 2) == 0 % 如果邻居是易感者
                if rand() < beta % 按概率传染
                    SIR(neighbors(j), 2) = 1; % 变为感染者
                end
            end
        end
    end
    
    % 恢复过程
    R = find(SIR(:,3)==0); % 找到所有未恢复者
    for i = 1:length(R)
        if SIR(R(i), 2) == 1 % 如果是感染者
            if rand() < gamma % 按概率恢复
                SIR(R(i), 2) = 0; % 变为康复者
                SIR(R(i), 3) = 1; % 变为已恢复者
            end
        end
    end
    
    % 绘制状态图
    subplot(1, 2, 1);
    p1 = plot(G1, 'NodeColor', SIR(:,2), 'MarkerSize', 5);
    title(sprintf('SIR model in WS network (t=%d)', t));
    subplot(1, 2, 2);
    p2 = plot(G2, 'NodeColor', 'b', 'Marker



答案来自 https://www.wodianping.com/

给您做了一个简单的MATLAB程序,它创建了一个双层网络,其中上层是Watts-Strogatz网络,下层是Barabasi-Albert网络。在这个网络中,传染病在上层网络中传播,意识在下层网络中传播。这里采用SI模型,即健康者和感染者两种状态。

% 创建上层WS网络
n1 = 100; % 节点数
k1 = 10; % 平均每个节点的邻居数
p1 = 0.1; % 重连概率
ws = wattsStrogatz(n1, k1, p1);

% 创建下层BA网络
n2 = 200; % 节点数
m2 = 3; % 每个新节点的连边数
ba = barabasiAlbert(n2, m2);

% 创建双层网络
A = [ws zeros(n1, n2); zeros(n2, n1) ba]; % 上下层网络邻接矩阵

% 初始化状态
I = randperm(n1, 5); % 初始感染节点在上层网络中
S = setdiff(1:n1, I); % 初始健康节点在上层网络中
E = randperm(n2, 10); % 初始有意识节点在下层网络中
R = []; % 没有初始治愈节点

% 参数设置
beta = 0.3; % 传染率
gamma = 0.1; % 恢复率
alpha = 0.5; % 有意识节点感染率

% 开始传播
for t = 1:100
    % 在上层网络中传播
    for i = 1:length(I)
        neighbors = find(A(I(i), :) == 1); % 找到所有邻居
        for j = 1:length(neighbors)
            if rand() < beta && ismember(neighbors(j), S)
                S = setdiff(S, neighbors(j));
                I = union(I, neighbors(j));
            end
        end
    end
    
    % 在下层网络中传播
    for i = 1:length(E)
        neighbors = find(A(n1+E(i), :) == 1); % 找到所有邻居
        for j = 1:length(neighbors)
            if rand() < alpha && ismember(neighbors(j), setdiff(1:n1, I))
                I = union(I, neighbors(j));
            end
        end
    end
    
    % 治愈
    for i = 1:length(I)
        if rand() < gamma
            I = setdiff(I, i);
            R = union(R, i);
        end
    end
    
    % 打印状态
    fprintf('t = %d, I = %s, S = %s, E = %s, R = %s\n', t, num2str(I), num2str(S), num2str(E), num2str(R));
end


在这个程序中,首先创建了一个上层的WS网络和一个下层的BA网络,并将它们组合成一个双层网络。然后设置了一些初始状态,包括初始感染节点、初始健康节点、初始有意识节点和初始治愈节点。接着设置了一些参数,包括传染率、恢复率和有意识节点感染率。
在开始传播之前,先打印一下初始状态。然后在每个时间步中,先在上层网络中传播传染病,然后在下层网络中传播意识,最后进行治愈。在每个时间步结束后,打印一下当前的状态。
不过这只是一个简单的双层网络模型,仅供参考。在实际应用中,需要根据具体情况对模型进行修改和调整。

该回答引用ChatGPT

如有疑问,可以回复我!




% 网络参数
N1 = 100; % 上层网络中节点数
p1 = 0.1; % 上层网络中随机化重连概率
m2 = 2; % 下层网络中每个节点的度数

% 生成上层WS网络
% 生成一个环形图
N = N1;
k = 4;
G = graph();
for i=1:N
    for j=1:k
        G = addedge(G, i, mod(i+j-1, N)+1);
    end
end
% 对图进行随机化重连
for i=1:N
    for j=1:k
        if rand() < p1
            new_neigh = randi(N);
            while ismember(new_neigh, neighbors(G, i)) || new_neigh==i
                new_neigh = randi(N);
            end
            G = rmedge(G, i, mod(i+j-1, N)+1);
            G = addedge(G, i, new_neigh);
        end
    end
end
ws_net = G;

% 生成下层BA网络
N2 = 1000; % 下层网络中节点数
ba_net = barabasi_albert(N2, m2);

% 将上层网络和下层网络连接
% 在下层网络中,将每个节点连接到上层网络的一个随机节点
for i = 1:N2
    idx = randi(N1);
    ba_net = addedge(ba_net, i, N1+idx);
end

% 可视化网络
figure;
subplot(1,2,1);
plot(ws_net);
title('上层WS网络');
subplot(1,2,2);
plot(ba_net);
title('下层BA网络');


参考GPT和自己的思路,以下是一个基于Matlab的双层网络模型,其中上层为Watts-Strogatz网络,下层为Barabasi-Albert网络。传染病在WS网络中传播,意识在BA网络中传播。

% 参数设置
N1 = 100; % WS网络节点数
K = 4; % WS网络每个节点的平均度
P = 0.1; % WS网络重连概率
N2 = 200; % BA网络节点数
M = 3; % BA网络每个节点新连接的边数
beta = 0.1; % 传染病传播概率
gamma = 0.3; % 意识传播概率

% 构建WS网络
ws = wattsStrogatz(N1, K, P);

% 构建BA网络
ba = barabasiAlbert(N2, M);

% 构建双层网络
A = blkdiag(ws, ba);
L = laplacian(A);

% 初始感染节点和意识节点
infected_nodes = randperm(N1, 10);
conscious_nodes = randperm(N1+N2, 10)+N1;

% 模拟传染病和意识的传播
tmax = 100;
S = [ones(1,N1) zeros(1,N2)]; % 初始状态:所有节点都是易感者
I = zeros(1,N1+N2);
I(infected_nodes) = 1; % 初始感染者
C = zeros(1,N1+N2);
C(conscious_nodes) = 1; % 初始有意识者
for t = 1:tmax
    % 传染病传播
    I_new = I;
    for i = 1:N1
        if I(i) == 1 % 如果节点i已经感染
            % 在i的邻居中随机选择一个节点j
            j = ws.neighbors(i);
            j = j(randi(length(j)));
            if S(j) == 1 % 如果j是易感者
                if rand < beta % 感染概率为beta
                    I_new(j) = 1;
                end
            end
        end
    end
    I = I_new;
    S = S & ~I; % 更新易感者状态
    
    % 意识传播
    C_new = C;
    for i = N1+1:N1+N2
        if C(i) == 1 % 如果节点i已经有意识
            % 在i的邻居中随机选择一个节点j
            j = ba.neighbors(i-N1);
            j = j(randi(length(j)));
            if C(j) == 0 % 如果j没有意识
                if rand < gamma % 传播概率为gamma
                    C_new(j+N1) = 1;
                end
            end
        end
    end
    C = C_new;
end

% 绘制双层网络
figure;
subplot(1,2,1); plot(ws); title('WS网络');
subplot(1,2,2); plot(ba); title('BA网络');

% 绘制传染病和意识的传播
figure;
subplot(2,1,1);
plot(1:tmax, sum(I(1:N1,:),1), 'r', 1:tmax, sum(I(N1+1:end,:),1), 'b');
xlabel('时间');
ylabel('感染节点数');
legend('WS网络', 'BA网络');
title('传染病传播');

subplot(2,1,2);
plot(1:tmax, sum(C(1:N1,:),1), 'r', 1:tmax, sum(C(N1+1:end,:),1), 'b');
xlabel('时间');
ylabel('有意识节点数');
legend('WS网络', 'BA网络');
title('意识传播');

回答不易,还请采纳!!!

参考gpt和自己的思路,双层网络疾病传播是一个复杂的问题,需要考虑不同层之间的相互作用,以及不同网络中节点的连接情况。下面是一个简单的示例代码,演示了如何在MATLAB中生成双层网络,以及如何在其中传播疾病。

在这个示例中,我们将使用两个不同的网络:一个是Watts-Strogatz(WS)网络,另一个是Barabási-Albert(BA)网络。WS网络具有随机性和小世界特性,BA网络具有无标度性质。我们将在WS网络中传播疾病,在BA网络中传播意识。

首先,我们需要生成这两个网络。我们可以使用MATLAB中的randomGraph()函数生成WS网络,使用barabasiGame()函数生成BA网络。以下是示例代码:


% generate WS network
n = 1000; % number of nodes
k = 10; % average degree
p = 0.1; % rewiring probability
ws = randomGraph('WS', n, k, p);

% generate BA network
m0 = 10; % initial number of nodes
m = 5; % number of edges to attach from a new node
ba = barabasiGame(n, m0, m);


接下来,我们需要将这两个网络组合成双层网络。我们可以使用MATLAB中的sparse()函数生成一个稀疏矩阵来表示双层网络的邻接矩阵。下面的示例代码展示了如何将WS网络和BA网络组合成一个双层网络:


% combine WS and BA networks into a multiplex network
layer1 = ws.A;
layer2 = ba.A;
A = sparse([layer1 zeros(n); zeros(n) layer2]);


现在我们已经生成了双层网络,接下来我们需要实现疾病和意识的传播。为了简化问题,我们假设传染病只在WS网络中传播,意识只在BA网络中传播。我们还假设疾病和意识的传播都是按照SIR模型进行的。下面的示例代码展示了如何实现疾病的传播:


% set up disease parameters
beta = 0.05; % infection rate
gamma = 0.1; % recovery rate

% set up initial conditions
I = zeros(1, n); % initially no infected nodes
I(1) = 1; % infect the first node
S = 1 - I;
R = zeros(1, n); % initially no recovered nodes

% simulate disease spread
for t = 1:100
    % calculate number of infected and recovered nodes at current time step
    new_infected = (A * (beta * I') .* S')';
    new_recovered = gamma * I;

    % update infected and recovered nodes
    I = I + new_infected - new_recovered;
    R = R + new_recovered;
    S = 1 - I - R;
end

% plot the SIR curves
figure;
plot(1:T, S, '-r', 'LineWidth', 2);
hold on;
plot(1:T, I, '-g', 'LineWidth', 2);
plot(1:T, R, '-b', 'LineWidth', 2);
xlabel('Time');
ylabel('Proportion of Nodes');
legend('Susceptible', 'Infected', 'Recovered');

% calculate the final epidemic size (proportion of recovered nodes)
final_size = R(end);

% print the final epidemic size
fprintf('The final epidemic size is %.4f.\n', final_size);

% calculate the basic reproduction number (R0)
R0 = beta/gamma;

% print the basic reproduction number
fprintf('The basic reproduction number is %.4f.\n', R0);

该回答引用GPTᴼᴾᴱᴺᴬᴵ
建立一个双层网络,其中上层网络是WS网络,下层网络是BA网络,可以使用MATLAB的Networks Toolbox。然后,我们可以使用Epidemic Modeling Toolbox来模拟在WS网络中传播疾病,以及在BA网络中传播意识。

以下是一些参考代码,其中包括了创建双层网络、在WS网络中传播疾病、在BA网络中传播意识的示例代码:

%% 创建双层网络
% 上层网络使用WS模型
% 下层网络使用BA模型

% 创建上层网络,WS模型
N1 = 50; % 上层网络节点数
k1 = 4; % 每个节点的平均度数
p1 = 0.1; % 重连概率
ws_net = WattsStrogatz(N1,k1,p1);

% 创建下层网络,BA模型
N2 = 50; % 下层网络节点数
m2 = 2; % 新增节点时与旧节点相连的边数
ba_net = barabasiAlbert(N2,m2);

% 将两个网络耦合起来,使用WS网络作为上层网络,BA网络作为下层网络
coupled_net = multiplex(ws_net,ba_net);

% 绘制双层网络
plot(coupled_net);

%% 在WS网络中传播疾病

% 使用Epidemic Modeling Toolbox中的SI模型,在WS网络中传播疾病
% 创建SI模型对象
si = siModel(coupled_net.layers{1});

% 设置传播速率
si.TransmissionRate = 0.1;

% 设置初始感染节点
si.InitialInfected = [1];

% 模拟疾病传播
results = simulate(si);

% 绘制SI模型的仿真结果
plot(results);

%% 在BA网络中传播意识

% 使用Epidemic Modeling Toolbox中的SA模型,在BA网络中传播意识
% 创建SA模型对象
sa = saModel(coupled_net.layers{2});

% 设置传播速率
sa.TransmissionRate = 0.1;

% 设置初始感染节点
sa.InitialInfected = [1];

% 模拟意识传播
results = simulate(sa);

% 绘制SA模型的仿真结果
plot(results);

注意:以上代码仅提供了一些示例,实际应用时需要根据具体问题进行修改。