元胞自动机——流言模型展示

推荐阅读

不错的ppt讲解

模型展示

别人的模型(传播方式没能理解)

(https://blog.csdn.net/your_answer/article/details/79287367)

from matplotlib import pyplot as plt

def count_rumour(matrix, rumour):  # 二维矩阵,计算流言的数目
    sum_rumour = 0
    for sublist in matrix:
        sum_rumour += sublist.count([0, rumour]) + sublist.count([rumour, rumour])+sublist.count([rumour,0])#在这位博主设计的传播方式中不会出现这种情况+sublist.count([rumour,0])
    return sum_rumour
    
def spread(size, rumour, start_x, start_y):
    # 初始化
    rumour_matrix = [[[0, 0] for i in range(size)] for j in range(size)]
    rumour_spread = []
    rumour_matrix[start_x][start_y] = [1, 1]
    rumour_spread.append(count_rumour(rumour_matrix, rumour))
    # 个体更新
    while count_rumour(rumour_matrix, rumour) < size * size:
        for i in range(size):
            for j in range(size):
                # 制定流言传播规则1、准备传播流言的传播给邻居(上下左右)2、上次听到流言的,变为准备传播流言
                if rumour_matrix[i][j][0] == rumour:
                    if i - 1 >= 0:
                        rumour_matrix[i - 1][j] = [rumour, rumour]  # 已经遍历了
                    if i + 1 < size:
                        rumour_matrix[i + 1][j][1] = rumour
                    if j - 1 >= 0:
                        rumour_matrix[i][j - 1] = [rumour, rumour]  # 已经遍历了
                    if j + 1 < size:
                        rumour_matrix[i][j + 1][1] = rumour
                elif rumour_matrix[i][j][1] == rumour:
                    rumour_matrix[i][j][0] = rumour
        rumour_spread.append(count_rumour(rumour_matrix, rumour))
    print(rumour_spread[:10])  # 打印前十个时间步流言传播的速度
    plt.plot(rumour_spread)

# 设置参数
size = 200
rumour = 1

center_x, center_y = int(size / 2), int(size / 2)
spread(size, rumour, center_x, center_y)
spread(size, rumour, 0, 0)
plt.show()

自己写的传播方式1
最简单的传播方式,无条件的传给周围的8个邻元

def spread(size, rumour, start_x, start_y):
    # 初始化
    rumour_matrix = [[[0, 0] for i in range(size)] for j in range(size)]
    rumour_spread = []
    rumour_matrix[start_x][start_y] = [1, 1]
    rumour_spread.append(count_rumour(rumour_matrix, rumour))
    # 个体更新
    while count_rumour(rumour_matrix, rumour) < size * size:
#     for k in range(5):
        for i in range(size):
            for j in range(size):
                # 制定流言传播规则1、准备传播流言的传播给邻居(上下左右)2、上次听到流言的,变为准备传播流言
                if rumour_matrix[i][j][0] == rumour:
                    rumour_matrix[i][j][1] == rumour
                    if i - 1 >= 0:
                        rumour_matrix[i - 1][j] = [rumour, rumour]
                        if j - 1 >= 0:
                            rumour_matrix[i-1][j - 1][1] = rumour
                    if i + 1 < size:
                        rumour_matrix[i + 1][j] = [rumour, rumour]
                        if j - 1 >= 0:
                            rumour_matrix[i+1][j - 1][1] = rumour
                    if j - 1 >= 0:
                        rumour_matrix[i][j - 1][1] = rumour
                if rumour_matrix[i][j][1] == rumour:
                    rumour_matrix[i][j][0] == rumour
                    if i - 1 >= 0:
                        rumour_matrix[i - 1][j] = [rumour,rumour]
                        if j+1 < size :
                            rumour_matrix[i-1][j + 1][0] = rumour
                    if i + 1 < size:
                        rumour_matrix[i + 1][j] = [rumour, rumour]
                        if j + 1 < size:
                            rumour_matrix[i + 1][j+1][0] = rumour
                    if j + 1 < size:
                        rumour_matrix[i][j + 1][0] = rumour
                # print(rumour_matrix)
        rumour_spread.append(count_rumour(rumour_matrix, rumour))

    print(rumour_spread[:10])  # 打印前十个时间步流言传播的速度
    plt.plot(rumour_spread)

在这里插入图片描述

传播方式2
引入随机因素,传播给周围8个邻元的概率服从[0,1]的均匀分布(也可以固定概率)

#加入随机因素
def spread(size, rumour, start_x, start_y):
    # 初始化
    rumour_matrix = [[[0, 0] for i in range(size)] for j in range(size)]
    rumour_spread = []
    rumour_matrix[start_x][start_y] = [1, 1]
    rumour_spread.append(count_rumour(rumour_matrix, rumour))
    # 个体更新
    while count_rumour(rumour_matrix, rumour) < size * size:
        for i in range(size):
            for j in range(size):
                # 制定流言传播规则1、准备传播流言的传播给邻居(上下左右)2、上次听到流言的,变为准备传播流言
                if rumour_matrix[i][j][0] == rumour:
                    if rumour_matrix[i][j][1] != rumour:
                        rumour_matrix[i][j][1] = rumour*round(np.matlib.rand(1).item())
                    if i - 1 >= 0:
                        if rumour_matrix[i - 1][j] != [rumour, rumour]:
                            rumour_matrix[i - 1][j] = [rumour*round(np.matlib.rand(1).item()), rumour*round(np.matlib.rand(1).item())]
                        if j - 1 >= 0:
                            if rumour_matrix[i-1][j - 1][1] != rumour:
                                rumour_matrix[i-1][j - 1][1] = rumour*round(np.matlib.rand(1).item())
                    if i + 1 < size:
                        if rumour_matrix[i + 1][j] != [rumour, rumour]:
                            rumour_matrix[i + 1][j] = [rumour*round(np.matlib.rand(1).item()), rumour*round(np.matlib.rand(1).item())]
                        if j - 1 >= 0:
                            if rumour_matrix[i+1][j - 1][1]!= rumour:
                                rumour_matrix[i + 1][j - 1][1] = rumour*round(np.matlib.rand(1).item())
                    if j - 1 >= 0:
                        if rumour_matrix[i][j - 1][1] != rumour:
                            rumour_matrix[i][j - 1][1] = rumour*round(np.matlib.rand(1).item())
                if rumour_matrix[i][j][1] == rumour:
                    if rumour_matrix[i][j][0] != rumour:
                        rumour_matrix[i][j][0] = rumour*round(np.matlib.rand(1).item())
                    if i - 1 >= 0:
                        if rumour_matrix[i - 1][j] != [rumour,rumour]:
                            rumour_matrix[i - 1][j] = [rumour*round(np.matlib.rand(1).item()), rumour*round(np.matlib.rand(1).item())]
                        if j+1 < size :
                            if rumour_matrix[i-1][j + 1][0]!= rumour:
                                rumour_matrix[i - 1][j + 1][0] = rumour*round(np.matlib.rand(1).item())
                    if i + 1 < size:
                        if rumour_matrix[i + 1][j] != [rumour, rumour]:
                            rumour_matrix[i + 1][j] = [rumour*round(np.matlib.rand(1).item()), rumour*round(np.matlib.rand(1).item())]
                        if j + 1 < size:
                            if rumour_matrix[i + 1][j+1][0] != rumour:
                                rumour_matrix[i + 1][j + 1][0] = rumour*round(np.matlib.rand(1).item())
                    if j + 1 < size:
                        if rumour_matrix[i][j + 1][0] != rumour:
                            rumour_matrix[i][j + 1][0] = rumour*round(np.matlib.rand(1).item())
                # print(rumour_matrix)
        rumour_spread.append(count_rumour(rumour_matrix, rumour))

    print(rumour_spread[:10])  # 打印前十个时间步流言传播的速度
    plt.plot(rumour_spread)
    return rumour_matrix

在这里插入图片描述
传播方式3
引入0.1的遗忘概率
此时的size不能设置太大,不然很难跑出来,我这里设置的是30

#以一定概率产生0,1的函数
def single_prob(p):
    if np.matlib.rand(1).item() < p:
        return 0
    else:
        return 1

def spread(size, rumour, start_x, start_y):
    # 初始化
    rumour_matrix = [[[0, 0] for i in range(size)] for j in range(size)]
    rumour_spread = []
    rumour_matrix[start_x][start_y] = [1, 1]
    rumour_spread.append(count_rumour(rumour_matrix, rumour))
    # 个体更新
    while count_rumour(rumour_matrix, rumour) < size * size:
        for i in range(size):
            for j in range(size):
                # 制定流言传播规则1、准备传播流言的传播给邻居(上下左右)2、上次听到流言的,变为准备传播流言
                if rumour_matrix[i][j][0] == rumour:
                    rumour_matrix[i][j][0] = single_prob(0.1)
                    if rumour_matrix[i][j][1] != rumour:
                        rumour_matrix[i][j][1] = rumour*round(np.matlib.rand(1).item())
                    if i - 1 >= 0:
                        if rumour_matrix[i - 1][j] != [rumour, rumour]:
                            rumour_matrix[i - 1][j] = [rumour*round(np.matlib.rand(1).item()), rumour*round(np.matlib.rand(1).item())]
                        if j - 1 >= 0:
                            if rumour_matrix[i-1][j - 1][1] != rumour:
                                rumour_matrix[i-1][j - 1][1] = rumour*round(np.matlib.rand(1).item())
                    if i + 1 < size:
                        if rumour_matrix[i + 1][j] != [rumour, rumour]:
                            rumour_matrix[i + 1][j] = [rumour*round(np.matlib.rand(1).item()), rumour*round(np.matlib.rand(1).item())]
                        if j - 1 >= 0:
                            if rumour_matrix[i+1][j - 1][1]!= rumour:
                                rumour_matrix[i + 1][j - 1][1] = rumour*round(np.matlib.rand(1).item())
                    if j - 1 >= 0:
                        if rumour_matrix[i][j - 1][1] != rumour:
                            rumour_matrix[i][j - 1][1] = rumour*round(np.matlib.rand(1).item())
                if rumour_matrix[i][j][1] == rumour:
                    rumour_matrix[i][j][1] = single_prob(0.1)
                    if rumour_matrix[i][j][0] != rumour:
                        rumour_matrix[i][j][0] = rumour*round(np.matlib.rand(1).item())
                    if i - 1 >= 0:
                        if rumour_matrix[i - 1][j] != [rumour,rumour]:
                            rumour_matrix[i - 1][j] = [rumour*round(np.matlib.rand(1).item()), rumour*round(np.matlib.rand(1).item())]
                        if j+1 < size :
                            if rumour_matrix[i-1][j + 1][0]!= rumour:
                                rumour_matrix[i - 1][j + 1][0] = rumour*round(np.matlib.rand(1).item())
                    if i + 1 < size:
                        if rumour_matrix[i + 1][j] != [rumour, rumour]:
                            rumour_matrix[i + 1][j] = [rumour*round(np.matlib.rand(1).item()), rumour*round(np.matlib.rand(1).item())]
                        if j + 1 < size:
                            if rumour_matrix[i + 1][j+1][0] != rumour:
                                rumour_matrix[i + 1][j + 1][0] = rumour*round(np.matlib.rand(1).item())
                    if j + 1 < size:
                        if rumour_matrix[i][j + 1][0] != rumour:
                            rumour_matrix[i][j + 1][0] = rumour*round(np.matlib.rand(1).item())
                # print(rumour_matrix)
        rumour_spread.append(count_rumour(rumour_matrix, rumour))

    print(rumour_spread[:10])  # 打印前十个时间步流言传播的速度
    plt.plot(rumour_spread)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值