2014-10-28 70 views
0
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細胞替換。

我想知道我的代碼是否符合這些規則,以及爲了正確模擬病毒,我必須在代碼中進行哪些更改。請任何人幫我解決這個問題。在此先感謝

+1

你是什麼意思_右側細胞保持原樣,沒有任何變化_? – 2014-10-28 22:48:13

+0

我的意思是在模擬發生後,第60列的單元格保持不變。它就像在你運行代碼之後,圖像就會隨着它的變化而出現。如果你正確地觀察圖像,圖像的右側保持原樣而沒有任何變化,但是不應該發生,我只想知道我的代碼中的缺陷以及我應該對模擬這種元胞自動機所做的更改。請幫我困在中間,我很無能。 – Hari 2014-10-29 03:22:34

回答

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,並將邊界設置爲零,然後在繪製時只需丟棄鬆弛的行和列。

希望這會有所幫助。

+0

感謝您的解決方案,但您可以爲我提供任何你所說的代碼。在此先感謝 – Hari 2014-10-29 13:55:11

+0

我試圖通過檢查3或5個鄰居來說明邊界單元,但沒有改變。如果你可以正確觀察圖像,它不只是最後兩列,而是大約5-10列保持不變,請幫助。提前致謝。 – Hari 2014-10-29 14:23:15