rbrain 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rbrain.rb +108 -0
  3. metadata +58 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 94d436bf835724644de52a76b76bc8a313286cac3fb1c2ed8fbcc497736d0478
4
+ data.tar.gz: f84fc8e8a08f045cf7c6a74dee7ad44144cb48c574d412df47f3b8d18314c6c6
5
+ SHA512:
6
+ metadata.gz: f0f6f0d1d580c73e6256648eb5ae3e31192532531cfd571ad9d5db92929942058b753642a0d16092bd52d95b49539a63f65f57b3d7452ed772bbf9cb1bf7a5b9
7
+ data.tar.gz: 93c34094d6cf3a318199f384d6479cc66d05d0f40fab7506bd0dd351413938733130927d4d3a08fa2e158452f7cf547819a6df349dfece6a05c630f4a11246d6
@@ -0,0 +1,108 @@
1
+ class Rbrain
2
+
3
+ @threshold = 30
4
+ def self.init_neuron(id)
5
+ #neuron[id, links, weights, thisstate, nextstate, fired?]
6
+ neuron = [id, [], [], 0, 0, false]
7
+ return neuron
8
+ end
9
+
10
+ def self.init_brain(n_count)
11
+ neurons = []
12
+ for i in 0..(n_count-1) do
13
+ neurons << init_neuron(i)
14
+ end
15
+ return neurons
16
+ end
17
+
18
+ def self.make_connections(brain)
19
+ num_conn = 0
20
+ brain.each do |neuron|
21
+ 0..(rand(1..10)).times do
22
+ link = rand(1..brain.size-1)
23
+ weight = rand(1..5)
24
+ if not neuron[1].include?(link)
25
+ neuron[1] << link
26
+ neuron[2] << weight
27
+ num_conn += 1
28
+ end
29
+ end
30
+ end
31
+ return num_conn
32
+ end
33
+
34
+ def self.accum_rand_neuron(brain, wt)
35
+ n = rand(brain.size)
36
+ brain[n][4] += wt
37
+ return n
38
+ end
39
+
40
+ def self.fire_neuron(neuron, brain)
41
+ fired = false
42
+ neuron[1].each do |n_link|
43
+ brain[n_link][4] += neuron[2][n_link] if not neuron[2][n_link].nil?
44
+ neuron[3] = 0
45
+ neuron[4] = 0
46
+ fired = true
47
+ end
48
+ return fired
49
+ end
50
+
51
+ def self.accum_all_neurons(brain, wt)
52
+ neurons = brain
53
+ neurons.each do |n|
54
+ n[4] += wt
55
+ end
56
+ end
57
+ def self.update_neuron(n, brain)
58
+ fired = false
59
+ if n[3] >= @threshold
60
+ self.fire_neuron(n, brain)
61
+ fired = true
62
+ end
63
+ n[3] += n[4]
64
+ n[4] = 0
65
+ n[5] = true
66
+ return fired
67
+ end
68
+
69
+ def self.print_fired_neurons(neurons)
70
+ neurons.each do |n|
71
+ puts "Fired #{n[0]}"
72
+ end
73
+ end
74
+
75
+ def self.set_threshold(val)
76
+ @threshold = val
77
+ end
78
+
79
+ def self.get_threshold
80
+ @threshold
81
+ end
82
+
83
+ def self.csv_init_ids(file)
84
+ data ||= CSV.read(file)
85
+ @neuron_data = {}
86
+ data.each do |n|
87
+ k = n[0]
88
+ v = n[1].to_i
89
+ @neuron_data[k] = v
90
+ end
91
+ return @neuron_data
92
+ end
93
+
94
+ def self.csv_make_connections(brain, dat, file)
95
+ no_conn = 0
96
+ conn_file_data ||= CSV.read(file)
97
+ conn_file_data.each do |connection|
98
+ from_id = connection[0]
99
+ to_id = connection[1]
100
+ weight = connection[2]
101
+ brain[dat[from_id].to_i][1] << dat[to_id].to_i
102
+ brain[dat[from_id].to_i][2] << weight.to_i
103
+ no_conn += 1
104
+ end
105
+ return no_conn
106
+ end
107
+
108
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbrain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Auxilus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: csv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email: raghavsphadke@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/rbrain.rb
34
+ homepage: https://Auxilus.github.io
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.7.7
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Ruby implimentation of how brain works.
58
+ test_files: []