taboo_reactive_search 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/lib/taboo_reactive_search/version.rb +3 -0
- data/lib/taboo_reactive_search.rb +176 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/taboo_reactive_search_spec.rb +15 -0
- data/taboo_reactive_search.gemspec +24 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZGFkNzBhMjI1NzhmYzkwYTc5YTkzOTI4OWM1ODU3NjI4OTUzZWY1OA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZjkwYjUyZmFhN2FiNzc3MzE5ZTg3MGNkMjk0MjViY2Q3Nzc3ZmY3Ng==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NjZmYjc0YjYwYTA4MDAxOGFiZDA1YzQ0ODgzMWQ3YmY5MGY5M2I3Y2Q5ZTll
|
10
|
+
YzE5Zjg1YjgyNmExNmU1MGUyZjM1YzdmYmMwYjlmZTU5NDliYTEzZjAyZjEx
|
11
|
+
Nzg3ODZhYjdlYmYxNzVjNDc1MDg5NWFhYWEyYWFjMGZhZWZjYWI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NWY4NDY2YTgzZWEyZjNmZDk1OGMxNTRkMDRjNGVjNTUyNzI1NjYyZWQxMWEw
|
14
|
+
ZDhlY2U3ZjhjNjk1OTg2ZmQ5ZDNhOTVjMzA1OWFhNDgzODMxNTliNjgxYjM1
|
15
|
+
YTY2YzY5Zjk4M2U2NmE1MmM1NjViYTNiYzQ5MzhmNjI4YmZiN2E=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 dusan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# TabooReactiveSearch
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'taboo_reactive_search'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install taboo_reactive_search
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
require "taboo_reactive_search/version"
|
2
|
+
|
3
|
+
module TabooReactiveSearch
|
4
|
+
class TabooReactiveSearch
|
5
|
+
# get distance between cities
|
6
|
+
def euc_2d(c1, c2)
|
7
|
+
Math.sqrt((c2[0] - c1[0]) ** 2.0 + (c2[1] - c1[1]) ** 2.0).round
|
8
|
+
end
|
9
|
+
|
10
|
+
# cost
|
11
|
+
def cost(shake, cities)
|
12
|
+
distance = 0
|
13
|
+
shake.each_with_index do |c1, i|
|
14
|
+
c2 = (i == (shake.size - 1)) ? shake[0] : shake[i + 1]
|
15
|
+
# +++ get distance between two cities
|
16
|
+
distance += euc_2d cities[c1], cities[c2]
|
17
|
+
end
|
18
|
+
distance
|
19
|
+
end
|
20
|
+
|
21
|
+
# shake
|
22
|
+
def shake(cities)
|
23
|
+
shake = Array.new(cities.size){|i| i}
|
24
|
+
shake.each_index do |i|
|
25
|
+
r = rand(shake.size - 1) + 1
|
26
|
+
shake[i], shake[r] = shake[r], shake[i]
|
27
|
+
end
|
28
|
+
shake
|
29
|
+
end
|
30
|
+
|
31
|
+
# reverse in range
|
32
|
+
def two_opt(shake)
|
33
|
+
perm = Array.new(shake)
|
34
|
+
c1, c2 = rand(perm.size), rand(perm.size)
|
35
|
+
collection = [c1]
|
36
|
+
collection << ((c1 == 0 ? perm.size - 1 : c1 - 1))
|
37
|
+
collection << ((c1 == perm.size - 1) ? 0 : c1 + 1)
|
38
|
+
c2 = rand(perm.size) while collection.include? (c2)
|
39
|
+
c1, c2 = c2, c1 if c2 < c1
|
40
|
+
# +++ reverses in range
|
41
|
+
perm[c1...c2] = perm[c1...c2].reverse
|
42
|
+
return perm, [[shake[c1 - 1], shake[c1]], [shake[c2 - 1], shake[c2]]]
|
43
|
+
end
|
44
|
+
|
45
|
+
# is taboo
|
46
|
+
def is_taboo?(edge, taboo_list, iter, prohib_period)
|
47
|
+
taboo_list.each do |entry|
|
48
|
+
if entry[:edge] = edge
|
49
|
+
return true if entry[:iter] >= iter - prohib_period
|
50
|
+
return false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
# make taboo
|
57
|
+
def make_taboo(taboo_list, edge, iter)
|
58
|
+
taboo_list.each do |entry|
|
59
|
+
if entry[:edge] == edge
|
60
|
+
entry[:iter] = iter
|
61
|
+
return entry
|
62
|
+
end
|
63
|
+
end
|
64
|
+
entry = {:edge => edge, :iter => iter}
|
65
|
+
taboo_list.push(entry)
|
66
|
+
entry
|
67
|
+
end
|
68
|
+
|
69
|
+
# to edge list
|
70
|
+
def to_edge_list(shake)
|
71
|
+
list = []
|
72
|
+
shake.each_with_index do |c1, i|
|
73
|
+
c2 = (i == (shake.size - 1)) ? shake[0] : shake[i + 1]
|
74
|
+
c1, c2 = c2, c1 if c2 < c1
|
75
|
+
list << [c1, c2]
|
76
|
+
end
|
77
|
+
list
|
78
|
+
end
|
79
|
+
|
80
|
+
# same
|
81
|
+
def equivalent(el1, el2)
|
82
|
+
el1.each {|e| return false if el2.nil? || !el2.include?(e)}
|
83
|
+
true
|
84
|
+
end
|
85
|
+
|
86
|
+
# get candidate
|
87
|
+
def get_candidate(best, cities)
|
88
|
+
candidate = {}
|
89
|
+
candidate[:vector], edges = two_opt(best[:vector])
|
90
|
+
candidate[:cost] = cost(candidate[:vector], cities)
|
91
|
+
return candidate, edges
|
92
|
+
end
|
93
|
+
|
94
|
+
# get entry
|
95
|
+
def get_candidate_entry(visited_list, shake)
|
96
|
+
edge_list = to_edge_list(shake)
|
97
|
+
visited_list.each do |entry|
|
98
|
+
return entry if equivalent(edge_list, entry[:edge_list])
|
99
|
+
end
|
100
|
+
nil
|
101
|
+
end
|
102
|
+
|
103
|
+
# store shake
|
104
|
+
def store_shake(visited_list, shake, iteration)
|
105
|
+
entry = {}
|
106
|
+
entry[:entry_list] = to_edge_list(shake)
|
107
|
+
entry[:iter] = iteration
|
108
|
+
entry[:visits] = 1
|
109
|
+
visited_list.push(entry)
|
110
|
+
entry
|
111
|
+
end
|
112
|
+
|
113
|
+
# sort hood
|
114
|
+
def sort_neighborhood(candidates, taboo_list, prohib_period, iteration)
|
115
|
+
taboo, admissable = [], []
|
116
|
+
candidates.each do |a|
|
117
|
+
if is_taboo?(a[1][0], taboo_list, iteration, prohib_period) or
|
118
|
+
is_taboo?(a[1][1], taboo_list, iteration, prohib_period)
|
119
|
+
taboo << a
|
120
|
+
else
|
121
|
+
admissable << a
|
122
|
+
end
|
123
|
+
end
|
124
|
+
return [taboo, admissable]
|
125
|
+
end
|
126
|
+
|
127
|
+
# search
|
128
|
+
def search(cities, max_cand, max_iter, increase, decrease)
|
129
|
+
# setup
|
130
|
+
current = {:vector => shake(cities)}
|
131
|
+
current[:cost] = cost(current[:vector], cities)
|
132
|
+
best = current
|
133
|
+
taboo_list, prohib_period = [], 1
|
134
|
+
visited_list, avg_size, last_change = [], 1, 0
|
135
|
+
max_iter.times do |iter|
|
136
|
+
candidate_entry = get_candidate_entry(visited_list, current[:vector])
|
137
|
+
if !candidate_entry.nil?
|
138
|
+
repetition_interval = iter - candidate_entry[:iter]
|
139
|
+
candidate_entry[:iter] = iter
|
140
|
+
candidate_entry[:visits] += 1
|
141
|
+
if repetition_interval < 2 * (cities.size - 1)
|
142
|
+
avg_size = 0.1 * (iter - candidate_entry[:iter]) + 0.9 * avg_size
|
143
|
+
prohib_period = (prohib_period.to_f * increase)
|
144
|
+
last_change = iter
|
145
|
+
end
|
146
|
+
else
|
147
|
+
store_shake(visited_list, current[:vector], iter)
|
148
|
+
end
|
149
|
+
if iter - last_change > avg_size
|
150
|
+
prohib_period = [prohib_period * decrease, 1].max
|
151
|
+
last_change = iter
|
152
|
+
end
|
153
|
+
candidates = Array.new(max_cand) do |i|
|
154
|
+
get_candidate(current, cities)
|
155
|
+
end
|
156
|
+
candidates.sort!{|x, y| x.first[:cost] <=> y.first[:cost] }
|
157
|
+
taboo, admis = sort_neighborhood(candidates, taboo_list, prohib_period, iter)
|
158
|
+
if admis.size < 2
|
159
|
+
prohib_period = cities.size - 2
|
160
|
+
last_change = iter
|
161
|
+
end
|
162
|
+
current, best_move_edges = (admis.empty?) ? taboo.first : admis.first
|
163
|
+
if !taboo.empty?
|
164
|
+
tf = taboo.first[0]
|
165
|
+
if tf[:cost] < best[:cost] and tf[:cost] < current[:cost]
|
166
|
+
current, best_move_edges = taboo.first
|
167
|
+
end
|
168
|
+
end
|
169
|
+
best_move_edges.each{|edge| make_taboo(taboo_list, edge, iter)}
|
170
|
+
best = candidates.first[0] if candidates.first[0][:cost] < best[:cost]
|
171
|
+
puts " > iter #{(iter + 1)}, best #{best[:cost]}"
|
172
|
+
end
|
173
|
+
best
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TabooReactiveSearch do
|
4
|
+
it 'gets 7542 or greater for berlin' do
|
5
|
+
expect(TabooReactiveSearch::TabooReactiveSearch.new.search([[565,575],[25,185],[345,750],[945,685],[845,655],
|
6
|
+
[880,660],[25,230],[525,1000],[580,1175],[650,1130],[1605,620],
|
7
|
+
[1220,580],[1465,200],[1530,5],[845,680],[725,370],[145,665],
|
8
|
+
[415,635],[510,875],[560,365],[300,465],[520,585],[480,415],
|
9
|
+
[835,625],[975,580],[1215,245],[1320,315],[1250,400],[660,180],
|
10
|
+
[410,250],[420,555],[575,665],[1150,1160],[700,580],[685,595],
|
11
|
+
[685,610],[770,610],[795,645],[720,635],[760,650],[475,960],
|
12
|
+
[95,260],[875,920],[700,500],[555,815],[830,485],[1170,65],
|
13
|
+
[830,610],[605,625],[595,360],[1340,725],[1740,245]], 50, 100, 1.3, 0.9)[:cost]).to be >= 7542
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'taboo_reactive_search/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "taboo_reactive_search"
|
8
|
+
spec.version = TabooReactiveSearch::VERSION
|
9
|
+
spec.authors = ["no-glue"]
|
10
|
+
spec.email = ["nikolapav1985@gmail.com"]
|
11
|
+
spec.description = %q{taboo reactive search to solve travelling salesman}
|
12
|
+
spec.summary = %q{taboo reactive search to solve travelling salesman}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: taboo_reactive_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- no-glue
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: taboo reactive search to solve travelling salesman
|
56
|
+
email:
|
57
|
+
- nikolapav1985@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/taboo_reactive_search.rb
|
68
|
+
- lib/taboo_reactive_search/version.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
- spec/taboo_reactive_search_spec.rb
|
71
|
+
- taboo_reactive_search.gemspec
|
72
|
+
homepage: ''
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.1.11
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: taboo reactive search to solve travelling salesman
|
96
|
+
test_files:
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/taboo_reactive_search_spec.rb
|