编程语言
首页 > 编程语言> > 【元胞自动机】基于元胞自动机模拟商场人流matlab源码

【元胞自动机】基于元胞自动机模拟商场人流matlab源码

作者:互联网

元胞自动机的初步理解

%% lzu
close all ; clear all ;clc

a=rand(200,50);                                  %生成元胞自动机的大小(200,50)200行50列
m=find(a<0.1); n=find(a>0.9);                  %向上,向下的各占10%
aa=zeros(size(a));aa(m)=1;up=aa;
bb=zeros(size(a));bb(n)=3;down=bb;               %初始化元胞  up,down
clear a* m n bb
% 生成 up down
N=nnz(up)+nnz(down);


%%  循环开始的地方
for t=1:10000


    x=cutx(w_zx(i));y=w_zy(i);
    down_z_g(andx(x),y)=3;
    while down(x,y)==3
        down_z_g(x,y)=3;
        d_b(i)=d_b(i)+1;
        x=cutx(x);
    end
end
clear x y i
up_z_g=zeros(size(up));
for i=1:length(w_z1)
    u_b(i)=0;
    x=andx(andx(w_zx(i)));y=w_zy(i);
    up_z_g(cutx(x),y)=1;
    while up(x,y)==1
        up_z_g(x,y)=1;
        u_b(i)=u_b(i)+1;
        x=andx(x);
    end
end
clear x y i
% u_b  向下走的后面跟的个数
% d_b  向上走的后面跟的个数
%[w_z1;w_zx; w_zy ;u_b ;d_b]
%% 找出拐的那串粒子
%% 找出直走的(不包含相碰的后面跟的)
down_d=down;
for i=1:length(w_z1)
    x=w_zx(i);y=w_zy(i);
    for j=0:d_b(i)
        down_d(x,y)=0;
        x=cutx(x);
    end
end                                                 % down_d (down_directly)直接向下走的粒子
clear x y i j
up_d=up;

end                                                  % up_d (up_directly)直接向上走的粒子
clear x y i j
%f=nnz(up_d)+length(w_zx)+sum(u_b)-nnz(up)
%g=nnz(down_d)+length(w_zy)+sum(d_b)-nnz(down)
%end
a_down_d=[down_d(end,:);down_d(1:end-1,:)];               % alread_down_directly
a_up_d=[up_d(2:end,:);up_d(1,:)];                           % alread_down_directly(是假的还要考虑碰头的中间隔一个得情况)
%% 两个粒子对头时中间空一个(定义往下走的占了这个空格)
judge=a_down_d+a_up_d;
[ww_zx,ww_zy]=find(judge==4);
for i=1:length(ww_zx)
    x=ww_zx(i);y=ww_zy(i);
    judge(x,y)=3;
    x=andx(x);
    while judge(x,y)==1
        x=andx(x);
    end
    judge(x,y)=1;
end
clear i
% judge 里面存放的是 alread 走了的粒子(上下粒子都走了一步)
r_down_d=zeros(size(down)); r_down_d(find(judge==3))=3;      % r_down_d 里面放的是真的直走|了|的
r_up_d=zeros(size(up)); r_up_d(find(judge==1))=1;
%pp=nnz(find(judge==3))+nnz(find(judge==1))-nnz(judge);
%% 找出拐弯的(也包含它后面跟的)(把拐的写进去)
%% 先做向下
n=0;      %拐的串中直走的
down_g=zeros(size(down));
for i=1:length(w_z1)
    x=w_zx(i);y=w_zy(i);
    up_z_gg=up_z_g; up_z_gg(:,y)=0;
    down_z_gg=down_z_g; down_z_gg(:,y)=0;
    for j=0:d_b(i)
        cc=judge+down_g+up_z_gg+down_z_gg;
        if j==0             
            if y>=2&&y<=49&&cc(x,y+1)==0&&cc(x,y-1)==0&&cc(cutx(x),y-


down=r_down_d+down_g; up=r_up_d+up_g;         %  下一轮回的初始值
V(t)=(nnz(r_down_d)+nnz(r_up_d)+n)/N;
cla
ht(up,down)

clear a* c* d_b down_*  i j* r* u_b up1 up_* w* x y
end
figure(2)
plot(V)

        
        
        










标签:end,up,down,源码,元胞,judge,自动机
来源: https://blog.csdn.net/qq_59747472/article/details/119361080