这是模拟带有一些感染者的人群的代码,感染者可以使未感染者感染。任务的一部分是添加额外的东西,我添加了绿色的免疫力强的人。我希望免疫的人也能使感染者也免疫,但是我不知道该怎么做,我是编码的初学者,所以如果有人可以解释如何做到这一点,我将非常感谢。
最后有一些额外的代码,但我认为这对问题没有影响。我知道我粘贴了很多代码,但不确定与我要执行的操作和需要更改的内容有关。最后一部分是我添加的大部分内容,它显示了具有免疫力的人,但他们不会改变其他人。如果您认为最后的代码也很重要,我可以将其发送过来。
# COMMAND LINE ARGUMENTS
if (len(sys.argv)<7):
INIT_POP = 100
INIT_INFECTED = 5
NUM_COLS = 10
NUM_ROWS = 10
NUM_STEPS = 10
INIT_IMMUNE = 5
print('***Default*** \nInitial Population = '+str(INIT_POP)+'\nInitial Infected = '+str(INIT_INFECTED)+'\nNumber of Columns = '+str(NUM_COLS)
+'\nNumber of rows = '+str(NUM_ROWS)+'\nNumber of Steps = '+str(NUM_STEPS)+'\nNumber Immune = '+str(INIT_IMMUNE))
else:
INIT_POP = int(sys.argv[1])
INIT_INFECTED = int(sys.argv[2])
NUM_COLS = int(sys.argv[3])
NUM_ROWS = int(sys.argv[4])
NUM_STEPS = int(sys.argv[5])-1
INIT_IMMUNE = int(sys.argv[6])
print('****THE EPIDEMIC**** \nThe Population of the world = '+str(INIT_POP)+'\nThe Infected = '+str(INIT_INFECTED)+'\nColumns = '+str(NUM_COLS)
+'\nrows = '+str(NUM_ROWS)+'\nNumber of Steps = '+str(NUM_STEPS)+'\nThe Immune = '+str(INIT_IMMUNE))
def distribute(grid, num_r, num_c, numpeep):
for i in range(numpeep):
rpos = random.randint(0, num_r-1)
cpos = random.randint(0, num_c-1)
grid[rpos, cpos] += 1
# print("Adding 1 to (", xpos, ",", ypos,")")
````
def makeScatter(grid, num_r, num_c):
r_values = []
c_values = []
count_values = []
for row in range(num_r):
for col in range(num_c):
if grid[row,col] > 0:
r_values.append(row)
c_values.append(col)
count_values.append(grid[row,col]*100)
# print("Value at (", row, ",", col, ") is ", grid[row, col])
return(r_values, c_values, count_values)
`````
def displayGrid(grid, num_r, num_c):
for row in range(num_r-1, -1, -1):
for col in range(num_c):
print(grid[row,col], end=" ")
print()
def plotGrids():
plt.figure(figsize=(20,20))
Irows, Icols, Icount = makeScatter(infected, NUM_ROWS, NUM_COLS)
plt.scatter(Icols, Irows, s=Icount, c="r", alpha=0.5)
Urows, Ucols, Ucount = makeScatter(uninfected, NUM_ROWS, NUM_COLS)
plt.scatter(Ucols, Urows, s=Ucount, c="b", alpha=0.5)
Mrows, Mcols, Mcount = makeScatter(immune, NUM_ROWS, NUM_COLS)
plt.scatter(Mrows, Mcols, s=Mcount, c="y", alpha=0.8)
plt.show()
````
def movePeeps(cur, next, r, c):
print("Pos (", r, ",", c, ") has ", cur[r,c], " people")
for peep in range(cur[r,c]):
rMove = random.randint(-1,1)
cMove = random.randint(-1,1)
print("Move from (", r, ",", c, ") to (", r+rMove, "," , c+cMove, ")")
if (r + rMove) > (NUM_ROWS-1) or (r + rMove) < 0:
rMove = 0
if (c + cMove) > (NUM_COLS-1) or (c + cMove) < 0:
cMove = 0
next[r + rMove, c + cMove] +=1
print(" (", r, ",", c, ") to (", r+rMove, "," , c+cMove, ")")
`````
def infect(inf, notinf, r, c, prob):
# print("Pos (", r, ",", c, ") has ", inf[r,c], " inf people and ", notinf[r,c], " well people")
prob = prob * inf[r,c]
if prob:
for peep in range(notinf[r,c]):
if random.random() < prob:
inf[r, c] +=1
notinf[r, c] -=1
print("***** New infection (", r, ",", c, ")")
`````
`````
def immune(imm, notimm, r, c, prob):
# print("Pos (", r, ",", c, ") has ", inf[r,c], " inf people and ", notinf[r,c], " well people")
prob = prob * imm[r,c]
if prob:
for peep in range(inf[r,c]):
if random.random() < prob:
imm[r, c] +=1
inf[r, c] -=1
`````