我的元胞自动机代码不工作,因为我打算它工作
问题描述:
clear all;
clc;
%% Creating a grid with random value
n = 64;
Gpop = rand(n,n);
temp=Gpop;
Gpop(temp(:,:)<0.99) = 1; %Healthy percentage 99%
Gpop(temp(:,:)>0.99 & temp(:,:)<0.994) = 2; %Healthy percentage .04%
Gpop(temp(:,:)>0.994 & temp(:,:)<0.998) = 3; %Healthy percentage .04%
Gpop(temp(:,:)>0.998) = 4; %Healthy percentage .02%
%% Our Rules of cellular automata
x = 2:n-1; % Intializing x and y values to access the cells of CA
y = 2:n-1;
rule = Gpop;
figure
count=0;
time = 0;
while(count<25)
rule((rule(x-1,y-1)==2)|(rule(x,y-1)==2)|(rule(x+1,y-1)==2)|(rule(x-1,y)==2)|(rule(x+1,y)==2)...
|(rule(x-1,y+1)==2)|(rule(x,y+1)==2)|(rule(x+1,y+1)==2) & time==1)=2 ; %1st Rule a
if((rule(x,y-1)==3)| (rule(x-1,y)==3)|(rule(x+1,y)==3)|(rule(x,y+1)==3) & time ==2);
rule(x,y)==2;
else((rule(x-1,y-1)==3)|(rule(x+1,y-1)==3)|(rule(x-1,y+1)==3)|(rule(x+1,y+1)==3) & time ==3);
rule(x,y)==2;
end
rule((rule(x-1,y-1)==3)|(rule(x,y-1)==3)|(rule(x+1,y-1)==3)|(rule(x-1,y)==3)|(rule(x+1,y)==3)...
|(rule(x-1,y+1)==3)|(rule(x,y+1)==3)|(rule(x+1,y+1)==3) & time==4)=3; %2nd rule
rule((rule(x-1,y-1)==4)|(rule(x,y-1)==4)|(rule(x+1,y-1)==4)|(rule(x-1,y)==4)|(rule(x+1,y)==4)...
|(rule(x-1,y+1)==4)|(rule(x,y+1)==4)|(rule(x+1,y+1)==4&time==6))=4; %3rd rule
newMatrix=rand(n,n);
newtemp=newMatrix;
newMatrix(newtemp(:,:)<=.1)=1;
newMatrix(newtemp(:,:)>.1)=0;
rule(((rule(x-1,y-1)==4)|(rule(x,y-1)==4)|(rule(x+1,y-1)==4)|(rule(x-1,y)==4)|(rule(x+1,y)==4)...
|(rule(x-1,y+1)==4)|(rule(x,y+1)==4)|(rule(x+1,y+1)==4)) & newMatrix(x,y)==1 & time == 8)=1; %1st part 4th rule
rule(((rule(x-1,y-1)==4)|(rule(x,y-1)==4)|(rule(x+1,y-1)==4)|(rule(x-1,y)==4)|(rule(x+1,y)==4)...
|(rule(x-1,y+1)==4)|(rule(x,y+1)==4)|(rule(x+1,y+1)==4)) & newMatrix(x,y)==0 & time == 10)=2; %1st part 4th rule
imagesc(rule)
axis off;
cmap = jet(4); % assign colormap
colormap(cmap)
hold on
L = line(ones(4), ones(4), 'LineWidth',2); % generate line
set(L,{'color'},mat2cell(cmap,ones(1,4),3)); % set the colors according to cmap
legend('H','I1','I2','D') %Addings Legends at the top right corner of image
count=count+1;
time = time+1;
pause(3.0)
end
以上是模拟HIV病毒4个阶段的元胞自动机代码。当我运行上面的代码右侧单元格保持原样,没有任何更改,我试图很难找到什么错误,但无法太。我的元胞自动机代码不工作,因为我打算它工作
以下是我的自动机的规则,
规则1:如果H单元满足以下列出的规则中的至少一个,就在下一步骤中的I1细胞: (ⅰ)至少一种I1小区在最近的邻居或第二个最近的邻居; (ii)最近的邻居中至少有x个I2个单元,第二个最近的邻居中的I2个单元。
规则2:I1单元在下一步中变为I2单元。规则3:由于免疫识别和清除,I2步骤在τ步骤后变成D细胞。规则4:D细胞可以被概率为Pinf的I1细胞替换,或者在下一步中被概率为(Prep - Pinf)的H细胞替换。
我想知道我的代码是否符合这些规则,以及为了正确模拟病毒,我必须在代码中进行哪些更改。请任何人帮我解决这个问题。在此先感谢
答
您的问题是,当您测试每个节点的8邻居的规则时,0-1决策矩阵是62*62
(因为您设置x/y = 2:n-1
),然后0/1设置为规则矩阵,所以最后两列始终保持不变,因为你永远不会“触摸”它们!
要理解我的意思,只需在任何规则上设置一个断点即可。通过打印上述结果
(rule(x-1,y-1)==2)|(rule(x,y-1)==2)|(rule(x+1,y-1)==2)|(rule(x-1,y)==2)|(rule(x+1,y)==2)...
|(rule(x-1,y+1)==2)|(rule(x,y+1)==2)|(rule(x+1,y+1)==2)
,你会发现这是一个62*62
矩阵。
我知道你想用矩阵计算来简化代码,并且同时避免边界问题。但现在我不能提出更好的解决方案,除了通过x和y设置循环,并且如果点位于边界上,只需使用3或5邻居。
另一种方法是创建“松弛”行和列,如rule.size()=66*66
,并将边界设置为零,然后在绘制时只需丢弃松弛的行和列。
希望这会有所帮助。
你是什么意思_右侧细胞保持原样,没有任何变化_? – 2014-10-28 22:48:13
我的意思是在模拟发生后,第60列的单元格保持不变。它就像在你运行代码之后,图像就会随着它的变化而出现。如果你正确地观察图像,图像的右侧保持原样而没有任何变化,但是不应该发生,我只想知道我的代码中的缺陷以及我应该对模拟这种元胞自动机所做的更改。请帮我困在中间,我很无能。 – Hari 2014-10-29 03:22:34