Algorithmically 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/Algorithmically.rb +2 -0
- data/lib/Algorithmically/Neural/perceptron.rb +78 -0
- data/lib/Algorithmically/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 553511b544e3ba3256109470cec9aa74afacf883
|
4
|
+
data.tar.gz: 7c369687176d34c9105f30f068592b92ff8a8247
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69b85819ba8ba9e9f51c009263a2f66080977bf98bc71a7a4d92c98e54a654f1b0ead68adc6fc8e6955bc8312d4ed33f9fc8d37643fd5e8f947b69d8aafd1f79
|
7
|
+
data.tar.gz: eace3c0a9fd63ee2d635cff1a3dc018a4b4a9dcf214d9d743445ea8a61cdcb285ce4417f633606d750e321157fa1c7b95a516ec4529e79a7d81ce0f907dc64da
|
data/README.md
CHANGED
data/lib/Algorithmically.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'Algorithmically/version'
|
2
|
+
require 'Algorithmically/Neural/perceptron'
|
2
3
|
require 'Algorithmically/Stochastic/random_search'
|
3
4
|
require 'Algorithmically/Stochastic/hill_climbing'
|
4
5
|
require 'Algorithmically/Stochastic/guided_local_search'
|
5
6
|
|
6
7
|
|
7
8
|
module Algorithmically
|
9
|
+
include Neural
|
8
10
|
include Stochastic
|
9
11
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Neural
|
2
|
+
|
3
|
+
class Perceptron
|
4
|
+
|
5
|
+
def initialize(or_problem, inputs, iterations, learning_rate)
|
6
|
+
execute(or_problem, inputs, iterations, learning_rate)
|
7
|
+
end
|
8
|
+
|
9
|
+
def random_vector(minmax)
|
10
|
+
Array.new(minmax.size) do |i|
|
11
|
+
minmax[i][0] + ((minmax[i][1] - minmax[i][0]) * rand())
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize_weights(problem_size)
|
16
|
+
minmax = Array.new(problem_size + 1) { [-1.0, 1.0] }
|
17
|
+
random_vector(minmax)
|
18
|
+
end
|
19
|
+
|
20
|
+
def update_weights(num_inputs, weights, input, out_exp, out_act, l_rate)
|
21
|
+
num_inputs.times do |i|
|
22
|
+
weights[i] += l_rate * (out_exp - out_act) * input[i]
|
23
|
+
end
|
24
|
+
weights[num_inputs] += l_rate * (out_exp - out_act) * 1.0
|
25
|
+
end
|
26
|
+
|
27
|
+
def activate(weights, vector)
|
28
|
+
sum = weights[weights.size-1] * 1.0
|
29
|
+
vector.each_with_index do |input, i|
|
30
|
+
sum += weights[i] * input
|
31
|
+
end
|
32
|
+
sum
|
33
|
+
end
|
34
|
+
|
35
|
+
def transfer(activation)
|
36
|
+
(activation >= 0) ? 1.0 : 0.0
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_output(weights, vector)
|
40
|
+
activation = activate(weights, vector)
|
41
|
+
transfer(activation)
|
42
|
+
end
|
43
|
+
|
44
|
+
def train_weights(weights, domain, num_inputs, iterations, lrate)
|
45
|
+
iterations.times do |epoch|
|
46
|
+
error = 0.0
|
47
|
+
domain.each do |pattern|
|
48
|
+
input = Array.new(num_inputs) { |k| pattern[k].to_f }
|
49
|
+
output = get_output(weights, input)
|
50
|
+
expected = pattern.last.to_f
|
51
|
+
error += (output - expected).abs
|
52
|
+
update_weights(num_inputs, weights, input, expected, output, lrate)
|
53
|
+
end
|
54
|
+
puts "> epoch=#{epoch}, error=#{error}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_weights(weights, domain, num_inputs)
|
59
|
+
correct = 0
|
60
|
+
domain.each do |pattern|
|
61
|
+
input_vector = Array.new(num_inputs) { |k| pattern[k].to_f }
|
62
|
+
output = get_output(weights, input_vector)
|
63
|
+
correct += 1 if output.round == pattern.last
|
64
|
+
end
|
65
|
+
puts "Finished test with a score of #{correct}/#{domain.size}"
|
66
|
+
correct
|
67
|
+
end
|
68
|
+
|
69
|
+
def execute(domain, num_inputs, iterations, learning_rate)
|
70
|
+
weights = initialize_weights(num_inputs)
|
71
|
+
train_weights(weights, domain, num_inputs, iterations, learning_rate)
|
72
|
+
test_weights(weights, domain, num_inputs)
|
73
|
+
weights
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Algorithmically
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- popac
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- bin/console
|
77
77
|
- bin/setup
|
78
78
|
- lib/Algorithmically.rb
|
79
|
+
- lib/Algorithmically/Neural/perceptron.rb
|
79
80
|
- lib/Algorithmically/Stochastic/adaptive_random_search.rb
|
80
81
|
- lib/Algorithmically/Stochastic/guided_local_search.rb
|
81
82
|
- lib/Algorithmically/Stochastic/hill_climbing.rb
|
@@ -102,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
103
|
version: '0'
|
103
104
|
requirements: []
|
104
105
|
rubyforge_project:
|
105
|
-
rubygems_version: 2.
|
106
|
+
rubygems_version: 2.5.1
|
106
107
|
signing_key:
|
107
108
|
specification_version: 4
|
108
109
|
summary: Algorithmically
|