SharedCS166 Assignment 2.ipynbOpen in CoCalc
class TrafficSimulation():
    
    def __init__(self,road_length,traffic_density,max_velocity,slow_down_probability):
        """The constructor for the Traffic Simulation class"""
        
        self.road = np.zeros((1,road_length)).astype(int)
        self.road[self.road] = -1
        self.max_velocity = max_velocity
        self.slow_down_probability = slow_down_probability

        number_of_cars = int(traffic_density*road_length)
        car_indexes = np.random.choice(range(road_length),number_of_cars,replace=False)
        for i in car_indexes:
            self.road[0,i] = np.random.randint(0,max_velocity)
        
        current_state = self.road
        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
road = np.zeros((1,road_length)).astype(int)
road[road] = -1
# self.slow_down_probability = slow_down_probability

number_of_cars = int(traffic_density*road_length)
car_indexes = np.random.choice(range(road_length),number_of_cars,replace=False)
for i in car_indexes:
    road[0,i] = np.random.randint(0,max_velocity)
    def set_cars(self):
        
        # creates a [RoadLength] element long scipy array of zeroes 
        self.road = scipy.zeros(self.RoadLength)
        
        # 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
        for i in range(0, self.RoadLength):
            self.road[i] = -1
            
        # randomises the velocity of the cars
        for j in self.cars:
            self.road[j] = random.randint(0,self.MaximumVelocity)


    for c in range(len(self.cars)):     
            # Rule 4:
            velocity = int(self.road[self.cars[c]])
            position = self.cars[c]
            self.road[(position + velocity)%self.RoadLength] = velocity
            if self.road[position] > 0:
                self.road[position] = -1
            self.cars[c] = int((position + velocity)%self.RoadLength)

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
                    
        transition_array = [-1]*self.road_length
        for x in movement_array:
            transition_array[(x[0]+x[1])%self.road_length] = x[1]
            self.state = np.array(transition_array)