一、 任意车道离开
1.公路模型创建:创建两个二维数组,一个用于保存上一步时状态用于判断,一个用于保存改变后的状态用于更新,在数组中某位置的值表示公路状态,0代表公路此位置无车,1代表公路此位置有人的驾驶车辆,2代表公路此位置有自动驾驶车辆。
2.模拟车辆的进入:由于规定一般条件下前方三个车位的空间内有车辆,此车会在此步时处于静止状态以模拟减速,所以如果每步时对会有随机车辆生成,道路覆盖率会难以控制
因此我么们定义每四步时进行一次模拟车辆的进入,此时将会以定义道路覆盖率生成车辆来模拟车辆的进入,并根据自动驾驶车辆所占比例随机确定车的类型。
3.模拟车辆的前进:遍历数组所有元素,若为含有自动驾驶车辆的公路且满足自动驾驶汽车正常行驶条件,则将其信息传递给右侧元素。若为含有人工驾驶的车辆的公路且前方三个车位空间无车辆,则有20%概率前进两步来模拟加速操作,此外为了模拟人驾驶车辆的不稳定性,我们设定人驾驶的汽车有5%概率前进5%概率后退,来模拟非匀速运动。
4.模拟车辆的离开:若车辆到达模拟公路末端,计数器会加一,然后数组此位置元素换为0完成离开状况模拟,并可以统计通过车量数目
5.模拟更改车道:由问题一可知,人驾驶的车辆具有根据路况更换车道的能力来模拟人的灵活性,所以若在移动时此车辆为人驾驶的车辆,会在一定约束条件下,完成更换车道,即将其值赋给目标车道的相同位置的下一位置。
import numpy as np
import random
import copy
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def make_car(way, auto_p, car_p):
if cell_temp[way][0] == 0:
leng[way] = leng[way] + 1
if random.randint(1, 100) <= car_p *(1- auto_p) * 100:
cell_temp[way][0] = 1 # people's car
elif random.randint(1, 100) <= car_p *auto_p * 100:
cell_temp[way][0] = 2 # auto car
def move(way):
for j in range(0, width-1):
if j==0 and cell[way][0]!=0:
x = 0
for i in range(j+1,j+4):
if cell[way][i]!=0:
x=1
if x==0:
cell_temp[way][1]=cell_temp[way][0]
cell_temp[way][0]=0
elif cell[way][j]!=0:
if change_way(way, j) == 1:
continue
x = 0
for i in range(j+1,min(j+3,width-1)):
if cell[way][i]!=0:
x=1
if x==0:
if j+3<width-4 and cell[way][j+3]== 0 and cell_temp[way][j]==1 and random.randint(1,100)<=20:
cell_temp[way][j+2]=cell_temp[way][j]
cell_temp[way][j] = 0
k = j+2
else:
cell_temp[way][j+1]=cell_temp[way][j]
cell_temp[way][j]=0
k=min(j+1,width-2)
if cell_temp[way][k]==1:
if random.randint(1,100)<=5 and cell_temp[way][k-1]==0:
cell_temp[way][k-1]=1
cell_temp[way][k]=0
elif random.randint(1,100)<=5 and cell_temp[way][k+1]==0:
cell_temp[way][k + 1] = 1
cell_temp[way][k] = 0
def remove(way):
if cell_temp[way][width-1]!=0:
leng[3] = leng[3]+1
leng[way] = leng[way] -1
cell_temp[way][width-1]=0
def change_way(way,local):
m = 0
n = 0
if way ==1:
if leng[way]*2/3 > leng[way+1] and leng[way]>10:
for i in range(local-3,local+4):