1、2014 美赛相关 MATLAB 程序(基于 NS 模型)主程序:NaSch_3.m 程序代码% 单车道最大速度3 个元胞开口边界条件加速减速随机慢化clfclear all%build the GUI %define the plot button plotbutton=uicontrol(style,pushbutton,.string,Run, .fontsize,12, .position,100,400,50,20, .callback, run=1;);%define the stop button erasebutton=uicontrol(style,pushbutton,.s
2、tring,Stop, .fontsize,12, .position,200,400,50,20, .callback,freeze=1;);%define the Quit button quitbutton=uicontrol(style,pushbutton,.string,Quit, .fontsize,12, .position,300,400,50,20, .callback,stop=1;close;);number = uicontrol(style,text, .string,1, .fontsize,12, .position,20,400,50,20);%CA setu
3、p n=100;%数据初始化z=zeros(1,n);%元胞个数z=roadstart(z,5);%道路状态初始化,路段上随机分布5辆cells=z;vmax=3;%最大速度v=speedstart(cells,vmax);%速度初始化x=1;%记录速度和车辆位置memor_cells=zeros(3600,n);memor_v=zeros(3600,n);imh=imshow(cells);%初始化图像白色有车,黑色空元胞set(imh, erasemode, none)axis equalaxis tightstop=0; %wait for a quit button pushrun=0
4、; %wait for a drawfreeze=0; %wait for a freeze(冻结)while (stop=0)if(run=1)%边界条件处理,搜素首末车,控制进出,使用开口条件a=searchleadcar(cells);b=searchlastcar(cells);cells,v=border_control(cells,a,b,v,vmax);i=searchleadcar(cells);%搜索首车位置for j=1:iif i-j+1=nz,v=leadcarupdate(z,v);continue;else %=加速、减速、随机慢化if cells(i-j+1)=0
5、;%判断当前位置是否非空continue;else v(i-j+1)=min(v(i-j+1)+1,vmax);%加速%=减速k=searchfrontcar(i-j+1),cells);%搜素前方首个非空元胞位置if k=0;%确定于前车之间的元胞数d=n-(i-j+1);else d=k-(i-j+1)-1;endv(i-j+1)=min(v(i-j+1),d);%=%减速%随机慢化v(i-j+1)=randslow(v(i-j+1);new_v=v(i-j+1); %=加速、减速、随机慢化%更新车辆位置z(i-j+1)=0; z(i-j+1+new_v)=1;%更新速度v(i-j+1)=
6、0;v(i-j+1+new_v)=new_v;endendendcells=z;memor_cells(x,:)=cells;%记录速度和车辆位置memor_v(x,:)=v;x=x+1;set(imh,cdata,cells)%更新图像%update the step number diaplaypause(0.2);stepnumber = 1 + str2num(get(number,string);set(number,string,num2str(stepnumber)endif (freeze=1)run = 0;freeze = 0;enddrawnowend函数:border_
7、control.m 程序代码Functionnew_matrix_cells,new_v=border_control(matrix_cells,a,b,v,vmax) %边界条件,开口边界,控制车辆出入%出口边界,若头车在道路边界,则以一定该路0.9离去n=length(matrix_cells);if a=nrand(state,sum(100*clock)*rand(1);%p_1=rand(1);%产生随机概率if p_1vmaxt=1;q=0.25;x=1;p=(q*t)x*exp(-q*t)/prod(x);%1s内有1辆车到达的概率rand(state,sum(100*clock
8、)*rand(1);p_2=rand(1);if p_2=p m=min(b-vmax,vmax);matrix_cells(m)=1;v(m)=m; endendnew_matrix_cells=matrix_cells;new_v=v; 函数:leadcarrupdate.m 程序代码function new_matrix_cells,new_v=leadcarupdate(matrix_cells,v)%第一辆车更新规则n=length(matrix_cells);if v(n)=0matrix_cells(n)=0;v(n)=0;endnew_matrix_cells=matrix_c
9、ells;new_v=v;函数:randslow.m 程序代码function new_v=randslow(v)p=0.3;%慢化概率rand(state,sum(100*clock)*rand(1);p_rand=rand;%产生随机概率if p_rand=pv=max(v-1,0);endnew_v=v;函数:roadstart.m 程序代码function matrix_cells_start=roadstart(matrix_cells,n)%道路上的车辆初始化状态,元胞矩阵随机为0或1,matrix_cells初始矩阵,n初始车辆数k=length(matrix_cells);z=
10、round(k*rand(1,n);for i=1:nj=z(i);if j=0 matrix_cells(j)=0;elsematrix_cells(j)=1;endendmatrix_cells_start=matrix_cells;函数:searchfrontcar.m 程序代码function location_frontcar=searchfrontcar(current_location,matrix_cells)i=length(matrix_cells);if current_location=ilocation_frontcar=0;elsefor j=current_loc
11、ation+1:iif matrix_cells(j)=0location_frontcar=j;break;elselocation_frontcar=0;endendend函数:searchlastcar.m 程序代码function location_lastcar=searchlastcar(matrix_cells)%搜索尾车位置for i=1:length(matrix_cells)if matrix_cells(i)=0location_lastcar=i;break;else %如果路上无车,则空元胞数设定为道路长度location_lastcar=length(matrix_
12、cells);endend函数:searchleadcar.m 程序代码function location_leadcar=searchleadcar(matrix_cells)i=length(matrix_cells);for j=1:iif matrix_cells(i-j+1)=0location_leadcar=i-j+1;break;elselocation_leadcar=0;endend函数:speadstart.m 程序代码function v_matixcells=speedstart(matrix_cells,vmax)%道路初始状态车辆速度初始化v_matixcells=zeros(1,length(matrix_cells);for i=1:length(matrix_cells)if matrix_cells(i)=0v_matixcells(i)=round(vmax*rand(1);endend