Exemplo n.º 1
0
def main(weights, height, width, init_type=1, step_size=100):
    state = init_state(init_type, height, width)
    visualizer = MatrixVisualizer()
    for i in range(step_size):
        print(i)
        state = update_state(state, weights)
        visualizer.update(1 - state)
    input('enter to finish')
# pattern = game_of_life_patterns.STATIC
# pattern = game_of_life_patterns.GLIDER
pattern = game_of_life_patterns.GLIDER_GUN
state[2:2 + pattern.shape[0], 2:2 + pattern.shape[1]] = pattern

while visualizer:  # visualizerはウィンドウが閉じられるとFalseを返す
    for i in range(HEIGHT):
        for j in range(WIDTH):
            # 自分と近傍のセルの状態を取得
            # c: center (自分自身)
            # nw: north west, ne: north east, c: center ...
            nw = state[i - 1, j - 1]
            n = state[i - 1, j]
            ne = state[i - 1, (j + 1) % WIDTH]
            w = state[i, j - 1]
            c = state[i, j]
            e = state[i, (j + 1) % WIDTH]
            sw = state[(i + 1) % HEIGHT, j - 1]
            s = state[(i + 1) % HEIGHT, j]
            se = state[(i + 1) % HEIGHT, (j + 1) % WIDTH]
            neighbor_cell_sum = nw + n + ne + w + e + sw + s + se
            if c == 0 and neighbor_cell_sum == 3:
                next_state[i, j] = 1
            elif c == 1 and neighbor_cell_sum in (2, 3):
                next_state[i, j] = 1
            else:
                next_state[i, j] = 0
    state, next_state = next_state, state
    # 表示をアップデート
    visualizer.update(1 - state)  # 1を黒, 0を白で表示する
Exemplo n.º 3
0
a_res = 1.02
a_res_end = 1.0
a_res_step = (a_res - a_res_end) / 2000

# 初期化
# (ここで行っている初期配置に関しては、冒頭の論文のp104に記載あり。)
a = np.ones((X_SIZE, Y_SIZE))
b = np.zeros((X_SIZE, Y_SIZE))
c = np.zeros((X_SIZE, Y_SIZE))

square_size_real = 0.1
a[100:110,100:110] = 0.45 + np.random.rand(10, 10)*0.1
b[100:110,100:110] = 0.45 + np.random.rand(10, 10)*0.1
c[101:108,112:119] = 1.5

while visualizer:
    for i in range(visualization_step):
        laplacian_a = (np.roll(a, 1, axis=0) + np.roll(a, -1, axis=0) + np.roll(a, 1, axis=1) + np.roll(a, -1, axis=1) - 4*a) / (dx*dx)
        laplacian_b = (np.roll(b, 1, axis=0) + np.roll(b, -1, axis=0) + np.roll(b, 1, axis=1) + np.roll(b, -1, axis=1) - 4*b) / (dx*dx)
        laplacian_c = (np.roll(c, 1, axis=0) + np.roll(c, -1, axis=0) + np.roll(c, 1, axis=1) + np.roll(c, -1, axis=1) - 4*c) / (dx*dx)
        dadt = Da * laplacian_a - a*b*b + r*(a_res - a)
        dbdt = Db * laplacian_b + a*b*b - k_2*b*c*c - k_1*b
        dcdt = Dc * laplacian_c + k_2*b*c*c - k_3*c
        a += dt * dadt
        b += dt * dbdt
        c += dt * dcdt
        # a_resを減らす(a_res_endよりは減らさない)
        a_res = max(a_res - a_res_step, a_res_end)
    # ここでは、b + c をグレースケールで表示。見たいものに変更してみましょう。
    visualizer.update(b+c)
Exemplo n.º 4
0
# 中央に初期パターンを置く
square_size = 10
a[X_SIZE // 2 - square_size // 2:X_SIZE // 2 + square_size // 2,
  Y_SIZE // 2 - square_size // 2:Y_SIZE // 2 + square_size // 2] = 0
b[X_SIZE // 2 - square_size // 2:X_SIZE // 2 + square_size // 2,
  Y_SIZE // 2 - square_size // 2:Y_SIZE // 2 + square_size // 2] = 1

a += np.random.rand(X_SIZE, Y_SIZE) * 0.1
b += np.random.rand(X_SIZE, Y_SIZE) * 0.1
p += np.random.rand(X_SIZE, Y_SIZE) * 0.1

while visualizer:
    for i in range(visualization_step):
        # ラプラシアンの計算
        laplacian_a = (np.roll(a, 1, axis=0) + np.roll(a, -1, axis=0) +
                       np.roll(a, 1, axis=1) + np.roll(a, -1, axis=1) -
                       4 * a) / (dx * dx)
        laplacian_b = (np.roll(b, 1, axis=0) + np.roll(b, -1, axis=0) +
                       np.roll(b, 1, axis=1) + np.roll(b, -1, axis=1) -
                       4 * b) / (dx * dx)
        # アップデート
        dadt = Da * laplacian_a - np.exp(-w * p) * a * b * b + r * (1.0 - a)
        dbdt = Db * laplacian_b + np.exp(-w * p) * a * b * b - k * b
        dpdt = k * b - k_p * p
        a += dt * dadt
        b += dt * dbdt
        p += dt * dpdt
    # ここでは、b + p / 50 をグレースケールで表示。見たいものに変更してみましょう。
    visualizer.update(b + p / 50)
Exemplo n.º 5
0
v = img_pixels_norm * 0.25

# 中央にSQUARE_SIZE四方の正方形を置く
"""
SQUARE_SIZE = 20
u[SPACE_GRID_SIZE//2-SQUARE_SIZE//2:SPACE_GRID_SIZE//2+SQUARE_SIZE//2,
  SPACE_GRID_SIZE//2-SQUARE_SIZE//2:SPACE_GRID_SIZE//2+SQUARE_SIZE//2] = 0.5
v[SPACE_GRID_SIZE//2-SQUARE_SIZE//2:SPACE_GRID_SIZE//2+SQUARE_SIZE//2,
  SPACE_GRID_SIZE//2-SQUARE_SIZE//2:SPACE_GRID_SIZE//2+SQUARE_SIZE//2] = 0.25
"""
# 対称性を壊すために、少しノイズを入れる
u += np.random.rand(SPACE_GRID_SIZE, SPACE_GRID_SIZE) * 0.1
v += np.random.rand(SPACE_GRID_SIZE, SPACE_GRID_SIZE) * 0.1

while visualizer:  # visualizerはウィンドウが閉じられるとFalseを返す
    for i in range(VISUALIZATION_STEP):
        # ラプラシアンの計算
        laplacian_u = (np.roll(u, 1, axis=0) + np.roll(u, -1, axis=0) +
                       np.roll(u, 1, axis=1) + np.roll(u, -1, axis=1) -
                       4 * u) / (dx * dx)
        laplacian_v = (np.roll(v, 1, axis=0) + np.roll(v, -1, axis=0) +
                       np.roll(v, 1, axis=1) + np.roll(v, -1, axis=1) -
                       4 * v) / (dx * dx)
        # Gray-Scottモデル方程式
        dudt = Du * laplacian_u - u * v * v + f * (1.0 - u)
        dvdt = Dv * laplacian_v + u * v * v - (f + k) * v
        u += dt * dudt
        v += dt * dvdt
    # 表示をアップデート
    visualizer.update(u)
Exemplo n.º 6
0
u[0, SPACE_SIZE // 2 - INIT_PATTERN_SIZE // 2:SPACE_SIZE // 2 +
  INIT_PATTERN_SIZE // 2] = 0.5
v[0, SPACE_SIZE // 2 - INIT_PATTERN_SIZE // 2:SPACE_SIZE // 2 +
  INIT_PATTERN_SIZE // 2] = 0.25
# 対称性を壊すために、少しノイズを入れる
u[0, :] += np.random.rand(SPACE_SIZE) * 0.01
v[0, :] += np.random.rand(SPACE_SIZE) * 0.01

t = 0
while visualizer:  # visualizerはウィンドウが閉じられるとFalseを返す
    for i in range(visualization_step):
        current_line = (t * visualization_step + i) % VISUALIZATION_TIME
        next_line = (current_line + 1) % VISUALIZATION_TIME
        current_u = u[current_line]
        current_v = v[current_line]
        # ラプラシアンの計算
        laplacian_u = (np.roll(current_u, 1) + np.roll(current_u, -1) -
                       2 * current_u) / (dx * dx)
        laplacian_v = (np.roll(current_v, 1) + np.roll(current_v, -1) -
                       2 * current_v) / (dx * dx)
        # Gray-Scottモデル方程式
        dudt = Du * laplacian_u - current_u * current_v * current_v + f * (
            1.0 - current_u)
        dvdt = Dv * laplacian_v + current_u * current_v * current_v - (
            f + k) * current_v
        u[next_line] = current_u + dt * dudt
        v[next_line] = current_v + dt * dvdt
        t += 1
    # 表示をアップデート。uは0-1なので、255階調に変換する。
    visualizer.update(u * 256)