rgraphum 0.0.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rgraphum.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ice
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # Rgraphum
2
+
3
+ rgraphum is the open source for graph and complicated network analysis written by ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rgraphum'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rgraphum
18
+
19
+ ## Usage
20
+
21
+ ![rgraphum-struct](graph_struct.jpg)
22
+
23
+ rgraphum is framework and a console application for analysis of graph and complex network.
24
+ It is made of ruby, you can accept the code of ruby.
25
+ Now, let’s see while using the console application an overview.
26
+
27
+ start console application
28
+
29
+ $ rgraphum_console
30
+
31
+ #### graph structure of rgraphum (new, added, traverse)
32
+
33
+ By creating an instance of the Graph class, rgraphum will have the entity graph.
34
+ graph = Graph.new
35
+ Instance of Graph will have an instance of Vertices and an instance of the Edges, such as Array.
36
+ graph.vertices
37
+ graph.edges
38
+ (An array of empty initial both)
39
+
40
+ When adding vertex or edge, it is operation on vertices, edges, and vertex.edges.
41
+ vertex or edge is specified in an instance such as the Hash.
42
+
43
+ v_a = graph.vertices.build(label:"A")
44
+ v_b = graph.vertices.build(label:"B")
45
+ graph.edges.build(source:v_a,target:v_b)
46
+ v_a.edges.build(source:v_b,target:v_a)
47
+
48
+ vertex will have edges being like a Array.
49
+ edge in the edges refers to the object.
50
+ This is the same as the one in the edges of the graph.
51
+
52
+ v_a.edges
53
+ e_a_b = v_a.edges[0]
54
+ graph.edges
55
+ graph.edges[0].object_id
56
+ e_a_b.object_id
57
+
58
+ edge has a target and source, these are objects of vertex.
59
+ This is the same as the one in the graph.vertices.
60
+
61
+ s_v= e_a_b.source
62
+ t_v = e_a_b.target
63
+ s_v.object_id
64
+ t_v.object_id
65
+ graph.vertices[0].object_id
66
+ graph.vertices[1].object_id
67
+
68
+ #### search
69
+
70
+ search is done in graph.vertices, graph.edges, and vertex.edges.
71
+
72
+ v_a = graph.vertices.where(label:"A").first
73
+ v_a = graph.vertices.where(label:"A")[0]
74
+ e_a_b = graph.edges.where(source:v_a).first
75
+ e_a_b = v_a.edges.where(source:v_a).first
76
+
77
+ #### delete
78
+
79
+ Delete as well, do in graph.vertices, graph.edges, and vertex.edges.
80
+
81
+ graph.edges
82
+ v_a.edges
83
+ e_b_a = v_a.edges[1]
84
+ v_a.delete(e_b_a)
85
+ v_a.edges
86
+
87
+ graph.edges
88
+ v_a.edges
89
+ e_a_b = v_a.edges[0]
90
+ graph.edges.delete(e_a_b)
91
+ v_a.edges
92
+
93
+ graph.vertices
94
+ graph.vertices.delete(v_a)
95
+ graph.vertices
96
+
97
+ #### sample
98
+
99
+ As a sample, The graph above, on the console, was made the following script.
100
+
101
+
102
+ graph = Graph.new
103
+ v_g = graph.vertices.build(label:"Graph")
104
+ v_vs = graph.vertices.build(label:"Vertices")
105
+ v_es = graph.vertices.build(label:"Edges")
106
+ graph.edges.build(source:v_g,target:v_vs)
107
+ graph.edges.build(source:v_g,target:v_es)
108
+
109
+ v_v = graph.vertices.build(label:"Vertex")
110
+ v_e = graph.vertices.build(label:"Edge")
111
+ graph.edges.build(source:v_vs,target:v_v)
112
+ graph.edges.build(source:v_es,target:v_e)
113
+
114
+ v_v_es = graph.vertices.build(label:"Vertex.edges")
115
+ graph.edges.build(source:v_v,target:v_v_es)
116
+ graph.edges.build(source:v_v_es,target:v_e)
117
+
118
+ v_e_s = graph.vertices.build(label:"Edge.source")
119
+ v_e_t = graph.vertices.build(label:"Edge.target")
120
+ graph.edges.build(source:v_e,target:v_e_s)
121
+ graph.edges.build(source:v_e,target:v_e_t)
122
+ graph.edges.build(source:v_e_s,target:v_v)
123
+ graph.edges.build(source:v_e_t,target:v_v)
124
+
125
+ Parsers::GraphvizParser.export(graph,"graph_struct","jpg")
126
+
127
+ ## Contributing
128
+
129
+ 1. Fork it
130
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
131
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
132
+ 4. Push to the branch (`git push origin my-new-feature`)
133
+ 5. Create new Pull Request
134
+
135
+
136
+
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'rake/testtask'
5
+
6
+ # require 'pry'
7
+ # require 'pry-nav'
8
+ # require 'pry-exception_explorer'
9
+ # require 'pry-stack_explorer'
10
+ # require 'pry-coolline'
11
+
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.pattern = "test/**/*_test.rb"
14
+ t.libs << "lib"
15
+ t.libs << "test"
16
+ end
data/bin/.irbrc ADDED
@@ -0,0 +1,41 @@
1
+ # -*- mode: ruby coding: utf-8 -*-
2
+
3
+ IRB.conf[:IRB_NAME] = "rgraphum"
4
+
5
+ # :DEFAULT => {
6
+ # :PROMPT_I => "%N(%m):%03n:%i> ",
7
+ # :PROMPT_N => "%N(%m):%03n:%i> ",
8
+ # :PROMPT_S => "%N(%m):%03n:%i%l ",
9
+ # :PROMPT_C => "%N(%m):%03n:%i* ",
10
+ # :RETURN => "=> %s\n"
11
+ # },
12
+ IRB.conf[:PROMPT][:RGRAPHUM] = {
13
+ :PROMPT_I => "%N(%m):%03n:%i> ",
14
+ :PROMPT_N => "%N(%m):%03n:%i> ",
15
+ :PROMPT_S => "%N(%m):%03n:%i%l ",
16
+ :PROMPT_C => "%N(%m):%03n:%i* ",
17
+ :RETURN => "=> %s\n"
18
+ }
19
+ # IRB.conf[:PROMPT_MODE] = :RGRAPHUM
20
+ IRB.conf[:AUTO_INDENT] = true
21
+
22
+ puts "Welcome to Rgraphum!"
23
+ puts
24
+ puts "This is an example for Rgraphum"
25
+ puts
26
+ puts "> graph = Graph.new"
27
+ puts ">"
28
+ puts "> vertex1 = graph.vertices.build(label: \"Vertex 1\")"
29
+ puts "> vertex2 = graph.vertices.build(label: \"Vertex 2\")"
30
+ puts "> vertex3 = graph.vertices.build(label: \"Vertex 3\")"
31
+ puts "> "
32
+ puts "> edge1 = graph.edges.build(source: vertex1, target: vertex2, weight: 1)"
33
+ puts "> edge2 = graph.edges.build(source: vertex2, target: vertex3, weight: 1)"
34
+ puts "> edge3 = graph.edges.build(source: vertex3, target: vertex1, weight: 1)"
35
+ puts "> "
36
+ puts "> open('sample.gexf', 'w') do |f|"
37
+ puts "> f.puts graph.to_gephi"
38
+ puts "> end"
39
+ puts
40
+ puts "Enter 'quit' to exit."
41
+ puts
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby coding: utf-8 -*-
3
+
4
+ require 'optparse'
5
+ require 'fileutils'
6
+ require 'irb'
7
+ require 'irb/completion'
8
+
9
+ begin
10
+ require 'rgraphum'
11
+ rescue LoadError
12
+ require_relative "../lib/rgraphum"
13
+ end
14
+
15
+ include Rgraphum
16
+
17
+ options = {}
18
+
19
+ ARGV.clone.options do |opts|
20
+ script_name = File.basename($0)
21
+ opts.banner = "Usage: rgraphum_console [options]"
22
+
23
+ opts.separator ""
24
+
25
+ opts.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = true }
26
+
27
+ opts.separator ""
28
+
29
+ opts.on("-h", "--help", "Show this help message.") { $stdout.puts opts; exit }
30
+
31
+ opts.separator ""
32
+ opts.separator "> graph = Graph.new"
33
+ opts.separator ">"
34
+ opts.separator "> vertex1 = graph.vertices.build(label: \"Vertex 1\")"
35
+ opts.separator "> vertex2 = graph.vertices.build(label: \"Vertex 2\")"
36
+ opts.separator "> vertex3 = graph.vertices.build(label: \"Vertex 3\")"
37
+ opts.separator "> "
38
+ opts.separator "> edge1 = graph.edges.build(source: vertex1, target: vertex2, weight: 1)"
39
+ opts.separator "> edge2 = graph.edges.build(source: vertex2, target: vertex3, weight: 1)"
40
+ opts.separator "> edge3 = graph.edges.build(source: vertex3, target: vertex1, weight: 1)"
41
+ opts.separator "> "
42
+ opts.separator "> open('sample.gexf', 'w') do |f|"
43
+ opts.separator "> f.puts graph.to_gephi"
44
+ opts.separator "> end"
45
+
46
+ opts.parse!
47
+ end
48
+
49
+ if options[:debugger]
50
+ begin
51
+ require 'ruby-debug'
52
+ puts "=> Debugger enabled"
53
+ rescue Exception
54
+ puts "You need to install ruby-debug to run the console in debugging mode. With gems, use 'gem install ruby-debug'"
55
+ exit
56
+ end
57
+ end
58
+
59
+ ENV['IRBRC'] = File.join(File.dirname(__FILE__), '.irbrc')
60
+
61
+ IRB.start
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby coding: utf-8 -*-
3
+
4
+ require 'optparse'
5
+ begin
6
+ require 'rgraphum'
7
+ rescue LoadError
8
+ require_relative "../lib/rgraphum"
9
+ end
10
+ include Rgraphum
11
+
12
+ code_or_file = nil
13
+
14
+ if ARGV.first.nil?
15
+ ARGV.push "-h"
16
+ end
17
+
18
+ ARGV.clone.options do |opts|
19
+ script_name = File.basename($0)
20
+ opts.banner = "Usage: rgraphum_runner [options] ('Some.ruby(code)' or a filename)"
21
+
22
+ opts.separator ""
23
+
24
+ opts.on("-h", "--help", "Show this help message.") { $stdout.puts opts; exit }
25
+
26
+ opts.separator ""
27
+ opts.separator "$ cat sample.rb"
28
+ opts.separator "graph = Rgraphum::Graph.new"
29
+ opts.separator ""
30
+ opts.separator "vertex1 = graph.vertices.build(label: \"Vertex 1\")"
31
+ opts.separator "vertex2 = graph.vertices.build(label: \"Vertex 2\")"
32
+ opts.separator "vertex3 = graph.vertices.build(label: \"Vertex 3\")"
33
+ opts.separator ""
34
+ opts.separator "edge1 = graph.edges.build(source: vertex1, target: vertex2, weight: 1)"
35
+ opts.separator "edge2 = graph.edges.build(source: vertex2, target: vertex3, weight: 1)"
36
+ opts.separator "edge3 = graph.edges.build(source: vertex3, target: vertex1, weight: 1)"
37
+ opts.separator ""
38
+ opts.separator "open('sample.gexf', 'w') do |f|"
39
+ opts.separator " f.puts graph.to_gephi"
40
+ opts.separator "end"
41
+ opts.separator ""
42
+ opts.separator "$ rgraphum_runner example.rb"
43
+
44
+ opts.order! { |o| code_or_file ||= o } rescue retry
45
+ end
46
+
47
+ ARGV.delete(code_or_file)
48
+
49
+ if code_or_file.nil?
50
+ $stderr.puts "Run '#{$0} -h' for help."
51
+ exit 1
52
+ elsif File.exist?(code_or_file)
53
+ $0 = code_or_file
54
+ eval(File.read(code_or_file), nil, code_or_file)
55
+ else
56
+ eval(code_or_file)
57
+ end
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require_relative '../../lib/rgraphum'
5
+ require 'csv'
6
+ require 'time'
7
+
8
+ include Rgraphum
9
+ p start_time = Time.now
10
+
11
+ graph = Graph.new
12
+ after_graph = graph.simulate("BAModel",:round =>100 )
13
+ after_graph.dump_to("./examples/ba_model/test.rgraphum")
14
+
15
+ p Time.now - start_time
16
+
17
+ file = File.open( File.dirname(__FILE__) + "/test.gexf", "w+" )
18
+ file.write( after_graph.to_gephi )
19
+
File without changes
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require_relative '../../lib/rgraphum'
5
+
6
+ # 1 - 2
7
+ # # \ /
8
+ # # 3
9
+ # # / \
10
+ # # 4 - 5
11
+ #
12
+
13
+ graph = Rgraphum::Graph.new
14
+ graph.vertices = [{id:1,label:"A"},
15
+ {id:2,label:"B"},
16
+ {id:3,label:"C"},
17
+ {id:4,label:"D"},
18
+ {id:5,label:"E"}]
19
+
20
+ graph.edges = [{id:0,source:1,target:2,weight:1},
21
+ {id:1,source:1,target:3,weight:1},
22
+ {id:2,source:2,target:3,weight:1},
23
+ {id:3,source:3,target:4,weight:1},
24
+ {id:4,source:3,target:5,weight:1},
25
+ {id:5,source:4,target:5,weight:1}] # !> assigned but unused variable - i
26
+
27
+ p graph.modularity # => 0.1111111111111111
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require_relative '../../lib/rgraphum'
5
+
6
+ graph = Rgraphum::Graph.new()
7
+ graph.vertices = [ {id:1,label:"1"},{id:2,label:"2"},{id:3,label:"3"} ]
8
+ graph.edges = [ {id:1,source:1,target:2,weight:1},{id:2,source:2,target:3,weight:1},{id:3,source:3,target:1,weight:1}]
9
+ json = graph.to_miserables
10
+ file = File.open( "public/miserables.js", "w+" )
11
+ file.write(json)
12
+
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require_relative '../../lib/rgraphum'
5
+
6
+ include Rgraphum
7
+
8
+ graph = Rgraphum::Graph.new
9
+
10
+ # add vertices
11
+ graph.vertices << {id:1,name:"marko", age:29 }
12
+ graph.vertices << {id:2,name:"vadas", age:27 }
13
+ graph.vertices << {id:3,name:"lop", lang:"java"}
14
+ graph.vertices << {id:4,name:"josh", age:32 }
15
+ graph.vertices << {id:5,name:"ripple", lang:"java"}
16
+ graph.vertices << {id:6,name:"peter", age:35}
17
+
18
+ # add vertices
19
+ graph.edges << {id:7, label:"knows", weight:0.5,source:1,target:2}
20
+ graph.edges << {id:8, label:"knows", weight:1.0,source:1,target:4}
21
+ graph.edges << {id:9, label:"created",weight:0.4,source:1,target:3}
22
+ graph.edges << {id:10,label:"created",weight:1.0,source:4,target:5}
23
+ graph.edges << {id:11,label:"created",weight:0.4,source:4,target:3}
24
+
25
+ #graph.to_graphviz
26
+ p Rgraphum::Parsers::GraphvizParser.new.export(graph, File.dirname(__FILE__) + "/hoge","jpg")
27
+
28
+
@@ -0,0 +1,161 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require_relative '../../lib/rgraphum'
5
+ require 'optparse'
6
+
7
+ module RgraphumSisLifeGame
8
+ include Rgraphum
9
+
10
+ def self.run(argv)
11
+ options = build_options(argv)
12
+ periods = options.delete(:periods)
13
+ row_size = options.delete(:row_size)
14
+ col_size = options.delete(:col_size)
15
+ interval = options.delete(:interval)
16
+
17
+ sis = Simulator::SISModel.new(options)
18
+
19
+ puts "Row Size: #{row_size}"
20
+ puts "Col Size: #{col_size}"
21
+ puts "Infection rate: #{options[:infection_rate]}"
22
+ puts "Recovery rate: #{options[:recovery_rate]}"
23
+
24
+ sis.simulate(periods: periods) do |period, sis|
25
+ puts_sis_grid_graph sis, period, periods, row_size, col_size
26
+ sleep interval
27
+ reset_cursor row_size unless periods == period
28
+ end
29
+ end
30
+
31
+ def self.build_options(argv)
32
+ row_size, col_size = 9, 9
33
+ periods = 10
34
+ infection_rate = 0.2 # S -> I
35
+ recovery_rate = 0.3 # I -> S
36
+ t_per_period = 1
37
+ si_pattern = nil
38
+ interval = 1
39
+
40
+ argv.clone.options do |opts|
41
+ script_name = File.basename($0)
42
+ opts.banner = "Usage: #{script_name} [options]"
43
+
44
+ opts.separator ""
45
+
46
+ opts.on("--size=SIZExSIZE", 'Row x Col size ex) 9x9') { |v| row_size, col_size = v.split("x").map(&:to_i) }
47
+ opts.on("--periods=Periods", 'Periods', Integer) { |v| periods = v.to_i }
48
+ opts.on("--infection-rate=RATE", 'Infection rate', Float) { |v| infection_rate = v.to_f }
49
+ opts.on("--recovery-rate=RATE", 'Recovery rate', Float) { |v| recovery_rate = v.to_f }
50
+ opts.on("--si-pattern=PATTERN", 'Pattern string line like "SISIS" or "isis"') { |v| si_pattern = v }
51
+ opts.on("--interval=SECONDS", 'Animation interval in [S]: default 1.0', Float) { |v| interval = v.to_f }
52
+
53
+ opts.separator ""
54
+
55
+ opts.separator "ex)"
56
+ opts.separator " ./examples/sis_model/lifegame.rb --size=50x100 --periods=1000 --interval=0.2 --si-pattern=ssiiiss"
57
+ opts.separator ""
58
+
59
+ opts.on("-h", "--help", "Show this help message.") { $stdout.puts opts; exit }
60
+
61
+ opts.parse!
62
+ end
63
+
64
+ graph = build_grid_graph(row_size, col_size)
65
+
66
+ if si_pattern
67
+ si_pattern = si_pattern.downcase.split(//).map(&:to_sym)
68
+ k = 0
69
+ si_map = row_size.times.map do |i|
70
+ col_size.times.map do |j|
71
+ si = si_pattern[k]
72
+ k = (k + 1) % si_pattern.size
73
+ si
74
+ end
75
+ end
76
+ else
77
+ si_map = row_size.times.map do |i|
78
+ col_size.times.map do |j|
79
+ if i.even?
80
+ j.even? ? :s : :i
81
+ else
82
+ j.even? ? :i : :s
83
+ end
84
+ end
85
+ end
86
+ end
87
+ si_map.flatten!
88
+
89
+ {
90
+ graph: graph,
91
+ si_map: si_map,
92
+ infection_rate: infection_rate, # S -> I
93
+ recovery_rate: recovery_rate, # I -> S
94
+ t_per_period: t_per_period,
95
+
96
+ row_size: row_size,
97
+ col_size: col_size,
98
+ periods: periods,
99
+ interval: interval,
100
+ }
101
+ end
102
+
103
+ def self.build_grid_graph(row_size, col_size)
104
+ graph = Graph.new
105
+
106
+ row_size.times do |i|
107
+ col_size.times do |j|
108
+ graph.vertices.build(label: "#{i}-#{j}")
109
+ end
110
+ end
111
+
112
+ row_size.times do |i|
113
+ (1...col_size).each do |j|
114
+ source_vertex = graph.vertices[i * col_size + j - 1]
115
+ target_vertex = graph.vertices[i * col_size + j]
116
+ graph.edges.build(source: source_vertex, target: target_vertex)
117
+ end
118
+ end
119
+
120
+ (1...row_size).each do |i|
121
+ col_size.times do |j|
122
+ source_vertex = graph.vertices[(i - 1) * col_size + j]
123
+ target_vertex = graph.vertices[(i) * col_size + j]
124
+ graph.edges.build(source: source_vertex, target: target_vertex)
125
+ end
126
+ end
127
+
128
+ graph
129
+ end
130
+
131
+ def self.reset_cursor(row_size)
132
+ magic_str = "\r"
133
+ (row_size+2).times do
134
+ magic_str += "\e[1A"
135
+ end
136
+ print magic_str
137
+ end
138
+
139
+ def self.puts_sis_grid_graph(sis, period, periods, row_size, col_size)
140
+ si_map = sis.si_map
141
+
142
+ puts " Priod: %4d/%4d ---------------------- " % [period, periods]
143
+ row_size.times do |i|
144
+ line = " "
145
+ col_size.times do |j|
146
+ si = si_map[i * col_size + j]
147
+ case si
148
+ when :i
149
+ line += "\e[31mI"
150
+ when :s
151
+ line += "\e[32mS"
152
+ end
153
+ line += "\e[0m "
154
+ end
155
+ puts line
156
+ end
157
+ puts "----------------------------------------"
158
+ end
159
+ end
160
+
161
+ RgraphumSisLifeGame.run(ARGV)
data/graph_struct.jpg ADDED
Binary file
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # LinearRegression
4
+ # resolve with least squares method.
5
+ # ex. x = [1,2,3], y = [5,7,9]
6
+ # y = ax + b
7
+ # a = 2, b = 3
8
+ # @param [Array] x_array x paramaters
9
+ # @param [Array] y_array y paramaters
10
+ # @return [Array] a,b
11
+ class Rgraphum::Analyzer::LinearRegression
12
+ def analyze(x_array, y_array, degree=1, round=5)
13
+
14
+ n = x_array.size
15
+ x_sum = 0.0
16
+ y_sum = 0.0
17
+ xy_sum = 0.0
18
+ x2_sum = 0.0
19
+ [x_array,y_array].transpose.each do |x,y|
20
+ x_sum += x
21
+ y_sum += y
22
+ x2_sum += x**2
23
+ xy_sum += x*y
24
+ end
25
+
26
+ a = ( n * xy_sum - x_sum * y_sum) / ( n * x2_sum - x_sum**2 )
27
+ b = ( x2_sum * y_sum - xy_sum * x_sum ) / ( n * x2_sum - x_sum**2)
28
+
29
+ [a.round(round),b.round(round)]
30
+ end
31
+ end