生成规则30元胞自动机的行

3

规则30是一种一维元胞自动机,当前的一代仅考虑上一代中的单元格。一个单元格可以处于两个状态之一:10。创建下一代的规则在下面的行中表示,并取决于当前单元格上方的单元格以及其相邻单元格。

该元胞自动机使用以下规则进行应用(使用位运算符):

left_cell ^ (central_cell | right_cell)

这个规则形成了下面的表格:

enter image description here

现在我尝试用 numpy 在 Python 中实现这些规则。我定义了一个初始状态,接受 width 作为参数,并生成一个以 1 为中心的全零初始行。
def initial_state(width):
    initial = np.zeros((1, width), dtype=int)
    if width % 2 == 0:
        initial = np.insert(initial, int(width / 2), values=0, axis=1)
        initial[0, int(width / 2)] = 1
        return initial
    else:
        initial[0, int(width / 2)] = 1
        return initial

下面的函数根据给定的初始行生成第二代。如何创建一个for循环,一直生成新的后代,直到最后一行底部的第一个元素变为1?
def rule30(array):
    row1 = np.pad(array,[(0,0), (1,1)], mode='constant')
    next_row = array.copy()
    for x in range(1, array.shape[0]+1):
        for y in range(1, array.shape[1]+1):
            if row1[x-1][y-1] == 1 ^ (row1[x-1][y] == 1 or row1[x-1][y+1] == 1):
                next_row[x - 1, y - 1] = 1
            else:
                next_row[x - 1, y - 1] = 0
        return np.concatenate((array, next_row))

例如,如果输入为

A = [0, 0, 0, 1, 0, 0, 0]

输出应该是:
>>> print(rule30(A))
[[0, 0, 0, 1, 0, 0, 0],
 [0, 0, 1, 1, 1, 0, 0],
 [0, 1, 1, 0, 0, 1, 0],
 [1, 1, 0, 1, 1, 1, 1]]

3
如果你将rule30实现为一个函数,它接受一个状态并返回下一个状态,则可能更加直观,而不是填充现有的二维数组直到完成。 - Ry-
1
可能最简单的解决方案是预先计算一个查找表,根据规则将三个位映射到一个位。或者更简单的方法是,将这三个位打包成一个从0到7的单个数字n,并使用规则号(例如30)的第n位作为输出位,因为这就是Wolfram规则编号的定义方式 - Ilmari Karonen
所以函数现在可以工作了,我已经更新了问题中的代码。它生成了下一代。现在,我的问题是 - 如何创建一个for循环,在初始状态行“下面”附加更多的生成行?for循环应该停止生成新行,直到最后一行的第一个元素变为1。 - coding girl
2个回答

2

以下是基于字符串表示和查找的代码。它使用了上面评论中的一些想法。此外,我为处理边缘单元格添加了填充 - 条件对此不太清楚。还要注意,您提出的模式表不对称。比较“110”和“011”的新状态。

def rule30(a):
    patterns = {'111': '0', '110': '0', '101': '0', '100': '1',
                '011': '1', '010': '1', '001': '1', '000': '0', }
    a = '0' + a + '0'  # padding
    return ''.join([patterns[a[i:i+3]] for i in range(len(a)-2)])

a = '0001000'
result = [list(map (int, a))]

while a[0] != '1':
    a = rule30(a)
    result.append (list(map (int, a)))
print (result)  # list of lists 
print (np.array(result))  # np.array

列表的列表:

[[0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0], [1, 1, 0, 1, 1, 1, 1]]

np.array:

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

1

方法一 - Numpy

您可以通过对当前代码进行略微修改来实现此目的 - 改变rule30的返回值为return np.array(next_row)。然后您可以使用以下函数:

def apply_rule(n):
    rv = initial_state(n)
    while rv[-1][0] == 0:
        rv = np.append(rv, rule30(rv[-1].reshape(1,-1)), axis=0)
    return rv

用法:

>>> apply_rule(7)
array([[0, 0, 0, 1, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 1, 1, 0, 0, 1, 0],
       [1, 1, 0, 1, 1, 1, 1]])

或者绘制成:

>>> plt.imshow(apply_rule(7), cmap='hot')

enter image description here

方法二 - 列表

另一种方法是不使用numpy,而是使用几个函数将Rule 30逻辑应用于每个填充列表中的三元组,直到满足停止条件。

代码:

def rule(t):
    return t[0] ^ (t[1] or t[2])

def initial_state(width):
    initial = [0]*width
    if width%2:
        initial[width // 2] = 1
    else:
        initial.insert(width//2, 1)
    return initial

def get_triples(l):
    return zip(l,l[1:],l[2:])       

def rule30(l):
    return [rule(t) for t in get_triples([0] + l + [0])]

def apply_rule(width):
    rv = [initial_state(width)]
    while not rv[-1][0]:
        rv.append(rule30(rv[-1]))
    return rv

使用方法:

>>> apply_rule(7)
[[0, 0, 0, 1, 0, 0, 0],
 [0, 0, 1, 1, 1, 0, 0],
 [0, 1, 1, 1, 0, 1, 0],
 [1, 1, 1, 0, 0, 1, 1]]
>>> [''.join(str(y) for y in x) for x in apply_rule(7)]
['0001000',
 '0011100',
 '0111010',
 '1110011']

Matplotlib 可视化(使用任何方法):

import matplotlib.pyplot as plt
plt.figure(figsize=(10,6))
plt.imshow(apply_rule(250), cmap='hot')

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接