***I have edited the infect function and included a vaccinate function which both run and produce new matrices. However, for i==n and j==m, the infect function says that it is improperly indexed and it wont write to the right and bottom boundary of the function
import random
from copy import copy
import numpy as np
def infect(Pop,i,j,n,m, tau):
B = copy(Pop)
if (i==n): #this is how I have tried to get around this matrix boundary issue
if (j==0): #but when I run it, the code behaves exactly as it did when this wasnt here
is_infected = False
for neighbor in [Pop[i-1,j],Pop[i,j+1]]:
if neighbor:
B[i,j] = int(Pop[i,j] or (np.random.rand() < tau))
if (j==m):
is_infected = False
for neighbor in [Pop[i-1,j],Pop[i,j-1]]:
if neighbor:
B[i,j] = int(Pop[i,j] or (np.random.rand() < tau))
if (j>0) and (j<m):
is_infected = False
for neighbor in [Pop[i-1,j],Pop[i,j+1],Pop[i,j-1]]:
if neighbor:
B[i,j] = int(Pop[i,j] or (np.random.rand() < tau))
if (j==m):
if (i==0):
is_infected = False
for neighbor in [Pop[i+1,j],Pop[i,j-1],Pop[i-1,j]]:
if neighbor:
B[i,j] = int(Pop[i,j] or (np.random.rand() < tau))
for i in range(n): ##changed to: for i in range(1,n-1) #this is the part that
for j in range(m): ##changed to: for j in range(1,m-1) #works excpet for the
is_infected = False #final row and column
for neighbor in [Pop[i-1,j],Pop[i,j-1], Pop[i+1,j], Pop[i,j+1]]:
if neighbor:
B[i,j] = int(Pop[i,j] or (np.random.rand() < tau))
return B
def vaccinate(Pop,i,j,n,m,mu):
V = copy(Pop) #since this function is purely random and doesn't rely on
for i in range(n): #neighbor values, it runs fine
for j in range(m):
if Pop[i,j] == -2 or Pop[i,j] == -1 or Pop[i,j] > 0:
V[i,j] = Pop[i,j]
if Pop[i,j] == 0:
t = (np.random.rand() < mu)
if (t==True):
V[i,j] = -2
return V
i = 0
j = 0
n = 10
m = 10
k = int(input("How many days should it take to recover?"))
tau = 0.5
mu = 0.2
D = np.zeros((k + 1,n,m))
A = np.zeros((n,m))
n1 = random.sample(range(n),1)
m1 = random.sample(range(m),1)
A[n1,m1] = 1
print(A)
for d in range(k):
D[d,:,:] = A #rather than a for loop would it be easier/better to
A = vaccinate(A,i,j,n,m,mu) #implement a while loop?
A = infect(A,i,j,n-1,m-1,tau)
print(A)
D[k,:,:] = A
should i maybe make the entire matrix one row and column larger and just pad it with zeros? I don't actually have to present the final matrix, it is solely for computational purposes so maybe that could work. But i do have to have the simulation terminate once all values within the matrix are not equal to zero. Is there a way around this? Could I just delete the extra row and column before then or since im placing the function into a thread, will it screw everything up?
as for the while loop:
def infectAll(Pop,n,m,tau):
Aout = copy(Pop)
#for i in range(n):
#for j in range(m):
if Pop[i,j] == -2:
Aout = Pop[i,j]
vac = []
if Pop[i,j] == -1:
Aout = Pop[i,j]
if Pop[i,j]>0 and Pop[i,j]<=k:
Aout[i,j] = Pop[i,j] + 1
inf = []
if Pop[i,j]>k:
Aout[i,j] = -1
if Pop[i,j] == 0:
Aout = infect(Pop,i,j,n,m,tau)
if Aout[i,j]==0:
Aout = vaccinate(Pop,i,j,n,m,nu)
return Aout
A[n1,m1] = 1
d = 0
print(A) #i need d to count the iterations per day, but i also need
while d < 100: #to be able to creat 3 different vectors base on the
for i in range(n): #matrix data (# total recovered, total #vaccinated and
for j in range(m): #total infected per day
if A[i,j]>=0:
A = infectAll(A,n,m,tau)
d = d+1
else:
break