added exam ex13

This commit is contained in:
Daniel Knüttel 2019-02-23 21:01:16 +01:00
parent d813e674a0
commit b3b6692de8
3 changed files with 15 additions and 14 deletions

View File

@ -35,9 +35,10 @@ def count_living_cells_around(model, i, j):
cells_around = get_cells_around(model, i, j)
living_cells_around = (cells_around & CELL_IS_ALIVE) == 1
living_cells_around[1][1] = False
_, counts = np.unique(living_cells_around, return_counts=True)
print(counts)
return counts[True]
unique, counts = np.unique(living_cells_around, return_counts=True)
if(True not in unique):
return 0
return counts[np.where(unique == True)[0]]
@ -60,5 +61,7 @@ def after_tick_finished(model):
def total_living_cells(model):
living_cells = (model & CELL_IS_ALIVE) == 1
_, counts = np.unique(living_cells, return_counts=True)
return counts[True]
unique, counts = np.unique(living_cells, return_counts=True)
if(True not in unique):
return 0
return counts[np.where(unique == True)[0]]

View File

@ -7,9 +7,9 @@ from executor import execute_tick
def cells_alive_at_ticks(model, ticks):
for i in range(ticks):
execute_tick(model)
cell_count = total_living_cells(model)
yield i, cell_count
yield [i, cell_count]
execute_tick(model)
plot_data = {
0.3 : ("r", "p=0.3")
@ -17,9 +17,12 @@ plot_data = {
, 0.7: ("b", "p=0.7")
}
handles = []
for p, (color, label) in plot_data.items():
model = prepare_model(100, p)
living_cells = np.array(list(cells_alive_at_ticks(model, 1000)))
plt.plot(living_cells[:, 0], living_cells[:, 1], color, label=label)
living_cells = np.array(list(cells_alive_at_ticks(model, 10)))
plot, = plt.plot(living_cells[:, 0], living_cells[:, 1], color, label=label)
handles.append(plot)
plt.legend(handles=handles)
plt.show()

View File

@ -6,8 +6,3 @@ def prepare_model(n, p):
"""
return (np.random.rand(n, n) < p).astype(np.int8)