Probability model of traffic jam on ring road (with detailed analysis)

Basic theory

There are n vehicles on the road, driving forward at different speeds to simulate traffic jams.
There are the following assumptions:

  1. Suppose the current speed of a certain car is v.
  2. If there is no car in the visible range ahead, it will increase its speed to v+1 in the next second until it reaches the prescribed maximum speed limit.
  3. If there is a car ahead, and the distance of the preceding car is d, and d <v, then its speed in the next second is reduced to d-1.
  4. Each car will decelerate randomly by v-1 with probability p. ,

Code

// An highlighted block
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

#对车辆的位移进行限制,当大于水平的公路长度时,则认为是拐外后的位移
def clip(x, path):
    for i in range(len(x)):
        if x[i] >= path:
            x[i] %= path

if __name__ == "__main__":
    
    #文字转化
    mpl.rcParams['font.sans-serif'] = [u'SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    
    '''初始化定义'''
    path = 5000     # 环形公路的长度
    n = 100         # 公路中的车辆数目
    v0 = 50          # 车辆的初始速度
    p = 0.3         # 随机减速概率
    Times = 1000    #模拟时间
    
    '''模拟车辆的位移和速度'''
    np.random.seed(0)
    #模拟100辆车辆的位置
    x = np.random.rand(n) * path
    #车辆位移进行排序
    x.sort()
    #将100个车辆的速度定义为50,并设为float类型数据
    v = np.tile([v0], n).astype(np.float)
    #
    '''画布大小'''
    plt.figure(figsize=(10, 8), facecolor='w')
    
    '''模拟在时间范围内的堵车情况'''
    for t in range(Times):
        #绘图 x:车辆位置 [t]*n:时间
        plt.scatter(x, [t]*n, s=1, c='b', alpha=0.05)
        
        '''依次判断100辆车的速度和位移情况'''
        for i in range(n):
            #计算前后车辆的距离
            if x[(i+1)%n] > x[i]:
                d = x[(i+1) % n] - x[i]
            else:
                d = path - x[i] + x[(i+1) % n]

            '''判断此刻的速度和与前车的距离大小'''
            if v[i] < d:
            #若前方可见范围内没车,则它在下一秒的车速提高到v+1,直到达到规定的最高限速。同时,每辆车会以概率p随机减速v-1if np.random.rand() > p:
                    v[i] += 1
                else:
                    v[i] -= 1
            else:
            #若前方有车,前车的距离为d,且d < v,则它下 一秒的车速降低到d-1 。
                v[i] = d - 1
        #限制速度,v<0,则将v定义为0;v>150,则将v定义为150
        v = v.clip(0, 150)
        #车辆的位移在增加,车在往前开
        x += v
        #调用clip
        clip(x, path)
        
    '''限制坐标轴'''
    #对x轴的坐标进行限制
    plt.xlim(0, path)
    #对y轴坐标进行限制
    plt.ylim(0, Times)
    
    '''标签'''
    plt.xlabel(u'车辆位置', fontsize=16)
    plt.ylabel(u'模拟时间', fontsize=16)
    plt.title(u'环形公路车辆堵车模拟', fontsize=20)
    
    '''自动调整子图参数,使之填充整个图像区域'''
    plt.tight_layout(pad=2)
    
    '''画图'''
    plt.show()

Insert picture description here

Graphical analysis

The heavier the color in the graph, it means that many cars have the same displacement, which means that a traffic jam has occurred. If there are many places with heavy colors, it means that the traffic jam is serious.
The greater the probability of random deceleration, the more serious the traffic jam.
Random deceleration probability = 0.3
Insert picture description here
Random deceleration probability = 0.5
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42567027/article/details/107398651