rgraphum 0.0.1.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. data/.gitignore +26 -0
  2. data/GLOSSARIES.md +108 -0
  3. data/GREMLIN.md +1398 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +136 -0
  7. data/Rakefile +16 -0
  8. data/bin/.irbrc +41 -0
  9. data/bin/rgraphum_console +61 -0
  10. data/bin/rgraphum_runner +57 -0
  11. data/examples/ba_model/make.rb +19 -0
  12. data/examples/ba_model/make_dummy_twitter_rt_data.rb +0 -0
  13. data/examples/basic/check_modularity.rb +27 -0
  14. data/examples/basic/make_graph.rb +12 -0
  15. data/examples/parser/dot.rb +28 -0
  16. data/examples/sis_model/lifegame.rb +161 -0
  17. data/graph_struct.jpg +0 -0
  18. data/lib/rgraphum/analyzer/linear_regression.rb +31 -0
  19. data/lib/rgraphum/analyzer/meme_tracker.rb +296 -0
  20. data/lib/rgraphum/analyzer/twitter/rt_at_mark.rb +45 -0
  21. data/lib/rgraphum/analyzer.rb +8 -0
  22. data/lib/rgraphum/cluster.rb +67 -0
  23. data/lib/rgraphum/communities.rb +65 -0
  24. data/lib/rgraphum/community.rb +86 -0
  25. data/lib/rgraphum/cosine_similarity_matrix.rb +40 -0
  26. data/lib/rgraphum/edge.rb +194 -0
  27. data/lib/rgraphum/edges.rb +161 -0
  28. data/lib/rgraphum/ext/cosine_similarity_matrix.rb +79 -0
  29. data/lib/rgraphum/ext/linear_regression.rb +22 -0
  30. data/lib/rgraphum/ext/tf_idf.rb +52 -0
  31. data/lib/rgraphum/graph/gremlin.rb +193 -0
  32. data/lib/rgraphum/graph/math/clustering_coefficient.rb +53 -0
  33. data/lib/rgraphum/graph/math/community_detection.rb +141 -0
  34. data/lib/rgraphum/graph/math/degree_distribution.rb +50 -0
  35. data/lib/rgraphum/graph/math/dijkstra.rb +331 -0
  36. data/lib/rgraphum/graph/math.rb +45 -0
  37. data/lib/rgraphum/graph.rb +267 -0
  38. data/lib/rgraphum/importer.rb +97 -0
  39. data/lib/rgraphum/marshal.rb +26 -0
  40. data/lib/rgraphum/motifs.rb +8 -0
  41. data/lib/rgraphum/parsers/flare.rb +42 -0
  42. data/lib/rgraphum/parsers/gephi.rb +193 -0
  43. data/lib/rgraphum/parsers/graphviz.rb +78 -0
  44. data/lib/rgraphum/parsers/miserables.rb +54 -0
  45. data/lib/rgraphum/parsers.rb +32 -0
  46. data/lib/rgraphum/path.rb +37 -0
  47. data/lib/rgraphum/query.rb +130 -0
  48. data/lib/rgraphum/rgraphum_array.rb +159 -0
  49. data/lib/rgraphum/rgraphum_array_dividers.rb +43 -0
  50. data/lib/rgraphum/rgraphum_random.rb +5 -0
  51. data/lib/rgraphum/simulator/ba_model.rb +140 -0
  52. data/lib/rgraphum/simulator/sir_model.rb +178 -0
  53. data/lib/rgraphum/simulator/sis_model.rb +158 -0
  54. data/lib/rgraphum/simulator.rb +29 -0
  55. data/lib/rgraphum/statistic/power_law.rb +9 -0
  56. data/lib/rgraphum/t.rb +12 -0
  57. data/lib/rgraphum/tf_idf.rb +27 -0
  58. data/lib/rgraphum/version.rb +3 -0
  59. data/lib/rgraphum/vertex.rb +354 -0
  60. data/lib/rgraphum/vertices.rb +97 -0
  61. data/lib/rgraphum.rb +38 -0
  62. data/performance/add-vertices-edges.rb +20 -0
  63. data/performance/add-vertices.rb +12 -0
  64. data/performance/build-graph.rb +19 -0
  65. data/performance/delete-graph.rb +24 -0
  66. data/performance/delete-vertices.rb +25 -0
  67. data/performance/refer-graph.rb +23 -0
  68. data/rgraphum.gemspec +30 -0
  69. data/test/lib/rgraphum/analyzer/linear_regression_test.rb +20 -0
  70. data/test/lib/rgraphum/analyzer/meme_tracker_test.rb +383 -0
  71. data/test/lib/rgraphum/analyzer/twitter/rt_at_mark_test.rb +120 -0
  72. data/test/lib/rgraphum/array_test.rb +95 -0
  73. data/test/lib/rgraphum/bubble_test.rb +7 -0
  74. data/test/lib/rgraphum/communities_test.rb +53 -0
  75. data/test/lib/rgraphum/cosine_similarity_test.rb +18 -0
  76. data/test/lib/rgraphum/edge_test.rb +89 -0
  77. data/test/lib/rgraphum/edges_test.rb +178 -0
  78. data/test/lib/rgraphum/graph_builder_test.rb +64 -0
  79. data/test/lib/rgraphum/graph_dup_test.rb +199 -0
  80. data/test/lib/rgraphum/graph_plus_test.rb +80 -0
  81. data/test/lib/rgraphum/graph_test.rb +512 -0
  82. data/test/lib/rgraphum/gremlin_test.rb +145 -0
  83. data/test/lib/rgraphum/importers/idg_json_edges.json +20 -0
  84. data/test/lib/rgraphum/importers/idg_json_test.rb +207 -0
  85. data/test/lib/rgraphum/importers/idg_json_vertices.json +46 -0
  86. data/test/lib/rgraphum/math/average_distance_matrix_test.rb +142 -0
  87. data/test/lib/rgraphum/math/clustering_coefficient_test.rb +219 -0
  88. data/test/lib/rgraphum/math/community_test.rb +78 -0
  89. data/test/lib/rgraphum/math/degree_distribution_test.rb +40 -0
  90. data/test/lib/rgraphum/math/dijkstra_test.rb +146 -0
  91. data/test/lib/rgraphum/math/modularity_test.rb +154 -0
  92. data/test/lib/rgraphum/math/quick_average_distance_matrix_test.rb +84 -0
  93. data/test/lib/rgraphum/path_test.rb +44 -0
  94. data/test/lib/rgraphum/query/enumerable_test.rb +42 -0
  95. data/test/lib/rgraphum/query/where_operators_test.rb +75 -0
  96. data/test/lib/rgraphum/query/where_test.rb +59 -0
  97. data/test/lib/rgraphum/simulator/ba_model_test.rb +75 -0
  98. data/test/lib/rgraphum/simulator/sir_model_test.rb +513 -0
  99. data/test/lib/rgraphum/simulator/sis_model_test.rb +478 -0
  100. data/test/lib/rgraphum/simulator_test.rb +22 -0
  101. data/test/lib/rgraphum/tf_idf_test.rb +30 -0
  102. data/test/lib/rgraphum/vertex_test.rb +50 -0
  103. data/test/lib/rgraphum/vertices_test.rb +180 -0
  104. data/test/test_helper.rb +98 -0
  105. data/tmp/.gitkeep +0 -0
  106. metadata +254 -0
@@ -0,0 +1,75 @@
1
+ # coding: utf-8
2
+
3
+ require 'test_helper'
4
+ require 'rgraphum'
5
+
6
+ class BAModelTest < MiniTest::Test
7
+ def setup
8
+ make_graph
9
+ end
10
+
11
+ def test_simurate
12
+
13
+ end
14
+
15
+ def test_edges_per_min
16
+ # 99 * 99 * 20 - 20 = 196_000 sec
17
+ # 196_000 / 60 min
18
+ ba = Rgraphum::Simulator::BAModel.new
19
+ assert_equal 99 / ( 196_000 / 60.0 ) , ba.edges_per_min(@graph)
20
+ end
21
+
22
+ def test_vertices_per_min
23
+ # 99 * 99 * 20 = 196_020 sec
24
+ # 196_020 / 60 = 3_267 min
25
+ ba = Rgraphum::Simulator::BAModel.new
26
+ assert_equal 100.0 / 3_267 , ba.vertices_per_min(@graph)
27
+ end
28
+
29
+ def test_edges_size_array_per_interval
30
+ ba = Rgraphum::Simulator::BAModel.new
31
+
32
+ mini_graph = Rgraphum::Graph.new
33
+ base_time = ( Time.now.to_i / 60 ) * 60
34
+ mini_graph.vertices = [ {id: 1,start: Time.now},{id: 2,start: Time.now},{id: 3,start: Time.now}]
35
+ mini_graph.edges = [ {id: 1,start: base_time + 30, source: 1,target: 2 },
36
+ {id: 2,start: base_time + 90, source: 2,target: 3 },
37
+ {id: 3,start: base_time + 60 * 2, source: 3,target: 1 } ]
38
+
39
+ array = ba.edges_size_array_per_interval( mini_graph, 1 )
40
+ assert_equal [1,1,1], array
41
+
42
+ mini_graph.edges.delete(3)
43
+ array = ba.edges_size_array_per_interval( mini_graph, 1 )
44
+ assert_equal [1,1], array
45
+
46
+ array = ba.edges_size_array_per_interval( @graph, 1 )
47
+
48
+ assert_equal 3268, array.size
49
+ assert_equal 99, array.inject(:+)
50
+ end
51
+
52
+ private
53
+ def make_graph
54
+ today = Time.at( ( Time.now.to_i/ ( 3600 * 24 ) ) * 3600 * 24 - Time.now.utc_offset )
55
+
56
+ @graph = Rgraphum::Graph.new
57
+
58
+ vertices = 100.times.map do |t|
59
+ {id: t, label: t.to_s, start: today + 20 * t * t }
60
+ end
61
+
62
+ @graph.vertices = vertices
63
+
64
+ edges = @graph.vertices.map do |vertex|
65
+ {id: vertex.id,source: @graph.vertices[0], target: vertex, start: vertex.start, weight: 1}
66
+ end
67
+
68
+ @graph.edges = edges
69
+
70
+ @graph.edges.delete(0)
71
+
72
+ @graph
73
+ end
74
+
75
+ end
@@ -0,0 +1,513 @@
1
+ # coding: utf-8
2
+
3
+ require 'test_helper'
4
+ require 'rgraphum'
5
+
6
+ class SIRModelTest < MiniTest::Test
7
+ def setup
8
+ # A -- B -- C
9
+ # | | |
10
+ # D -- E -- F
11
+ # | | |
12
+ # G -- H -- I
13
+ v = {}
14
+ @graph = Rgraphum::Graph.new
15
+ v[:a] = @graph.vertices.build(label: "A")
16
+ v[:b] = @graph.vertices.build(label: "B")
17
+ v[:c] = @graph.vertices.build(label: "C")
18
+ v[:d] = @graph.vertices.build(label: "D")
19
+ v[:e] = @graph.vertices.build(label: "E")
20
+ v[:f] = @graph.vertices.build(label: "F")
21
+ v[:g] = @graph.vertices.build(label: "G")
22
+ v[:h] = @graph.vertices.build(label: "H")
23
+ v[:i] = @graph.vertices.build(label: "I")
24
+
25
+ @graph.edges.build(source: v[:a], target: v[:b])
26
+ @graph.edges.build(source: v[:b], target: v[:c])
27
+ @graph.edges.build(source: v[:a], target: v[:d])
28
+ @graph.edges.build(source: v[:b], target: v[:e])
29
+ @graph.edges.build(source: v[:c], target: v[:f])
30
+ @graph.edges.build(source: v[:d], target: v[:e])
31
+ @graph.edges.build(source: v[:e], target: v[:f])
32
+ @graph.edges.build(source: v[:d], target: v[:g])
33
+ @graph.edges.build(source: v[:e], target: v[:h])
34
+ @graph.edges.build(source: v[:f], target: v[:i])
35
+ @graph.edges.build(source: v[:g], target: v[:h])
36
+ @graph.edges.build(source: v[:h], target: v[:i])
37
+ end
38
+
39
+ def test_initial_sir_map
40
+ sir = Rgraphum::Simulator::SIRModel.new(graph: @graph)
41
+
42
+ assert_equal %w(A B C D E F G H I), sir.vertices.map(&:label)
43
+ assert_equal %w(s s s s s s s s s).map(&:to_sym), sir.sir_map
44
+ end
45
+
46
+ def test_set_sir_map
47
+ sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
48
+ sir = Rgraphum::Simulator::SIRModel.new(graph: @graph, sir_map: sir_map)
49
+
50
+ assert_equal %w(A B C D E F G H I), sir.vertices.map(&:label)
51
+ assert_equal %w(s i s i s i s i s).map(&:to_sym), sir.sir_map
52
+ end
53
+
54
+ def test_infection_rate
55
+ sir = Rgraphum::Simulator::SIRModel.new(graph: @graph, infection_rate: 0.5)
56
+ assert_equal 0.5, sir.infection_rate
57
+
58
+ assert_raises(ArgumentError) do
59
+ Rgraphum::Simulator::SIRModel.new(graph: @graph, infection_rate: -0.1)
60
+ end
61
+
62
+ assert_raises(ArgumentError) do
63
+ Rgraphum::Simulator::SIRModel.new(graph: @graph, infection_rate: 1.1)
64
+ end
65
+ end
66
+
67
+ def test_recovery_rate
68
+ sir = Rgraphum::Simulator::SIRModel.new(graph: @graph, recovery_rate: 0.5)
69
+ assert_equal 0.5, sir.recovery_rate
70
+
71
+ assert_raises(ArgumentError) do
72
+ Rgraphum::Simulator::SIRModel.new(graph: @graph, recovery_rate: -0.1)
73
+ end
74
+
75
+ assert_raises(ArgumentError) do
76
+ Rgraphum::Simulator::SIRModel.new(graph: @graph, recovery_rate: 1.1)
77
+ end
78
+ end
79
+
80
+ def test_zero_periods_simulate
81
+ initial_sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
82
+ sir = Rgraphum::Simulator::SIRModel.new(graph: @graph, sir_map: initial_sir_map)
83
+
84
+ sir.simulate periods: 0
85
+
86
+ expected_sir_map = initial_sir_map
87
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
88
+ end
89
+
90
+ def test_infected_1?
91
+ # A -- B -- C S -- I -- S
92
+ # | | | | | |
93
+ # D -- E -- F I -- S -- I
94
+ # | | | | | |
95
+ # G -- H -- I S -- I -- S
96
+ options = {
97
+ graph: @graph,
98
+ sir_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
99
+ infection_rate: 1.0,
100
+ recovery_rate: 0.0,
101
+ }
102
+ sir = Rgraphum::Simulator::SIRModel.new(options)
103
+
104
+ v1 = sir.vertices[0]
105
+ assert_equal false, sir.infected?(v1)
106
+ assert_equal false, sir.infected?(v1, 0)
107
+ assert_equal true, sir.infected?(v1, 1)
108
+
109
+ v2 = sir.vertices[1]
110
+ assert_equal true, sir.infected?(v2)
111
+ assert_equal true, sir.infected?(v2, 0)
112
+ assert_equal true, sir.infected?(v2, 1)
113
+ end
114
+
115
+ def test_infected_2?
116
+ # A -- B -- C S -- I -- S
117
+ # | | | | | |
118
+ # D -- E -- F I -- S -- I
119
+ # | | | | | |
120
+ # G -- H -- I S -- I -- S
121
+ options = {
122
+ graph: @graph,
123
+ sir_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
124
+ infection_rate: 0.1,
125
+ recovery_rate: 0.0,
126
+ t_per_period: 0.5,
127
+ }
128
+ sir = Rgraphum::Simulator::SIRModel.new(options)
129
+
130
+ v1 = sir.vertices[0]
131
+ assert_equal false, sir.infected?(v1, 0)
132
+ assert_equal false, sir.infected?(v1, 1)
133
+ assert_equal false, sir.infected?(v1, 2)
134
+ assert_equal false, sir.infected?(v1, 3)
135
+ assert_equal false, sir.infected?(v1, 9)
136
+ assert_equal true, sir.infected?(v1, 10)
137
+ end
138
+
139
+ def test_recovered?
140
+ # A -- B -- C I -- I -- I
141
+ # | | | | | |
142
+ # D -- E -- F I -- I -- I
143
+ # | | | | | |
144
+ # G -- H -- I I -- I -- I
145
+ options = {
146
+ graph: @graph,
147
+ sir_map: [:i, :i, :i, :i, :i, :i, :i, :i, :i],
148
+ infection_rate: 0.0,
149
+ recovery_rate: 1.0,
150
+ }
151
+ sir = Rgraphum::Simulator::SIRModel.new(options)
152
+
153
+ v1 = sir.vertices[0]
154
+ assert_equal false, sir.recovered?(v1)
155
+ assert_equal false, sir.recovered?(v1, 0)
156
+ assert_equal true, sir.recovered?(v1, 1)
157
+ end
158
+
159
+ # def test_susceptible?
160
+ # # A -- B -- C S -- I -- I
161
+ # # | | | | | |
162
+ # # D -- E -- F I -- I -- I
163
+ # # | | | | | |
164
+ # # G -- H -- I I -- I -- I
165
+ # options = {
166
+ # graph: @graph,
167
+ # sir_map: [:s, :i, :i, :i, :i, :i, :i, :i, :i],
168
+ # infection_rate: 1.0,
169
+ # recovery_rate: 0.0,
170
+ # }
171
+ # sir = Rgraphum::Simulator::SIRModel.new(options)
172
+
173
+ # v1 = sir.vertices[0]
174
+ # assert_equal true, sir.susceptible?(v1)
175
+ # assert_equal true, sir.susceptible?(v1, 0)
176
+ # assert_equal false, sir.susceptible?(v1, 1)
177
+
178
+ # v2 = sir.vertices[1]
179
+ # assert_equal false, sir.susceptible?(v2)
180
+ # assert_equal false, sir.susceptible?(v2, 0)
181
+ # assert_equal false, sir.susceptible?(v2, 1)
182
+ # end
183
+
184
+ def test_next_period_only_with_infection_rate
185
+ # A -- B -- C S -- I -- S
186
+ # | | | | | |
187
+ # D -- E -- F I -- S -- I
188
+ # | | | | | |
189
+ # G -- H -- I S -- I -- S
190
+ options = {
191
+ graph: @graph,
192
+ sir_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
193
+ infection_rate: 0.2,
194
+ recovery_rate: 0.0,
195
+ t_per_period: 1,
196
+ }
197
+ sir = Rgraphum::Simulator::SIRModel.new(options)
198
+
199
+ # initial state
200
+ # A -- B -- C S -- I -- S 0 I 0
201
+ # | | | | | |
202
+ # D -- E -- F I -- S -- I I 0 I
203
+ # | | | | | |
204
+ # G -- H -- I S -- I -- S 0 I 0
205
+ expected_sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
206
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
207
+
208
+ # 1st period
209
+ # A -- B -- C S -- I -- S 0.4 I 0.4
210
+ # | | | | | |
211
+ # D -- E -- F I -- S -- I I 0.8 I
212
+ # | | | | | |
213
+ # G -- H -- I S -- I -- S 0.4 I 0.4
214
+ sir.next_period
215
+ expected_sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
216
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
217
+
218
+ # 2nd period
219
+ # A -- B -- C S -- I -- S 0.8 I 0.8
220
+ # | | | | | |
221
+ # D -- E -- F I -- S -- I I I I
222
+ # | | | | | |
223
+ # G -- H -- I S -- I -- S 0.8 I 0.8
224
+ sir.next_period
225
+ expected_sir_map = [:s, :i, :s, :i, :i, :i, :s, :i, :s]
226
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should change"
227
+
228
+ # 3rd period
229
+ # A -- B -- C S -- I -- S I I I
230
+ # | | | | | |
231
+ # D -- E -- F I -- S -- I I I I
232
+ # | | | | | |
233
+ # G -- H -- I S -- I -- S I I I
234
+ sir.next_period
235
+ expected_sir_map = [:i, :i, :i, :i, :i, :i, :i, :i, :i]
236
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should only contain I"
237
+
238
+ # 4th period
239
+ # A -- B -- C S -- I -- S I I I
240
+ # | | | | | |
241
+ # D -- E -- F I -- S -- I I I I
242
+ # | | | | | |
243
+ # G -- H -- I S -- I -- S I I I
244
+ sir.next_period
245
+ expected_sir_map = [:i, :i, :i, :i, :i, :i, :i, :i, :i]
246
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should only contain I"
247
+ end
248
+
249
+ def test_next_period_only_with_recovery_rate
250
+ # A -- B -- C S -- I -- S
251
+ # | | | | | |
252
+ # D -- E -- F I -- S -- I
253
+ # | | | | | |
254
+ # G -- H -- I S -- I -- S
255
+ options = {
256
+ graph: @graph,
257
+ sir_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
258
+ infection_rate: 0,
259
+ recovery_rate: 0.3,
260
+ t_per_period: 1,
261
+ }
262
+ sir = Rgraphum::Simulator::SIRModel.new(options)
263
+
264
+ # initial state
265
+ # A -- B -- C S -- I -- S S 0 S
266
+ # | | | | | |
267
+ # D -- E -- F I -- S -- I 0 S 0
268
+ # | | | | | |
269
+ # G -- H -- I S -- I -- S S 0 S
270
+ expected_sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
271
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
272
+
273
+ # 1st period
274
+ # A -- B -- C S -- I -- S S 0.3 S
275
+ # | | | | | |
276
+ # D -- E -- F I -- S -- I 0.3 S 0.3
277
+ # | | | | | |
278
+ # G -- H -- I S -- I -- S S 0.3 S
279
+ sir.next_period
280
+ expected_sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
281
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
282
+
283
+ # 2nd period
284
+ # A -- B -- C S -- I -- S S 0.6 S
285
+ # | | | | | |
286
+ # D -- E -- F I -- S -- I 0.6 S 0.6
287
+ # | | | | | |
288
+ # G -- H -- I S -- I -- S S 0.6 S
289
+ sir.next_period
290
+ expected_sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
291
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
292
+
293
+ # 3rd period
294
+ # A -- B -- C S -- I -- S S 0.9 S
295
+ # | | | | | |
296
+ # D -- E -- F I -- S -- I 0.9 S 0.9
297
+ # | | | | | |
298
+ # G -- H -- I S -- I -- S S 0.9 S
299
+ sir.next_period
300
+ expected_sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
301
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
302
+
303
+ # 4th period
304
+ # A -- B -- C S -- I -- S S R S
305
+ # | | | | | |
306
+ # D -- E -- F I -- S -- I R S R
307
+ # | | | | | |
308
+ # G -- H -- I S -- I -- S S R S
309
+ sir.next_period
310
+ expected_sir_map = [:s, :r, :s, :r, :s, :r, :s, :r, :s]
311
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should only contain S"
312
+ end
313
+
314
+ def test_next_period_1
315
+ # A -- B -- C I -- I -- S
316
+ # | | | | | |
317
+ # D -- E -- F I -- S -- I
318
+ # | | | | | |
319
+ # G -- H -- I S -- I -- S
320
+ options = {
321
+ graph: @graph,
322
+ sir_map: [:i, :i, :s, :i, :s, :i, :s, :i, :s],
323
+ infection_rate: 0.2, # S -> I
324
+ recovery_rate: 0.3, # I -> S
325
+ t_per_period: 1,
326
+ }
327
+ sir = Rgraphum::Simulator::SIRModel.new(options)
328
+
329
+ # initial state
330
+ # A -- B -- C I -- I -- S I0 I0 S0
331
+ # | | | | | |
332
+ # D -- E -- F I -- S -- I I0 S0 I0
333
+ # | | | | | |
334
+ # G -- H -- I S -- I -- S S0 I0 S0
335
+ expected_sir_map = [:i, :i, :s, :i, :s, :i, :s, :i, :s]
336
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
337
+
338
+ # 1st period
339
+ # A -- B -- C I -- I -- S I0.3 I0.3 S0.4
340
+ # | | | | | |
341
+ # D -- E -- F I -- S -- I I0.3 S0.8 I0.3
342
+ # | | | | | |
343
+ # G -- H -- I S -- I -- S S0.4 I0.3 S0.4
344
+ sir.next_period
345
+ expected_sir_map = [:i, :i, :s, :i, :s, :i, :s, :i, :s]
346
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
347
+
348
+ # 2nd period
349
+ # A -- B -- C I -- I -- S I0.6 I0.6 S0.8
350
+ # | | | | | |
351
+ # D -- E -- F I -- I -- I I0.6 I0 I0.6
352
+ # | | | | | |
353
+ # G -- H -- I S -- I -- S S0.8 I0.6 S0.8
354
+ sir.next_period
355
+ expected_sir_map = [:i, :i, :s, :i, :i, :i, :s, :i, :s]
356
+ assert_equal expected_sir_map, sir.sir_map
357
+
358
+ # 3rd period
359
+ # A -- B -- C I -- I -- I I0.9 I0.9 I0
360
+ # | | | | | |
361
+ # D -- E -- F I -- I -- I I0.9 I0.3 I0.9
362
+ # | | | | | |
363
+ # G -- H -- I I -- I -- I I0 I0.9 I0
364
+ sir.next_period
365
+ expected_sir_map = [:i, :i, :i, :i, :i, :i, :i, :i, :i]
366
+ assert_equal expected_sir_map, sir.sir_map
367
+
368
+ # 4th period
369
+ # A -- B -- C S -- S -- I R R I0.3
370
+ # | | | | | |
371
+ # D -- E -- F S -- I -- S R I0.6 R
372
+ # | | | | | |
373
+ # G -- H -- I I -- S -- I I0.3 R I0.3
374
+ sir.next_period
375
+ expected_sir_map = [:r, :r, :i, :r, :i, :r, :i, :r, :i]
376
+ assert_equal expected_sir_map, sir.sir_map
377
+
378
+ # 5th period
379
+ # A -- B -- C S -- S -- S R R I0.6
380
+ # | | | | | |
381
+ # D -- E -- F S -- I -- S R I0.9 R
382
+ # | | | | | |
383
+ # G -- H -- I I -- S -- I I0.6 R I0.6
384
+ sir.next_period
385
+ expected_sir_map = [:r, :r, :i, :r, :i, :r, :i, :r, :i]
386
+ assert_equal expected_sir_map, sir.sir_map
387
+
388
+ # 6th period
389
+ # A -- B -- C S -- S -- I R R I0.9
390
+ # | | | | | |
391
+ # D -- E -- F S -- S -- I R R R
392
+ # | | | | | |
393
+ # G -- H -- I I -- I -- I I0.9 R I0.9
394
+ sir.next_period
395
+ expected_sir_map = [:r, :r, :i, :r, :r, :r, :i, :r, :i]
396
+ assert_equal expected_sir_map, sir.sir_map
397
+
398
+ # 7th period
399
+ # A -- B -- C S -- S -- I R R R
400
+ # | | | | | |
401
+ # D -- E -- F S -- S -- I R R R
402
+ # | | | | | |
403
+ # G -- H -- I I -- I -- I R R R
404
+ sir.next_period
405
+ expected_sir_map = [:r, :r, :r, :r, :r, :r, :r, :r, :r]
406
+ assert_equal expected_sir_map, sir.sir_map
407
+ end
408
+
409
+ def test_simulate_only_with_infection_rate
410
+ # A -- B -- C S -- I -- S
411
+ # | | | | | |
412
+ # D -- E -- F I -- S -- I
413
+ # | | | | | |
414
+ # G -- H -- I S -- I -- S
415
+ options = {
416
+ graph: @graph,
417
+ sir_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
418
+ infection_rate: 0.2,
419
+ recovery_rate: 0.0,
420
+ t_per_period: 1,
421
+ }
422
+ sir = Rgraphum::Simulator::SIRModel.new(options)
423
+
424
+ # initial state
425
+ # A -- B -- C S -- I -- S 0.4 I 0.4
426
+ # | | | | | |
427
+ # D -- E -- F I -- S -- I I 0.8 I
428
+ # | | | | | |
429
+ # G -- H -- I S -- I -- S 0.4 I 0.4
430
+ expected_sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
431
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
432
+
433
+ # 3rd period
434
+ # A -- B -- C S -- I -- S I I I
435
+ # | | | | | |
436
+ # D -- E -- F I -- S -- I I I I
437
+ # | | | | | |
438
+ # G -- H -- I S -- I -- S I I I
439
+ sir.simulate periods: 3
440
+ expected_sir_map = [:i, :i, :i, :i, :i, :i, :i, :i, :i]
441
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should only contain I"
442
+ end
443
+
444
+ def test_simulate_only_with_recovery_rate
445
+ # A -- B -- C S -- I -- S
446
+ # | | | | | |
447
+ # D -- E -- F I -- S -- I
448
+ # | | | | | |
449
+ # G -- H -- I S -- I -- S
450
+ options = {
451
+ graph: @graph,
452
+ sir_map: [:s, :i, :s, :i, :s, :i, :s, :i, :s],
453
+ infection_rate: 0,
454
+ recovery_rate: 0.3,
455
+ t_per_period: 1,
456
+ }
457
+ sir = Rgraphum::Simulator::SIRModel.new(options)
458
+
459
+ # initial state
460
+ # A -- B -- C S -- I -- S S I S
461
+ # | | | | | |
462
+ # D -- E -- F I -- S -- I I S I
463
+ # | | | | | |
464
+ # G -- H -- I S -- I -- S S I S
465
+ expected_sir_map = [:s, :i, :s, :i, :s, :i, :s, :i, :s]
466
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
467
+
468
+ # 4th period
469
+ # A -- B -- C S -- I -- S S R S
470
+ # | | | | | |
471
+ # D -- E -- F I -- S -- I R S R
472
+ # | | | | | |
473
+ # G -- H -- I S -- I -- S S R S
474
+ sir.simulate periods: 4
475
+ expected_sir_map = [:s, :r, :s, :r, :s, :r, :s, :r, :s]
476
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should only contain S"
477
+ end
478
+
479
+ def test_simulate
480
+ # A -- B -- C I -- I -- S
481
+ # | | | | | |
482
+ # D -- E -- F I -- S -- I
483
+ # | | | | | |
484
+ # G -- H -- I S -- I -- S
485
+ options = {
486
+ graph: @graph,
487
+ sir_map: [:i, :i, :s, :i, :s, :i, :s, :i, :s],
488
+ infection_rate: 0.2, # S -> I
489
+ recovery_rate: 0.3, # I -> S
490
+ t_per_period: 1,
491
+ }
492
+ sir = Rgraphum::Simulator::SIRModel.new(options)
493
+
494
+ # initial state
495
+ # A -- B -- C I -- I -- S I0 I0 S0
496
+ # | | | | | |
497
+ # D -- E -- F I -- S -- I I0 S0 I0
498
+ # | | | | | |
499
+ # G -- H -- I S -- I -- S S0 I0 S0
500
+ expected_sir_map = [:i, :i, :s, :i, :s, :i, :s, :i, :s]
501
+ assert_equal expected_sir_map, sir.sir_map, "sir_map should not change"
502
+
503
+ # 6th period
504
+ # A -- B -- C S -- S -- I R R I
505
+ # | | | | | |
506
+ # D -- E -- F S -- S -- I R R R
507
+ # | | | | | |
508
+ # G -- H -- I I -- I -- I I R I
509
+ sir.simulate periods: 6
510
+ expected_sir_map = [:r, :r, :i, :r, :r, :r, :i, :r, :i]
511
+ assert_equal expected_sir_map, sir.sir_map
512
+ end
513
+ end