Algorithmically 0.1.1 → 0.1.2

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: c91ca3b58d7d092993dc20a3f8ecf2434f34fd45
4
- data.tar.gz: e3506c96aa0aae8e90cd47c463430bd0cbf2111a
3
+ metadata.gz: 7a20a235b28515eea9f3bc92defec55d22d6729d
4
+ data.tar.gz: 5d21f847f53c63d35c3418e2b0f66c84486494b1
5
5
  SHA512:
6
- metadata.gz: 3a9feda8f6f49348e02129966da3e153ba96134ea893415ceaa18623e8b84baf750fa26e81c9f1e6a23b2ac704f63a39a905f5772fb6ea66ccdd785ba28d1fa5
7
- data.tar.gz: f9ee9e53fe0449c5f32686fd751cc1f0ea47b84e82c96bdad03daf741497c47f02b3bf3d3c9d7eb3d21e592c9a09b2f91cf561909b91493da63dbcdcea407a46
6
+ metadata.gz: 875e6766cd3c27056098131cd6ddd7bf997a36df96ef8cbb6c5ce7bcb1f4327cfe3c91441d0e0f9cdfda4b6a4b88c17c9b1a45e063eeefc5589018ae446b93f1
7
+ data.tar.gz: 93adaaae0738b14f82936259b788c3b89d95875bd6745704f35ca640957f95da2919991c7e3c9bdb2e4356d9f123a70426b12b16da25f52264c1c0e24055a7f9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- Algorithmically (0.1.1)
4
+ Algorithmically (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -17,4 +17,13 @@ And then execute:
17
17
  Or install it yourself as:
18
18
 
19
19
  $ gem install Algorithmically
20
+
21
+ ## Usage
22
+
23
+ ### Stochastic Algorithms
24
+
25
+ Algorithmically::RandomSearch.new(2, 50)
26
+
27
+ Algorithmically::AdaptiveRandomSearch.new(1000, 2, 0.05, 1.3, 3.0, 10, 30)
28
+
20
29
 
@@ -1,6 +1,7 @@
1
1
  require 'Algorithmically/version'
2
2
  require 'Algorithmically/Stochastic/random_search'
3
- require 'Algorithmically/Stochastic/adaptive_random_search'
3
+ require 'Algorithmically/Stochastic/hill_climbing'
4
+
4
5
 
5
6
  module Algorithmically
6
7
  include Stochastic
@@ -0,0 +1,42 @@
1
+ module Stochastic
2
+
3
+ class HillClimbing
4
+
5
+ def initialize(max_iterations, num_bits)
6
+ best = search(max_iterations, num_bits)
7
+ puts "Done. Best Solution: c=#{best[:cost]}, v=#{best[:vector].join}"
8
+ end
9
+
10
+ def onemax(vector)
11
+ vector.inject(0.0) { |sum, v| sum + ((v=="1") ? 1 : 0) }
12
+ end
13
+
14
+ def random_bitstring(num_bits)
15
+ Array.new(num_bits) { |i| (rand<0.5) ? "1" : "0" }
16
+ end
17
+
18
+ def random_neighbor(bitstring)
19
+ mutant = Array.new(bitstring)
20
+ pos = rand(bitstring.size)
21
+ mutant[pos] = (mutant[pos]=='1') ? '0' : '1'
22
+ mutant
23
+ end
24
+
25
+ def search(max_iterations, num_bits)
26
+ candidate = {}
27
+ candidate[:vector] = random_bitstring(num_bits)
28
+ candidate[:cost] = onemax(candidate[:vector])
29
+ max_iterations.times do |iter|
30
+ neighbor = {}
31
+ neighbor[:vector] = random_neighbor(candidate[:vector])
32
+ neighbor[:cost] = onemax(neighbor[:vector])
33
+ candidate = neighbor if neighbor[:cost] >= candidate[:cost]
34
+ puts " > iteration #{(iter+1)}, best=#{candidate[:cost]}"
35
+ break if candidate[:cost] == num_bits
36
+ end
37
+ candidate
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -1,3 +1,3 @@
1
1
  module Algorithmically
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Algorithmically
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
  - popac
@@ -77,6 +77,7 @@ files:
77
77
  - bin/setup
78
78
  - lib/Algorithmically.rb
79
79
  - lib/Algorithmically/Stochastic/adaptive_random_search.rb
80
+ - lib/Algorithmically/Stochastic/hill_climbing.rb
80
81
  - lib/Algorithmically/Stochastic/random_search.rb
81
82
  - lib/Algorithmically/version.rb
82
83
  homepage: https://github.com/popac/Algorithmically