一、简介
1 元胞自动机CA
元胞自动机(cellular automata,CA) 是一种时间、空间、状态都离散,空间相互作用和时间因果关系为局部的网格动力学模型,具有模拟复杂系统时空演化过程的能力。其中,元胞自动机又称为细胞自动机,而生命游戏(Game of Life)又是一种十分典型的CA。
以下是我做的笔记,CA一共有四个要点:
2 生命游戏
The Game of Life (生命游戏) ,是由英国数学家 John Horton Conway于1970年提出的。作为元胞自动机系统,生命游戏是一个零玩家游戏,用户确定初始状态与演变的规则后,无须其他操作,便可模拟得到形态的演变过程。
元胞自动机的基本思想可以追溯到上世纪Von Neumann在“Theory of Self-Reproducing Automata”提出的“机器繁殖”一概念:由一群细胞构成的小机器根据一些简单规则和初始图形进行演化的动力系统,机器可以不断自我进化和延续。
3 生命游戏的MatLab实现
我认为网上大多数代码都没有考虑到边界问题,所以我进行了一定的改进。
游戏规则在注释有详细解释,一共有三条。
下面这个表格帮助大家理解:(想象一下左下角是原点坐标)
二、源代码
%元胞自动机之生命游戏
%规则:假设元胞只有生和死两种状态
%1. 如果一个活细胞周围(包括对角相邻)有2或3个细胞为生,则该细胞保持为生;
%2. 如果一个死细胞周围有3个细胞为生,则该细胞转为生;
%3. 在其它情况下,死细胞保持死,活细胞转为死。
clc;
clear;
%定义元胞空间的大小为100,也即是100个元胞
x = 20;
y = 20;
%定义迭代次数
epoch = 100;
%初始化网格点
net = rand(x,y);
game = zeros(x,y);
for i = 1:x
for j = 1:y
if net(i,j)<= 0.3
% 按照(i-1,j)->(i,j)->(i,j-1)->(i-1,j-1)进行勾勒图形
fx = [i-1,i,i,i-1];
fy = [j,j,j-1,j-1];
fill(fx,fy,'g')
%表示元胞状态为生
game(i,j)=1;
hold on
end
end
end
pause(0.1)
for k = 1:epoch
temp = zeros(x,y);
fx=[0,x,x,0];
fy=[0,0,y,y];
fill(fx,fy,'w');
hold on;
for i = 1:x
for j = 1:y
if i~=1 && i~= x && j~=1 && j~=y
%在四个角邻居只有三个,在边界邻居只有五个
a = game(i-1,j-1)+game(i,j-1)+game(i+1,j-1)+game(i-1,j)+game(i+1,j)+game(i-1,j+1)+game(i,j+1)+game(i+1,j+1);
if game(i,j) == 1
if a == 3 || a ==2
temp(i,j) = 1;
end
else
if a== 3
temp(i,j) = 1;
end
end
elseif i==1
if j==1
a = game(i,j+1)+game(i+1,j)+game(i+1,j+1);
if game(i,j)==1
if a == 3 || a ==2
temp(i,j) = 1;
end
else
if a == 3
temp(i,j) = 1;
end
end
elseif j==y
a = game(i,j-1)+game(i+1,j-1)+game(i+1,j);
if game(i,j)==1
if a == 3 || a ==2
temp(i,j) = 1;
end
else
if a == 3
temp(i,j) = 1;
end
end
else
a = game(i,j-1)+game(i+1,j-1)+game(i+1,j)+game(i,j+1)+game(i+1,j+1);
if game(i,j)==1
if a == 3 || a ==2
temp(i,j) = 1;
end
else
if a == 3
temp(i,j) = 1;
end
end
end
elseif i == x
if j==1
a = game(i-1,j)+game(i-1,j+1)+game(i,j+1);
if game(i,j)==1
if a == 3 || a ==2
temp(i,j) = 1;
end
else
if a == 3
temp(i,j) = 1;
end
end
elseif j==y
a = game(i-1,j-1)+game(i-1,j)+game(i,j-1);
if game(i,j)==1
if a == 3 || a ==2
temp(i,j) = 1;
end
else
if a == 3
temp(i,j) = 1;
end
end
else
a = game(i-1,j-1)+game(i,j-1)+game(i-1,j)+game(i-1,j+1)+game(i,j+1);
if game(i,j)==1
if a == 3 || a ==2
temp(i,j) = 1;
end
else
if a == 3
temp(i,j) = 1;
end
end
end
elseif j == 1
a = game(i-1,j)+game(i+1,j)+game(i-1,j+1)+game(i,j+1)+game(i+1,j+1);
if game(i,j)==1
if a == 3 || a ==2
temp(i,j) = 1;
end
else
if a == 3
temp(i,j) = 1;
end
end
elseif j == y
a = game(i-1,j-1)+game(i,j-1)+game(i+1,j-1)+game(i-1,j)+game(i+1,j);
if game(i,j)==1
if a == 3 || a ==2
temp(i,j) = 1;
end
else
if a == 3
temp(i,j) = 1;
end
end
end
end
end
if game == temp
disp(strcat('在第',num2str(k),'轮元胞自动机达到稳态'))
for i=1:x
for j=1:y
if temp(i,j)==1
fx=[i-1,i-1,i,i];
fy=[j-1,j,j,j-1];
fill(fx,fy,'g');
hold on;
end
end
end
break;
end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
三、运行结果
其中初始状态我用均匀分布初始化矩形框,在20*20的区域内。