added game of life and verkehrssimulation

This commit is contained in:
git-sandro 2025-11-28 10:58:28 +01:00
parent b234517791
commit 4ffdefead9
2 changed files with 120 additions and 0 deletions

66
game_of_life/conway_sk.py Normal file
View File

@ -0,0 +1,66 @@
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
GRIDSIZE = 50 # grid size in x- and y-direction
INIT_CELLS = 1000 # number of cells initially set 'alive'
# query number of 'living' neighbours
def get_neighbours(grid, i, j):
living = 0
""" Check all neighbours for status """
"""
Insert your code here
"""
living = grid[i-1,j] + grid[i-1,j+1] + grid[i,j+1] + grid[i+1,j+1] + grid[i+1,j] + grid[i+1,j-1] + grid[i,j-1] + grid[i-1,j-1]
return living
# state transition t -> t + dt
def update(grid_old, grid_new):
for i in range(1, GRIDSIZE+1):
for j in range(1, GRIDSIZE+1):
nln = get_neighbours(grid_old, i, j)
""" Update cells according to rules using value 'nln' """
"""
Insert your code here
"""
if grid_old[i,j] == 0 and nln == 3:
grid_new[i,j] = 1
elif grid_old[i,j] == 1 and nln < 2:
grid_new[i,j] = 0
elif grid_old[i,j] == 1 and (nln == 2 or nln == 3):
grid_new[i,j] = 1
elif grid_old[i,j] == 1 and nln > 3:
grid_new[i,j] == 0
# allocate memory and initialise grids
grid_old = np.zeros((GRIDSIZE+2, GRIDSIZE+2), dtype=np.int32)
grid_new = np.zeros((GRIDSIZE+2, GRIDSIZE+2), dtype=np.int32)
# set random starting points
no_repeat = []
for k in range(INIT_CELLS):
i = int(float(GRIDSIZE)*np.random.random())+1
j = int(float(GRIDSIZE)*np.random.random())+1
grid_old[i,j] = 1
# run updates
plt.ion()
plt.imshow(grid_old)
plt.pause(0.1)
for t in range(50):
plt.clf()
update(grid_old, grid_new)
for i in range(GRIDSIZE+2):
for j in range(GRIDSIZE+2):
grid_old[i,j] = grid_new[i,j]
grid_new[i,j] = 0
plt.imshow(grid_old)
plt.pause(0.1)
plt.ioff()

View File

@ -0,0 +1,54 @@
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
GRIDSIZE = 300
INIT_CELLS = int(GRIDSIZE*0.1)
MAX_TIME = 300
EMPTY_CELL = -1
# query distance between two cars
def get_distance(grid, i):
dist = 0
""" Compute distance between current position and next car """
"""
Insert your code here
"""
return dist
# state transition t -> t + dt
def update(grid_old, grid_new):
for i in range(GRIDSIZE):
dist = get_distance(grid_old, i)
""" Update cars according rules using value 'dist' """
"""
Insert your code here
"""
# allocate memory and initialise grids
grid_old = np.full((GRIDSIZE), EMPTY_CELL, dtype=np.int32)
grid_new = np.full((GRIDSIZE), EMPTY_CELL, dtype=np.int32)
traffic = np.zeros((MAX_TIME, GRIDSIZE), dtype=np.int32)
# set intial car positions and velocities
for k in range(INIT_CELLS):
while True:
i = int(float(GRIDSIZE)*np.random.random())
if grid_old[i] == EMPTY_CELL:
grid_old[i] = int(float(6)*np.random.random())
break
# run updates
for t in range(MAX_TIME):
traffic[t,:] = grid_old[:]
update(grid_old, grid_new)
for i in range(GRIDSIZE):
grid_old[i] = grid_new[i]
grid_new[i] = EMPTY_CELL
plt.xlabel('Cells')
plt.ylabel('Timesteps')
plt.imshow(traffic, cmap='Blues')
plt.show()