コード例 #1
0
def day_and_night(state, k=None):
    """
    'Day & night' automata state transition
    http://www.conwaylife.com/wiki/Day_%26_Night
    """
    # set up kernel if not given
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1],
                                                                [1, 0, 1],
                                                                [1, 1, 1]])

    # computes sums around each pixel
    b = fft_convolve2d(state, k).round()
    c = np.zeros(b.shape)

    c[np.where((b == 3) & (state == 1))] = 1
    c[np.where((b == 6) & (state == 1))] = 1
    c[np.where((b == 7) & (state == 1))] = 1
    c[np.where((b == 8) & (state == 1))] = 1

    c[np.where((b == 3) & (state == 0))] = 1
    c[np.where((b == 4) & (state == 0))] = 1
    c[np.where((b == 6) & (state == 0))] = 1
    c[np.where((b == 7) & (state == 0))] = 1
    c[np.where((b == 8) & (state == 0))] = 1

    # return new state
    return c
コード例 #2
0
ファイル: two_by_two.py プロジェクト: wsyxbcl/game-of-life
def two_by_two(state, k=None):
    """
    '2x2' automata state transition
    http://www.conwaylife.com/wiki/2x2
    """
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1],
                                                                [1, 0, 1],
                                                                [1, 1, 1]])

    # computes sums around each pixel
    b = fft_convolve2d(state, k).round()
    c = np.zeros(b.shape)
    # checks the values, and sets alive vs. dead state

    c[np.where((b == 1) & (state == 1))] = 1
    c[np.where((b == 2) & (state == 1))] = 1
    c[np.where((b == 5) & (state == 1))] = 1

    c[np.where((b == 3) & (state == 0))] = 1
    c[np.where((b == 6) & (state == 0))] = 1

    # return new state
    return c
コード例 #3
0
ファイル: custom.py プロジェクト: 2016011ak47/game-of-life
def automata(state, rule = 'B3/S23', k=None):
    """
    Apply a custom automata transition from a rulestring
    """
    # set up kernel if not given
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

    # computes sums around each pixel
    b = fft_convolve2d(state,k).round()
    c = np.zeros(b.shape)
    # checks the values, and sets alive vs. dead state

    born = [int(i) for i in rule.split('/')[0].replace('B','')]
    survive = [int(i) for i in rule.split('/')[1].replace('S','')]

    for i in born:
        c[np.where((b == i) & (state == 0))] = 1
    for i in survive:
        c[np.where((b == i) & (state == 1))] = 1

    # return new state
    return c
コード例 #4
0
def day_and_night(state, k=None):
    """
    'Day & night' automata state transition
    http://www.conwaylife.com/wiki/Day_%26_Night
    """
    # set up kernel if not given
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

    # computes sums around each pixel
    b = fft_convolve2d(state,k).round()
    c = np.zeros(b.shape)

    c[np.where((b == 3) & (state == 1))] = 1
    c[np.where((b == 6) & (state == 1))] = 1
    c[np.where((b == 7) & (state == 1))] = 1
    c[np.where((b == 8) & (state == 1))] = 1

    c[np.where((b == 3) & (state == 0))] = 1
    c[np.where((b == 4) & (state == 0))] = 1
    c[np.where((b == 6) & (state == 0))] = 1
    c[np.where((b == 7) & (state == 0))] = 1
    c[np.where((b == 8) & (state == 0))] = 1

    # return new state
    return c
コード例 #5
0
ファイル: custom.py プロジェクト: wsyxbcl/game-of-life
def automata(state, rule='B3/S23', k=None):
    """
    Apply a custom automata transition from a rulestring
    """
    # set up kernel if not given
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1],
                                                                [1, 0, 1],
                                                                [1, 1, 1]])

    # computes sums around each pixel
    b = fft_convolve2d(state, k).round()
    c = np.zeros(b.shape)
    # checks the values, and sets alive vs. dead state

    born = [int(i) for i in rule.split('/')[0].replace('B', '')]
    survive = [int(i) for i in rule.split('/')[1].replace('S', '')]

    for i in born:
        c[np.where((b == i) & (state == 0))] = 1
    for i in survive:
        c[np.where((b == i) & (state == 1))] = 1

    # return new state
    return c
コード例 #6
0
    def test_uniform(self):

        A = np.random.randn(10, 10)
        K = np.ones((10, 10))
        a, b = fft_convolve2d(A, K).max(), A.sum()

        self.assertAlmostEqual(a, b)
コード例 #7
0
    def test_uniform(self):

        A = np.random.randn(10,10)
        K = np.ones((10,10))
        a,b = fft_convolve2d(A,K).max(), A.sum()

        self.assertAlmostEqual(a, b)
コード例 #8
0
def seeds(state, k):
    """
    'Seeds' cellular automaton state transition
    http://www.conwaylife.com/wiki/Seeds
    """
    b = fft_convolve2d(state,k).round()
    c = np.zeros(b.shape)
    # checks the values, and sets alive vs. dead state
    c[np.where((b == 2) & (state == 0))] = 1

    # return new state
    return c
コード例 #9
0
ファイル: seeds.py プロジェクト: wsyxbcl/game-of-life
def seeds(state, k):
    """
    'Seeds' cellular automaton state transition
    http://www.conwaylife.com/wiki/Seeds
    """
    b = fft_convolve2d(state,k).round()
    c = np.zeros(b.shape)
    # checks the values, and sets alive vs. dead state
    c[np.where((b == 2) & (state == 0))] = 1

    # return new state
    return c
コード例 #10
0
def replicator(state, k=None):
    """
    'Replicator' cellular automaton state transition
    http://www.conwaylife.com/wiki/Replicator_(CA)
    """
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

    b = fft_convolve2d(state,k).round()
    c = np.zeros(b.shape)
    # checks the values, and sets alive vs. dead state
    c[np.where((b + 1) % 2 == 0)] = 1

    # return new state
    return c
コード例 #11
0
def replicator(state, k=None):
    """
    'Replicator' cellular automaton state transition
    http://www.conwaylife.com/wiki/Replicator_(CA)
    """
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1],
                                                                [1, 0, 1],
                                                                [1, 1, 1]])

    b = fft_convolve2d(state, k).round()
    c = np.zeros(b.shape)
    # checks the values, and sets alive vs. dead state
    c[np.where((b + 1) % 2 == 0)] = 1

    # return new state
    return c
コード例 #12
0
ファイル: conway.py プロジェクト: terrabakky/game-of-life
def conway(state, k=None):
    """
    Conway's game of life state transition
    """

    # set up kernel if not given
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

    # computes sums around each pixel
    b = fft_convolve2d(state,k).round()
    c = np.zeros(b.shape)

    c[np.where((b == 2) & (state == 1))] = 1
    c[np.where((b == 3) & (state == 1))] = 1

    c[np.where((b == 3) & (state == 0))] = 1

    # return new state
    return c
コード例 #13
0
ファイル: highlife.py プロジェクト: 2016011ak47/game-of-life
def high_life(state, k=None):
    """
    'HighLife' automata state transition
    http://www.conwaylife.com/wiki/HighLife
    """
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

    # computes sums around each pixel
    b = fft_convolve2d(state,k).round()
    c = np.zeros(b.shape)

    c[np.where((b == 2) & (state == 1))] = 1
    c[np.where((b == 3) & (state == 1))] = 1

    c[np.where((b == 3) & (state == 0))] = 1
    c[np.where((b == 6) & (state == 0))] = 1

    # return new state
    return c
コード例 #14
0
ファイル: highlife.py プロジェクト: terrabakky/game-of-life
def high_life(state, k=None):
    """
    'HighLife' automata state transition
    http://www.conwaylife.com/wiki/HighLife
    """
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

    # computes sums around each pixel
    b = fft_convolve2d(state,k).round()
    c = np.zeros(b.shape)

    c[np.where((b == 2) & (state == 1))] = 1
    c[np.where((b == 3) & (state == 1))] = 1

    c[np.where((b == 3) & (state == 0))] = 1
    c[np.where((b == 6) & (state == 0))] = 1

    # return new state
    return c
コード例 #15
0
ファイル: image.py プロジェクト: wsyxbcl/game-of-life
def conway(state, k=None):
    """
    Conway's game of life state transition
    """

    # set up kernel if not given
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m / 2 - 1:m / 2 + 2, n / 2 - 1:n / 2 + 2] = np.array([[1, 1, 1],
                                                                [1, 0, 1],
                                                                [1, 1, 1]])

    # computes sums around each pixel
    b = fft_convolve2d(state, k).round()
    c = np.zeros(b.shape)

    c[np.where((b == 2) & (state == 1))] = 1
    c[np.where((b == 3) & (state == 1))] = 1

    c[np.where((b == 3) & (state == 0))] = 1

    # return new state
    return c
コード例 #16
0
def two_by_two(state, k=None):
    """
    '2x2' automata state transition
    http://www.conwaylife.com/wiki/2x2
    """
    if k == None:
        m, n = state.shape
        k = np.zeros((m, n))
        k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

    # computes sums around each pixel
    b = fft_convolve2d(state,k).round()
    c = np.zeros(b.shape)
    # checks the values, and sets alive vs. dead state

    c[np.where((b == 1) & (state == 1))] = 1
    c[np.where((b == 2) & (state == 1))] = 1
    c[np.where((b == 5) & (state == 1))] = 1

    c[np.where((b == 3) & (state == 0))] = 1
    c[np.where((b == 6) & (state == 0))] = 1

    # return new state
    return c