元胞自动机基础 matlab 兰顿蚂蚁 沃尔夫勒姆初等元胞自动机

以下使用的是matlabR2020a版本

兰顿蚂蚁

注意点:
1.把左转和右转写成函数会更加方便阅读
2.迭代次数超过10000才会出现“高速公路”
3.为了快速显示最终结果,没有使用pause()函数查看每代的结果,因此在最终结果出来前是不会有图像的,大概40多秒出结果。

tic
height = 200;
width = 200;
iter = 12000;  % 迭代次数
dirction = [1,2,3,4]; % 1-北,2-东,3-南,4-西
cells = zeros(height,width);
% 刚开始蚂蚁的坐标及方向
% 黑--1,白--0
x = height/2;
y = width/2;
d = 2;  % 初始方向
for i=1:iter
    [x,y] = coordinate(x,y,d);  % 根据上一步的坐标的和方向确定当前的坐标
    if cells(x,y)==0
        cells(x,y) = 1;
        d = turnLeft(d);  % 进入白元胞则左转
    else
        cells(x,y) = 0;
        d = turnRight(d);  % 进入黑元胞则右转
    end
    imagesc(cells);
%     pause(0.1);  % 为了快速显示最终结果,没有使用pause来查看图像
end
toc

function [x,y] = coordinate(xP,yP,direction)  % 根据蚂蚁的坐标和方向确定,下一个时间步的坐标
if direction==1
    x = xP-1;
    y = yP;
elseif direction==2
    x = xP;
    y = yP+1;
elseif direction==3
    y = yP;
    x = xP+1;
elseif direction==4
    x = xP;
    y = yP-1;
else
    error("direction只能取1,2,3,4中任意一个");
end
end

function d = turnLeft(dP)  % dP为蚂蚁当前的方向,d为蚂蚁左转后的方向
if dP==1
    d = 4;
elseif dP==2
    d = 1;
elseif dP==3
    d =2;
elseif dP==4
    d =3;
else
    d=0;
    error("direction只能取1,2,3,4中任意一个");
end
end

function d = turnRight(dP)  % dP为蚂蚁当前的方向,d为蚂蚁右转后的方向
if dP==1
    d = 2;
elseif dP==2
    d = 3;
elseif dP==3
    d =4;
elseif dP==4
    d =1;
else
    d=0;
    error("direction只能取1,2,3,4中任意一个");
end
end

改变初始方向,最终“高速公路”的方向也会变

沃尔夫勒姆的初等元胞自动机

需要注意的是:图像的每一行就代表某一次迭代的结果,而图像的行数有限,当迭代次数大于行数以后就需要舍弃第一行的数据,整个矩阵“向上移动”,空出最后一行放入最新的迭代结果。

tic
load('rules.mat', 'rules')
height = 200;  % 图像高度,即矩阵行数
width = 200;  % 图片宽度,即矩阵列数
iter = 200;  % 迭代次数
ruleIndex = 110;  % 规则序号,ruleIndex=100就代表使用110号规则,所有规则存储在rules中,具体见下一个函数
cellst = zeros(1,width);  % t时刻元胞的状态
cellst1 = zeros(1,width);  % t+1时刻元胞的状态
his = zeros(height,width);
% 白--0,黑--1
cellst(width/2) = 1;  % 初始化
rule = rules(ruleIndex,:);
for i=1:iter
    for j=1:width
        % 边界的两个细胞需要特殊处理
        if j==1
            arrange = strcat(num2str(cellst(width)),num2str(cellst(j)),num2str(cellst(j+1)));
        elseif j==width
            arrange = strcat(num2str(cellst(width-1)),num2str(cellst(width)),num2str(cellst(1)));
        else
            arrange = strcat(num2str(cellst(j-1)),num2str(cellst(j)),num2str(cellst(j+1)));
        end
        indice = bin2dec(arrange);  % 二进制转成数字,根据左右两个邻居和自己的状态所组成的串确定下一步的状态
        cellst1(j) = rule(indice+1);
    end
    cellst = cellst1;
    % 图像只显示height行的数据,当步数超过height时,就需要移动
    if i>height
        his(1:height-1,:) = his(2:height,:);  % 向上移动一格
        his(height,:) = cellst1;  % 空出来的最后一行给最新的迭代结果
    else
        his(i,:) = cellst1;
    end
    imagesc(his);
    pause(0.1);
end
disp("程序结束");
toc

generateRules.m文件,生成所有的rule

rules=zeros(256,8);
for i=1:256
    str = dec2bin(i,8);
    for j=1:8
        rules(i,j)=str2double(str(9-j));
    end
end   

元胞200x200 30号规则迭代200次的结果:
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值