初级版元胞自动机(2021-11-17)

初级版元胞自动机

2021-11-17

1 原因:
  • 最近接触了群体智能,想起了元胞自动机
  • 之前玩过一个游戏,类似生命游戏,但找不到了
2 实现:
import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns  # 为了简便,用热图来可视

# 定义一个格子周围的八个格子
Xround = [-1,-1,-1,0,0,1,1,1]
Yround = [-1,0,1,-1,1,-1,0,1]

# 计算细胞总数
def count_cells(CellM):
    rnum = len(CellM)
    cnum = len(CellM[0])
    cellnum = 0
    for i in range(rnum):
        for j in range(cnum):
            if(CellM[i,j] == 1):
                cellnum += 1
    return cellnum

# 计算(i,j)格子周围的细胞数目
def count_round_cell(CellM,i,j,Xround=Xround,Yround=Yround):
    '''
    count the cells' number around the (i,j) grid
    Parameters
    ----------
    CellM : 2-D array
        the description of the cells grids.
    i : x-position
    j : y-position
    Xround : list, optional
        DESCRIPTION. The default is Xround.
    Yround : list, optional
        DESCRIPTION. The default is Yround.

    Returns
    -------
    round_cellnum.

    '''
    rnum = len(CellM)
    cnum = len(CellM[0])
    round_cellnum = 0
    for x in Xround:
        xi = i + x
        if(xi < 0 or xi >= rnum):
            continue
        for y in Yround:
            yi = j + y
            if(yi < 0 or yi >= cnum):
                continue
            if(CellM[xi,yi] == 1):
                round_cellnum += 1
    return round_cellnum   

# 模拟一轮:
# 遍历,如果这个格子没有细胞,且周围少于等于3个细胞,则生成一个细胞;
#      如果这个格子有细胞,且周围多于3个(大于等于4个)细胞,则这个细胞死亡
# 即9个格子最多有4个细胞
def one_mod(CellM):
    rnum = len(CellM)
    cnum = len(CellM[0])
    for i in range(rnum):
        for j in range(cnum):
            # 这个格子有没有细胞
            cellstate = CellM[i,j]
            # 这个格子周围的细胞数目
            round_cellnum = count_round_cell(CellM, i, j)
            # 依据条件判断
            if(cellstate == 0 and round_cellnum <= 3):
                CellM[i,j] = 1
            elif(cellstate == 1 and round_cellnum >= 4):
                CellM[i,j] = 0    

# 将矩阵绘制出来——可视化细胞情况
def plot_cellm(CellM): 
    sns.set()
    ax = sns.heatmap(CellM)
    plt.show()  
    
############################################################################
# 定义一个矩阵,作为细胞格子
CellM = np.zeros((50,50))

# 初始化细胞(1---有细胞,0---没有细胞)
CellM[3,3] = 1
CellM[3,4] = 1
CellM[4,3] = 1
CellM[4,4] = 1

print(CellM)
print('number of cells: ',count_cells(CellM))


### 模拟进行n次
n = 100

# 保存下这n次模拟时的总的细胞数目
cellnum_list = []
#cellnum_list.append(count_cells(CellM)) # 这里只记录模拟的细胞数,不记录原始的值了

for k in range(n):
    one_mod(CellM)
    cellnum_list.append(count_cells(CellM))
    plot_cellm(CellM)
    #print(CellM)
    #print('the number of cells is: ',count_cells(CellM))

# 统计可视化细胞数目的变化(好美丽的波形!)
plt.plot(range(n),cellnum_list) 

细胞数目变化结果可视化:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值