質問編集履歴
3
文法の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
### 前提
|
2
2
|
|
3
3
|
Pythonで通常のSIRモデルを拡張し,ネットワーク上のエージェントを対象としたモデルを作成しています.
|
4
|
-
|
4
|
+
結果のプロットが上手くいきません
|
5
5
|
|
6
6
|
### 実現したいこと
|
7
7
|
|
8
|
-
- 初期感染者の決定
|
9
8
|
- 各状態のリストの作成
|
10
9
|
- グラフへの正しいプロット
|
11
|
-
- 最終的にはコードの完成を目指しています
|
12
10
|
|
13
11
|
### 発生している問題・エラーメッセージ
|
14
12
|
|
@@ -108,17 +106,7 @@
|
|
108
106
|
if num_i == 0:
|
109
107
|
print('spreading finished')
|
110
108
|
break
|
111
|
-
|
112
|
-
|
113
|
-
plt.plot(max_iter,num_s, color=(0.2,0.2,0.2), linewidth=1.0, label='Susceptible')
|
114
|
-
plt.plot(max_iter,num_i, color=(1.0,0,0.0), linewidth=1.0, label='Infective')
|
115
|
-
plt.plot(max_iter,num_r, color=(0.0,1.0,0.0), linewidth=1.0, label='Removal')
|
116
109
|
|
117
|
-
plt.xlim(0,100)
|
118
|
-
plt.legend()
|
119
|
-
plt.xlabel('t')
|
120
|
-
plt.ylabel('count')
|
121
|
-
plt.show()
|
122
110
|
return num_s, num_i, num_r
|
123
111
|
|
124
112
|
|
@@ -136,6 +124,15 @@
|
|
136
124
|
|
137
125
|
initialize_state(agents, num_initial_infected_agents)
|
138
126
|
num_s, num_i, num_r = disease_spreading(agents, beta, gamma, max_iter)
|
127
|
+
|
128
|
+
plt.plot(max_iter, num_s, color=(0.2,0.2,0.2), linewidth=1.0, label='Susceptible')
|
129
|
+
plt.plot(max_iter, num_i, color=(1.0,0,0.0), linewidth=1.0, label='Infective')
|
130
|
+
plt.plot(max_iter, num_r, color=(0.0,1.0,0.0), linewidth=1.0, label='Removal')
|
131
|
+
plt.xlim(0,100)
|
132
|
+
plt.legend()
|
133
|
+
plt.xlabel('t')
|
134
|
+
plt.ylabel('count')
|
135
|
+
plt.show()
|
139
136
|
|
140
137
|
|
141
138
|
if __name__=='__main__':
|
@@ -144,16 +141,7 @@
|
|
144
141
|
|
145
142
|
|
146
143
|
### 試したこと
|
144
|
+
ウェブサイトの検索
|
147
145
|
|
148
|
-
- printを挟みデバッグ
|
149
|
-
- スペルミスの確認
|
150
|
-
- 参考サイトでの確認
|
151
|
-
|
152
|
-
自分のとしては def initialize_state の関数が間違っており,初期感染者の決定ができていないのでは,と考えています.
|
153
146
|
### 補足情報(FW/ツールのバージョンなど)
|
154
|
-
|
155
|
-
https://qiita.com/Keyskey/items/8e0d7a67b222a7866154
|
156
|
-
上記のウェブサイトを参考にしました.
|
157
|
-
ゲーム理論を排除してSIRモデルとしての完成を目指しています.
|
158
|
-
|
159
147
|
Python, Jupyter Notebook
|
2
タイトルの変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
SIRモデル
|
1
|
+
SIRモデル グラフへのプロットが上手くいかず困っています
|
body
CHANGED
@@ -28,7 +28,6 @@
|
|
28
28
|
|
29
29
|
class Agent:
|
30
30
|
def __init__(self):
|
31
|
-
state = ['S', 'I', 'R']
|
32
31
|
self.state = 'S'
|
33
32
|
self.neighbors_id = list()
|
34
33
|
|
1
自分でデバッグした,結果エラーのありそうな範囲を絞りこめたので更新しました.
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
SIRモデル
|
1
|
+
SIRモデル 各状態のリストの作成が上手くいかず困っています
|
body
CHANGED
@@ -5,29 +5,34 @@
|
|
5
5
|
|
6
6
|
### 実現したいこと
|
7
7
|
|
8
|
+
- 初期感染者の決定
|
9
|
+
- 各状態のリストの作成
|
8
|
-
-
|
10
|
+
- グラフへの正しいプロット
|
9
11
|
- 最終的にはコードの完成を目指しています
|
10
12
|
|
11
13
|
### 発生している問題・エラーメッセージ
|
12
14
|
|
13
|
-

|
14
16
|
|
17
|
+
空のグラフがプロットされてしまいます
|
15
|
-
|
18
|
+
状態のリストが作成されていないのでプロットされないと思っています
|
16
19
|
|
17
|
-
### 該当のソースコード
|
20
|
+
### 該当のソースコード
|
18
21
|
|
19
|
-
```
|
22
|
+
```python
|
20
23
|
import networkx as nx
|
24
|
+
import random as rnd
|
25
|
+
import numpy as np
|
26
|
+
import matplotlib.pyplot as plt
|
21
27
|
|
28
|
+
|
22
29
|
class Agent:
|
23
|
-
"""
|
24
|
-
state = ['S', 'I', 'R']
|
25
|
-
"""
|
26
30
|
def __init__(self):
|
31
|
+
state = ['S', 'I', 'R']
|
27
32
|
self.state = 'S'
|
28
33
|
self.neighbors_id = list()
|
29
34
|
|
30
|
-
|
35
|
+
|
31
36
|
def generate_agents(num_agent, average_degree):
|
32
37
|
"""
|
33
38
|
ネットワークの作成とエージェントのリスト作成
|
@@ -38,56 +43,52 @@
|
|
38
43
|
for agent_id, agent in enumerate(agents):
|
39
44
|
agent.nebghbors_id = list(network[agent_id])
|
40
45
|
return agents
|
41
|
-
|
42
|
-
|
46
|
+
|
47
|
+
|
43
|
-
def
|
48
|
+
def count_state_num(agents):
|
44
49
|
"""
|
45
|
-
|
50
|
+
各状態のエージェントの数
|
46
51
|
"""
|
47
|
-
|
52
|
+
num_s = len([agent for agent in agents if agent.state == 'S'])
|
48
|
-
fi = len([agent for agent in agents if agent.state == 'I'])/len(agents)
|
49
|
-
fr = 1 - fs -fi
|
50
|
-
return fs, fi, fr
|
51
|
-
|
52
|
-
|
53
|
-
def count_num_i(agents):
|
54
|
-
"""
|
55
|
-
感染状態のエージェントの数
|
56
|
-
"""
|
57
53
|
num_i = len([agent for agent in agents if agent.state == 'I'])
|
58
|
-
|
54
|
+
num_r = len([agent for agent in agents if agent.state == 'R'])
|
59
|
-
```
|
60
55
|
|
61
|
-
|
56
|
+
return num_s, num_i, num_r
|
62
57
|
|
63
|
-
```epidemics.py
|
64
|
-
import random as rnd
|
65
|
-
import society
|
66
58
|
|
59
|
+
"""
|
60
|
+
1. 自分はまだ健康(S状態) → 隣人の中に感染者(I状態)がいたら、感染者数に比例する確率で自分も感染してI状態に遷移。
|
61
|
+
感染者一人との接触による感染確率はβという値で設定。
|
62
|
+
2. 自分は既に感染していて回復待ち → 回復確率(gamma)に応じて病気から回復(R状態へ)
|
63
|
+
3. 自分は既に病気にかかった上で免疫を獲得している(R状態) → 何も起こらない。
|
64
|
+
"""
|
65
|
+
|
66
|
+
|
67
|
-
def initialize_state(agents, num_initial_infected_agents
|
67
|
+
def initialize_state(agents, num_initial_infected_agents):
|
68
68
|
"""
|
69
69
|
初期保有者をランダムに決定
|
70
70
|
"""
|
71
|
+
possibility_id = [agent_id for agent_id, agent in enumerate(agents) if agent.state == 'S']
|
71
|
-
initial_infected_agent_id = rnd.sample(k =
|
72
|
+
initial_infected_agent_id = rnd.sample(possibility_id, k = num_initial_infected_agents)
|
72
73
|
|
73
|
-
for i, agent in
|
74
|
+
for i, agent in enumerate(agents):
|
74
75
|
if i in initial_infected_agent_id:
|
75
76
|
agent.state = 'I'
|
76
77
|
else:
|
77
78
|
agent.state = 'S'
|
78
|
-
|
79
79
|
|
80
|
-
def disease_spreading(agents, beta, gammma):
|
80
|
+
def disease_spreading(agents, beta, gammma, max_iter):
|
81
81
|
"""
|
82
82
|
SIRダイナミクスの計算("I"状態のエージェントがいなくなるまで)
|
83
83
|
"""
|
84
|
-
for t in range(1,
|
84
|
+
for t in range(1, max_iter):
|
85
85
|
state_changeable_agents = [agent for agent in agents if agent.state in ['S', 'I']]
|
86
86
|
next_states = ['S' for i in range(len(state_changeable_agents))]
|
87
87
|
|
88
88
|
for i, agent in enumerate(state_changeable_agents):
|
89
89
|
if agent.state == 'S':
|
90
|
+
num_infected_neighbors = len([agents[agent_id]
|
90
|
-
|
91
|
+
for agent_id in agent.neighbors_id if agents[agent_id].state == 'I'])
|
91
92
|
if rnd.random() <= beta*num_infected_neighbors:
|
92
93
|
next_states[i] = 'I'
|
93
94
|
else:
|
@@ -103,52 +104,40 @@
|
|
103
104
|
for agent, next_state in zip(state_changeable_agents, next_states):
|
104
105
|
agent.state = next_state
|
105
106
|
|
106
|
-
|
107
|
+
num_s, num_i, num_r = count_state_num(agents)
|
107
|
-
num_i = society.count_num_i(state_changeable_agents)
|
108
|
-
print(f'time:{time}, Fs:{fs:2f}, Fi:{fi:2f}, Fr:{fr:.2f}')
|
109
108
|
|
110
109
|
if num_i == 0:
|
111
110
|
print('spreading finished')
|
112
111
|
break
|
112
|
+
|
113
|
+
|
114
|
+
plt.plot(max_iter,num_s, color=(0.2,0.2,0.2), linewidth=1.0, label='Susceptible')
|
113
|
-
|
115
|
+
plt.plot(max_iter,num_i, color=(1.0,0,0.0), linewidth=1.0, label='Infective')
|
114
|
-
|
116
|
+
plt.plot(max_iter,num_r, color=(0.0,1.0,0.0), linewidth=1.0, label='Removal')
|
115
117
|
|
118
|
+
plt.xlim(0,100)
|
119
|
+
plt.legend()
|
120
|
+
plt.xlabel('t')
|
121
|
+
plt.ylabel('count')
|
122
|
+
plt.show()
|
123
|
+
return num_s, num_i, num_r
|
116
124
|
|
117
|
-
### 該当のソースコード③
|
118
125
|
|
119
|
-
```main.py
|
120
|
-
import numpy as np
|
121
|
-
import pandas as pd
|
122
|
-
import random as rnd
|
123
|
-
import society
|
124
|
-
import epidemics
|
125
|
-
|
126
126
|
def main():
|
127
|
-
|
128
|
-
|
127
|
+
"""
|
128
|
+
メイン処理
|
129
|
+
"""
|
129
|
-
num_agent = 81306
|
130
|
+
num_agent = 81306 # エージェントの総数
|
130
|
-
average_degree = 43
|
131
|
+
average_degree = 43 # 平均次数
|
132
|
+
max_iter = 100 # 時間の上限
|
131
133
|
beta = 0.05 # 感染率
|
132
134
|
gamma = 0.6 # 回復率
|
133
|
-
max_season = 100
|
134
|
-
num_initial_infected_agents = 1
|
135
|
+
num_initial_infected_agents = 1 # 初期感染者数
|
136
|
+
agents = generate_agents(num_agent, average_degree)
|
135
137
|
|
136
|
-
|
138
|
+
initialize_state(agents, num_initial_infected_agents)
|
137
|
-
result = pd.DataFrame({'FES':[]})
|
138
|
-
fes_hist = []
|
139
|
-
|
140
|
-
for season in range(1, max_season+1):
|
141
|
-
|
139
|
+
num_s, num_i, num_r = disease_spreading(agents, beta, gamma, max_iter)
|
142
|
-
fes_hist.append(fr)
|
143
|
-
fes_eq = fr
|
144
|
-
|
145
|
-
new_result = pd.DataFrame([fes_eq], columns = ['FES'])
|
146
|
-
result = result.append(new_result)
|
147
|
-
print(f'Season finished with FES: {fes_eq:.2f}')
|
148
|
-
|
149
|
-
result.to_csv(f'result.csv')
|
150
140
|
|
151
|
-
|
152
141
|
|
153
142
|
if __name__=='__main__':
|
154
143
|
main()
|
@@ -157,8 +146,11 @@
|
|
157
146
|
|
158
147
|
### 試したこと
|
159
148
|
|
149
|
+
- printを挟みデバッグ
|
150
|
+
- スペルミスの確認
|
160
|
-
|
151
|
+
- 参考サイトでの確認
|
161
152
|
|
153
|
+
自分のとしては def initialize_state の関数が間違っており,初期感染者の決定ができていないのでは,と考えています.
|
162
154
|
### 補足情報(FW/ツールのバージョンなど)
|
163
155
|
|
164
156
|
https://qiita.com/Keyskey/items/8e0d7a67b222a7866154
|