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

Above is the cellular automaton code for simulating HIV virus 4 stages. When i run the above code the right side cells remain as it is without any changes i tried very hard to find whats wrong but unable too.

Following are the rules of my automata,

Rule 1: If an H cell satisfies at least one of the rules listed below, it becomes an I1 cell in the next step: (i) At least one I1 cell in the nearest neighbor or the second nearest neighbor; (ii) At least x I2 cells in the nearest neighbor, y I2 cells in the second nearest neighbor.

Rule 2: An I1 cell becomes an I2 cell in the next step.

Rule 3: An I2 cell becomes a D cell after τ steps because of the immune recognition and clear.

Rule 4: A D cell can be replaced by an I1 cell with probability Pinf or replaced by an H cell with probability (Prep − Pinf) in the next step.

I want to know whether my code matches these rules and what changes i have to do in my code to get correct simulation of the virus. Please anyone help me out with this. Thanks in advance

Hari
  • 1
  • 6
  • 1
    What do you mean by _the right side cells remain as it is without any changes_ ? – Benoit_11 Oct 28 '14 at 22:48
  • I mean the cells from 60th column remain unchanged ever after the simulation takes place. Its like after u run the code an image appear with changes taking place in it. If u observe the image properly right side of the image remain as it is without any change but that shouldn't happen i just want to know the flaws in my code and changes that i should make to simulate this cellular automata. Please help i am stuck in between and i am clueless. – Hari Oct 29 '14 at 03:22

1 Answers1

0

Your problem is that when you test the rules on the 8-neighbors of each nodes, the 0-1 decision matrix is 62*62 (because you set x/y = 2:n-1), then 0/1 is set to rule matrix, so the last two columns remain the same all the time, because you never "touch" them!

To understand what I mean, just set a breakpoint on any rule, e.g.

(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)

by printing the above result, you will find it is a 62*62 matrix.

I know you want to use matrix computation to simplify the code, and at the same time avoid boundary problem. But for now I cannot come up with better solution except set for loop through x and y and if the point is on boundary, just use 3 or 5-neighbors.

Another way is to create "slack" rows and columns, like rule.size()=66*66, and set the boundary to be zero, then when drawing just throw away the slack rows and columns.

Hope this helps.

Awtszs
  • 323
  • 1
  • 8
  • 33
luochenhuan
  • 1,057
  • 11
  • 17
  • Thanks for your solutions but can you please provide me with the code for whatever u said. Thanks in advance – Hari Oct 29 '14 at 13:55
  • I tried whatever u said with the boundary cells by checking for 3 or 5 neighbors but there has been no change. If u can observe the image properly its not just last two columns but around 5-10 columns remain unchanged please help. Thanks in advance. – Hari Oct 29 '14 at 14:23