元胞自动机是一种基于离散空间的动态系统,由许多简单单元按照某些规则进行相互作用和演化而形成的复杂结构。元胞自动机可以用于模拟物理、生物、社会等领域的现象,以及进行优化、图像处理、噪声生成等方面的应用。
例1:生命游戏
nextStateCalculation.m
% 下一个状态的计算函数
function nextState = nextStateCalculation(currentState)
[m, n] = size(currentState);
nextState = zeros(m, n);
for i = 1:m
for j = 1:n
% 统计邻居细胞的存活数
liveNeighbours = sum(sum(currentState(max(i-1,1):min(i+1,m), max(j-1,1):min(j+1,n)))) - currentState(i, j);
if currentState(i, j) == 1
% 活细胞规则
if liveNeighbours < 2 || liveNeighbours > 3
nextState(i, j) = 0; % 孤立或拥挤死亡
else
nextState(i, j) = 1; % 继续存活
end
else
% 死细胞规则
if liveNeighbours == 3
nextState(i, j) = 1; % 繁殖
else
nextState(i, j) = 0; % 仍然死亡
end
end
end
end
end
主程序:
% 定义初始状态
initialState = randi([0 1], 50, 50); % 50x50 的随机初始状态
% 显示初始状态
figure;
imagesc(initialState);
colormap(summer);
title('初始状态');
% 模拟演化
numIterations = 100;
for t = 1:numIterations
% 计算下一个状态
nextState = nextStateCalculation(initialState);
% 显示下一个状态
imagesc(nextState);
colormap(summer);
title(['第', num2str(t), '代']);
pause(0.1);
% 更新状态
initialState = nextState;
end
效果如下:
例2:森林火灾(完全烧毁)
simulateForestFire.m
% 定义森林火灾模拟函数
function simulateForestFire(rows, cols, pTree, pBurning, pIgnition, numIterations)
% 初始化森林状态
forest = zeros(rows, cols); % 0代表空地,1代表树木,2代表正在燃烧
% 随机生成树木
forest(rand(rows, cols) < pTree) = 1;
% 随机选择一个树木点作为起火点
burningTree = randi([1, rows], 1, 2);
forest(burningTree(1), burningTree(2)) = 2;
% 模拟森林火灾传播过程
for t = 1:numIterations
forest = updateForest(forest, pBurning, pIgnition);
% 可视化当前森林状态
imagesc(forest);
colormap([1 1 1; 0 1 0; 1 0 0]); % 白色-空地,绿色-树木,红色-着火
title(['第', num2str(t), '代']);
pause(0.1);
end
end
updateForest.m
% 更新森林状态
function newForest = updateForest(forest, pBurning, pIgnition)
[rows, cols] = size(forest);
newForest = forest;
for i = 1:rows
for j = 1:cols
if forest(i, j) == 1 % 树木
% 根据周围树木着火情况更新当前点状态
if any(neighbors(forest, i, j) == 2) || rand < pIgnition
newForest(i, j) = 2; % 着火
end
elseif forest(i, j) == 2 % 着火
newForest(i, j) = 0; % 燃尽
end
end
end
end
neighbors.m
% 获取邻居状态
function neighborStates = neighbors(forest, i, j)
[rows, cols] = size(forest);
neighborStates = zeros(1, 8);
for k = -1:1
for l = -1:1
if k == 0 && l == 0
continue;
end
if i+k >= 1 && i+k <= rows && j+l >= 1 && j+l <= cols
neighborStates((k+1)*3+l+2) = forest(i+k, j+l);
end
end
end
end
调用函数
% 调用函数进行森林火灾模拟
simulateForestFire(50, 50, 0.7, 0.01, 0.4, 100); % 行数、列数、树木密度、树木燃烧概率、点燃概率、迭代次数
效果如下:
例3:种群繁殖模拟(以性别比例为例)
% 初始化参数
gridSize = 50; % 定义格子空间大小
nSteps = 100; % 模拟步数
initialDensity = 0.1; % 初始种群密度
reproductionRate = 0.05; % 繁殖率
mortalityRate = 0.02; % 死亡率
foodSupply = rand(gridSize); % 食物供应随机分布
% 初始化格子空间
populationGrid = zeros(gridSize, gridSize, nSteps);
genderRatioGrid = zeros(gridSize, gridSize, nSteps); % 性别比例,假设初始时0.5(1代表全雄性,0代表全雌性)
% 初始种群和性别比例
populationGrid(:,:,1) = rand(gridSize) < initialDensity;
genderRatioGrid(:,:,1) = 0.5 * ones(gridSize);
% 元胞自动机主循环
for t = 2:nSteps
for x = 1:gridSize
for y = 1:gridSize
% 获取邻居索引,考虑周期边界条件
[neighX, neighY] = meshgrid(x-1:x+1, y-1:y+1);
neighX = mod(neighX - 1, gridSize) + 1;
neighY = mod(neighY - 1, gridSize) + 1;
% 计算邻居的平均食物供应
avgFoodSupply = mean(mean(foodSupply(neighX, neighY)));
% 更新种群和性别比例
currentPopulation = populationGrid(x, y, t-1);
currentGenderRatio = genderRatioGrid(x, y, t-1);
newPopulation = currentPopulation + reproductionRate * avgFoodSupply * currentPopulation - mortalityRate * currentPopulation;
newGenderRatio = currentGenderRatio; % 可以添加基于食物供应或其他因素的性别比例调整规则
% 更新状态
populationGrid(x, y, t) = newPopulation;
genderRatioGrid(x, y, t) = newGenderRatio;
end
end
end
% 可视化最终步骤的种群密度
imagesc(populationGrid(:,:,end));
colorbar;
title('Final Population Density');
xlabel('X');
ylabel('Y');
运行效果: