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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 923dab150a1c719d4578803afb1f2f00f0cefa9d
4
- data.tar.gz: df4628866699eab63f79a99aa315dba1cc58d466
3
+ metadata.gz: 553511b544e3ba3256109470cec9aa74afacf883
4
+ data.tar.gz: 7c369687176d34c9105f30f068592b92ff8a8247
5
5
  SHA512:
6
- metadata.gz: 5cac3441e02e4de16906501f54904bee60c51259ba2dcff2c9dbbe14e48f52e2160a42b043b96aff8a9db53820fa4e71937db65a9fd8d043eb434c8ff9895a8e
7
- data.tar.gz: 931ca10d22d3a4a4bbb6896a93337b0b9ac96d33ee4e3e38fbd825c360711733aebb82f2d52ac24aeb1cfec24b728ee2f0b3bb0df835852776ec5dd3ade3ddad
6
+ metadata.gz: 69b85819ba8ba9e9f51c009263a2f66080977bf98bc71a7a4d92c98e54a654f1b0ead68adc6fc8e6955bc8312d4ed33f9fc8d37643fd5e8f947b69d8aafd1f79
7
+ data.tar.gz: eace3c0a9fd63ee2d635cff1a3dc018a4b4a9dcf214d9d743445ea8a61cdcb285ce4417f633606d750e321157fa1c7b95a516ec4529e79a7d81ce0f907dc64da
data/README.md CHANGED
@@ -20,6 +20,10 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ ### Neural Algorithms
24
+
25
+ Algorithmically::Perceptron.new([[0,0,0], [0,1,1], [1,0,1], [1,1,1]], 2, 20, 0.1)
26
+
23
27
  ### Stochastic Algorithms
24
28
 
25
29
  Algorithmically::RandomSearch.new(2, 50)
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Algorithmically
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  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.3
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-25 00:00:00.000000000 Z
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.6.6
106
+ rubygems_version: 2.5.1
106
107
  signing_key:
107
108
  specification_version: 4
108
109
  summary: Algorithmically