%新模型
clear;clc;
k=0.5;lamda=0.2;vmax=16.66;V1=6.75;V2=7.91;C1=0.13;C2=1.57;lc=9; %FVD跟车模型参数,(lc原来是5,被我改成了9,因为我觉得这样停车的车头空距才比较合理,约6.5m)
N=20;gb=25;ge=60;C=85;dett=0.1;Stopline=500;detx0=50;vfree=14.66; %测试基本条件:N车辆数,gb绿灯开始时刻,ge绿灯结束时刻,C周期时长,dett仿真步长,Stopline停车线位置,detx0初始车头间距,vfree稳定时车速
D=500;vmin=6;hd=6.5;deltaone=0.5;deltatwo=2.9; %新模型所需的参数:D引导区域范围,vmin最小速度,hd停车的车头空距,deltaone加速策略时的safety interval,deltatwo启动时车头时距
Stopline2=[500 1000];
MaxT=200;
%初始条件
for n=1:N
if n==1
x(n,1)=0; %第一辆车初始位置
else
x(n,1)=x(n-1,1)-detx0;
end;
detx(n,1)=detx0; %初始车头空距
a(n,1)=0;
V(n,1)=V1+V2*tanh(C1*(detx(n,1)-lc)-C2);
v(n,1)=V(n,1);
detv(n,1)=0;
Strategy(n)=0; %第n辆车采用的策略(仅用在实行减速策略)
Strategyahead(n)=0; %第n辆车的前面所有车采用的策略(仅用在实行减速策略)
nstoped(n)=n-1; %已停止车辆数
end
%I(1)=1;I(2)=1;I(3)=1;I(4)=1;I(5)=1;I(6)=1;I(7)=1;I(8)=1;I(9)=1;I(10)=0;I(11)=0;I(12)=0;I(13)=0;I(14)=0;I(15)=0;%这15辆车都是智能车
%I(1)=0;I(2)=0;I(3)=1;I(4)=0;I(5)=1;I(6)=0;I(7)=1;I(8)=1;I(9)=1;I(10)=1;I(11)=1;;I(12)=1;I(13)=1;I(14)=1;I(15)=1 %智能车:3,8,13
%I(1)=0;I(2)=0;I(3)=0;I(4)=0;I(5)=0;I(6)=0;I(7)=0;I(8)=1;I(9)=1;I(10)=1;I(11)=1;I(12)=0;I(13)=1;I(14)=1;I(15)=1; %智能车:2,4,6,8,10,12,14
%I(1)=1;I(2)=1;I(3)=1;I(4)=0;I(5)=1;I(6)=1;I(7)=0;I(8)=1;I(9)=1;I(10)=1;I(11)=1;I(12)=1;I(13)=1;I(14)=1;I(15)=1; %智能车:1,2,4,5,6,8,9,10,12,13,14
%I(1)=1;I(2)=1;I(3)=1;I(4)=1;I(5)=1;I(6)=1;I(7)=1;I(8)=1;I(9)=1;I(10)=1;I(11)=1;I(12)=1;I(13)=1;I(14)=1;I(15)=1;I(16)=1;I(17)=1;I(18)=1;I(19)=1;I(20)=1;%I(21)=1;I(22)=1;I(23)=1;I(24)=1;I(25)=1;I(26)=1;
for n=1:N
I(n)=1;
end
time(1)=0;
%演化
for t=1:MaxT/dett;
time(t+1)=t*dett;
ge=60+floor(time(t+1)/C)*C; %每过一个周期,绿灯结束时刻都要调整
if time(t+1)<ge %过了绿灯结束时刻后,绿灯开始时刻就要调整
gb=floor(time(t+1)/C)*C;
else
gb=ceil(time(t+1)/C)*C;
end
for n=1:N
temp=find(Stopline2-x(n,t)>=0,1,'first');
if ~isempty(temp)
Stopline=Stopline2(temp);
if temp==1
Startline = -9999;
else
Startline = Stopline2(temp-1);
end
else
Stopline=Stopline2(end);
Startline =Stopline;
end
nstoped(n,t)=n-1;
for m=1:n-1
if Strategy(m)==3 %计算第n辆车的前面所有车是否存在采用减速策略的情况(如果采用那后面的车就只能跟驶)
Strategyahead(n)=3;
end
if (x(m,t)>Stopline || x(m,t)<Startline) && Strategy(m)~=3 %计算第n辆车的前面在停车排队的车辆数
% if (x(m,t)>Stopline ) && Strategy(m)~=3
nstoped(n,t)=nstoped(n,t)-1;
end
end
if n==1
if Stopline-50<x(n,t) & x(n,t)<=Stopline & (ge-time(t+1))*v(n,t)<Stopline-x(n,t) & v(n,t)>0 & ge-time(t+1)<3 %在停车线前50m绿灯结束前3s,车辆判断过不了停车线,那么停车线就视为前车
detx(n,t)=Stopline-x(n,t);
detv(n,t)=0-v(n,t);
elseif v(n,t)==0 & ge<time(t+1)
detx(n,t)=Stopline-x(n,t);
detv(n,t)=0-v(n,t);
else
detx(n,t)=10000-x(n,t);
detv(n,t)=0;
end
V(n,t)=V1+V2*tanh(C1*(detx(n,t)-lc)-C2);
a(n,t)=k*(V(n,t)-v(n,t))+lamda*detv(n,t);
else
if Stopline-D<x(n,t) & x(n,t)<=Stopline
if v(n,t)==0 & ge<time(t+1) %停着的车总是停着
a(n,t)=0;
elseif Strategyahead(n)==3 & I(n)==0 %前车采用减速策略,后车就只能跟驶
detx(n,t)=x(n-1,t)-x(n,t);
detv(n,t)=v(n-1,t)-v(n,t);
V(n,t)=V1+V2*tanh(C1*(detx(n,t)-lc)-C2);
a(n,t)=k*(V(n,t)-v(n,t))+lamda*detv(n,t);
elseif (ge-time(t+1))*v(n,t)>=Stopline-x(n,t) & Strategy(n)~=2 & I(n)==0 & Strategy(n-1)~=2 & (Strategy(n)~=3 | v(n-1,t)>=v(n,t-1)) %不引导:按当前速度能在绿灯期间通过交叉口就无需引导 v(n-1,t)>14
detx(n,t)=x(n-1,t)-x(n,t);
detv(n,t)=v(n-1,t)-v(n,t);
V(n,t)=V1+V2*tanh(C1*(detx(n,t)-lc)-C2);
a(n,t)=k*(V(n,t)-v(n,t))+lamda*detv(n,t);
elseif (ge-time(t+1))*v(n,t)<Stopline-x(n,t) &I(n)==1 &v(n,t)>0 & (ge-time(t+1)-deltaone)*vmax>=Stopline-x(n,t) | Strategy(n)==2 %加速策略:按当前速度无法在绿灯期间通过交叉口,但按最大允许速度能在绿灯期间通过交叉口
if Strategy(n)==2 | x(n-1,t)-x(n,t)>D/(vmax)*(vmax-vfree)
Strategy(n)=2;
%detv(n,t)=v(n-1,t)-v(n,t);%%%%%%%%%加速
a(n,t)=k*(vmax-v(n,t));%+lamda*detv(n,t);
else
detx(n,t)=x(n-1,t)-x(n,t);
detv(n,t)=v(n-1,t)-v(n,t);
V(n,t)=V1+V2*tanh(C1*(detx(n,t)-lc)-C2);
a(n,t)=k*(V(n,t)-v(n,t))+lamda*detv(n,t);
end
%elseif (Strategyahead(n)~=3 & (Stopline-x(n,t)-nstoped(n,401)*hd)>=(gb+deltatwo*nstoped(n,401)-time(t+1))*vmin & gb+deltatwo*nstoped(n,401)-time(t+1)>0 & v(n,t)>vmin) | (Strategy(n)==3 & (a(n-1,t)<=0 | v(n-1,t)<v(n,t-1))) %减速策略:一旦选择了减速策略就要一直执行下去(所以要凑好),直到前车开始加速或前车车速大于本车
elseif (t>400 & (Stopline-x(n,t)-nstoped(n,401)*hd)>=(gb+deltatwo*nstoped(n,401)-time(t+1))*vmin &I(n)==1 & gb+deltatwo*nstoped(n,401)-time(t+1)>0 & v(n,t)>vmin) | (Strategy(n)==3 & (a(n-1,t)<=0 | v(n-1,t)<v(n,t-1))) %减速策略:一旦选择了减速策略就要一直执行下去(所以要凑好),直到前车开始加速或前车车速大于本车
Strategy(n)=3;
detv(n,t)=v(n-1,t)-v(n,t);
a(n,t)=k*((D-(n-6)*hd)/(gb+deltatwo*(n-6)-(Stopline-D-x(n,1))/vfree)-v(n,t))+lamda*detv(n,t);%%%%%%%%%%%%%%重要改变
elseif x(n-1,t)>Stopline | (ge-time(t+1))*v(n-1,t)>=Stopline-x(n-1,t) | Strategy(n-1)==2 %前车已过了停车线或预计能过
if (ge-time(t+1))*v(n,t)<Stopline-x(n,t) & v(n,t)>0 & Stopline-x(n,t)<80 %车辆判断过不了停车线,那么停车线就视为前车
detx(n,t)=Stopline-x(n,t);
detv(n,t)=0-v(n,t);
V(n,t)=V1+V2*tanh(C1*(detx(n,t)-lc)-C2);
a(n,t)=(k+0.3*((D-Stopline+x(n,t))/D))*(V(n,t)-v(n,t))+lamda*detv(n,t);%
elseif ge<time(t+1) %红灯了%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%重要改变
detx(n,t)=Stopline-(nstoped(n,t)-3)*hd-x(n,t);%%%%%%%
detv(n,t)=v(n-1,t)-v(n,t);
V(n,t)=V1+V2*tanh(C1*(detx(n,t)-lc)-C2);
a(n,t)=(k+0.3*((D-Stopline+x(n,t))/D))*(V(n,t)-v(n,t))+lamda*detv(n,t);%(k+0.3*((D-Stopline+x(n,t))/D))
elseif (ge-time(t+1))*v(n,t)<Stopline-x(n,t) & v(n,t)>0 %车辆判断过不了停车线,那么停车线就视为前车
a(n,t)=0;
else
detx(n,t)=x(n-1,t)-x(n,t);
detv(n,t)=v(n-1,t)-v(n,t);
V(n,t)=V1+V2*tanh(C1*(detx(n,t)-lc)-C2);
a(n,t)=(k+0.3*((D-Stopline+x(n,t))/D))*(V(n,t)-v(n,t))+lamda*detv(n,t);%(k+0.3*((D-Stopline+x(n,t))/D))
end
else
detx(n,t)=x(n-1,t)-x(n,t);
detv(n,t)=v(n-1,t)-v(n,t);
V(n,t)=V1+V2*tanh(C1*(detx(n,t)-lc)-C2);
a(n,t)=k*(V(n,t)-v(n,t))+lamda*detv(n,t);
end
elseif Stopline-D>x(n,t) & a(n-1,t)>0
a(n,t)=0;
Q=n-1;
elseif Strategy(n)==2 & v(n,t)>vfree
detx(n,t)=x(n-1,t)-x(n,t);
detv(n,t)=vfree-v(n,t);
V(n,t)=vfree;
a(n,t)=k*(V(n,t)-v(n,t))+lamda*detv(n,t)+0*detv(n-1,t);
else
detx(n,t)=x(n-1,t)-x(n,t);
detv(n,t)=v(n-1,t)-v(n,t);
V(n,t)=V1+V2*tanh(C1*(detx(n,t)-lc)-C2);
a(n,t)=k*(V(n,t)-v(n,t))+lamda*detv(n,t)+0*detv(n-1,t);
end
end
if a(n,t)>9
a(n,t)=9;
end
if a(n,t)<-9
a(n,t)=-9;
end
v(n,t+1)=v(n,t)+a(n,t)*dett;
if v(n,t)>vmax
v(n,t)=vmax;
end
if v(n,t+1)<0
v(n,t+1)=0;
a(n,t)=0;
end
x(n,t+1)=x(n,t)+v(n,t)*dett+0.5*a(n,t)*dett^2;
end
end
detx(n,t+1)=d
评论18