Algorithmically 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/Gemfile.lock +2 -2
- data/README.md +14 -5
- data/lib/Algorithmically/Stochastic/iterated_local_search.rb +84 -0
- data/lib/Algorithmically/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 834e24f733f439348830da9a6eedcc2ace840fe4
|
4
|
+
data.tar.gz: 36df1221be7719139bdf5f39e0fa5cf799ada70d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81628e3e9a0881dcc61c025c0febfe61e0265e4a7841f878e5fe790f8e9c6214bebf8eb92e6aab04ca578e1755d0aecd28c1230e6aa1bb88533979089d635cfc
|
7
|
+
data.tar.gz: b0cc0c5471c98c879ad3b08ce912cb0b483ac581b5f00534ec93764807c9553f2a6b8f71298d4b3bb0a8e6a1c8d8d205b2476978a061f5ea4f8ae15427d4fa82
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Algorithmically
|
2
2
|
|
3
|
-
Nature-Inspired Programming Recipes
|
3
|
+
Nature-Inspired Programming Recipes [![Gem Version](https://badge.fury.io/rb/Algorithmically.svg)](https://badge.fury.io/rb/Algorithmically)
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -30,13 +30,22 @@ Or install it yourself as:
|
|
30
30
|
|
31
31
|
### Stochastic Algorithms
|
32
32
|
|
33
|
-
Algorithmically::
|
33
|
+
Algorithmically::Stochastic::RandomSearch.new(2, 50)
|
34
34
|
|
35
|
-
Algorithmically::
|
35
|
+
Algorithmically::Stochastic::AdaptiveRandomSearch.new(1000, 2, 0.05, 1.3, 3.0, 10, 30)
|
36
36
|
|
37
|
-
Algorithmically::
|
37
|
+
Algorithmically::Stochastic::HillClimbing.new(2, 1000)
|
38
38
|
|
39
|
-
Algorithmically::
|
39
|
+
Algorithmically::Stochastic::GuidedLocalSearch.new(150, [[565,575],[25,185],[345,750],[945,685]], 20, 0.3)
|
40
|
+
|
41
|
+
b52 = [[595,360],[1340,725],[1740,245]]
|
42
|
+
max_iterations = 150
|
43
|
+
max_no_improv = 20
|
44
|
+
alpha = 0.3
|
45
|
+
local_search_optima = 12000.0
|
46
|
+
lambda = alpha * (local_search_optima/b52.size.to_f)
|
47
|
+
|
48
|
+
Algorithmically::Stochastic::Iterated_Local_Search.search(max_iterations, b52, max_no_improv, lambda)
|
40
49
|
|
41
50
|
### Swarm Algorithms
|
42
51
|
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Algorithmically
|
2
|
+
module Stochastic
|
3
|
+
|
4
|
+
class Iterated_Local_Search
|
5
|
+
|
6
|
+
def euc_2d(c1, c2)
|
7
|
+
Math.sqrt((c1[0] - c2[0]) ** 2.0 + (c1[1] - c2[1]) ** 2.0).round
|
8
|
+
end
|
9
|
+
|
10
|
+
def cost(permutation, cities)
|
11
|
+
distance = 0
|
12
|
+
permutation.each_with_index do |c1, i|
|
13
|
+
c2 = (i == permutation.size - 1) ? permutation[0] : permutation[i + 1]
|
14
|
+
distance += euc_2d(cities[c1], cities[c2])
|
15
|
+
end
|
16
|
+
distance
|
17
|
+
end
|
18
|
+
|
19
|
+
def random_permutation(cities)
|
20
|
+
perm = Array.new(cities.size) { |i| i }
|
21
|
+
perm.each_index do |i|
|
22
|
+
r = rand(perm.size - i) + i
|
23
|
+
perm[r], perm[i] = perm[i], perm[r]
|
24
|
+
end
|
25
|
+
perm
|
26
|
+
end
|
27
|
+
|
28
|
+
def stochastic_two_opt(permutation)
|
29
|
+
perm = Array.new(permutation)
|
30
|
+
c1, c2 = rand(perm.size), rand(perm.size)
|
31
|
+
exclude = [c1]
|
32
|
+
exclude << ((c1 == 0) ? perm.size - 1 : c1 - 1)
|
33
|
+
exclude << ((c1 == perm.size - 1) ? 0 : c1 + 1)
|
34
|
+
c2 = rand(perm.size) while exclude.include?(c2)
|
35
|
+
c1, c2 = c2, c1 if c2 < c1
|
36
|
+
perm[c1...c2] = perm[c1...c2].reverse
|
37
|
+
perm
|
38
|
+
end
|
39
|
+
|
40
|
+
def local_search(best, cities, max_no_improv)
|
41
|
+
count = 0
|
42
|
+
begin
|
43
|
+
candidate = {:vector => stochastic_two_opt(best[:vector])}
|
44
|
+
candidate[:cost] = cost(candidate[:vector], cities)
|
45
|
+
count = (candidate[:cost] < best[:cost]) ? 0 : count + 1
|
46
|
+
best = candidate if candidate[:cost] < best[:cost]
|
47
|
+
end until count >= max_no_improv
|
48
|
+
best
|
49
|
+
end
|
50
|
+
|
51
|
+
def double_bridge_move(perm)
|
52
|
+
pos1 = 1 + rand(perm.size / 4)
|
53
|
+
pos2 = pos1 + 1 + rand(perm.size / 4)
|
54
|
+
pos3 = pos2 + 1 + rand(perm.size / 4)
|
55
|
+
p1 = perm[0...pos1] + perm[pos3..perm.size]
|
56
|
+
p2 = perm[pos2...pos3] + perm[pos1...pos2]
|
57
|
+
p1 + p2
|
58
|
+
end
|
59
|
+
|
60
|
+
def perturbation(cities, best)
|
61
|
+
candidate = {}
|
62
|
+
candidate[:vector] = double_bridge_move(best[:vector])
|
63
|
+
candidate[:cost] = cost(candidate[:vector], cities)
|
64
|
+
candidate
|
65
|
+
end
|
66
|
+
|
67
|
+
def search(cities, max_iterations, max_no_improv)
|
68
|
+
best = {}
|
69
|
+
best[:vector] = random_permutation(cities)
|
70
|
+
best[:cost] = cost(best[:vector], cities)
|
71
|
+
best = local_search(best, cities, max_no_improv)
|
72
|
+
max_iterations.times do |iter|
|
73
|
+
candidate = perturbation(cities, best)
|
74
|
+
candidate = local_search(candidate, cities, max_no_improv)
|
75
|
+
best = candidate if candidate[:cost] < best[:cost]
|
76
|
+
puts " > iteration #{(iter+1)}, best=#{best[:cost]}"
|
77
|
+
end
|
78
|
+
best
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
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.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- popac
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,7 @@ extensions: []
|
|
66
66
|
extra_rdoc_files: []
|
67
67
|
files:
|
68
68
|
- ".gitignore"
|
69
|
+
- ".ruby-version"
|
69
70
|
- Algorithmically.gemspec
|
70
71
|
- CODE_OF_CONDUCT.md
|
71
72
|
- Gemfile
|
@@ -81,6 +82,7 @@ files:
|
|
81
82
|
- lib/Algorithmically/Stochastic/adaptive_random_search.rb
|
82
83
|
- lib/Algorithmically/Stochastic/guided_local_search.rb
|
83
84
|
- lib/Algorithmically/Stochastic/hill_climbing.rb
|
85
|
+
- lib/Algorithmically/Stochastic/iterated_local_search.rb
|
84
86
|
- lib/Algorithmically/Stochastic/random_search.rb
|
85
87
|
- lib/Algorithmically/Swarm/particle_swarm.rb
|
86
88
|
- lib/Algorithmically/version.rb
|
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
107
|
version: '0'
|
106
108
|
requirements: []
|
107
109
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.
|
110
|
+
rubygems_version: 2.6.8
|
109
111
|
signing_key:
|
110
112
|
specification_version: 4
|
111
113
|
summary: Algorithmically
|