元胞自动机

该博客展示了如何用Python创建一个元胞自动机的类,并通过matplotlib库进行动态显示。元胞自动机的规则是根据周围细胞的状态更新当前细胞的状态,实现了细胞状态的迭代变化。代码中包含了绘制元胞数组的动画展示,并能保存为gif文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://github.com/xhguleixin123/Cellular-Automata

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation


class Autocell(object):
    """
    实例化
    width : 元胞数组的宽度
    height : 元胞数组的宽度
    从而实例化 cells 元胞数组
    """

    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.cells = np.random.randint(0, 2, (width, height))

    """
    向周围八个格子方向滚动,并相加,得到格子周围的活着的元胞数
    周围3个重生变为1
    周围2个且为1,则为1
    """

    def next_gengeration(self):
        
        #相互滚动
        
        nbrs_count = sum(np.roll(np.roll(self.cells, n, 0), m, 1)
                         for n in (-1, 0, 1) for m in (-1, 0, 1)
                         if (n != 0 or m != 0))
        self.cells = (nbrs_count == 3) | ((self.cells == 1) & (nbrs_count == 2)).astype('int')

    """
    画元胞数组
    """
    def display_animation(self):
        plt.fig, ax = plt.subplots()
        ax.patch.set_facecolor('gray')
        ax.set_aspect('equal', 'box')


        def update_ax(i):
            ax.cla()
            label = 'timestep{0}'.format(i)
            print(label)
            ax.set_title(label)
            # plt.xticks(())
            # plt.yticks(())
            ax.xaxis.set_major_locator(plt.NullLocator())
            ax.yaxis.set_major_locator(plt.NullLocator())
            for a in range(self.width):
                for b in range(self.height):
                    color = 'gray' if not self.cells[a, b] else 'black'
                    rect = plt.Rectangle([a * 1.2 + 0.2, b * 1.2 + 2], 1, 1, facecolor=color, edgecolor=color)
                    ax.add_patch(rect)
                    ax.autoscale_view()
            self.next_gengeration()
            return ax

        anim = animation.FuncAnimation(plt.fig, update_ax, frames=np.arange(0,10), interval=100)
        plt.show()
        # 保存gif
        anim.save("./元胞自动机.gif", dpi=80, writer='pillow')


if __name__ == '__main__':
    autocell = Autocell(20, 20)
    autocell.display_animation()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值