ant_colony_optimizer 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: e408d909edc458bbf40177c1660389e2e48f34d9
4
+ data.tar.gz: 7da6cf9b2f3097b09fca57059b8114b084ae7c00
5
+ SHA512:
6
+ metadata.gz: 1317dd16d277115333217d771fac152f68e35dac8c12f95ffa44398af3436aee8756c49b2cc376d51ea46376a408092777f08dea9a66f0ce1357f5117fa2b9f9
7
+ data.tar.gz: 93cb11aab75c1361f22979fc84f899d6caf0b8a9f47e619a21c96ae63efc4f21911c7831a1a19ff05d62b7a6dd9114e40ea086e2af1b30adf49a2a52c9a9ad2d
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ Gemfile.lock
46
+ .ruby-version
47
+ .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ant_colony_optimizer.gemspec
4
+ gemspec
5
+
6
+ gem "rake"
7
+ gem "test-unit"
8
+ gem "simplecov"
9
+ gem "test-unit-power_assert"
10
+ gem "pry"
11
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Tomoyuki Chikanaga
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # ant_colony_optimizer
2
+ Ant Colony Optimization Library
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ if ENV["TESTS"].nil? or ENV["TESTS"].empty?
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ else
10
+ t.test_files = FileList[ENV["TESTS"]]
11
+ end
12
+ end
13
+
14
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ant_colony_optimizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ant_colony_optimizer"
8
+ spec.version = AntColonyOptimizer::VERSION
9
+ spec.authors = ["nagachika"]
10
+ spec.email = ["nagachika@ruby-lang.org"]
11
+
12
+ spec.summary = %q{Ant Colony Optimizer Library}
13
+ spec.description = %q{Ant Colony Optimizer Library}
14
+ spec.homepage = "https://github.org/nagachika/ant_colony_optimizer"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ant_colony_optimizer"
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
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,3 @@
1
+ class AntColonyOptimizer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,130 @@
1
+ # coding: utf-8
2
+ # frozen-string-literal: true
3
+
4
+ class AntColonyOptimizer
5
+ def initialize(graph_matrix, alpha: 1.0, beta: 0.5, ro: 0.8, ants: graph_matrix[0].size, q: 1.0)
6
+ @matrix = graph_matrix
7
+ @node_num = @matrix.size
8
+ @alpha = alpha
9
+ @beta = beta
10
+ @ro = ro
11
+ @q = q
12
+ @ants = ants
13
+ initialize_pheromone
14
+ end
15
+
16
+ def self.greedy_path_cost(matrix, start)
17
+ node_num = matrix.size
18
+ visited = [start]
19
+ i = start
20
+ cost = 0.0
21
+ while visited.size < node_num
22
+ min_cost = Float::MAX
23
+ next_cand = nil
24
+ matrix[i].each_with_index do |c, j|
25
+ if not(visited.include?(j)) and min_cost > c
26
+ min_cost = c
27
+ next_cand = j
28
+ end
29
+ end
30
+ i = next_cand
31
+ cost += min_cost
32
+ visited << i
33
+ end
34
+ cost += matrix[i][start]
35
+ cost
36
+ end
37
+
38
+ def initialize_pheromone
39
+ total_greedy_path_cost = 0
40
+ @node_num.times do |i|
41
+ total_greedy_path_cost += self.class.greedy_path_cost(@matrix, i)
42
+ end
43
+ avg = total_greedy_path_cost.to_f / @node_num
44
+ initial_phero = @ants / avg
45
+ @pheromone = @node_num.times.map{|i|
46
+ @node_num.times.map{|j|
47
+ if i == j
48
+ 0
49
+ else
50
+ initial_phero
51
+ end
52
+ }
53
+ }
54
+ end
55
+
56
+ def find_path
57
+ # start from index 0
58
+ cost = 0.0
59
+ i = 0
60
+ visited = [0]
61
+ remain = (1..(@node_num-1)).to_a
62
+ until remain.empty?
63
+ sum = 0.0
64
+ probs = remain.map{|j|
65
+ p = (@pheromone[i][j] ** @alpha) * (@matrix[i][j] ** -@beta)
66
+ sum += p
67
+ sum
68
+ }
69
+ probs.map!{|p| p / sum }
70
+ idx = probs.index{|p| p > rand }
71
+ next_i = remain[idx]
72
+ remain.delete(next_i)
73
+ visited << next_i
74
+ cost += @matrix[i][next_i]
75
+ i = next_i
76
+ end
77
+ visited << 0
78
+ cost += @matrix[i][0]
79
+ [visited, cost]
80
+ end
81
+
82
+ def update_pheromone(paths)
83
+ @node_num.times do |i|
84
+ @node_num.times do |j|
85
+ next if i == j
86
+ curr = @pheromone[i][j]
87
+ new = curr * @ro + paths.inject(0.0){|sum, (path, cost)|
88
+ if path.each_cons(2).find{|cons| cons == [i,j] or cons == [j,i] }
89
+ sum + @q.to_f / cost
90
+ else
91
+ sum
92
+ end
93
+ }
94
+ @pheromone[i][j] = @pheromone[j][i] = new
95
+ end
96
+ end
97
+ end
98
+
99
+ def step
100
+ min_cost = Float::MAX
101
+ best_path = nil
102
+ paths = []
103
+ # trace path by every ant
104
+ @ants.times do
105
+ path, cost = find_path
106
+ if cost < min_cost
107
+ min_cost = cost
108
+ best_path = path
109
+ end
110
+ paths << [path, cost]
111
+ end
112
+ # update pheromone
113
+ update_pheromone(paths)
114
+
115
+ [best_path, min_cost]
116
+ end
117
+
118
+ def optimize(iteration)
119
+ min_cost = Float::MAX
120
+ best_path = nil
121
+ iteration.times do
122
+ path, cost = step
123
+ if cost < min_cost
124
+ min_cost = cost
125
+ best_path = path
126
+ end
127
+ end
128
+ [best_path, min_cost]
129
+ end
130
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ant_colony_optimizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nagachika
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ant Colony Optimizer Library
14
+ email:
15
+ - nagachika@ruby-lang.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - ant_colony_optimizer.gemspec
26
+ - bin/console
27
+ - bin/setup
28
+ - lib/ant_colony_optimizer.rb
29
+ - lib/ant_colony_optimizer/version.rb
30
+ homepage: https://github.org/nagachika/ant_colony_optimizer
31
+ licenses: []
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.5.1
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Ant Colony Optimizer Library
53
+ test_files: []