 ### 时间：2018/9/16 21:39:30 点击： 核心提示：matlab代做，FPGA代做，淘宝，专业代做MATLAB、FPGA博士/硕士/本科课题仿真、项目仿真、Coursework、Assignment QQ: 1224848052...

QQ: 1224848052

MATLAB程序下载――一基于MATLAB的-路口排队模型模拟。

%The simulation of four direction circles;

function [waitlen,waittime,passtime,his,wait,copy]=roundsimul(roundservt,roundlim,arrv,tmax)

%

% Auther:幸玮  Data:2009-5-19

% The essential thing in this program is simulate the progress that cars get into the circle and cross the conjunction.

%

% I use a matrix "judge" to know when to act at every important point which leads to different results

% that are when to make desicion or judge whether to go or wait.

%

% "judge" contants four characters:time,road,turn direction,style,nextsection and number(used to recognise every car）

%

% style decides what to do:1 get in the circle,2 get into next section,3 get out of the circle

% Generate the basic judge matrix by simulate the arrival of four roads

judge=[];

for i=1:4

for j=1:3

arnum(i,j)=poissrnd(arrv{i}(j)*tmax);

arrprog=sort(rand(1,arnum(i,j))*tmax);

judge=[judge,[arrprog;ones(1,arnum(i,j))*i;ones(1,arnum(i,j))*j;ones(1,arnum(i,j))*1;ones(1,arnum(i,j))*i]];

end

end

temp=sortrows(judge');

judge=temp';

judge=[judge;[1:size(judge,2)]];

copy=judge;                             % use to keep the initial judge matrix

sectstat=zeros(1,4);                    % the number of cars in each section

wait=zeros(2,size(judge,2));            % record each car's waittime and pass time

qlen=floor(size(judge,2)/5);            % max length of the queue

sectqueue.num=zeros(4,qlen);            % queue using number

sectqueue.start=zeros(4,qlen);          % queue using starttime

for i=1:4

his{i}=[0 ; 0];

end

% simulate the proccess of getting through the conjunction

while size(judge,2)>0;

if judge(4,1)==1

if sectstat(judge(5,1))<roundlim                     %the section is not full

sectstat(judge(5,1))=sectstat(judge(5,1))+1;     %car number +1

new=judge(:,1);                                  %generate new judge point

new(1)=judge(1,1)+roundservt(1);

new(5)=mod((judge(5,1)+1),4);

if mod(judge(2,1)+judge(3,1),4)==new(5)

new(4)=3;

else

new(4)=2;

end

if new(5)==0

new(5)=4;

end

insertpoint=find(judge(1,:)<new(1),1,'last');   %refresh judge matrix

judge=[judge(:,2:insertpoint),new,judge(:,insertpoint+1:size(judge,2))];

else                                                %the queue is full put the car into the queue

index=find(sectqueue.num(judge(5,1),:)==0,1,'first');

his{judge(5,1)}=[his{judge(5,1)},[judge(1,1);index]];

sectqueue.num(judge(5,1),index)=judge(6,1);

sectqueue.start(judge(5,1),index)=judge(1,1);

judge=judge(:,2:size(judge,2));

end

elseif judge(4,1)==2

if sectstat(judge(5,1))<roundlim                   %the section is not full

sectstat(judge(5,1))=sectstat(judge(5,1))+1;   %car number +1

new=judge(:,1);                                %generate a new judge point

new(1)=judge(1,1)+roundservt(1);

new(5)=mod((judge(5,1)+1),4);

if mod(judge(2,1)+judge(3,1),4)==new(5)

new(4)=3;

else

new(4)=2;

end

if new(5)==0

new(5)=4;

end

wait(2,new(6))=wait(2,new(6))+roundservt(1);

insertpoint=find(judge(1,:)<new(1),1,'last');  %insert the new judge point

judge=[judge(:,1:insertpoint),new,judge(:,insertpoint+1:size(judge,2))];

lastsect=mod(judge(5,1)-1,4);                  %get the last section

if lastsect==0

lastsect=4;

end

if size(find(sectqueue.num(lastsect,:)~=0),2)~=0     %when a car leave the last sect,let a car get out of the queue

index=find(copy(6,:)==sectqueue.num(lastsect,1));

new=copy(:,index);                               %generate a new judge point

new(1)=judge(1,1)+roundservt(1);

new(5)=mod(judge(5,1),4);

if mod(new(2)+new(3),4)==new(5)

new(4)=3;

else

new(4)=2;

end

if new(5)==0

new(5)=4;

end

wait(1,new(6))=wait(1,new(6))+judge(1,1)-sectqueue.start(lastsect,1);                 %change wait time and pass time

wait(2,new(6))=wait(2,new(6))+judge(1,1)-sectqueue.start(lastsect,1)+roundservt(1);

index=find(sectqueue.num(lastsect,:)==0,1,'first');                                   %record queue history

his{lastsect}=[his{lastsect},[judge(1,1);index-2]];

sectqueue.num(lastsect,:)=[sectqueue.num(lastsect,2:qlen),0];                         %refresh queue

sectqueue.start(lastsect,:)=[sectqueue.start(lastsect,2:qlen),0];

insertpoint=find(judge(1,:)<new(1),1,'last');                                         %refresh judge marix

judge=[judge(:,2:insertpoint),new,judge(:,insertpoint+1:size(judge,2))];

else                         %when the queue is empty,simplely decrease the car numver and change the judge matrix

sectstat(lastsect)=sectstat(lastsect)-1;

judge=judge(:,2:size(judge,2));

end

else  %When the next section is full,it shoud get stack.

%But in real situation,the cars in the circle can still go,because they can move when each one in it still goes

%So we assume the car get to the next section and generate a new judge point

new=judge(:,1);

new(1)=judge(1,1)+roundservt(1);

new(5)=mod((judge(5,1)+1),4);

if mod(judge(2,1)+judge(3,1),4)==new(5)

new(4)=3;

else

new(4)=2;

end

if new(5)==0

new(5)=4;

end

wait(2,new(6))=wait(2,new(6))+roundservt(1);

insertpoint=find(judge(1,:)<new(1),1,'last');

judge=[judge(:,2:insertpoint),new,judge(:,insertpoint+1:size(judge,2))];

end

elseif judge(4,1)==3        %this part are just like the situation in judge(4,1)==2

lastsect=mod(judge(5,1)-1,4);

if lastsect==0

lastsect=4;

end

if size(find(sectqueue.num(lastsect,:)~=0),2)~=0

index=find(copy(6,:)==sectqueue.num(lastsect,1));

new=copy(:,index);

new(1)=judge(1,1)+roundservt(1);

new(5)=mod(judge(5,1),4);

if mod(new(2)+new(3),4)==new(5)

new(4)=3;

else

new(4)=2;

end

if new(5)==0

new(5)=4;

end

wait(1,new(6))=wait(1,new(6))+judge(1,1)-sectqueue.start(lastsect,1);

wait(2,new(6))=wait(2,new(6))+judge(1,1)-sectqueue.start(lastsect,1)+roundservt(1);

index=find(sectqueue.num(lastsect,:)==0,1,'first');

his{lastsect}=[his{lastsect},[judge(1,1);index-2]];

sectqueue.num(lastsect,:)=[sectqueue.num(lastsect,2:qlen),0];

sectqueue.start(lastsect,:)=[sectqueue.start(lastsect,2:qlen),0];

insertpoint=find(judge(1,:)<new(1),1,'last');

judge=[judge(:,2:insertpoint),new,judge(:,insertpoint+1:size(judge,2))];

else

sectstat(lastsect)=sectstat(lastsect)-1;

judge=judge(:,2:size(judge,2));

end

end

end

%get the average wait time,pass time and queue length

waittime=mean(wait(1,:));

passtime=mean(wait(2,:));

waitlen=0;

for i=1:4

waitlen=waitlen+his{i}(2,:)*([his{i}(1,2:size(his{i},2))-his{i}(1,1:size(his{i},2)-1),0])';

end

waitlen=waitlen/tmax/4;

% % function quelenplot(his)

% %plot the change of the length of the queue

% for i=1:4

%     subplot(2,2,i);

%     stairs(his{i}(1,:),his{i}(2,:));

% end

% %plot the distrubite situation of the wait time in each roads.

% for i=1:4

% end

% for i=1:size(wait,2)

% end

% for i=1:4

%     subplot(2,2,i);

% end

% % function directpassplot(wait)

% %plot the cross time distrubite situation of the cross time with differnt directions

% for i=1:3

%     directpasstime{i}=[];

% end

% for i=1:size(wait,2)

%    directpasstime{copy(3,i)}=[directpasstime{copy(3,i)} wait(:,i)];

% end

% Direct{1}='Right';

% Direct{2}='straight';

% Direct{3}='Left';

% for i=1:3

%     subplot(3,1,i);

%     hist(directpasstime{i}(1,:),30);

%     title(Direct{i});

% end

-------------------------------------------------------------------------------------

matlab有偿代做,matlab留学生作业代做，matlab留学生代做，matlab项目代做，FPGA项目代做

QQ:1224848052

QQ ：1224848052 • ##### 您是如何找到本站的？
• 百度搜索
• 查阅资料过程中
• 论坛发现
• 百度贴吧发现
• 朋友介绍 ###### 相关评论 ###### 发表我的评论                              • 大名：
• 内容： ##### 本类固顶  