2019-ics-malariafreek/malaria.py

105 lines
2.9 KiB
Python
Raw Normal View History

2019-03-07 16:06:20 +00:00
import matplotlib.pyplot as plt
import matplotlib.colors
2019-03-07 15:42:41 +00:00
import numpy as np
import random
from enum import IntEnum
class Model:
def __init__(self, width=32, height=32, humandens=0.15, mosquitodens=0.10,
2019-03-07 16:06:20 +00:00
immunepct=0.1, mosqinfpct=0.1, hm_infpct=0.5, mh_infpct=0.5,
2019-03-07 15:42:41 +00:00
hinfdiepct=0.01, mhungrypct=0.1, humandiepct=10**-6,
mosqdiepct=10**-3, mosqnetdens=0.05, time_steps=500):
self.width = width
self.height = height
2019-03-07 16:06:20 +00:00
2019-03-07 15:42:41 +00:00
self.humandens = humandens
self.mosquitodens = mosquitodens
self.immunepct = immunepct
self.mosqinfpct = mosqinfpct
self.hm_infpct = hm_infpct
self.mh_infpct = mh_infpct
self.hinfdiepct = hinfdiepct
self.mhungrypct = mhungrypct
self.humandiepct = humandiepct
self.mosqdiepct = mosqdiepct
self.mosqnetdens = mosqnetdens
self.time_steps = time_steps
self.humans = self.gen_humans()
self.mosquitos = self.gen_mosquitos()
def recycle_human(self):
# Get all living humans
2019-03-07 16:06:20 +00:00
humans = np.transpose(np.where(self.grid != Human.DEAD))
# Get a mask of humans to kill
deaths = np.random.rand(len(humans)) < self.humandiepct
# Kill them.
self.grid[humans[deaths][:, 0], humans[deaths][:, 1]] = Human.DEAD
2019-03-07 15:42:41 +00:00
# Pick a random, unpopulated spot
2019-03-07 16:06:20 +00:00
x, y = random.choice(np.transpose(np.where(self.grid == Human.DEAD)),
size=np.count_nonzero(deaths))
2019-03-07 15:42:41 +00:00
def gen_humans(self):
2019-03-07 16:06:20 +00:00
# Calculate the probabilities
2019-03-07 15:42:41 +00:00
p_dead = 1 - self.humandens
p_immune = self.humandens * self.immunepct
2019-03-07 16:06:20 +00:00
p_healthy = self.humandens - p_immune
2019-03-07 15:42:41 +00:00
2019-03-07 16:06:20 +00:00
# Create the grid with humans.
2019-03-07 15:42:41 +00:00
return np.random.choice((Human.DEAD, Human.HEALTHY, Human.IMMUNE),
size=(self.width, self.height),
2019-03-07 16:06:20 +00:00
p=(p_dead, p_healthy, p_immune))
2019-03-07 15:42:41 +00:00
def gen_mosquitos(self):
count = self.width * self.height * self.mosquitodens
def run(self):
for _ in range(self.time_steps):
self.step()
self.draw()
def step(self):
2019-03-07 16:06:20 +00:00
# dingen
2019-03-07 15:42:41 +00:00
pass
2019-03-07 16:06:20 +00:00
2019-03-07 15:42:41 +00:00
def draw(self):
# this function draws the humans
2019-03-07 16:06:20 +00:00
colors = matplotlib.colors.ListedColormap(
["black", "green", "red", "yellow"])
bounds = [Human.DEAD, Human.HEALTHY, Human.INFECTED, Human.IMMUNE]
norm = matplotlib.colors.BoundaryNorm(bounds, colors.N)
plt.imshow(self.humans, cmap=colors, norm=norm)
2019-03-07 15:42:41 +00:00
plt.pause(0.0001)
plt.clf()
class Mosquito:
def __init__(self, x: int, y: int, infected: bool, hungry: bool):
self.x = x
self.y = y
self.infected = infected
self.hungry = hungry
class Human(IntEnum):
DEAD = 0
HEALTHY = 1
INFECTED = 2
IMMUNE = 3
if __name__ == "__main__":
plt.ion()
2019-03-07 16:06:20 +00:00
model = Model()
while True:
model.draw()
2019-03-07 15:42:41 +00:00