rbrain 0.1.1 → 0.1.2

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 +4 -4
  2. data/lib/rbrain.rb +85 -81
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 94d436bf835724644de52a76b76bc8a313286cac3fb1c2ed8fbcc497736d0478
4
- data.tar.gz: f84fc8e8a08f045cf7c6a74dee7ad44144cb48c574d412df47f3b8d18314c6c6
3
+ metadata.gz: 0e4a24336e395eb28572c7aae82fbc8e4dc90d2cd582e25cb5f0a70a2bedaba6
4
+ data.tar.gz: e3c8a27c2f788bb849abb78f7be8a60cb15630f0859caa3c3c2f9bc1e670bbb8
5
5
  SHA512:
6
- metadata.gz: f0f6f0d1d580c73e6256648eb5ae3e31192532531cfd571ad9d5db92929942058b753642a0d16092bd52d95b49539a63f65f57b3d7452ed772bbf9cb1bf7a5b9
7
- data.tar.gz: 93c34094d6cf3a318199f384d6479cc66d05d0f40fab7506bd0dd351413938733130927d4d3a08fa2e158452f7cf547819a6df349dfece6a05c630f4a11246d6
6
+ metadata.gz: 4ca160b64ba9fe15cf4faf38164a59a57b0774b28b8a5690705f486aa27d953610463d42718bc03056ebe7ed12467fc2d8b396cba0820e643165ef28c1fa9b91
7
+ data.tar.gz: 314e842c0ab79e74a715a741066537d3fa3fbab409c736c9e93eb70e1f49835e79bb205d3d64d4a4f08f6229b742e7f1644066750e937d8b7b95f81d4aa8e3a4
@@ -1,108 +1,112 @@
1
1
  class Rbrain
2
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
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
9
 
10
- def self.init_brain(n_count)
11
- neurons = []
12
- for i in 0..(n_count-1) do
13
- neurons << init_neuron(i)
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
14
16
  end
15
- return neurons
16
- end
17
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
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
28
29
  end
29
30
  end
31
+ return num_conn
30
32
  end
31
- return num_conn
32
- end
33
33
 
34
- def self.accum_rand_neuron(brain, wt)
35
- n = rand(brain.size)
36
- brain[n][4] += wt
37
- return n
38
- end
34
+ def self.accum_rand_neuron(brain, wt)
35
+ n = rand(brain.size)
36
+ brain[n][4] += wt
37
+ return n
38
+ end
39
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
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
47
49
  end
48
- return fired
49
- end
50
50
 
51
- def self.accum_all_neurons(brain, wt)
52
- neurons = brain
53
- neurons.each do |n|
54
- n[4] += wt
51
+ def self.accum_all_neurons(brain, wt)
52
+ neurons = brain
53
+ neurons.each do |n|
54
+ n[4] += wt
55
+ end
55
56
  end
56
- end
57
- def self.update_neuron(n, brain)
57
+
58
+ def self.update_neuron(neuron, brain)
58
59
  fired = false
59
- if n[3] >= @threshold
60
- self.fire_neuron(n, brain)
60
+ if neuron[3] >= @threshold
61
+ self.fire_neuron(neuron, brain)
61
62
  fired = true
62
63
  end
63
- n[3] += n[4]
64
- n[4] = 0
65
- n[5] = true
64
+ neuron[3] += neuron[4]
65
+ neuron[4] = 0
66
+ neuron[5] = true
66
67
  return fired
67
- end
68
+ end
69
+
68
70
 
69
- def self.print_fired_neurons(neurons)
70
- neurons.each do |n|
71
- puts "Fired #{n[0]}"
71
+ def self.print_fired_neurons(neurons)
72
+ neurons.each do |n|
73
+ puts "Fired #{n[0]}"
74
+ end
72
75
  end
73
- end
74
76
 
75
- def self.set_threshold(val)
76
- @threshold = val
77
- end
77
+ def self.set_threshold(value)
78
+ @threshold = value
79
+ end
78
80
 
79
- def self.get_threshold
80
- @threshold
81
- end
81
+ def self.get_threshold
82
+ @threshold
83
+ end
84
+
85
+ def self.csv_init_brain(file)
86
+ data ||= CSV.read(file)
87
+ neuron_data = {}
88
+ data.each do |n|
89
+ k = n[0]
90
+ v = n[1].to_i
91
+ neuron_data[k] = v
92
+ end
82
93
 
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
94
+ brain = self.init_brain(neuron_data.size - 1)
95
+ return neuron_data, brain
90
96
  end
91
- return @neuron_data
92
- end
93
97
 
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
98
+ def self.csv_make_connections(brain, data, file)
99
+ no_conn = 0
100
+ conn_file_data ||= CSV.read(file)
101
+ conn_file_data.each do |connection|
102
+ from_id = connection[0]
103
+ to_id = connection[1]
104
+ weight = connection[2]
105
+ brain[data[from_id].to_i][1] << data[to_id].to_i
106
+ brain[data[from_id].to_i][2] << weight.to_i
107
+ no_conn += 1
108
+ end
109
+ return no_conn
104
110
  end
105
- return no_conn
106
- end
107
111
 
108
112
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbrain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Auxilus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-12 00:00:00.000000000 Z
11
+ date: 2018-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: csv
@@ -31,7 +31,7 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
33
  - lib/rbrain.rb
34
- homepage: https://Auxilus.github.io
34
+ homepage: https://github.com/Auxilus/rbrain
35
35
  licenses:
36
36
  - MIT
37
37
  metadata: {}