元胞自动机是一种基于离散空间的动态系统,由许多简单单元按照某些规则进行相互作用和演化而形成的复杂结构。元胞自动机可以用于模拟物理、生物、社会等领域的现象,以及进行优化、图像处理、噪声生成等方面的应用。
例1:生命游戏
nextStateCalculation.m
% 下一个状态的计算函数
function nextState = nextStateCalculation(currentState)[m, n] = size(currentState);nextState = zeros(m, n);for i = 1:mfor 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 > 3nextState(i, j) = 0; % 孤立或拥挤死亡elsenextState(i, j) = 1; % 继续存活endelse% 死细胞规则if liveNeighbours == 3nextState(i, j) = 1; % 繁殖elsenextState(i, j) = 0; % 仍然死亡endendendend
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:numIterationsforest = 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:rowsfor j = 1:colsif forest(i, j) == 1 % 树木% 根据周围树木着火情况更新当前点状态if any(neighbors(forest, i, j) == 2) || rand < pIgnitionnewForest(i, j) = 2; % 着火endelseif forest(i, j) == 2 % 着火newForest(i, j) = 0; % 燃尽endendend
end
neighbors.m
% 获取邻居状态
function neighborStates = neighbors(forest, i, j)[rows, cols] = size(forest);neighborStates = zeros(1, 8);for k = -1:1for l = -1:1if k == 0 && l == 0continue;endif i+k >= 1 && i+k <= rows && j+l >= 1 && j+l <= colsneighborStates((k+1)*3+l+2) = forest(i+k, j+l);endendend
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:nStepsfor x = 1:gridSizefor 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;endend
end% 可视化最终步骤的种群密度
imagesc(populationGrid(:,:,end));
colorbar;
title('Final Population Density');
xlabel('X');
ylabel('Y');
运行效果: