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


% [ flux,biand,acc,distant ] = model3( 120, 0.5, 0.4, 3, 1,30 ,4, 6, 0.1, 1, 2, 1, 1) 
function [ flux,biand,acc,distant ] = model3(time, density1, density2, length_car1, length_car2, length_road , maxspeed1, maxspeed2, possibility, acc1,acc2,min_dis1,min_dis2 ) 
 
t=1;    %时间步长 
totaltime=time;  %总模拟时间s 
rou(1)=density1;%密度 
rou(2)=density2; 
l(1)=length_car1;    %车长m 
l(2)=length_car2; 
r=length_road;   %路长m(先用短点的吧) 
n1=round(length_road*rou(1)/length_car1);   %车辆数 
n2=round(length_road*rou(2)/length_car2);  
vmax(1)=maxspeed1; %慢车道最大速度m/s 
vmax(2)=maxspeed2; %快最大速度m/s 
p=possibility;  %随机减速概率 
a(2)=acc2;   %加速度(简化为定值) 
a(1)=acc1;   %两种不同车型,1是大车,2是小车 
dis(2)=min_dis2; %要求不同超车车头距 
dis(1)=min_dis1; 
 
flux=[0,0] ; 
biand=[0,0] ; 
acc=[0,0]; 
distant=[0,0]; 
 
temp=randperm(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(1,i);  %初始车头位置 
    car{i}.v=randi(vmax(1));%初始速度(取整) 
end 
for i=n1+1:n1+n2 
    car{i}.m=2;         %different car model 
    car{i}.lane=3;      %初始在快车道上 
    car{i}.x=temp(1,i);  %初始车头位置 
    car{i}.v=randi(vmax(2));%初始速度(取整) 
end 
color(1,r)=uint8(0);%慢车道 
color(3,r)=uint8(0);%快车道 
color(5,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); 
        tempc=color(1,:);%只是为了好看,让上面是超车道,下面是慢车道 
        color(1,:)=color(5,:); 
        color(5,:)=tempc; 
        image(color); 
        axis([0.5 r+0.5 -r/2 r/2+5]); 
        pause(1); 
 
        for j=1:n1 
            switch (car{j}.lane) 
                case 1  %大车在慢车道 
                    for k=1:r 
                        if(color(1,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{i}.m),vmax(car{i}.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(1,mod(car{j}.x+k-1,r)+1)~=0)  %寻找前一辆车 
                            break; 
                        end 
                    end 
                    car{j}.v=min(car{j}.v,k-1); %安全刹车 
                     
                case 3 
                    for k=1:r 
                        if(color(3,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格没车 
                        if(color(car{j}.lane,mod(car{j}.x:car{j}.x+dis(car{j}.m)-1,r)+1)==0) %前面dis格里没车, 
                            car{j}.lane=1;  %变道 
                            biand(car{j}.m)=biand(car{j}.m)+1; 
                        end 
                    else 
                        if(color(3,mod(car{j}.x:car{j}.x+2,r)+1)==0)%前面三格里没车 
                            car{j}.v=min(car{j}.v+a1,v2max);%加速过程(加速度1) 
                            acc(car{j}.m)=acc(car{j}.m)+1; 
                        end 
                    end    
                    for k=1:r  %保证安全,不撞车 
                        if(color(3,mod(car{j}.x+k-1,r)+1)==1)  %寻找前一辆车 
                             break; 
                        end 
                    end 
                    car{j}.v=min(car{j}.v,k-1); %安全刹车 
                     
                otherwise 
                    disp('heavy car error!'); 
            end 
                      
            if(rand(1)<=p) 
                car{j}.v=max(car{j}.v-1,0);%随机慢化过程 
            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 
         
        for j=n1+1:n2  %小车 
            switch (car{j}.lane) 
                case 3  %小车在快车道 
                    for k=1:r 
                        if(color(3,mod(car{j}.x+k-1,r)+1)~=0)  %寻找前一辆车 
                            break; 
                        end 
                    end 
                    distant(car{j}.m)=distant(car{j}.m)+k;%平均距离格子数 
 
                    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; 
                    else 
                        if(color(5,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=5;  %变道 
                                biand(car{j}.m)=biand(car{j}.m)+1; 
                            end 
                        end 
                    end                
                    for k=1:r  %保证安全,不撞车 
                        if(color(3,mod(car{j}.x+k-1,r)+1)~=0)  %寻找前一辆车 
                            break; 
                        end 
                    end 
                    car{j}.v=min(car{j}.v,k-1); %安全刹车 
                     
                case 5  %小车在超车道 
                    for k=1:r 
                        if(color(1,mod(car{j}.x+k-1,r)+1)~=0)  %寻找前一辆车 
                            break; 
                        end 
                    end 
                    distant(car{j}.m)=distant(car{j}.m)+k;%平均距离格子数 
 
                    if(color(3,mod(car{j}.x-1:car{j}.x+2,r)+1)==0)%右及右前3格没车 
                        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 
                    else 
                        if(color(5,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:3  %保证安全,不撞车 
                        if(color(5,mod(car{j}.x+k-1,r)+1)==1)  %寻找前一辆车 
                             break; 
                        end 
                    end 
                    car{j}.v=min(car{j}.v,k-1); %安全刹车 
                     
                otherwise 
                    disp('small car error!'); 
            end 
                      
            if(rand(1)<=p) 
                car{j}.v=max(car{j}.v-1,0);%随机慢化 
            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; 
        color(5,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=distant/n/(totaltime/t)*l; 
%rourou=crnum*rou/n ; 
%{ 
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)]) 
%}