verkehrsimulation
This commit is contained in:
parent
424a7ced87
commit
014df61e4f
55
verkehrssimulation/verkehr.py
Normal file
55
verkehrssimulation/verkehr.py
Normal file
@ -0,0 +1,55 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
GRIDSIZE = 300
|
||||
INIT_CELLS = int(GRIDSIZE*0.3)
|
||||
MAX_TIME = 300
|
||||
EMPTY_CELL = -1
|
||||
DALLY_LIMIT = 0.2
|
||||
|
||||
|
||||
# query distance between two cars
|
||||
def get_distance(grid, i):
|
||||
dist = 0
|
||||
while grid[(i+dist+1)%GRIDSIZE] == EMPTY_CELL and dist < 5:
|
||||
dist = dist + 1
|
||||
return dist
|
||||
|
||||
# state transition t -> t + dt
|
||||
def update(grid_old, grid_new):
|
||||
for i in range(GRIDSIZE):
|
||||
|
||||
if grid_old[i] != EMPTY_CELL:
|
||||
v = np.min((grid_old[i]+1, 5)) # accelerate
|
||||
dist = get_distance(grid_old, i)
|
||||
if v > dist: v = dist # break
|
||||
p = np.random.random()
|
||||
if p == DALLY_LIMIT: v = max(grid_old[i-1], 0)
|
||||
grid_new[(i+v)%GRIDSIZE] = v # move
|
||||
|
||||
# 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()
|
||||
@ -9,12 +9,6 @@ INIT_CELLS = int(GRIDSIZE*0.1)
|
||||
MAX_TIME = 300
|
||||
EMPTY_CELL = -1
|
||||
|
||||
def unit_distance(grid, v):
|
||||
for i in range(1, v):
|
||||
if grid[i] != -1:
|
||||
return i
|
||||
return -1
|
||||
|
||||
# query distance between two cars
|
||||
def get_distance(grid, i):
|
||||
dist = 0
|
||||
@ -23,10 +17,7 @@ def get_distance(grid, i):
|
||||
Insert your code here
|
||||
"""
|
||||
|
||||
print(i % 300)
|
||||
quit()
|
||||
|
||||
for n in range(1, grid[i]+1):
|
||||
for n in range(1, (grid[i]+1)&GRIDSIZE):
|
||||
if grid[i + n] != -1:
|
||||
return n
|
||||
return -1
|
||||
@ -40,15 +31,7 @@ def update(grid_old, grid_new):
|
||||
Insert your code here
|
||||
"""
|
||||
if dist != -1:
|
||||
if dist == 0:
|
||||
grid_new[i] = 0
|
||||
elif dist - grid_old[i] < 0:
|
||||
grid_new[i] = dist - 1
|
||||
else:
|
||||
if grid_old[i] != 5:
|
||||
grid_new[i] += 1
|
||||
else:
|
||||
grid_new[i] = grid_old[i]
|
||||
print(dist)
|
||||
|
||||
# allocate memory and initialise grids
|
||||
grid_old = np.full((GRIDSIZE), EMPTY_CELL, dtype=np.int32)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user