extremal_opt_tsp 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +6 -0
- data/extremal_opt_tsp.gemspec +24 -0
- data/lib/extremal_opt_tsp.rb +156 -0
- data/lib/extremal_opt_tsp/version.rb +3 -0
- data/spec/extremal_opt_tsp_spec.rb +15 -0
- data/spec/spec_helper.rb +6 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZWU1MmQwOGNjM2NhMWEzOWRiMGRmYWM1OGM4YjkzYTUwNDU5OTZlZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OWNiZDNlOTU3YmFhMGMxZjI3NzFiZjRhZmVkYWQ3NDBlNTQ5MWUwYw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZDk4NzFhZGI1NGE4YWZiODM3ZGQyZDU0YjE5MTU3NDQ0MTQ0OTgyMGI1MTYy
|
10
|
+
ZGY5Y2Q0NGE3NzM2YmIyZGM4NDg1YzhhMzBjMDg5MDIyYmE2OGI3NmFkMjIz
|
11
|
+
ODc0MmNlMTgzZDZiOTNiMzAwMjM5YTIzYmRiNTJjYTU0YmJmZmM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NzhlMWRmZWI2MmUyZTQzN2VlNTAwODJiMzMwOTEyNzQ0YTgyYmE5MTRkZTcw
|
14
|
+
NmNhZWRhZmZhMzU4MGRlZjZjNzljNjUzMjE1NDI4NjI0NjYwOGZiZmI2MDU0
|
15
|
+
YmZiMWZkYmFlMWE4NmRiMjNhNzVhYzA0NGY4ZjlhNDAyNGNjMjI=
|
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,31 @@
|
|
1
|
+
# ExtremalOptTsp
|
2
|
+
|
3
|
+
Extremal optimization to solve travelling salesman
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'extremal_opt_tsp'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install extremal_opt_tsp
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
ExtremalOptTsp::ExtremalOptTsp.new.search([[565,575],[25,185]], 250, 1.8)[:cost])
|
22
|
+
250 - max iterations
|
23
|
+
1.8 - t
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -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 'extremal_opt_tsp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "extremal_opt_tsp"
|
8
|
+
spec.version = ExtremalOptTsp::VERSION
|
9
|
+
spec.authors = ["no-glue"]
|
10
|
+
spec.email = ["nikolapav1985@gmail.com"]
|
11
|
+
spec.description = %q{extremal optimization to solve travelling salesman}
|
12
|
+
spec.summary = %q{extremal optimization 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
|
@@ -0,0 +1,156 @@
|
|
1
|
+
require "extremal_opt_tsp/version"
|
2
|
+
|
3
|
+
module ExtremalOptTsp
|
4
|
+
class ExtremalOptTsp
|
5
|
+
# distance, between two cities
|
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
|
+
# cost, of the tour
|
11
|
+
def cost(sh, cities)
|
12
|
+
distance = 0
|
13
|
+
sh.each_with_index do |c1, i|
|
14
|
+
c2 = (i == sh.size - 1) ? sh[0] : sh[i + 1]
|
15
|
+
distance += euc_2d(cities[c1], cities[c2])
|
16
|
+
end
|
17
|
+
distance
|
18
|
+
end
|
19
|
+
|
20
|
+
# shake, cities
|
21
|
+
def shake(cities)
|
22
|
+
sh = Array.new(cities.size){|i| i}
|
23
|
+
sh.each_index do |i|
|
24
|
+
r = rand(sh.size - i) + i
|
25
|
+
sh[r], sh[i] = sh[i], sh[r]
|
26
|
+
end
|
27
|
+
sh
|
28
|
+
end
|
29
|
+
|
30
|
+
# neighbors, get them ranked
|
31
|
+
def get_neighbor_rank(city_number, cities, ignore=[])
|
32
|
+
neighbors = []
|
33
|
+
cities.each_with_index do |city, i|
|
34
|
+
next if i == city_number or ignore.include?(i)
|
35
|
+
neighbor = {:number => i}
|
36
|
+
neighbor[:distance] = euc_2d(cities[city_number], city)
|
37
|
+
neighbors << neighbor
|
38
|
+
end
|
39
|
+
return neighbors.sort!{|x, y| x[:distance] <=> y[:distance]}
|
40
|
+
end
|
41
|
+
|
42
|
+
# edges, for city
|
43
|
+
def get_edges_for_city(city_number, sh)
|
44
|
+
c1, c2 = nil, nil
|
45
|
+
sh.each_with_index do |c, i|
|
46
|
+
if c == city_number
|
47
|
+
c1 = (i == 0) ? sh.last : sh[i - 1]
|
48
|
+
c2 = (i == sh.size - 1) ? sh.first : sh[i + 1]
|
49
|
+
break
|
50
|
+
end
|
51
|
+
end
|
52
|
+
return [c1, c2]
|
53
|
+
end
|
54
|
+
|
55
|
+
# fitness, city
|
56
|
+
def get_city_fitness(sh, city_number, cities)
|
57
|
+
c1, c2 = get_edges_for_city(city_number, sh)
|
58
|
+
neighbors = get_neighbor_rank(city_number, cities)
|
59
|
+
n1, n2 = -1, -1
|
60
|
+
neighbors.each_with_index do |neighbor, i|
|
61
|
+
n1 = i + 1 if neighbor[:number] == c1
|
62
|
+
n2 = i + 1 if neighbor[:number] == c2
|
63
|
+
end
|
64
|
+
return 3.0 / (n1.to_f + n2.to_f)
|
65
|
+
end
|
66
|
+
|
67
|
+
# fitnesses, cities
|
68
|
+
def get_city_fitnesses(cities, sh)
|
69
|
+
city_fitnesses = []
|
70
|
+
cities.each_with_index do |city, i|
|
71
|
+
city_fitness = {:number => i}
|
72
|
+
city_fitness[:fitness] = get_city_fitness(sh, i, cities)
|
73
|
+
city_fitnesses << city_fitness
|
74
|
+
end
|
75
|
+
return city_fitnesses.sort!{|x, y| x[:fitness] <=> y[:fitness]}
|
76
|
+
end
|
77
|
+
|
78
|
+
# probs, components
|
79
|
+
def get_component_probs(ordered_comps, t)
|
80
|
+
sum = 0.0
|
81
|
+
ordered_comps.each_with_index do |component, i|
|
82
|
+
component[:prob] = (i + 1.0) ** (-t)
|
83
|
+
sum += component[:prob]
|
84
|
+
end
|
85
|
+
sum
|
86
|
+
end
|
87
|
+
|
88
|
+
# select
|
89
|
+
def select(comps, sum_probs)
|
90
|
+
selection = rand()
|
91
|
+
comps.each_with_index do |comp, i|
|
92
|
+
selection -= (comp[:prob] / sum_probs)
|
93
|
+
return comp[:number] if selection <= 0.0
|
94
|
+
end
|
95
|
+
return comps.last[:number]
|
96
|
+
end
|
97
|
+
|
98
|
+
# select, city maybe
|
99
|
+
def select_maybe(ordered_comps, t, skip = [])
|
100
|
+
sum = get_component_probs(ordered_comps, t)
|
101
|
+
selected_city = nil
|
102
|
+
begin
|
103
|
+
selected_city = select(ordered_comps, sum)
|
104
|
+
end while skip.include?(selected_city)
|
105
|
+
selected_city
|
106
|
+
end
|
107
|
+
|
108
|
+
# shake, vary
|
109
|
+
def vary_shake(sh, selected, new, long_edge)
|
110
|
+
_sh = Array.new(sh)
|
111
|
+
c1, c2 = _sh.rindex(selected), _sh.rindex(selected)
|
112
|
+
p1, p2 = (c1 < c2) ? [c1, c2] : [c2, c1]
|
113
|
+
right = (c1 == _sh.size - 1) ? 0 : c1 + 1
|
114
|
+
if _sh[right] == long_edge
|
115
|
+
_sh[p1 + 1 ... p2] = _sh[p1 + 1 ... p2].reverse
|
116
|
+
else
|
117
|
+
_sh[p1 ... p2] = _sh[p1 ... p2].reverse
|
118
|
+
end
|
119
|
+
_sh
|
120
|
+
end
|
121
|
+
|
122
|
+
# longer edge, get
|
123
|
+
def get_longer_edge(edges, neighbor_distances)
|
124
|
+
n1 = neighbor_distances.find{|x| x[:number] = edges[0]}
|
125
|
+
n2 = neighbor_distances.find{|x| x[:number] = edges[1]}
|
126
|
+
return (n1[:distance] > n2[:distance]) ? n1[:number] : n2[:number]
|
127
|
+
end
|
128
|
+
|
129
|
+
# new shake
|
130
|
+
def new_shake(cities, t, sh)
|
131
|
+
city_fitnesses = get_city_fitnesses(cities, sh)
|
132
|
+
selected_city = select_maybe(city_fitnesses.reverse, t)
|
133
|
+
edges = get_edges_for_city(selected_city, sh)
|
134
|
+
neighbors = get_neighbor_rank(selected_city, cities)
|
135
|
+
new_neighbor = select_maybe(neighbors, t, edges)
|
136
|
+
long_edge = get_longer_edge(edges, neighbors)
|
137
|
+
return vary_shake(sh, selected_city, new_neighbor, long_edge)
|
138
|
+
end
|
139
|
+
|
140
|
+
# search
|
141
|
+
def search(cities, max_iterations, t)
|
142
|
+
current = {:vector => shake(cities)}
|
143
|
+
current[:cost] = cost(current[:vector], cities)
|
144
|
+
best = current
|
145
|
+
max_iterations.times do |iter|
|
146
|
+
candidate = {}
|
147
|
+
candidate[:vector] = new_shake(cities, t, current[:vector])
|
148
|
+
candidate[:cost] = cost(candidate[:vector], cities)
|
149
|
+
current = candidate
|
150
|
+
best = candidate if candidate[:cost] < best[:cost]
|
151
|
+
puts " > iter #{(iter + 1)}, best #{best[:cost]}"
|
152
|
+
end
|
153
|
+
best
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ExtremalOptTsp do
|
4
|
+
it 'gets 7542 or greater for berlin' do
|
5
|
+
expect(ExtremalOptTsp::ExtremalOptTsp.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]], 250, 1.8)[:cost]).to be >= 7542
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: extremal_opt_tsp
|
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-27 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: extremal optimization 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
|
+
- extremal_opt_tsp.gemspec
|
68
|
+
- lib/extremal_opt_tsp.rb
|
69
|
+
- lib/extremal_opt_tsp/version.rb
|
70
|
+
- spec/extremal_opt_tsp_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
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: extremal optimization to solve travelling salesman
|
96
|
+
test_files:
|
97
|
+
- spec/extremal_opt_tsp_spec.rb
|
98
|
+
- spec/spec_helper.rb
|