Last active
May 6, 2018 05:01
-
-
Save ven-kyoshiro/0eb707def0ab0873902bb9c19a395618 to your computer and use it in GitHub Desktop.
This is simple implementation of outbreak model introduced at '情報学修論' at Waseda University graduate school.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
''' | |
ある人が抗体を持っている確率 | |
p :[0-1) | |
想定する人数を配置する正方形の一辺 | |
N | |
''' | |
import numpy as np | |
import random | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
p = 0.6 | |
N = 40 | |
''' | |
再起アルゴリズムで, | |
まず自分の位置を塗りつぶす | |
隣接するマスを(4方向)を調べて,抗体がない(1)ならそいつに対して再帰 | |
''' | |
def pandemic(x,y,data): | |
data[x][y]=-5.0 | |
# 端っこは探索しない | |
#右 | |
if x+1!=N and data[x+1][y]==0.0: data = pandemic(x+1,y,data) | |
if x-1!=-1 and data[x-1][y]==0.0: data = pandemic(x-1,y,data) | |
if y+1!=N and data[x][y+1]==0.0: data = pandemic(x,y+1,data) | |
if y-1!=-1 and data[x][y-1]==0.0: dara = pandemic(x,y-1,data) | |
return data | |
''' | |
値の意味(float): 1:抗体ある | |
0:抗体ない | |
-5:感染している | |
''' | |
def main(): | |
''' | |
感染前のデータを準備 | |
''' | |
before_data = np.array([[ | |
1.0 if random.random()<p else 0.0 for i in range(N)] for j in range(N)]) | |
# sns.heatmap(before_data,vmin = -2,vmax = 2,linewidths=.5) | |
# NxNでランダムに一点感染させる | |
x = random.choice(range(N)) | |
y = random.choice(range(N)) | |
before_data[x][y]=-5.0 | |
# sns.heatmap(before_data,vmin = -2,vmax = 2,linewidths=.5) | |
after_data = pandemic(x,y,before_data) | |
sns.heatmap(after_data,vmin = -2,vmax = 2,linewidths=.5) | |
plt.show() | |
plt.savefig("seaborntest.png") # save as png file | |
# 感染した人の人数 | |
print(len(np.where(after_data==-5)[0])) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment