回答編集履歴

4

ミスってました

2018/12/14 10:32

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -112,11 +112,9 @@
112
112
 
113
113
  def get_neighbors(i, j):
114
114
 
115
- i_pair = [x for x in [i+1, i-1] if 0 <= x < field_h]
115
+ return [x for x in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]
116
116
 
117
- j_pair = [x for x in [j+1, j-1] if 0 <= x < field_w]
117
+ if (0 <= x[0] < field_h) and (0 <= x[1] < field_w)]
118
-
119
- return list(product(i_pair, j_pair))
120
118
 
121
119
 
122
120
 

3

修正

2018/12/14 10:32

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -66,7 +66,7 @@
66
66
 
67
67
 
68
68
 
69
- infection_map = [[0.51 if random.random() > 0.995 else 0 for x in range(field_w)]
69
+ infection_map = [[0.51 if random.random() > 0.999 else 0 for x in range(field_w)]
70
70
 
71
71
  for y in range(field_h)]
72
72
 

2

編集

2018/12/12 01:37

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -166,6 +166,10 @@
166
166
 
167
167
  img2 = ax2.imshow(infection_map, cmap="gray", vmin=0, vmax=2)
168
168
 
169
+ plt.draw()
170
+
171
+ plt.pause(2)
172
+
169
173
 
170
174
 
171
175
  for i in range(1000):

1

追記

2018/12/12 01:27

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -37,3 +37,151 @@
37
37
 
38
38
 
39
39
  あとは全座標総なめにして、返り値を次のステップのフィールド用の配列に突っ込んでいけば更新できます。
40
+
41
+
42
+
43
+ ###### 追記
44
+
45
+ ```python
46
+
47
+ import random
48
+
49
+ from itertools import product
50
+
51
+ import matplotlib.pyplot as plt
52
+
53
+
54
+
55
+ alpha = 0.7
56
+
57
+ field_w, field_h = 100, 100
58
+
59
+
60
+
61
+ # animal_map = [[random.randrange(2) for x in range(field_w)]
62
+
63
+ # for y in range(field_h)]
64
+
65
+ animal_map = [[0]*field_w]*(field_h//2) + [[1]*field_w]*(field_h//2)
66
+
67
+
68
+
69
+ infection_map = [[0.51 if random.random() > 0.995 else 0 for x in range(field_w)]
70
+
71
+ for y in range(field_h)]
72
+
73
+
74
+
75
+ color_conds = {(True, True): 1, (True, False):2,
76
+
77
+ (False, True):3, (False, False):4}
78
+
79
+ def gen_color_map():
80
+
81
+ result = [[None]*field_w for _ in range(field_h)]
82
+
83
+ for i in range(field_h):
84
+
85
+ for j in range(field_w):
86
+
87
+ result[i][j] = color_conds[(animal_map[i][j] == 0,
88
+
89
+ infection_map[i][j] > 0.5)]
90
+
91
+ return result
92
+
93
+
94
+
95
+ color_map = gen_color_map()
96
+
97
+
98
+
99
+ def next_field():
100
+
101
+ result = [[None]*field_w for _ in range(field_h)]
102
+
103
+ for i in range(field_h):
104
+
105
+ for j in range(field_w):
106
+
107
+ result[i][j] = next_cell(i, j)
108
+
109
+ return result
110
+
111
+
112
+
113
+ def get_neighbors(i, j):
114
+
115
+ i_pair = [x for x in [i+1, i-1] if 0 <= x < field_h]
116
+
117
+ j_pair = [x for x in [j+1, j-1] if 0 <= x < field_w]
118
+
119
+ return list(product(i_pair, j_pair))
120
+
121
+
122
+
123
+ # infect_probs = {(0,0):0.25, (0,1):0.10, (1,0):0.75, (1,1):0.40}
124
+
125
+ infect_probs = {(0,0):0.18, (0,1):0.10, (1,0):0.55, (1,1):0.20}
126
+
127
+ def next_cell(i, j):
128
+
129
+ if infection_map[i][j] > 0.5:
130
+
131
+ return infection_map[i][j]*(1-alpha)
132
+
133
+ else:
134
+
135
+ self_kind = animal_map[i][j]
136
+
137
+ s = 0
138
+
139
+ for ni, nj in get_neighbors(i, j):
140
+
141
+ n_kind = animal_map[ni][nj]
142
+
143
+ n_infection = infection_map[ni][nj]
144
+
145
+ s += infect_probs[(n_kind, self_kind)] * n_infection
146
+
147
+ return infection_map[i][j]*(1-alpha) + s
148
+
149
+
150
+
151
+ def exec_field():
152
+
153
+ global infection_map, color_map
154
+
155
+ infection_map = next_field()
156
+
157
+ color_map = gen_color_map()
158
+
159
+
160
+
161
+ fig, axes = plt.subplots(ncols=2)
162
+
163
+ ax, ax2 = axes
164
+
165
+ img = ax.imshow(color_map)
166
+
167
+ img2 = ax2.imshow(infection_map, cmap="gray", vmin=0, vmax=2)
168
+
169
+
170
+
171
+ for i in range(1000):
172
+
173
+ print(i, "step")
174
+
175
+ exec_field()
176
+
177
+ img.set_data(color_map)
178
+
179
+ img2.set_data(infection_map)
180
+
181
+ plt.draw()
182
+
183
+ plt.pause(0.05)
184
+
185
+
186
+
187
+ ```