variable_neighborhood_search 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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWY2ZGZjNDYyNmYxYTAzNGU5YjA3Yzg3MDIzZjhlODI3NzJlNjVhNw==
5
+ data.tar.gz: !binary |-
6
+ ZGM1OGMyMDMwNzhkODg5YzUzMjVlNjQ1NmIzOWVjOThlNmU3OGUxNw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YzM3MDE4NzZkMzYwMDJhNWQ4YTZmMTUwYjdjMzEyMmRjMjdkNGRmOTA5YmZk
10
+ N2YwMzlkYjdhYjliODA5NTA5NTkwN2Y4MDljMDU2NjI4MjY3ODE5OTc0MmY0
11
+ ZTVmODA0NThiMjQxY2I3MWY1MzY0NjE3ZWU5NzFhOThhZmY4MDQ=
12
+ data.tar.gz: !binary |-
13
+ NmZjYzg1OTFmM2QxYjA3MDI0OTI5Y2UwNzE1NmFmMjJlNDExM2YxMzQ1OTMz
14
+ MDE2ZDlkZWJiMTdiMzJlYjlkNTY3NmRjMWQwYmYyZjRmNWMwMzVkN2U0NmEx
15
+ ODJiNjI3NjlkYjYzMTEzMzA3ZjcyOTExMjYyNWRhY2FkY2EwOWU=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in variable_neighborhood_search.gemspec
4
+ gemspec
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
+ # VariableNeighborhoodSearch
2
+
3
+ Solves travelling salesman with variable neighborhood search
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'variable_neighborhood_search'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install variable_neighborhood_search
18
+
19
+ ## Usage
20
+
21
+ An example VariableNeighborhoodSearch::VariableNeighborhoodSearch([[50, 50], [70, 70]], 1...20, 50, 70)
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,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new("spec")
5
+
6
+ task :default => :spec
@@ -0,0 +1,85 @@
1
+ require "variable_neighborhood_search/version"
2
+
3
+ module VariableNeighborhoodSearch
4
+ class VariableNeighborhoodSearch
5
+ # get distance
6
+ def euc_2d(c1, c2)
7
+ Math.sqrt((c1[0] - c2[0]) ** 2 + (c1[1] - c2[1]) ** 2).round
8
+ end
9
+
10
+ # shake
11
+ def shake(cities)
12
+ shake = Array.new(cities.size){|i| i}
13
+ shake.each_index do |i|
14
+ r = rand(shake.size - 1) + 1
15
+ shake[i], shake[r] = shake[r], shake[i]
16
+ end
17
+ shake
18
+ end
19
+
20
+ # shake in range
21
+ def two_opt(shake)
22
+ sh = Array.new(shake)
23
+ c1, c2 = rand(sh.size), rand(sh.size)
24
+ pool = [c1]
25
+ pool << ((c1 == 0) ? sh.size - 1 : c1 + 1)
26
+ pool << ((c1 == (sh.size - 1)) ? 0 : c1 - 1)
27
+ c2 = rand(sh.size) while pool.include? c2
28
+ c1, c2 = c2, c1 if c2 < c1
29
+ sh[c1...c2] = sh[c1...c2].reverse
30
+ sh
31
+ end
32
+
33
+ # cost
34
+ def cost(shake, cities)
35
+ distance = 0
36
+ shake.each_with_index do |c1, i|
37
+ c2 = (i == (shake.size - 1)) ? shake[0] : shake[i + 1]
38
+ c1, c2 = c2, c1 if c2 < c1
39
+ distance += euc_2d(cities[c1], cities[c2])
40
+ end
41
+ distance
42
+ end
43
+
44
+ # local search
45
+ def local_search(best, cities, max_no_improv, neighborhood)
46
+ count = 0
47
+ begin
48
+ candidate = {:vector => shake(cities)}
49
+ neighborhood.times{two_opt(candidate[:vector])}
50
+ candidate[:cost] = cost(candidate[:vector], cities)
51
+ if candidate[:cost] < best[:cost]
52
+ count, best = 0, candidate
53
+ else
54
+ count += 1
55
+ end
56
+ end until count >= max_no_improv
57
+ best
58
+ end
59
+
60
+ # search
61
+ def search(cities, neighborhoods, max_no_improv, max_no_improv_ls)
62
+ best = {}
63
+ best[:vector] = shake(cities)
64
+ best[:cost] = cost(best[:vector], cities)
65
+ iter, count = 0, 0
66
+ begin
67
+ neighborhoods.each do |neigh|
68
+ candidate = {:vector => Array.new(best[:vector])}
69
+ neigh.times{two_opt(candidate[:vector])}
70
+ candidate[:cost] = cost(candidate[:vector], cities)
71
+ candidate = local_search(candidate, cities, max_no_improv_ls, neigh)
72
+ puts " > iteration #{(iter + 1)}, neigh = #{neigh}, best = #{best[:cost]}"
73
+ iter += 1
74
+ if candidate[:cost] < best[:cost]
75
+ best, count = candidate, 0
76
+ break
77
+ else
78
+ count += 1
79
+ end
80
+ end
81
+ end until count >= max_no_improv
82
+ best
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module VariableNeighborhoodSearch
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'variable_neighborhood_search'
4
+
5
+ RSpec.configure do |config|
6
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe VariableNeighborhoodSearch do
4
+ it 'gets 7542 or greater for berlin' do
5
+ expect(VariableNeighborhoodSearch::VariableNeighborhoodSearch.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]], 1...20, 50, 70)[: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 'variable_neighborhood_search/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "variable_neighborhood_search"
8
+ spec.version = VariableNeighborhoodSearch::VERSION
9
+ spec.authors = ["no-glue"]
10
+ spec.email = ["nikolapav1985@gmail.com"]
11
+ spec.description = %q{variable neighborhood search to solve travelling salesman}
12
+ spec.summary = %q{variable neighborhood 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: variable_neighborhood_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-19 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: variable neighborhood 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/variable_neighborhood_search.rb
68
+ - lib/variable_neighborhood_search/version.rb
69
+ - spec/spec_helper.rb
70
+ - spec/variable_neighborhood_search_spec.rb
71
+ - variable_neighborhood_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: variable neighborhood search to solve travelling salesman
96
+ test_files:
97
+ - spec/spec_helper.rb
98
+ - spec/variable_neighborhood_search_spec.rb