ant_tsp 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 +32 -0
- data/Rakefile +6 -0
- data/ant_tsp.gemspec +24 -0
- data/lib/ant_tsp.rb +119 -0
- data/lib/ant_tsp/version.rb +3 -0
- data/spec/ant_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
|
+
MDk4MDUwZmIyYmUyNjg2ZThhYWQxY2NlMTc5NWY4NjUzYmIyNjRiZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZGIxZGYyMjA4M2RkZjE2YjUzNDZmMzhiOTgyOTFhMWRlOGE0ZDIzZQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
Njg3ZDY2NTc3MGFlNTlhZTVhNDkwYzY4NjM1NGViOTg3MGQyMTZmMzQwMjc5
|
10
|
+
ZTNjNzVhYmVkYmI2MmYyODYzYjZhMzEwMWMyZmI2MWYyODMwZDcyZDgyNjRh
|
11
|
+
YTI1NjQyODNhY2U2MDBjODIwNWY1ODhiNzBlMTI5NTJiZmFhNGQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
N2JhNGVkNTNhNmNmNjRkZmY1YzFkYTRmM2U5NWQ2MDAwMDQwMGFmNTg3N2Q5
|
14
|
+
NGZhZWI3ZTFhZDExOTQxYmJkN2UyMWEzOGM4Yzg0YTcwOGY5ODg2YzAzMDEw
|
15
|
+
ZTI4ZjMxNzcwOWNlOTgyNThkOGJlYzFkNGM1NjA1YjdkOTdmMTk=
|
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,32 @@
|
|
1
|
+
# AntTsp
|
2
|
+
|
3
|
+
Ants to solve travelling salesman
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ant_tsp'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ant_tsp
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
AntTsp::AntTsp.new.search([[565,575],[25,185]], 50, 30, 0.6, 2.5, 1.0)
|
22
|
+
50 - max iterations
|
23
|
+
30 - number of ants
|
24
|
+
0.6 - pheromone decay
|
25
|
+
2.5 - c heuristic
|
26
|
+
1.0 - c history
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/ant_tsp.gemspec
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 'ant_tsp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ant_tsp"
|
8
|
+
spec.version = AntTsp::VERSION
|
9
|
+
spec.authors = ["no-glue"]
|
10
|
+
spec.email = ["nikolapav1985@gmail.com"]
|
11
|
+
spec.description = %q{ant to solve travelling salesman}
|
12
|
+
spec.summary = %q{ant 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
|
data/lib/ant_tsp.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require "ant_tsp/version"
|
2
|
+
|
3
|
+
module AntTsp
|
4
|
+
class AntTsp
|
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
|
+
# setup, pheromone matrix
|
31
|
+
def setup_phero_matrix(num_cities, naive_score)
|
32
|
+
v = num_cities.to_f / naive_score
|
33
|
+
return Array.new(num_cities){|i| Array.new(num_cities, v)}
|
34
|
+
end
|
35
|
+
|
36
|
+
# choices, get them
|
37
|
+
def get_choices(cities, last_city, skip, pheromone, c_heur, c_hist)
|
38
|
+
choices = []
|
39
|
+
cities.each_with_index do |position, i|
|
40
|
+
next if skip.include?(i)
|
41
|
+
prob = {:city => i}
|
42
|
+
prob[:history] = pheromone[last_city][i] ** c_hist
|
43
|
+
prob[:distance] = euc_2d(cities[last_city], position)
|
44
|
+
prob[:heuristic] = (1.0 / prob[:distance]) ** c_heur
|
45
|
+
prob[:prob] = prob[:history] * prob[:heuristic]
|
46
|
+
choices << prob
|
47
|
+
end
|
48
|
+
choices
|
49
|
+
end
|
50
|
+
|
51
|
+
# next city
|
52
|
+
def select_next_city(choices)
|
53
|
+
sum = choices.inject(0.0) {|sum, element| sum + element[:prob]}
|
54
|
+
return choices[rand(choices.size)][:city] if sum == 0.0
|
55
|
+
v = rand()
|
56
|
+
choices.each_with_index do |choice, i|
|
57
|
+
v -= (choice[:prob] / sum)
|
58
|
+
return choice[:city] if v <= 0.0
|
59
|
+
end
|
60
|
+
return choices.last[:city]
|
61
|
+
end
|
62
|
+
|
63
|
+
# next city, add them up
|
64
|
+
def stepwise_const(cities, phero, c_heur, c_hist)
|
65
|
+
shake = []
|
66
|
+
shake << rand(cities.size)
|
67
|
+
begin
|
68
|
+
choices = get_choices(cities, shake.last, shake, phero, c_heur, c_hist)
|
69
|
+
next_city = select_next_city(choices)
|
70
|
+
shake << next_city
|
71
|
+
end until shake.size == cities.size
|
72
|
+
shake
|
73
|
+
end
|
74
|
+
|
75
|
+
# decay, pheromone
|
76
|
+
def decay_pheromone(pheromone, decay_factor)
|
77
|
+
pheromone.each do |array|
|
78
|
+
array.each_with_index do |p, i|
|
79
|
+
array[i] = (1.0 - decay_factor) * p
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# update, pheromone
|
85
|
+
def update_pheromone(pheromone, solutions)
|
86
|
+
solutions.each do |other|
|
87
|
+
other[:vector].each_with_index do |x, i|
|
88
|
+
y = (i == (other[:vector].size - 1)) ? other[:vector][0] : other[:vector][i + 1]
|
89
|
+
pheromone[x][y] += (1.0 / other[:cost])
|
90
|
+
pheromone[y][x] += (1.0 / other[:cost])
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# search
|
96
|
+
def search(cities, max_it, num_ants, decay_factor, c_heur, c_hist)
|
97
|
+
best = {:vector => shake(cities)}
|
98
|
+
best[:cost] = cost(best[:vector], cities)
|
99
|
+
pheromone = setup_phero_matrix(cities.size, best[:cost])
|
100
|
+
max_it.times do |iter|
|
101
|
+
solutions = []
|
102
|
+
num_ants.times do
|
103
|
+
candidate = {}
|
104
|
+
# +++ get tour
|
105
|
+
candidate[:vector] = stepwise_const(cities, pheromone, c_heur, c_hist)
|
106
|
+
candidate[:cost] = cost(candidate[:vector], cities)
|
107
|
+
best = candidate if candidate[:cost] < best[:cost]
|
108
|
+
solutions << candidate
|
109
|
+
end
|
110
|
+
# +++ decay phero
|
111
|
+
decay_pheromone(pheromone, decay_factor)
|
112
|
+
# +++ update phero
|
113
|
+
update_pheromone(pheromone, solutions)
|
114
|
+
puts " > iteration #{(iter + 1)}, best #{best[:cost]}"
|
115
|
+
end
|
116
|
+
best
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AntTsp do
|
4
|
+
it 'gets 7542 or greater for berlin' do
|
5
|
+
expect(AntTsp::AntTsp.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, 30, 0.6, 2.5, 1.0)[: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: ant_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-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: ant 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
|
+
- ant_tsp.gemspec
|
68
|
+
- lib/ant_tsp.rb
|
69
|
+
- lib/ant_tsp/version.rb
|
70
|
+
- spec/ant_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: ant to solve travelling salesman
|
96
|
+
test_files:
|
97
|
+
- spec/ant_tsp_spec.rb
|
98
|
+
- spec/spec_helper.rb
|