SharedCS166 Assignment 2.ipynbOpen in CoCalc
class TrafficSimulation():

"""The constructor for the Traffic Simulation class"""

self.max_velocity = max_velocity
self.slow_down_probability = slow_down_probability

for i in car_indexes:

next_state = current_state

def update_rules(self):
for i in range(len(current_state[0])):
if current_state[0,i] == -1:
next_state[0,i] = -1
elif current_state[0,i] >= 0:
next_state[0,i + current_state[0,i]] = current_state[0,i]

for i in range(len(current_state[0])):
if current_state[0,i+1] == -1:
next_state[0,i] = current_state[0,i+1] + 1
elif

def rules(self):
if current_state[0,0] == -1:
return current_state[0,0]

velocity = current_state[0,0]

# acceleration rule
velocity = min(velocity + 1,self.max_velocity)

# considers gaps between cars
dist = 0
ind = 0
for car in current_state:
if ind == 0:
ind += 1
continue
if i == -1:
ind += 1
dist += 1
continue
break

velocity = min(velocity, dist)

# random slow down
if random.random() < self.slow_down_probability:
vel = max(velocity - 1, 0)

return velocity

 File "<ipython-input-1-44367b1fa9d5>", line 29 elif ^ SyntaxError: invalid syntax 
road_length = 100
max_velocity = 5
traffic_density = 0.3
# self.slow_down_probability = slow_down_probability

for i in car_indexes:

    def set_cars(self):

# creates a [RoadLength] element long scipy array of zeroes

# randomises where the cars are initialised
self.cars = random.sample(xrange(0, self.RoadLength - 1), self.NumberOfCars)

# changes the zeroes to a -1 in the scipy array

# randomises the velocity of the cars
for j in self.cars:

for c in range(len(self.cars)):
# Rule 4:
position = self.cars[c]


 File "<ipython-input-2-11e644cb328f>", line 18 for c in range(len(self.cars)): ^ IndentationError: unindent does not match any outer indentation level 
def update(self):
# output an array of positions where element is not -1, in order to
# find the places where cars are
cars = np.where(self.state != -1)
# only use relevant output from np.where above
cars = cars[0]
# create an array which has the position and state
# of each car and its velocity to be used for moving the cars
movement_array = np.array([[x, self.state[x]] for x in cars])

for x in range(len(cars)):
car_index = cars[x]
#find the distance to next car
# If total lenght is 100,
# if a car is at place 90 and the next car
# is at place 10, the distance should be 20
# cars % gives the correct next car
# if 10 - 90, should be 20 % 100
# all in all: simulate periodic boundaries
distance = (cars[(x+1)%len(cars)] - car_index) %self.road_length
#accelerate
if self.state[car_index] < self.maximum_velocity and distance > self.state[car_index]+1:
movement_array[x][1] += 1
#slow down
elif self.state[car_index] > distance :
movement_array[x][1] = distance-1
#random slow down
if random.random() < self.prob_slow_down:
## note this reference movement array not the current
## state so cars don't disapear
if movement_array[x][1] > 0:
movement_array[x][1] -= 1