Cellular Autometa Matlab algorithm

profileaseelismail
onerealizationmodelSI.docx

%%more neighborhood code %agent based modeL simulation

% randperm(n) writes the numbers 1,2,...n

%% think about: 1) how to modify the code to pick one neighbor at random and then get sick at random with prob p if the neighbor if sick

%% 2) if we would add a recovery prob (precvr) of agent is said to recover w/prob precover

%% recover ->> immunity or back to suscebtable.

N = 400; % number of healthy (susceptible) agents

nx = 50; %2-d grid size

ny = 50;

p = 0.1; %transmission prob

q = 0.1; %stay-at-home prob %wanderprob %%you can change thiss to play with prob of infections

T = 500; %final time (delta t = 1)

ng = nx*ny; %%total number of grid points

pMove = (1-q);

xGrid = (1:nx)'*ones(1,ny); %will go 1;2;3;4;5;6...50 (50 times)

yGrid = ones(nx,1)*(1:ny);

xVect = xGrid(:); %will stack each vector or coloums under each other

yVect = yGrid(:);

X = zeros(N+1,3);

r = randperm(ng); % will generate a random list of numbers form 1 to 2500

indxList = r(1:N+1); % will take the fist n+1 numbers of 2500 and they wil be where the agents are distributed

X(:,1) = xVect(indxList); % one coloumn of a vector to a matrix

X(:,2) = yVect(indxList);

X(:,3) = 1;

X(1,3) = 2; % disease state 2

gridView = zeros(nx,ny);

gridView(X(1,1),X(1,2)) = 2;

for iAgent = 2:N+1

gridView(X(iAgent,1),X(iAgent,2)) = 1;

end

for iTime = 1:T

indxList = randperm(N+1);

for jAgent = 1:N+1 %%loop through each agent

me = indxList(jAgent);

ego = X(me,1:2);

%

% find empty neighboring cells

%

iEmpty = 0;

iCellList = zeros(9,2); %for 3 by 3 matrix %%see where the empty cells are for people to move around for neighboring cells

for iCell = -1:1

for jCell = -1:1

iNbr = iCell+ego(1);

jNbr = jCell+ego(2);

if (iNbr>=1)&(iNbr<=nx)&(jNbr>=1)&(jNbr<=ny)

if gridView(iNbr,jNbr) == 0

iEmpty = iEmpty + 1;

iCellList(iEmpty,:) = [iNbr,jNbr];

end

end

end

end

%

% Should i stay or should i go?

%

iStay = (rand<=q);

if iStay==0 & (iEmpty>0)

iGo = randperm(iEmpty);

iGoWhere = iGo(1);

X(me,1:2) = iCellList(iGoWhere,:);

gridView(ego(1),ego(2)) = 0; %moving

gridView(X(me,1),X(me,2)) = X(me,3);

end

%

% Will i get sick? %interact

%

for iCell = -1:1

for jCell = -1:1

iNbr = iCell+ego(1);

jNbr = jCell+ego(2);

if (iNbr>=1)&(iNbr<=nx)&(jNbr>=1)&(jNbr<=ny)

if gridView(iNbr,jNbr) == 2

sickNow = (rand<=p);

if sickNow == 1

X(me,3) = 2;

gridView(X(me,1),X(me,2)) = X(me,3);

end

end

end

end

end

end

percentSick = (100/(N+1))*sum(X(:,3)==2);

figure(1)

imagesc(gridView),title(['Time Step: ',num2str(iTime),' Percent Infected: ',num2str(percentSick)]),pause(0.1)

end