├── README.md └── src ├── cellular.m ├── create_plaza.m ├── move_forward.m ├── new_cars.m ├── para_count.m ├── random_slow.m ├── show_plaza.m └── switch_lane.m /README.md: -------------------------------------------------------------------------------- 1 | # Cellular-Automata 2 | A Two-Lane Cellular Automaton Traffic Flow Model with the Keep-Right Rule 3 | -------------------------------------------------------------------------------- /src/cellular.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % A Two-Lane Cellular Automaton Traffic Flow Model with the Keep-Right Rule 3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 4 | clc; 5 | clear all; 6 | close all; 7 | 8 | B=3; % The number of the lanes 9 | plazalength=100; % The length of the simulation highways 10 | h=NaN; % The handle of the image 11 | 12 | 13 | [plaza,v]=create_plaza(B,plazalength); 14 | h=show_plaza(plaza,h,0.1); 15 | 16 | iterations=1000; % Number of iterations 17 | probc=0.1; % Density of the cars 18 | probv=[0.1 1]; % Density of two kinds of cars 19 | probslow=0.3; % The probability of random slow 20 | Dsafe=1; % The safe gap distance for the car to change the lane 21 | VTypes=[1,2]; % Maximum speed of two different cars 22 | [plaza,v,vmax]=new_cars(plaza,v,probc,probv,VTypes);% Generate cars on the lane 23 | 24 | size(find(plaza==1)) 25 | PLAZA=rot90(plaza,2); 26 | h=show_plaza(PLAZA,h,0.1); 27 | for t=1:iterations; 28 | size(find(plaza==1)) 29 | PLAZA=rot90(plaza,2); 30 | h=show_plaza(PLAZA,h,0.1); 31 | [v,gap,LUP,LDOWN]=para_count(plaza,v,vmax); 32 | [plaza,v,vmax]=switch_lane(plaza,v,vmax,gap,LUP,LDOWN); 33 | [plaza,v,vmax]=random_slow(plaza,v,vmax,probslow); 34 | [plaza,v,vmax]=move_forward(plaza,v,vmax); 35 | end 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/create_plaza.m: -------------------------------------------------------------------------------- 1 | function [plaza,v]=create_plaza(B,plazalength); 2 | 3 | plaza=zeros(plazalength,B+2); 4 | v=zeros(plazalength,B+2); 5 | 6 | plaza(1:plazalength,[1,2+B])=-1; 7 | 8 | -------------------------------------------------------------------------------- /src/move_forward.m: -------------------------------------------------------------------------------- 1 | function [plaza,v,vmax]=move_forward(plaza,v,vmax); 2 | [L,W]=size(plaza);% The size of the lane 3 | % Compute values to get type of cars: turn left or stay 4 | gap=zeros(L,W); 5 | for lanes=2:W-1; 6 | temp=find(plaza(:,lanes)==1); 7 | nn=length(temp);% The number of the cars in the lane 8 | for k=1:nn; 9 | i=temp(k); 10 | if(k==nn) 11 | gap(i,lanes)=L-(temp(k)-temp(1)+1);% periodic boundary 12 | continue; 13 | end 14 | gap(i,lanes)=temp(k+1)-temp(k)-1; 15 | end 16 | end 17 | 18 | for lanes=2:W-1; 19 | temp=find(plaza(:,lanes)==1); 20 | nn=length(temp); 21 | for k=1:nn; 22 | i=temp(k); 23 | if(v(i,lanes)<=gap(i,lanes)) 24 | pos=mod(i+v(i,lanes)-1,L)+1; 25 | end 26 | if(v(i,lanes)>gap(i,lanes)) 27 | pos=mod(i+gap(i,lanes)-1,L)+1; 28 | end 29 | if(pos~=i) 30 | plaza(pos,lanes)=1; 31 | v(pos,lanes)=v(i,lanes); 32 | vmax(pos,lanes)=vmax(i,lanes); 33 | plaza(i,lanes)=0; 34 | v(i,lanes)=0; 35 | vmax(i,lanes)=0; 36 | end 37 | end 38 | end 39 | 40 | end -------------------------------------------------------------------------------- /src/new_cars.m: -------------------------------------------------------------------------------- 1 | function [plaza,v,vmax]=new_cars(plaza,v,probc,probv,VTypes); 2 | [L,W]=size(plaza); 3 | vmax=zeros(L,W); 4 | for lanes=2:W-1; 5 | for i=1:L; 6 | if(rand<=probc) % Generate a car randomly on this pisition 7 | tmp=rand; 8 | plaza(i,lanes)=1; 9 | for k=1:length(probv) 10 | if(tmp<=probv(k)) 11 | vmax(i,lanes)=VTypes(k); 12 | v(i,lanes)=ceil(rand*vmax(i,lanes));% Initial speed for current position 13 | break; 14 | end 15 | end 16 | end 17 | end 18 | end 19 | % Cars do not meet density requirement 20 | needn=ceil((W-2)*L*probc); 21 | number=size(find(vmax~=0),1); 22 | if(numberneedn)% if density is bigger than expected 40 | temp=find(plaza==1); 41 | for k=1:number-needn; 42 | i=temp(k); 43 | plaza(i)=0; 44 | vmax(i)=0; 45 | v(i)=0; 46 | end 47 | end 48 | end -------------------------------------------------------------------------------- /src/para_count.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tinymilky/Cellular-Automata/6a29cf38c6803a4b2cbe52dd2e55f26de0f90f30/src/para_count.m -------------------------------------------------------------------------------- /src/random_slow.m: -------------------------------------------------------------------------------- 1 | function [plaza,v,vmax]=random_slow(plaza,v,vmax,probslow); 2 | [L,W]=size(plaza); 3 | for lanes=2:W-1; 4 | temp=find(plaza(:,lanes)==1); 5 | nn=length(temp); 6 | for k=1:nn; 7 | i=temp(k); 8 | if(rand<=probslow) 9 | v(i,lanes)=max(v(i,lanes)-1,0); 10 | end 11 | end 12 | end 13 | end -------------------------------------------------------------------------------- /src/show_plaza.m: -------------------------------------------------------------------------------- 1 | function h=show_plaza(plaza,h,n); 2 | 3 | [L,W]=size(plaza); 4 | temp=plaza; 5 | temp(temp==1)=0;%create the palza without any cars 6 | 7 | plaza_draw=plaza; 8 | 9 | PLAZA(:,:,1)=plaza_draw; 10 | PLAZA(:,:,2)=plaza_draw; 11 | PLAZA(:,:,3)=temp; 12 | PLAZA=1-PLAZA; 13 | PLAZA(PLAZA>1)=PLAZA(PLAZA>1)/6; 14 | 15 | 16 | if ishandle(h) 17 | set(h,'CData',PLAZA); 18 | pause(n); 19 | else 20 | figure('position',[100 100 200 700]); 21 | h=imagesc(PLAZA); 22 | %colorbar; 23 | hold on; 24 | plot([[0:W]',[0:W]']+0.5,[0,L]+0.5,'k'); 25 | plot([0,W]+0.5,[[0:L]',[0:L]']+0.5,'k'); 26 | axis image 27 | set(gca, 'xtick', [], 'ytick', []); 28 | pause(n); 29 | end 30 | end -------------------------------------------------------------------------------- /src/switch_lane.m: -------------------------------------------------------------------------------- 1 | function [plaza,v,vmax]=switch_lane(plaza,v,vmax,gap,LUP,LDOWN); 2 | [L,W]=size(plaza);% The size of the lane 3 | changeL=zeros(L,W);% can turn left 4 | changeR=zeros(L,W);% can turn right 5 | % can turn left? 6 | for lanes=2:W-2; 7 | temp=find(plaza(:,lanes)==1); 8 | nn=length(temp); 9 | for k=1:nn; 10 | i=temp(k); 11 | if(v(i,lanes)>gap(i,lanes)&LUP(i,lanes)==1&LDOWN(i,lanes)==1) 12 | changeL(i,lanes)=1; 13 | end 14 | end 15 | end 16 | % can turn right? 17 | for lanes=3:W-1; 18 | temp=find(plaza(:,lanes)==1); 19 | nn=length(temp); 20 | for k=1:nn; 21 | i=temp(k); 22 | if(plaza(i,lanes-1)==0&plaza(mod(i-1-1,L)+1,lanes-1)==0&plaza(mod(i-2-1,L)+1,lanes-1)==0&plaza(mod(i,L)+1,lanes-1)==0&plaza(mod(i+1,L)+1,lanes-1)==0) 23 | changeR(i,lanes)=1; 24 | end 25 | end 26 | end 27 | % turn right first 28 | for lanes=3:W-1; 29 | temp=find(changeR(:,lanes)==1); 30 | nn=length(temp); 31 | for k=1:nn; 32 | i=temp(k); 33 | plaza(i,lanes-1)=1; 34 | v(i,lanes-1)=max(v(i,lanes)-1,1); 35 | vmax(i,lanes-1)=vmax(i,lanes); 36 | plaza(i,lanes)=0; 37 | v(i,lanes)=0; 38 | vmax(i,lanes)=0; 39 | 40 | changeL(i,lanes)=0; 41 | end 42 | end 43 | % turn left 44 | for lanes=2:W-2 45 | temp=find(changeL(:,lanes)==1); 46 | nn=length(temp); 47 | for k=1:nn; 48 | i=temp(k); 49 | plaza(i,lanes+1)=1; 50 | v(i,lanes+1)=max(v(i,lanes)-1,1); 51 | vmax(i,lanes+1)=vmax(i,lanes); 52 | plaza(i,lanes)=0; 53 | v(i,lanes)=0; 54 | vmax(i,lanes)=0; 55 | end 56 | end 57 | end --------------------------------------------------------------------------------