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单元: (i)最近邻居或第二近邻居中至少一个I1小区; (ii)最近邻居中的至少x I2个小区,第二个最近邻居中的y I2个小区。
规则2:I1单元格在下一步中成为I2单元格。
规则3:由于免疫识别和清除,I2细胞在τ步后成为D细胞。
规则4:D细胞可以被概率为Pinf的I1细胞替换,或者在下一步中被具有概率的H细胞替换(Prep-Pinf)。
我想知道我的代码是否符合这些规则,以及我在代码中需要做些哪些更改才能正确模拟病毒。请有人帮我解决这个问题。提前致谢
答案 0 :(得分:0)
您的问题是,当您测试每个节点的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
,并将边界设置为零,然后在绘制时抛弃松弛的行和列。
希望这有帮助。