Python实现的matplotlib动画演示之细胞自动机
作者:小小明-代码实体 发布时间:2022-05-23 15:49:09
* 上有个有意思的话题叫细胞自动机:https://en.wikipedia.org/wiki/Cellular_automaton
在20世纪70年代,一种名为生命游戏的二维细胞自动机变得广为人知,特别是在早期的计算机界。由约翰 · 康威发明,马丁 · 加德纳在《科学美国人》的一篇文章中推广,其规则如下:
Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overpopulation.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
总结就是:任何活细胞在有两到三个活邻居时能活到下一代,否则死亡。任何有三个活邻居的死细胞会变成活细胞,表示繁殖。
在Conway’s Game of Life中,展示了几种初始状态:
下面我们用python来模拟,首先尝试表示Beacon:
import numpy as np
import matplotlib.pyplot as plt
universe = np.zeros((6, 6), "byte")
# Beacon
universe[1:3, 1:3] = 1
universe[3:5, 3:5] = 1
print(universe)
im = plt.imshow(universe, cmap="binary")
[[0 0 0 0 0 0]
[0 1 1 0 0 0]
[0 1 1 0 0 0]
[0 0 0 1 1 0]
[0 0 0 1 1 0]
[0 0 0 0 0 0]]
可以看到已经成功的打印出了Beacon的形状,下面我们继续编写细胞自动机的演化规则:
def cellular_auto(universe):
universe_new = universe.copy()
h, w = universe.shape
for y in range(h):
for x in range(w):
neighbor_num = universe[x-1:x+2, y-1:y+2].sum()-universe[x, y]
# 任何有三个活邻居的死细胞都变成了活细胞,繁殖一样。
if universe[x, y] == 0 and neighbor_num == 3:
universe_new[x, y] = 1
# 任何有两到三个活邻居的活细胞都能活到下一代,否则就会死亡。
if universe[x, y] == 1 and neighbor_num not in (2, 3):
universe_new[x, y] = 0
return universe_new
universe = cellular_auto(universe)
print(universe)
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
[[0 0 0 0 0 0]
[0 1 1 0 0 0]
[0 1 0 0 0 0]
[0 0 0 0 1 0]
[0 0 0 1 1 0]
[0 0 0 0 0 0]]
ArtistAnimation动画
基于此我们可以制作matplotlib的动画,下面直接将Blinker、Toad、Beacon都放上去:
from matplotlib import animation
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
def cellular_auto(universe):
universe_new = universe.copy()
h, w = universe.shape
for y in range(h):
for x in range(w):
neighbor_num = universe[x-1:x+2, y-1:y+2].sum()-universe[x, y]
# 任何有三个活邻居的死细胞都变成了活细胞,繁殖一样。
if universe[x, y] == 0 and neighbor_num == 3:
universe_new[x, y] = 1
# 任何有两到三个活邻居的活细胞都能活到下一代,否则就会死亡。
if universe[x, y] == 1 and neighbor_num not in (2, 3):
universe_new[x, y] = 0
return universe_new
universe = np.zeros((12, 12), "byte")
# Blinker
universe[2, 1:4] = 1
# Beacon
universe[4:6, 5:7] = 1
universe[6:8, 7:9] = 1
# Toad
universe[8, 2:5] = 1
universe[9, 1:4] = 1
fig = plt.figure()
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
frame = []
for _ in range(2):
frame.append((plt.imshow(universe, cmap="binary"),))
universe = cellular_auto(universe)
animation.ArtistAnimation(fig, frame, interval=500, blit=True)
然后我们画一下Pulsar:
# Pulsar
universe = np.zeros((17, 17), "byte")
universe[[2, 7, 9, 14], 4:7] = 1
universe[[2, 7, 9, 14], 10:13] = 1
universe[4:7, [2, 7, 9, 14]] = 1
universe[10:13, [2, 7, 9, 14]] = 1
fig = plt.figure()
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
frame = []
for _ in range(3):
frame.append((plt.imshow(universe, cmap="binary"),))
universe = cellular_auto(universe)
animation.ArtistAnimation(fig, frame, interval=500, blit=True)
FuncAnimation动画
另一种创建matplotlib动画的方法是使用FuncAnimation,完整代码:
from matplotlib import animation
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import HTML
# %matplotlib notebook
def cellular_auto(universe):
universe_new = universe.copy()
h, w = universe.shape
for y in range(h):
for x in range(w):
neighbor_num = universe[x-1:x+2, y-1:y+2].sum()-universe[x, y]
# 任何有三个活邻居的死细胞都变成了活细胞,繁殖一样。
if universe[x, y] == 0 and neighbor_num == 3:
universe_new[x, y] = 1
# 任何有两到三个活邻居的活细胞都能活到下一代,否则就会死亡。
if universe[x, y] == 1 and neighbor_num not in (2, 3):
universe_new[x, y] = 0
return universe_new
def update(i=0):
global universe
im.set_data(universe)
universe = cellular_auto(universe)
return im,
# Pulsar
universe = np.zeros((17, 17), "byte")
universe[[2, 7, 9, 14], 4:7] = 1
universe[[2, 7, 9, 14], 10:13] = 1
universe[4:7, [2, 7, 9, 14]] = 1
universe[10:13, [2, 7, 9, 14]] = 1
fig = plt.figure()
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
plt.show()
anim = animation.FuncAnimation(
fig, update, frames=3, interval=500, blit=True)
HTML(anim.to_jshtml())
这种动画生成速度较慢,好处是可以导出html文件:
with open("out.html", "w") as f:
f.write(anim.to_jshtml())
还可以保存MP4视频:
anim.save("out.mp4")
或gif动画:
anim.save("out.gif")
注意:保存MP4视频或GIF动画,需要事先将ffmpeg配置到环境变量中
ffmpeg下载地址:
链接: https://pan.baidu.com/s/1aioB_BwpKb6LxJs26HbbiQ?pwd=ciui
提取码: ciui
随机生命游戏
接下来,我们创建一个50*50的二维生命棋盘,并选取其中1500个位置作为初始活细胞点,我们看看最终生成的动画如何。
完整代码如下:
from matplotlib import animation
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
def cellular_auto(universe):
universe_new = universe.copy()
h, w = universe.shape
for y in range(1, h-1):
for x in range(1, w-1):
neighbor_num = universe[x-1:x+2, y-1:y+2].sum()-universe[x, y]
# 任何有三个活邻居的死细胞都变成了活细胞,繁殖一样。
if universe[x, y] == 0 and neighbor_num == 3:
universe_new[x, y] = 1
# 任何有两到三个活邻居的活细胞都能活到下一代,否则就会死亡。
if universe[x, y] == 1 and neighbor_num not in (2, 3):
universe_new[x, y] = 0
# 边缘置零
universe[[0, -1]] = 0
universe[:, [0, -1]] = 0
return universe_new
boardsize, pad = 50, 2
universe = np.zeros((boardsize+pad, boardsize+pad), "byte")
# 随机选取1500个点作为初始活细胞
for i in range(1500):
x, y = np.random.randint(1, boardsize+1, 2)
universe[y, x] = 1
fig = plt.figure()
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
frame = []
for _ in range(200):
frame.append((plt.imshow(universe, cmap="binary"),))
universe = cellular_auto(universe)
animation.ArtistAnimation(fig, frame, interval=50, blit=True)
来源:https://blog.csdn.net/as604049322/article/details/124309635
猜你喜欢
- 概述laravel服务容器就像一个高度自动化的工厂,你需要的东西,定制好模型,使用特定接口来制造。因为使用了服务容器,laravel中大部分
- 最近帮伙计做了一个从网页抓取股票信息并把相应信息存入MySQL中的程序。使用环境:Python 2.5 for WindowsMySQLdb
- 不同的是setInterval会每隔指定的时间段就执行一次代码,具有重复性。而setTimeout只会调用后执行一次。 下面通过函数的建立和
- <?php ////$strimgsrc = file_get_contents("http://127.0.0.1/530
- 如果你搞过ASP的开发,你就会为ASP中没有好的完整的调试环境而头疼不己。我收集了网上相关所有信息提示,想给它做成单机的ASP开发错误提示软
- 代理服务是一种复杂的技术,具有很多可配置的移动组件。详细信息如下:信息信息是指在服务代理应用程序中调用的基本信息单元。对于服务代理来说,信息
- 简介观察者模式是行为型模式的一种,定义了对象间一对多的关系。当对象的状态发生变化时候,依赖于它的对象会得到通知。适用场景类似触发钩子事件,可
- 本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下:前言:深度优先遍历:对
- PDOStatement::debugDumpParamsPDOStatement::debugDumpParams — 打印一条 SQL
- 本文实例总结了PHP图像处理技术。分享给大家供大家参考,具体如下:1、绘图场景: 验证码、图像水印、图像压缩处理php绘图坐标体系是从0,0
- 本文实例讲述了php利用cookies实现购物车的方法。分享给大家供大家参考。具体分析如下:php购物车是在电子商务网站会用到的,一种像超市
- XPath(XML Path language)是一种处理XML文档段的语言。XSLT(Extensible Stylesheet Lang
- < ?php if (!function_exists("T7FC56270E7A70FA81A5935B72EACBE29
- 环境准备好了!我们怎么使用这些东东?IIS用组件初始化是用这个过程Public Sub OnStartPage给个使用asp组件的例子:数字
- 一个简单的for语句就能循环字典的所有键,就像处理序列一样:In [1]: d = {'x':1, 'y':
- 这不仅仅是一个信息 * 的时代,也是一个服务 * 的时代。一切都是因为互联网,随着互联网技术的发展,信息的增多,服务的增多,用户需求的多样化。怎
- 前言:接口自动化是指模拟程序接口层面的自动化,由于接口不易变更,维护成本更小,所以深受各大公司的喜爱。接口自动化包含2个部分,功能性的接口自
- 数组:【重点1】implode(分隔,arr) 把数组值数据按指定字符连接起来例如:$arr=array('1','
- 在Windows vista之前的微软操作系统的地址栏与浏览器的地址栏是一样的,也是面包屑般的自上而下的线性等级形,如果想从D盘跳到C盘是不
- 如下所示:# -*- coding: UTF-8 -*-import numpy as npimport osfrom scipy.misc