I tried to implement a one-dimensional cellular automaton in Python

I wrote the code to draw the evolution of the one-dimensional cellular automaton in Python. Finally, the result of executing with some rules is posted.

Cellular automaton

Cellular automaton develops the state of a cell according to a local rule, and is a physicist Stephen Wolfram. 82% B9% E3% 83% 86% E3% 82% A3% E3% 83% BC% E3% 83% 96% E3% 83% B3% E3% 83% BB% E3% 82% A6% E3% 83% AB% E3% 83% 95% E3% 83% A9% E3% 83% A0) and mathematician John Horton Conway 83% A7% E3% 83% B3% E3% 83% BB% E3% 83% 9B% E3% 83% BC% E3% 83% 88% E3% 83% B3% E3% 83% BB% E3% 82% Its properties were studied in detail by B3% E3% 83% B3% E3% 82% A6% E3% 82% A7% E3% 82% A4) et al. By changing the rules, various natural phenomena can be imitated, and the two-dimensional one is known as "life game".

rule

Untitled Diagram.png

The rules for one-dimensional cellular automata are numbered. For example, the rule called rule 214 is as shown in the above figure. If you are not sure, please see this article for details.

Implemented in Python

Let's implement the above rule.

1d_ca.py


import matplotlib.pyplot as plt

def cellautomaton(l_bit, rule, pattern=False, padding=0):
    # pattern:Periodic boundary conditions, padding:Value of cells outside the frame
    l_bit_new = []
    if not pattern:
        l_bit = [padding] + l_bit
        l_bit.append(padding)
    else:
        l_bit = [l_bit[-1]] + l_bit
        l_bit.append(l_bit[1])
    for i in range(1, len(l_bit)-1):
        l_bit_new.append(next_state(l_bit[i-1],l_bit[i],l_bit[i+1], rule))
    return l_bit_new

def next_state(l, x, r, rule):
    #Determine the state of the next cell
    bin_str = format(rule, '08b')
    bin_num = int(str(l)+str(x)+str(r), 2)
    return int(bin_str[-(bin_num+1)])

def main():
    result = []
    loop = 200
    rule = 110 #Rule number(0~255)

    ##Specify the initial state in list x
    x = [0]*200
    x[-1] = 1
    result.append(x)
    for i in range(loop):
        x = cellautomaton(x, rule, pattern=False)
        result.append(x)
    plt.figure()
    plt.title("rule{}".format(rule))
    plt.imshow(result, cmap="binary")
    plt.show()

main()

By default, the length is 100, and only the rightmost cell is a one-dimensional array with 1 and 0. It is interesting to play around with it and change it. Finally, let's use this code to see an example with some rules.

Rule 30

[Stephen Wolfram](https://ja.wikipedia.org/wiki/%E3%82%B9%E3%83%86%E3%82%A3%E3%83%BC%E3%83%96%E3% 83% B3% E3% 83% BB% E3% 82% A6% E3% 83% AB% E3% 83% 95% E3% 83% A9% E3% 83% A0) has four behaviors of one-dimensional cellular automaton It is classified into classes. Simply put, it looks like this:

--Class 1: Converges to a state like all 0s, all 1s --Class 2: Converge to a state where fixed vibrations are repeated --Class 3: Become chaotic --Class 4: Periodic and chaotic states coexist (edge of chaos)

Rule 30 is a class 3 chaotic rule. rule30.png

You can see that the pattern is complicated and has no periodicity.

Rule 90

Rule 90 is a class 3 rule that draws a fractal shape called the Sierpinski gasket. rule90.png A fractal figure is a figure that appears in a similar shape when scaled.

Rule 110

Rule 110 is a class 4 rule that takes an intermediate state between periodic and chaos.

rule110.png

You can see that it has periodicity and breaks after a while. It is known that the dynamics between this order and chaos are often found in natural phenomena, and it is possible to imitate a Turing machine by using the rich dynamics of such areas.

that's all. There are many other rule shapes, such as this article. As a practical example, it seems that it is sometimes used in traffic simulation like this article.

References

[Illustrated trivia complex system (Norio Konno, Natsumesha)](https://hb.afl.rakuten.co.jp/ichiba/1bc63814.31bba873.1bc63815.5874eb3b/?pc=https%3A%2F%2Fitem.rakuten. co.jp% 2Fcomicset% 2F4816323899% 2F & link_type = hybrid_url & ut = eyJwYWdlIjoiaXRlbSIsInR5cGUiOiJoeWJyaWRfdXJsIiwic2l6ZSI6IjI0MHgyNDAiLCJuYW0iOjEsIm5hbXAiOiJyaWdodCIsImNvbSI6MSwiY29tcCI6ImRvd24iLCJwcmljZSI6MSwiYm9yIjoxLCJjb2wiOjEsImJidG4iOjEsInByb2QiOjB9)

Recommended Posts

I tried to implement a one-dimensional cellular automaton in Python
I tried to implement a pseudo pachislot in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
I tried to implement a card game of playing cards in Python
I tried to implement Dragon Quest poker in Python
I tried to implement GA (genetic algorithm) in Python
I tried to implement what seems to be a Windows snipping tool in Python
I tried "How to get a method decorated in Python"
I tried to implement the mail sending function in Python
I tried to make a stopwatch using tkinter in python
I tried to implement blackjack of card game in Python
[Python] I tried to implement stable sorting, so make a note
Implement a deterministic finite automaton in Python to determine multiples of 3
I want to create a window in Python
I tried playing a typing game in Python
[Memo] I tried a pivot table in Python
I tried adding a Python3 module in C
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to develop a Formatter that outputs Python logs in JSON
I tried to implement PCANet
I tried to implement StarGAN (1)
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
I tried to implement Minesweeper on terminal with python
I tried to draw a route map with Python
I want to write in Python! (2) Let's write a test
I tried to implement a recommendation system (content-based filtering)
I want to randomly sample a file in Python
I tried to implement an artificial perceptron with python
I want to work with a robot in python.
I tried to automatically generate a password with Python3
I tried to summarize how to use pandas in python
I tried to implement merge sort in Python with as few lines as possible
I tried to create a class that can easily serialize Json in Python
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
I tried to implement a volume moving average with Quantx
I tried to implement a basic Recurrent Neural Network model
I tried to implement Deep VQE
I tried to create API list.csv in Python from swagger.yaml
I tried to touch Python (installation)
[Markov chain] I tried to read a quote into Python.
I tried to implement adversarial validation
I tried "a program that removes duplicate statements in Python"
I created a class in Python and tried duck typing
I tried to implement hierarchical clustering
I tried to implement Realness GAN
A story about trying to implement a private variable in Python.
I want to make input () a nice complement in python
I tried Line notification in Python
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
I tried to automate "one heart even if separated" using a genetic algorithm in Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
I also tried to imitate the function monad and State monad with a generator in Python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"