matlab cellular automata

(mainly refer to the teaching video of zhou lvwen)
Cellular automata is a discrete dynamic system, which is defined in a finite time space and has a finite number of cells
Mathematical expression A=(L,d,s,N,f)
50: Cell grid space
d: Space dimension
s: Finite set of discrete states (for example, 0, 1)
N: The possibility of all cells in a neighborhood
f: Local mapping or local rules
Cell is its basic unit, each of which has the function of memorizing and storing state
Several different neighborhood relationships

Boundary conditions: periodic type, constant value type, absorption type, reflection type
Rules:
Summation: look at the total state of neighbors
Legal type:
Example: forest fire

Code

% simulate forest fire with cellular automata
n = 300;%Definition represents forest matrix size
Plight = 5e-6; %Lightning probability
Pgrowth = 1e-2;%Probability of tree growth
UL = [n 1:n-1]; %Upper left neighbor processing
DR = [2:n 1];
veg=zeros(n,n);%Initialization matrix
imh = image(cat(3,veg,veg,veg));%Visualization ( R,G,B Three level matrix)
% veg = empty=0 burning=1 green=2 Three different states
for i=1:3000%Loop start
    %nearby fires?
    sum =            (veg(UL,:)==1) + ...
        (veg(:,UL)==1)     +      (veg(:,DR)==1) + ...
                     (veg(DR,:)==1);%Four neighbors up and down
                 %Updating forest matrix according to rules: tree=total-Catch fire+newborn

    veg = 2*(veg==2) - ...
          ( (veg==2) & (sum>0 | (rand(n,n)<Plight)) ) + ...
          2*((veg==0) & rand(n,n)<Pgrowth) ;
     
    set(imh, 'cdata', cat(3,(veg==1),(veg==2),zeros(n)) )%visualization
    drawnow
end

Example: monorail problems
Code

function [rho, flux, vmean] = ns(rho, p, L, tmax, animation, spacetime)
%
% NS: This script implements the Nagel Schreckenberg cellular automata based
% traffic model. Car move forward governed by NS algorithm:
% USAGE: flux = ns(rho, p, L, tmax, isdraw)
%        rho       = density of the traffic Density is p(rou)
%        p         = probability of random braking Random deceleration probability
%        L         = length of the load
%        tmax      = number of the iterations Time step
%        animation = if show the animation of the traffic
%        spacetime = if plot the space-time after the simuation ended.
%        flux      = flux of the traffic
%


if nargin == 0; 
    rho = 0.25; p = 0.25; L = 100; tmax = 100; 
    animation = true; spacetime = true;
end

vmax = 5;                  % maximun speed
% place a distribution with density
ncar = round(L*rho);%ncar=L*rho
rho = ncar/L;

x = sort(randsample(1:L, ncar));%1:ncar Random number of no repetition
v = vmax * ones(1,ncar);   % start everyone initially at vmax

if animation; h = plotcirc(L,x,0.1); end

flux = 0;                  % number of cars that pass through the end
vmean = 0;
road = zeros(tmax, L);

for t = 1:tmax
    % acceleration Acceleration rule
    v = min(v+1, vmax);
    
    %collision prevention Collision avoidance
    gaps = gaplength(x,L); % determine the space vehicles have to move Vehicle distance
    v = min(v, gaps-1);
    
    % random speed drops Random deceleration
    vdrops = ( rand(1,ncar)<p );
    v = max(v-vdrops,0);
    
    % update the position
    x = x + v;
    passed = x>L;          % cars passed at time r
    x(passed) = x(passed) - L;% periodic boundary conditions Periodic boundary conditions
    
    if t>tmax/2
        flux = flux + sum(v/L); %flux = flux + sum(passed); 
        vmean = vmean + mean(v);
    end
    road(t,x) = 1;
    
    if animation; h = plotcirc(L,x,0.1,h); end
end
flux = flux/(tmax/2);
vmean = vmean/(tmax/2);

if spacetime; figure;imagesc(road);colormap([1,1,1;0,0,0]);axis image; end

% -------------------------------------------------------------------------

function gaps = gaplength(x,L)
% 
% GAPLENGTH: determine the gaps between vehicles
%
ncar = length(x);
gaps=zeros(1, ncar);
if ncar>0
    gaps = x([2:end 1]) -x;
    gaps(gaps<=0) = gaps(gaps<=0)+L;
end

% -------------------------------------------------------------------------

function h = plotcirc(L,x,dt,h)
W = 0.05;  R = 1;
ncar = length(x);

theta = [0 : 2*pi/L : 2*pi];
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;

xi = [xinner(x);  xinner(x+1); xouter(x+1); xouter(x)];
yi = [yinner(x);  yinner(x+1); youter(x+1); youter(x)];
if nargin == 3
    color = randperm(ncar);
    h = fill(xi,yi, color); hold on
    plot(xinner,yinner, 'k', xouter,youter, 'k','linewidth',1.5)
    plot([xinner; xouter], [yinner; youter],'k','linewidth',1.5)
    axis image; axis((R+2*W)*[-1 1 -1 1]); axis off
else
    for i=1:ncar;  set(h(i),'xdata',xi(:,i),'ydata',yi(:,i));  end
end
pause(dt)

It can be concluded that cellular automata has the following characteristics
1. Discrete space and time
2. Discrete finite state (fire, open space, growth of trees; with and without cars)
3. Cytoplasmic (no special)
4. Local action and calculation
Use: traffic problems, infectious diseases, etc
Be careful:
Cellular automata belongs to the mesh model. Pay attention to the difference of other particle models

Published 18 original articles, won praise 7, visited 1022
Private letter follow

Posted by beaux1 on Sun, 02 Feb 2020 08:35:13 -0800