康威生命游戏中的静物发生器



我在考虑静物生成器(它不需要严格)。代码应该尽可能简单。我想出了这个主意:首先我生成随机矩阵,然后遍历每个元素,并检查以下两条规则:1.活细胞必须有2个或3个活邻居。2.死细胞可以有除3个外的任何数量的活邻居。如果细胞不满足这个规则,我切换它(如果它死了,我让它活着等等),直到它满足这些规则。不幸的是,如果我换了一个细胞,另一个细胞需要修复,需要很长时间才能稳定下来。

我知道你可以对生命游戏进行X次迭代,直到它稳定下来,但你需要处理和检测一些振荡器。

问题是:如何更轻松地搜索静物?你能分享一些代码/想法吗

这是我根据自己的想法在matlab中创建的代码。代码工作不正常,尤其是在我试图切换符合规则的单元格的部分

% live cell must have either 2 or 3 live neighbors.
% dead cell can have any number of live neighbors except 3.
DIM = 20;
M = randi(2, DIM+1) - 1;
%zeros the bounds
M(1,:) = 0;
M(DIM,:) = 0;
M(:,1) = 0;
M(:,DIM) = 0;
for x = 2:length(M)
for y = 2:length(M)
%M = double((M & neighbours == 2) | neighbours == 3);
neighbours = conv2(M, [1 1 1; 1 0 1; 1 1 1], 'same'); % returns numbers of neighbours
if neighbours(x,y) == 1 || neighbours(x,y) == 0
M(x,y) = 0;
end
if M(x, y) == 1 && (neighbours(x,y) > 3) || M(x, y) == 0 && neighbours(x,y) == 3 % still life
if M(x,y)
todel = neighbours(x, y)-3;
else
todel = 1;
end
while todel
a = randi(3, 1) - 2; % randomly choose cell to toggle
b = randi(3, 1) - 2;
if (a || b)
M(x+a, y+b) = ~M(x+a, y+b); % toggle cell
sprintf('(%d, %d);;(%d, %d)',x, y, x+a, y+b)
neighbours = conv2(M, [1 1 1; 1 0 1; 1 1 1], 'same'); % returns numbers of neighbours
if ((M(x, y) == 1 && (neighbours(x,y) == 2 || neighbours(x,y) == 3)) ||  (M(x, y) == 0 && neighbours(x,y) ~= 3)) % still life
todel = todel-1;
end
end
end
end 
M(1,:) = 0;
M(DIM,:) = 0;
M(:,1) = 0;
M(:,DIM) = 0;
end
end
%end
imshow(M, 'InitialMagnification', 1000);
drawnow;

有一个NxN板,并设置为随机像素。然后,对于每个发生变化的像素,用+1对板进行评分。然后做一个随机变异,如果分数上升,拒绝它,如果分数下降,接受它。实际上,您可能必须使用模拟退火。

http://www.malcolmmclean.site11.com/www/SimulatedAnnealing/SimulatedAnnealing.html

或者还有其他搜索策略。最终,您应该得到一个==0的更改。我能看到的障碍是,这可能是琐碎的"死亡"生活。

最新更新