Numpy模拟生命

模拟生命

模拟生命类似一个小游戏,可以假设有很多个小生命,或小细胞,可生可灭,具体k看这个细胞邻居的多少,规则如下,更多参见

The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead. Every cell interacts with its eight neighbours, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

  • Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation.
  • Any live cell with more than three live neighbours dies, as if by overcrowding.
  • Any live cell with two or three live neighbours lives, unchanged, to the next generation.
  • Any dead cell with exactly three live neighbours becomes a live cell.

The initial pattern constitutes the 'seed' of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed – births and deaths happen simultaneously, and the discrete moment at which this happens is sometimes called a tick. (In other words, each generation is a pure function of the one before.) The rules continue to be applied repeatedly to create further generations.

目标就是根据这些规则,确定经过若干次演变后,生命的形态,哪些细胞生,哪些细胞灭。例如:

Z = np.array([[0,0,0,0,0,0],
              [0,0,0,1,0,0],
              [0,1,0,1,0,0],
              [0,0,1,1,0,0],
              [0,0,0,0,0,0],
              [0,0,0,0,0,0]])

演变后就是

array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1, 0],
       [0, 0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

算法的关键是确定每个细胞的邻居多少,以决定生死,且假定边界均死,用0表示。

def evolve(Z):
    N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +
         Z[1:-1,0:-2]                + Z[1:-1,2:] +
         Z[2:  ,0:-2] + Z[2:  ,1:-1] + Z[2:  ,2:])
    # Apply rules
    birth = (N==3) & (Z[1:-1,1:-1]==0) #若本来死,且邻居为3,则复活
    survive = ((N==2) | (N==3)) & (Z[1:-1,1:-1]==1) # 若本来活,且邻居为2 or 3,则幸存
    Z[...] = 0 # 重置为0
    Z[1:-1,1:-1][birth | survive] = 1 # 复活或幸存的置为1
    return Z
Z = np.random.randint(0,2,(256,512))
for i in range(100): evolve(Z)
# plot
size = np.array(Z.shape)
dpi = 72.0
figsize= size[1]/float(dpi),size[0]/float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi, facecolor="white")
fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False)
plt.imshow(Z,interpolation='nearest', cmap=plt.cm.hot)
plt.xticks([]), plt.yticks([])
plt.show()

Life

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏专知

【LeetCode 344】关关的刷题日记26 Reverse String

关关的刷题日记26 – Leetcode 344. Reverse String 题目 Write a function that takes a string...

2737
来自专栏C语言及其他语言

[每日一题]DNA

又到了今天C语言在线学习平台推送每日一题的时间了,今天分享的题目可能有点难,但我还是希望大家能够理解,这样以后遇到这样的问题也就不会害怕了 题目描述 小强从小...

2803
来自专栏鸿的学习笔记

关于英语,也许你忽视了很多东西

这是葛传椝老先生对英语学习者的教导,通篇用易读的英文短文讲述了英语学习过程中的种种心得,其中“大都是英语语法书和英语修辞学书不曾提到的”,文短而词丰,言简而意赅...

771
来自专栏好好学java的技术栈

从初级程序员到编程大牛,只需要每天坚持做这件事情……

程序员这个行业是很特殊的。之所以说特殊,就是因为它所有的技术大多来自欧美,所以最主流,最新鲜,最正确的技术文章都是英文,遗憾的是,大部分还没有译本。

972
来自专栏数据结构与算法

BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁(dp)

有一个小trick:在处理前缀和的时候我们可以保留下本层dp的信息,所以滚动数组是不需要的,具体看代码吧

451
来自专栏ml

HDUOJ---------2255奔小康赚大钱

奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja...

2926
来自专栏HansBug's Lab

2697: 特技飞行

2697: 特技飞行 Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 646  Solved: 393 [Su...

3147
来自专栏编程

C加加的学习方法!

学习C++重在理解其各种语言设施所代表的语义,以及C++所能表示的语义所代表的设计思想。首先从宏观上入手,你需要明白的是C++是程序设计语言的本质。在此我把C+...

1766
来自专栏tkokof 的技术,小趣及杂念

“快排”笔记

  关于排序,我想没有多少程序员会感觉陌生,信手拈来的可能就有不少:冒泡、选择或者归并等等;可在实际开发中,排序又让许多程序员感到很不熟悉,因为在大多数情况下,...

803
来自专栏HansBug's Lab

1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec  Memory Limit: 64 MB Sub...

3067

扫码关注云+社区