daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. import numpy
  3.  
  4. class TrafficSimulation:
  5.     def __init__(self, length, density, v_max, p_slow):
  6.         self.length = length
  7.         self.density = density
  8.         self.v_max = v_max
  9.         self.p_slow = p_slow
  10.         self.n_cars = int(self.length*self.density)
  11.    
  12.     def make_road(self):
  13.        
  14.         self.road = []
  15.        
  16.         #for the flow part
  17.         self.flow_counts = []
  18.        
  19.         self.cars = random.sample(xrange(0,self.length-1),self.n_cars)
  20.         self.cars.sort()
  21.        
  22.         for i in range(0,self.length):
  23.             self.road.append(-1)
  24.        
  25.         for y in self.cars:
  26.             vel = random.randint(0,self.v_max)
  27.             self.road[y] = vel
  28.            
  29.         #print(self.cars)
  30.         #print(self.road)
  31.        
  32.        
  33.     def run(self):
  34.        
  35.         for c in range(len(self.cars)):
  36.            
  37.             #first part makes the index loop
  38.             #second % thing is the conf
  39.             distance = (((self.cars[(c+1)%self.n_cars])) - (self.cars[c]))%self.length
  40.        
  41.    
  42.             #Rule 1
  43.             if self.road[self.cars[c]] < self.v_max and distance > self.v_max+1:
  44.                 self.road[self.cars[c]] += 1
  45.  
  46.             #Rule 2 --
  47.             if distance <= self.road[self.cars[c]]:
  48.                 self.road[self.cars[c]] = distance - 1
  49.  
  50.             #Rule 3 --
  51.             if self.road[self.cars[c]] >0 and random.uniform(0,1) < self.p_slow:
  52.                 self.road[self.cars[c]] -=  1
  53.            
  54.             #might need another "for c in range(len(self.cars)):" here for the simultaneous update thing
  55.        
  56.         #this will be added for all of the cars in one given time step
  57.         #the list of flow values needs to be for each time as a total, so it can't be within the loop
  58.        
  59.         self.flow = 0
  60.        
  61.         for c in range(len(self.cars)):
  62.            
  63.             #Rule 4 and density?
  64.             #future position (current + velocity) = velocity
  65.             position = self.cars[c]
  66.             velocity = self.road[self.cars[c]]
  67.             pos_before  = self.cars[c]
  68.             pos_after = int((position+velocity)%self.length)
  69.            
  70.             self.road[(position+velocity)%self.length] = velocity
  71.            
  72.             #if it's zero, it isn't moving so shouldn't be replacing with -1
  73.             if c == 0:
  74.                 self.flow = 0
  75.            
  76.             if (pos_before >= (self.length-self.v_max) and pos_before <= self.length-1) and (pos_after >= 0 and pos_after <= self.v_max-1):
  77.                 self.flow += 1
  78.            
  79.             if self.road[position] >0:
  80.                 self.road[position] = -1
  81.             self.cars[c] = int((position+velocity)%self.length)
  82.            
  83. #from 95 to 99 and 0 to 4      
  84.  #95 to 99 and then it's in position        
  85.    
  86.    
  87.     def display(self):
  88.         print(''.join('.' if x == -1 else str(x) for x in self.road))
  89.  
  90.    
  91.     #this lets you choose number of time steps for one set of parameters
  92.     def simulation(self,count):
  93.         self.make_road()
  94.         c = 0
  95.        
  96.         while c < count:
  97.             self.run()
  98.             self.display
  99.             self.flow_counts.append(self.flow)
  100.             c += 1
  101.        
  102.     def return_flow(self):
  103.         self.flow_value = numpy.mean(self.flow_counts)
  104.         return self.flow_value
  105.        
  106.        
  107. import matplotlib.pyplot as plt
  108. go = TrafficSimulation(100,.03,5,0.5)
  109.  
  110. #we want to make 2 lists, one with densities and one with flow
  111. dens = []
  112. flows = []
  113.  
  114. z = 0
  115. while z < .8:
  116.     go = TrafficSimulation(100,z,5,0.5)
  117.     go.simulation(200)
  118.     flows.append(go.return_flow())
  119.     dens.append(z)
  120.     z+= .01
  121.  
  122. plt.plot(dens,flows)
  123. plt.show()
RAW Paste Data
Top