回答編集履歴
4
ミスってました
answer
CHANGED
@@ -55,9 +55,8 @@
|
|
55
55
|
return result
|
56
56
|
|
57
57
|
def get_neighbors(i, j):
|
58
|
+
return [x for x in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]
|
58
|
-
|
59
|
+
if (0 <= x[0] < field_h) and (0 <= x[1] < field_w)]
|
59
|
-
j_pair = [x for x in [j+1, j-1] if 0 <= x < field_w]
|
60
|
-
return list(product(i_pair, j_pair))
|
61
60
|
|
62
61
|
# infect_probs = {(0,0):0.25, (0,1):0.10, (1,0):0.75, (1,1):0.40}
|
63
62
|
infect_probs = {(0,0):0.18, (0,1):0.10, (1,0):0.55, (1,1):0.20}
|
3
修正
answer
CHANGED
@@ -32,7 +32,7 @@
|
|
32
32
|
# for y in range(field_h)]
|
33
33
|
animal_map = [[0]*field_w]*(field_h//2) + [[1]*field_w]*(field_h//2)
|
34
34
|
|
35
|
-
infection_map = [[0.51 if random.random() > 0.
|
35
|
+
infection_map = [[0.51 if random.random() > 0.999 else 0 for x in range(field_w)]
|
36
36
|
for y in range(field_h)]
|
37
37
|
|
38
38
|
color_conds = {(True, True): 1, (True, False):2,
|
2
編集
answer
CHANGED
@@ -82,6 +82,8 @@
|
|
82
82
|
ax, ax2 = axes
|
83
83
|
img = ax.imshow(color_map)
|
84
84
|
img2 = ax2.imshow(infection_map, cmap="gray", vmin=0, vmax=2)
|
85
|
+
plt.draw()
|
86
|
+
plt.pause(2)
|
85
87
|
|
86
88
|
for i in range(1000):
|
87
89
|
print(i, "step")
|
1
追記
answer
CHANGED
@@ -17,4 +17,78 @@
|
|
17
17
|
|
18
18
|
セルオートマトンを書くなら、セルの座標値`i,j`を取って次の状態を返す関数を定義するところから始めると良いと思います。
|
19
19
|
|
20
|
-
あとは全座標総なめにして、返り値を次のステップのフィールド用の配列に突っ込んでいけば更新できます。
|
20
|
+
あとは全座標総なめにして、返り値を次のステップのフィールド用の配列に突っ込んでいけば更新できます。
|
21
|
+
|
22
|
+
###### 追記
|
23
|
+
```python
|
24
|
+
import random
|
25
|
+
from itertools import product
|
26
|
+
import matplotlib.pyplot as plt
|
27
|
+
|
28
|
+
alpha = 0.7
|
29
|
+
field_w, field_h = 100, 100
|
30
|
+
|
31
|
+
# animal_map = [[random.randrange(2) for x in range(field_w)]
|
32
|
+
# for y in range(field_h)]
|
33
|
+
animal_map = [[0]*field_w]*(field_h//2) + [[1]*field_w]*(field_h//2)
|
34
|
+
|
35
|
+
infection_map = [[0.51 if random.random() > 0.995 else 0 for x in range(field_w)]
|
36
|
+
for y in range(field_h)]
|
37
|
+
|
38
|
+
color_conds = {(True, True): 1, (True, False):2,
|
39
|
+
(False, True):3, (False, False):4}
|
40
|
+
def gen_color_map():
|
41
|
+
result = [[None]*field_w for _ in range(field_h)]
|
42
|
+
for i in range(field_h):
|
43
|
+
for j in range(field_w):
|
44
|
+
result[i][j] = color_conds[(animal_map[i][j] == 0,
|
45
|
+
infection_map[i][j] > 0.5)]
|
46
|
+
return result
|
47
|
+
|
48
|
+
color_map = gen_color_map()
|
49
|
+
|
50
|
+
def next_field():
|
51
|
+
result = [[None]*field_w for _ in range(field_h)]
|
52
|
+
for i in range(field_h):
|
53
|
+
for j in range(field_w):
|
54
|
+
result[i][j] = next_cell(i, j)
|
55
|
+
return result
|
56
|
+
|
57
|
+
def get_neighbors(i, j):
|
58
|
+
i_pair = [x for x in [i+1, i-1] if 0 <= x < field_h]
|
59
|
+
j_pair = [x for x in [j+1, j-1] if 0 <= x < field_w]
|
60
|
+
return list(product(i_pair, j_pair))
|
61
|
+
|
62
|
+
# infect_probs = {(0,0):0.25, (0,1):0.10, (1,0):0.75, (1,1):0.40}
|
63
|
+
infect_probs = {(0,0):0.18, (0,1):0.10, (1,0):0.55, (1,1):0.20}
|
64
|
+
def next_cell(i, j):
|
65
|
+
if infection_map[i][j] > 0.5:
|
66
|
+
return infection_map[i][j]*(1-alpha)
|
67
|
+
else:
|
68
|
+
self_kind = animal_map[i][j]
|
69
|
+
s = 0
|
70
|
+
for ni, nj in get_neighbors(i, j):
|
71
|
+
n_kind = animal_map[ni][nj]
|
72
|
+
n_infection = infection_map[ni][nj]
|
73
|
+
s += infect_probs[(n_kind, self_kind)] * n_infection
|
74
|
+
return infection_map[i][j]*(1-alpha) + s
|
75
|
+
|
76
|
+
def exec_field():
|
77
|
+
global infection_map, color_map
|
78
|
+
infection_map = next_field()
|
79
|
+
color_map = gen_color_map()
|
80
|
+
|
81
|
+
fig, axes = plt.subplots(ncols=2)
|
82
|
+
ax, ax2 = axes
|
83
|
+
img = ax.imshow(color_map)
|
84
|
+
img2 = ax2.imshow(infection_map, cmap="gray", vmin=0, vmax=2)
|
85
|
+
|
86
|
+
for i in range(1000):
|
87
|
+
print(i, "step")
|
88
|
+
exec_field()
|
89
|
+
img.set_data(color_map)
|
90
|
+
img2.set_data(infection_map)
|
91
|
+
plt.draw()
|
92
|
+
plt.pause(0.05)
|
93
|
+
|
94
|
+
```
|