www.pudn.com > model1-3.0.rar > model1.m, change:2014-02-09,size:5222b


% [ flux,biand,acc,distant ] = model1( 120, 0.5, 0.4, 3, 1 ,50, 4, 6, 0.1, 1, 2, 1, 1) 
function [ flux,biand,acc,distant ] = model1( time, density1, density2, length_car1, length_car2, length_road , maxspeed1, maxspeed2, possibility, acc1,acc2,min_dis1,min_dis2 ) 
 
t=1;    %时间步长 
totaltime=time;  %总模拟时间s 
rou1=density1;%密度 
rou2=density2; 
l(1)=length_car1;    %车长m 
l(2)=length_car2; 
r=length_road;   %路长m(先用短点的吧) 
n1=floor(length_road*rou1/length_car1);   %车辆数 
n2=floor(length_road*rou2/length_car2);  
vmax(1)=maxspeed1; %慢车道最大速度m/s 
vmax(2)=maxspeed2; %快最大速度m/s 
p=possibility;  %随机减速概率 
a(2)=acc2;   %加速度(简化为定值) 
a(1)=acc1;   %两种不同车型 
dis(2)=min_dis2; %要求不同超车车头距 
dis(1)=min_dis1; 
 
flux=[0,0] ; 
biand=[0,0] ; 
acc=[0,0]; 
distant=[0,0]; 
 
temp=randperm(2*r); 
car=cell(1,n1+n2); 
for i=1:n1          
    car{i}.m=1;         %different car model 
    car{i}.lane=1;      %初始在慢车道上 
    car{i}.x=temp(i);  %初始车头位置 
    car{i}.v=randi(vmax(car{i}.m));%初始速度(取整) 
end 
for i=n1+1:n1+n2 
    car{i}.m=2;         %different car model 
    car{i}.lane=3;      %初始在快车道上 
    car{i}.x=temp(i);  %初始车头位置 
    car{i}.v=randi(vmax(car{i}.m));%初始速度(取整) 
end 
color(1,r)=uint8(0);%慢车道 
color(3,r)=uint8(0);%快车道 
 
for i=1:n1+n2 
    color(car{i}.lane,mod(car{i}.x-l(car{i}.m):car{i}.x-1,r)+1)=car{i}.m; 
end 
 
%pic(1,:)=color(1,:); %慢车道时空演化图 
stepnumber=0; 
   
    while (stepnumber< (totaltime/t) )  
%         RGBmap = [1 1 1; 0 0 1; 1 0 0]; %0白(路),1蓝(coach),2red(car) 
%         colormap(RGBmap); 
%         image(color); 
%         axis([0.5 r+0.5 -r/2 r/2+5]); 
%         pause(0.2); 
 
        for j=1:n1+n2 
            if(car{j}.lane==1)%慢车道上 
                for k=1:r 
                    if(color(car{j}.lane,mod(car{j}.x+k-1,r)+1)~=0)  %寻找前一辆车 
                        break; 
                    end 
                end 
                distant(car{j}.m)=distant(car{j}.m)+k;%平均距离格子数 
                 
                if(color(1,mod(car{j}.x:car{j}.x+2,r)+1)==0)%前面三格里没车 
                    car{j}.v=min(car{j}.v+a(car{j}.m),vmax(car{j}.m));%加速过程(加速度1) 
                    acc(car{j}.m)=acc(car{j}.m)+1; 
                else 
                    if(color(3,mod(car{j}.x-1:car{j}.x+2,r)+1)==0)%前面一格里没车,左及左前三格没车 
                        if(color(car{j}.lane,mod(car{j}.x:car{j}.x+dis(car{j}.m)-1,r)+1)==0)    %前面dis格里没车 
                            car{j}.lane=3;  %变道 
                            biand(car{j}.m)=biand(car{j}.m)+1; 
                        end 
                    end 
                end                
                for k=1:r  %保证安全,不撞车 
                    if(color(car{j}.lane,mod(car{j}.x+k-1,r)+1)~=0)  %寻找前一辆车 
                        break; 
                    end 
                end 
                car{j}.v=min(car{j}.v,k-1); %安全刹车 
                             
            else if(car{j}.lane==3)%超车道上 
                    for k=1:r 
                        if(color(car{j}.lane,mod(car{j}.x+k-1,r)+1)~=0)  %寻找前一辆车 
                            break; 
                        end 
                    end 
                    distant(car{j}.m)=distant(car{j}.m)+k;%平均距离格子数 
 
                    if(color(1,mod(car{j}.x-1:car{j}.x+2,r)+1)==0)%右及右前3格没车 
                        car{j}.lane=1;  %变道  
                        biand(car{j}.m)=biand(car{j}.m)+1; 
                    else 
                        if(color(3,mod(car{j}.x:car{j}.x+2,r)+1)==0)%前面三格里没车 
                            car{j}.v=min(car{j}.v+a(car{j}.m),vmax(car{j}.m));%加速过程(加速度1) 
                            acc(car{j}.m)=acc(car{j}.m)+1; 
                        end 
                    end    
                    for k=1:r  %保证安全,不撞车 
                        if(color(car{j}.lane,mod(car{j}.x+k-1,r)+1)==1)  %寻找前一辆车 
                             break; 
                        end 
                    end 
                    car{j}.v=min(car{j}.v,k-1); %安全刹车 
                end 
            end 
            if(rand(1)<=p) 
                car{j}.v=max(car{j}.v-1,0);%随机慢化过程(加速度-1) 
            end 
             
            if (car{j}.x+car{j}.v > r) 
                 flux(car{j}.m)=flux(car{j}.m)+1; 
            end 
            car{j}.x=mod(car{j}.x+car{j}.v-1,r)+1;%位置更新,循环边界条件          
 
        end 
        color(1,1:r)=0; 
        color(3,1:r)=0; 
        for i=1:n1+n2    
            color(car{i}.lane,mod(car{i}.x-l(car{i}.m):car{i}.x-1,r)+1)=car{i}.m; 
        end 
        stepnumber = stepnumber+1 ; 
    end  
 
distant(1)=distant(1)/n1/(totaltime/t); 
distant(2)=distant(2)/n2/(totaltime/t); 
%{ 
disp(['车道利用率1:',num2str(rourou(1))]) 
disp(['总车流量1: ', num2str(flux(1))]) 
disp(['变道次数1:',num2str(biand(1))]) 
disp(['加速次数1: ', num2str(acc(1))]) 
 
disp(['车道利用率2:',num2str(rourou(2))]) 
disp(['总车流量2: ', num2str(flux(2))]) 
disp(['变道次数2:',num2str(biand(2))]) 
disp(['加速次数2: ', num2str(acc(2))]) 
disp(['车量平均间距1: ', num2str(distant)]) 
%}