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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +9 -0
- data/lib/Algorithmically.rb +2 -1
- data/lib/Algorithmically/Stochastic/hill_climbing.rb +42 -0
- data/lib/Algorithmically/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a20a235b28515eea9f3bc92defec55d22d6729d
|
4
|
+
data.tar.gz: 5d21f847f53c63d35c3418e2b0f66c84486494b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 875e6766cd3c27056098131cd6ddd7bf997a36df96ef8cbb6c5ce7bcb1f4327cfe3c91441d0e0f9cdfda4b6a4b88c17c9b1a45e063eeefc5589018ae446b93f1
|
7
|
+
data.tar.gz: 93adaaae0738b14f82936259b788c3b89d95875bd6745704f35ca640957f95da2919991c7e3c9bdb2e4356d9f123a70426b12b16da25f52264c1c0e24055a7f9
|
data/Gemfile.lock
CHANGED
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
|
|
data/lib/Algorithmically.rb
CHANGED
@@ -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
|
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.
|
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
|