tabu_search 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b995d10c83ee458580e2c3c7c05642c635d818c1
4
+ data.tar.gz: acc5fdb3cd382d56d8e465ecddffede0b6c9f3d1
5
+ SHA512:
6
+ metadata.gz: 67ae26f25d00358037fcb5e6adb1c337892f1b3a3d1d84fa1f4361949c6a53fd826672483c0f76f38b2f7fcd636a80b3d80f3606a0974067f31e7b2b26c26f2f
7
+ data.tar.gz: 0d07e17d3b131371c4bf118bd294dbbe6e47cb4a10851ed174529163c8ce9dec58d3375eb33c11ddf7f0f7718535daf444afd0f75240b5206fa7180bf1903583
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require gem_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tabu_search.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 jiangzhi.xie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # TabuSearch
2
+
3
+ A simple Tabu Search framework
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tabu_search'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tabu_search
20
+
21
+ ## Usage
22
+
23
+ Require methods:
24
+
25
+ * `Class#genome` Support dup
26
+ * `Class#genome=`
27
+ * `Class#fitness` Current unit fitness, the bigger than better
28
+ * `Class#search_neighbour(ts)` Search some neighbour units
29
+ * `Class#step(ts, data)` Update unit and TS context
30
+
31
+ Example:
32
+
33
+ ```
34
+ class Unit
35
+ include TabuSearch
36
+
37
+ LEN = 5
38
+
39
+ attr_accessor :genome
40
+
41
+ def initialize(genome)
42
+ @genome = genome.dup
43
+ end
44
+
45
+ def fitness
46
+ val = genome.join.to_i(2)
47
+ return 0 if val > 10
48
+ 10 * val - val * 3 + val ** 2
49
+ end
50
+
51
+ def step(ts, data)
52
+ id, index, val, new_fitness = data
53
+ genome[index] = val
54
+ ts.update(id, genome, new_fitness)
55
+ end
56
+
57
+ def search_neighbour(ts)
58
+ LEN.times.map do |i|
59
+ origin = genome[i]
60
+ new_val = 1 - origin
61
+ genome[i] = new_val
62
+ new_fitness = fitness
63
+ genome[i] = origin
64
+ ["#{i}-#{new_val}", i, new_val, new_fitness]
65
+ end
66
+ end
67
+ end
68
+
69
+ # Class.tabu_search(instance, times, tabu_size)
70
+ Unit.tabu_search(Unit.new([1,2,3]), 10, 10)
71
+ # or
72
+ Unit.new_ts_ctx(10).search(Unit.new([1,2,3]), 10)
73
+ ```
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xjz19901211]/tabu_search.
78
+
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
83
+
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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tabu_search"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,57 @@
1
+ require 'set'
2
+
3
+ module TabuSearch
4
+ class Context
5
+ attr_accessor :best_genome, :best_fitness
6
+ attr_accessor :tabu_list, :tabu_set, :tabu_size
7
+
8
+ def initialize(tabu_size = 10)
9
+ @tabu_list = []
10
+ @tabu_set = Set.new
11
+ @tabu_size = tabu_size
12
+
13
+ @best_genome = nil
14
+ @best_fitness = nil
15
+ end
16
+
17
+ def search(unit, times)
18
+ @best_genome = unit.genome
19
+ @best_fitness = unit.fitness
20
+
21
+ times.times do
22
+ data = search_best_neighbour(unit)
23
+ unit.step(self, data)
24
+ end
25
+ unit.genome = best_genome
26
+ unit
27
+ end
28
+
29
+ def update(id, genome, fitness)
30
+ if fitness > best_fitness
31
+ self.best_genome = genome.dup
32
+ self.best_fitness = fitness
33
+ end
34
+
35
+ unless tabu_set.include?(id)
36
+ tabu_set << id
37
+ tabu_list << id
38
+ tabu_set.delete(tabu_list.shift) if tabu_list.length > tabu_size
39
+ end
40
+ end
41
+
42
+
43
+ private
44
+
45
+ def search_best_neighbour(unit)
46
+ actions = unit.search_neighbour(self)
47
+
48
+ sorted_actions = actions.sort_by {|data| -data[-1] }
49
+ return sorted_actions[0] if sorted_actions[0][-1] > best_fitness
50
+
51
+ sorted_actions.each do |data|
52
+ return data unless tabu_set.include?(data[0])
53
+ end
54
+ return sorted_actions.first
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module TabuSearch
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,18 @@
1
+ require "tabu_search/version"
2
+ require "tabu_search/context"
3
+
4
+ module TabuSearch
5
+ def self.included(cls)
6
+ cls.extend(TabuSearch::ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def new_ts_ctx(tabu_size)
11
+ TabuSearch::Context.new(tabu_size)
12
+ end
13
+
14
+ def tabu_search(unit, times, tabu_size)
15
+ new_ts_ctx(tabu_size).search(unit, times)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tabu_search/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tabu_search"
8
+ spec.version = TabuSearch::VERSION
9
+ spec.authors = ["jiangzhi.xie"]
10
+ spec.email = ["xiejiangzhi@gmail.com"]
11
+
12
+ spec.summary = %q{Simple TabuSearch framework}
13
+ spec.description = %q{Simple TabuSearch framework}
14
+ # spec.homepage = "https://"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tabu_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jiangzhi.xie
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-30 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Simple TabuSearch framework
56
+ email:
57
+ - xiejiangzhi@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/tabu_search.rb
72
+ - lib/tabu_search/context.rb
73
+ - lib/tabu_search/version.rb
74
+ - tabu_search.gemspec
75
+ homepage:
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.5.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Simple TabuSearch framework
99
+ test_files: []