stop= 0; %wait for a quit button push
run = 0; %wait for a draw
freeze = 0; %wait for a freeze
while (stop==0)
if (run==1)
%nearest neighbor sum
sum(x,y) = cells(x,y-1) + cells(x,y+1) + ...
cells(x-1, y) + cells(x+1,y) + ...
cells(x-1,y-1) + cells(x-1,y+1) + ...
cells(3:n,y-1) + cells(x+1,y+1);
% The CA rule
cells = (sum==3) | (sum==2 & cells);
%draw the new image
set(imh, 'cdata', cat(3,cells,z,z) )
%update the step number diaplay
stepnumber = 1 + str2num(get(number,'string'));
set(number,'string',num2str(stepnumber))
end
if (freeze==1)
run = 0;
freeze = 0;
end
drawnow %need this in the loop for controls to work
End
例子
1.康威的生命。(Conway’s life with GUI)
其规则是:
总结8近邻
如果总和=2 那么状态不改变
如果总和=3,然后在状态= 1
否则状态= 0
完整代码:
%Conway's life with GUI
clf
clear all
clc
%=============================================%build the GUI%define the plot button
plotbutton=uicontrol('style','pushbutton',...
'string','Run', ...
'fontsize',12, ...
'position',[100,400,50,20], ...
'callback', 'run=1;');
%define the stop button
erasebutton=uicontrol('style','pushbutton',...
'string','Stop', ...
'fontsize',12, ...
'position',[200,400,50,20], ...
'callback','freeze=1;');
%define the Quit button
quitbutton=uicontrol('style','pushbutton',...
'string','Quit', ...
'fontsize',12, ...
'position',[300,400,50,20], ...
'callback','stop=1;close;');
number = uicontrol('style','text', ...
'string','1', ...
'fontsize',12, ...
'position',[20,400,50,20]);
%=============================================%CA setup
n=128;
%initialize the arrays
z = zeros(n,n);
cells = z;
sum = z;
%set a few cells to one
cells(n/2,.25*n:.75*n) = 1;
cells(.25*n:.75*n,n/2) = 1;
%cells(.5*n-1,.5*n-1)=1;%cells(.5*n-2,.5*n-2)=1;%cells(.5*n-3,.5*n-3)=1;
cells = (rand(n,n))<.5 ;
%how long for each case to stability or simple oscillators%build an image and display it
imh = image(cat(3,cells,z,z));
set(imh, 'erasemode', 'none')
axis equal
axis tight
%index definition for cell update
x = 2:n-1;
y = 2:n-1;
%Main event loop
stop= 0; %wait for a quit button push
run = 0; %wait for a draw
freeze = 0; %wait for a freezewhile (stop==0)
if (run==1)
%nearest neighbor sum
sum(x,y) = cells(x,y-1) + cells(x,y+1) + ...
cells(x-1, y) + cells(x+1,y) + ...
cells(x-1,y-1) + cells(x-1,y+1) + ...
cells(3:n,y-1) + cells(x+1,y+1);
% The CA rule
cells = (sum==3) | (sum==2 & cells);
%draw the new image
set(imh, 'cdata', cat(3,cells,z,z) )
%update the step number diaplay
stepnumber = 1 + str2num(get(number,'string'));
set(number,'string',num2str(stepnumber))
endif (freeze==1)
run = 0;
freeze = 0;
end
drawnow %need this in the loop for controls to workend
程序运行结果:
第一次测试初始状态
基本静止状态
第二次测试初始状态
运行中状态
运行中状态
基本静止状态
2.表面张力(Surface Tension )
其规则是:
总结8近邻和细胞本身
如果总和<4或总和= 5,则状态= 0
否则状态= 1
完整代码:
%Surface Tension with GUI
clf
clear all
clc
%=============================================
%build the GUI
%define the plot button
plotbutton=uicontrol('style','pushbutton',...'string','Run', ...'fontsize',12, ...'position',[100,400,50,20], ...'callback', 'run=1;');
%define the stop button
erasebutton=uicontrol('style','pushbutton',...'string','Stop', ...'fontsize',12, ...'position',[200,400,50,20], ...'callback','freeze=1;');
%define the Quit button
quitbutton=uicontrol('style','pushbutton',...'string','Quit', ...'fontsize',12, ...'position',[300,400,50,20], ...'callback','stop=1;close;');
number = uicontrol('style','text', ...'string','1', ...'fontsize',12, ...'position',[20,400,50,20]);
%=============================================
%CA setup
n=128;
%initialize the arrays
z = zeros(n,n);
cells = z;
sum = z;
%set a few cells to one
cells(n/2,.25*n:.75*n) = 1;
cells(.25*n:.75*n,n/2) = 1;
%cells(.5*n-1,.5*n-1)=1;
%cells(.5*n-2,.5*n-2)=1;
%cells(.5*n-3,.5*n-3)=1;
cells = (rand(n,n))<.5 ;
%how long for each case to stability or simple oscillators
%build an image and display it
imh = image(cat(3,cells,z,z));
set(imh, 'erasemode', 'none')
axis equal
axis tight
%index definition for cell update
x = 2:n-1;
y = 2:n-1;
%Main event loop
stop= 0; %wait for a quit button push
run = 0; %wait for a draw
freeze = 0; %wait for a freeze
while (stop==0)
if (run==1)
%nearest neighbor sum
sum(x,y) = cells(x,y-1) + cells(x,y+1) + ...
cells(x-1, y) + cells(x+1,y) + ...
cells(x-1,y-1) + cells(x-1,y+1) + ...
cells(3:n,y-1) + cells(x+1,y+1)+...
cells(x,y);
% The CA rule
cells = ~((sum<4) | (sum==5));
%draw the new image
set(imh, 'cdata', cat(3,cells,z,z) )
%update the step number diaplay
stepnumber = 1 + str2num(get(number,'string'));
set(number,'string',num2str(stepnumber))
end
if (freeze==1)
run = 0;
freeze = 0;
end
drawnow %need this in the loop for controls to work
end
%diffusion + dla
clc
clear all
clf
nx=200; %must be divisible by 4
ny=200;
z=zeros(nx,ny);
o=ones(nx,ny);
sand = z ;
sandNew = z;
sum = z;
gnd = z;
gnd(nx/2,ny/2) = 1 ;
sand = rand(nx,ny)<.1;
imh = image(cat(3,z,sand,gnd));
set(imh, 'erasemode', 'none')
axis equal
axis tight
for i=1:10000
p=mod(i,2); %margolis neighborhood
%upper left cell update
xind = [1+p:2:nx-2+p];
yind = [1+p:2:ny-2+p];
%random velocity choice
vary = rand(nx,ny)<.5 ;
vary1 = 1-vary;
%diffusion rule -- margolus neighborhood
%rotate the 4 cells to randomize velocity
sandNew(xind,yind) = ...
vary(xind,yind).*sand(xind+1,yind) + ... %cw
vary1(xind,yind).*sand(xind,yind+1) ; %ccw
sandNew(xind+1,yind) = ...
vary(xind,yind).*sand(xind+1,yind+1) + ...
vary1(xind,yind).*sand(xind,yind) ;
sandNew(xind,yind+1) = ...
vary(xind,yind).*sand(xind,yind) + ...
vary1(xind,yind).*sand(xind+1,yind+1) ;
sandNew(xind+1,yind+1) = ...
vary(xind,yind).*sand(xind,yind+1) + ...
vary1(xind,yind).*sand(xind+1,yind) ;
sand = sandNew;
%for every sand grain see if it near the fixed, sticky cluster
sum(2:nx-1,2:ny-1) = gnd(2:nx-1,1:ny-2) + gnd(2:nx-1,3:ny) + ...
gnd(1:nx-2, 2:ny-1) + gnd(3:nx,2:ny-1) + ...
gnd(1:nx-2,1:ny-2) + gnd(1:nx-2,3:ny) + ...
gnd(3:nx,1:ny-2) + gnd(3:nx,3:ny);
%add to the cluster
gnd = ((sum>0) & (sand==1)) | gnd ;
%and eliminate the moving particle
sand(find(gnd==1)) = 0;
set(imh, 'cdata', cat(3,gnd,gnd,(sand==1)) )
drawnow
end
程序运行结果:
8.砂桩
一堆沙子的横截面可以仿照使用一个Margolus的邻里传播细胞,但具有不同的运动规律
规则:
见参考文献[2]第2.2.6章..
细胞有2个状态。状态= 0是空的,状态=1表示沙agrain。
在任何步骤中,粒子可以落在朝下方的2×2的块。可能的转换如下所示。墙壁和地板停止运动。
为了使议案略有随机,我添加了一个规则,有时反转的两个较低的细胞状态,所有的动作都完成后。
完整代码:
%sand pile
clear all
clf
nx=52; %must be divisible by 4
ny=100;
Pbridge = .05;
z=zeros(nx,ny);
o=ones(nx,ny);
sand = z ;
sandNew = z;
gnd = z ;
gnd(1:nx,ny-3)=1 ; % the ground line
gnd(nx/4:nx/2+4,ny-15)=1; %the hole line
gnd(nx/2+6:nx,ny-15)=1; %the hole line
gnd(nx/4, ny-15:ny) = 1; %side line
gnd(3*nx/4, 1:ny) = 1 ;
imh = image(cat(3,z',sand',gnd'));
set(imh, 'erasemode', 'none')
axis equal
axis tight
fori=1:1000
p=mod(i,2); %margolis neighborhood
sand(nx/2,ny/2) = 1; %add a grain at the top%upper left cell update
xind = [1+p:2:nx-2+p];
yind = [1+p:2:ny-2+p];
vary = rand(nx,ny)<.95 ;
vary1 = 1-vary;
sandNew(xind,yind) = ...
gnd(xind,yind).*sand(xind,yind) + ...
(1-gnd(xind,yind)).*sand(xind,yind).*sand(xind,yind+1) .* ...
(sand(xind+1,yind+1)+(1-sand(xind+1,yind+1)).*sand(xind+1,yind));
sandNew(xind+1,yind) = ...
gnd(xind+1,yind).*sand(xind+1,yind) + ...
(1-gnd(xind+1,yind)).*sand(xind+1,yind).*sand(xind+1,yind+1) .* ...
(sand(xind,yind+1)+(1-sand(xind,yind+1)).*sand(xind,yind));
sandNew(xind,yind+1) = ...
sand(xind,yind+1) + ...
(1-sand(xind,yind+1)) .* ...
( sand(xind,yind).*(1-gnd(xind,yind)) + ...
(1-sand(xind,yind)).*sand(xind+1,yind).*(1-gnd(xind+1,yind)).*sand(xind+1,yind+1));
sandNew(xind+1,yind+1) = ...
sand(xind+1,yind+1) + ...
(1-sand(xind+1,yind+1)) .* ...
( sand(xind+1,yind).*(1-gnd(xind+1,yind)) + ...
(1-sand(xind+1,yind)).*sand(xind,yind).*(1-gnd(xind,yind)).*sand(xind,yind+1));
%scramble the sites to make it look better
temp1 = sandNew(xind,yind+1).*vary(xind,yind+1) + ...
sandNew(xind+1,yind+1).*vary1(xind,yind+1);
temp2 = sandNew(xind+1,yind+1).*vary(xind,yind+1) + ...
sandNew(xind,yind+1).*vary1(xind,yind+1);
sandNew(xind,yind+1) = temp1;
sandNew(xind+1,yind+1) = temp2;
sand = sandNew;
set(imh, 'cdata', cat(3,z',sand',gnd') )
drawnow
end
程序运行结果:
References
[1] Cellular Automata Machines by Tommaso Toffoli and Norman Margolus, MIT Press, 1987.
[2] Cellular Automata Modeling of Physical Systems by Bastien Chopard and Michel Droz, Cambridge University Press, 1998.