双圆道交通流CA模型Matlab程序.doc_第1页
双圆道交通流CA模型Matlab程序.doc_第2页
双圆道交通流CA模型Matlab程序.doc_第3页
双圆道交通流CA模型Matlab程序.doc_第4页
双圆道交通流CA模型Matlab程序.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

%function rho, flux, vmean = ns2(rho, p, L, tmax)%注意:主函数一定不能像子函数一样以function开头。此程序为含控制按钮NS模型改编双车道,无换道。% NS: This script implements the Nagel Schreckenberg cellular automata based% traffic model. Car move forward governed by NS algorithm:% % 1. Acceleration. If the vehicle can speed up without hitting the speed % limit vmax it will add one to its velocity, vn - vn + 1. Otherwise, % the vehicle has constant speed, vn 鈫? vn.% 2. Collision prevention. If the distance between the vehicle and the car% ahead of it, dn, is less than or equal to vn , i.e. the nth vehicle% will collide if it doesnt slow down, then vn 鈫? dn 鈭? 1.% 3. Random slowing. Vehicles often slow for non-traffic reasons (cell % phones, coffee mugs, even laptops) and drivers occasionally make % irrational choices. With some probability pbrake, vn =? vn -1, % presuming vn 0.% 4. Vehicle movement. The vehicles are deterministically moved by their % velocities, xn - xn + vn .% USAGE: flux = ns(rho, p, L, tmax, isdraw)% rho = density of the traffic% p = probability of random braking% L = length of the load% tmax = number of the iterations% animation = if show the animation (动画)of the traffic% spacetime = if plot the space-time after the simuation ended.% flux = flux of the traffic% zhou lvwen: rho = 0.25; p = 0.25; L = 100; tmax = 100; i=0;%=%build the GUI%define the plot buttonplotbutton=uicontrol(style,pushbutton,. string,Run, . fontsize,12, . position,60,400,50,20, . callback, run=1;);%define the stop buttonerasebutton=uicontrol(style,pushbutton,. string,Stop, . fontsize,12, . position,120,400,50,20, . callback,freeze=1;);%=%define the Quit buttonquitbutton=uicontrol(style,pushbutton,. string,Quit, . fontsize,12, . position,180,400,50,20, . callback,stop=1;close;);%=vmax = 5; % maximun speed% place a distribution with densityncar = round(L*rho);rho = ncar/L;x1= sort(randsample(1:L, ncar);x2= sort(randsample(1:L, ncar);x=x1;x2;%randsample(1:L, ncar),从1-100产生25个随机整数,sort 排序 默认升序v = vmax * ones(2,ncar); % start everyone initially at vmax h1,h2 = plotcirc(L,x,0.1); flux = 0; % number of cars that pass through the endvmean = 0;road = zeros(tmax, L);stop= 0; %wait for a quit button pushrun = 0; %wait for a draw freeze = 0; %wait for a freeze while (stop=0) if (run=1) if itmax i=i+1; % acceleration v = min(v+1, vmax); %collision prevention gaps = gaplength(x,L); % determine the space vehicles have to move v = min(v, gaps-1); % random speed drops vdrops = ( rand(2,ncar)L; % cars passed at time r x(passed) = x(passed) - L;% periodic boundary conditions flux = flux + sum(v/L); %flux = flux + sum(passed); vmean = vmean + mean(v); %自迭代一半后开始计算流量 road(i,x) = 1; h1,h2 = plotcirc(L,x,0.1,h1,h2); end end if (freeze=1) run = 0; freeze = 0; end xlabel(strcat(flux= ,num2str(mean(flux), . strcat(vmean = , num2str(mean(vmean),. strcat(i = ,num2str(i) drawnow %need this in the loop for controls to work end figure(2);imagesc(road);colormap(1,1,1;0,0,0);axis image;%实则就是将车辆的状况用图像表示出来,colormap(1,1,1;0,0,0)是将车辆表示为黑色。%function gaps = gaplength(x,L)% GAPLENGTH: determine the gaps between vehiclesncar = length(x(1,:);gaps=zeros(2, ncar);if ncar0 gaps = x(:,2:end 1) -x; gaps(gaps=0) = gaps(gaps=0)+L;end%function h1,h2 = plotcirc(L,x,dt,h1,h2)W = 0.05; R = 1;ncar = length(x(1,:);theta = (0-pi/L) : 2*pi/L : (2*pi+pi/L);xc = cos(theta); yc = sin(theta);xinner = (R-W/2)*xc; yinner = (R-W/2)*yc;xouter = (R+W/2)*xc; youter = (R+W/2)*yc;xouter1 = (R+1.5*W)*xc; youter1 = (R+1.5*W)*yc; if nargin = 3 color1 = randperm(ncar); color2 = randperm(ncar); xi = xinner(x(1,:); xinner(x(1,:)+1); xouter(x(1,:)+1); xouter(x(1,:); yi = yinner(x(1,:); yinner(x(1,:)+1); youter(x(1,:)+1); youter(x(1,:); xi1= xouter(x(2,:); xouter(x(2,:)+1);xouter1(x(2,:)+1); xouter1(x(2,:); yi1= youter(x(2,:); youter(x(2,:)+1);youter1(x(2,:)+1); youter1(x(2,:); h1 = fill(xi,yi, color1);hold on h2=fill(xi1,yi1, color2); plot(xinner,yinner, k, xouter,youter, k,xouter1,youter1, k,linewidth,1.5) plot(xinner;xouter;xouter1, yinner;youter;youter1,k,linewidth,1.5) axis image; axis(R+2*W)*-1 1 -1 1);set(gca, xtick, , ytick, );% 将坐标清除,比axis off使用好,else xi = xinner(x(1,:); xinner(x(1,:)+1); xouter(x(1,:)+1); xouter(x(1,:); yi = yinner(x(1,:); yinner(x(1,:)+1); youter(x(1,:)+1); youter(x(1,:); xi1= xouter(x(2,:); xouter(x(2,:)+1);xouter1(x(2,:)+1); xouter1(x(2,:); yi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论