I have wrote a simulation in python based on Nagel–Schreckenberg model but It dont seems to work do you know why?
import numpy as np
import random
import time
from IPython.display import clear_output
def delay():
time.sleep(1)
L = 100 #length
d = .22 #density
start_s = 1 #start speed
max_s = 5 #max speed
acc = 1 #acceleration
rand = 0.1
#initialise
road = np.zeros(L, dtype='int32')
for i in range(road.size):
if random.random() < d:
road[i] = start_s
def mod(x):
return x % L
#simulation
while True:
clear_output()
print(road)
for i in range(L):
if not road[i] == 0: #veh
if road[i] < max_s: #acc
road[i] += acc
for j in range(max_s): #slow
if not road[mod(i + j +1)] == 0:
road[i] = j + 1
break
if (random.random() < rand) and road[i] > 1: #rand
road[i] -= 1
new_road = np.zeros(L, dtype='int32')
for i in range(L):
if not road[i] == 0:
new_road[mod(i+road[i])] = road[i]
road = new_road
delay()
So why do it prints something like this
[0 0 0 4 0 0 0 4 1 0 2 0 0 0 0 0 5 0 0 0 4 0 2 0 0 0 4 0 2 0 0 0 0 4 0 0 0
0 5 0 0 0 4 0 0 0 0 5 0 0 0 0 5 0 0 0 0 4 0 0 0 0 4 0 0 3 0 0 0 4 0 0 0 0
0 0 3 0 0 0 4 0 0 0 0 0 4 1 0 2 0 2 0 0 3 0 0 0 0 5]
and freezes?
I tried making a new numpy array or maby array of arrays
New contributor
konto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.