ant_colony_optimizer 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e408d909edc458bbf40177c1660389e2e48f34d9
4
- data.tar.gz: 7da6cf9b2f3097b09fca57059b8114b084ae7c00
3
+ metadata.gz: c2c50dc0a687d8571ee8dc1625a4e9b012b2a4de
4
+ data.tar.gz: faa09ae4dab05be3948524d2b35ecb1f4cf67079
5
5
  SHA512:
6
- metadata.gz: 1317dd16d277115333217d771fac152f68e35dac8c12f95ffa44398af3436aee8756c49b2cc376d51ea46376a408092777f08dea9a66f0ce1357f5117fa2b9f9
7
- data.tar.gz: 93cb11aab75c1361f22979fc84f899d6caf0b8a9f47e619a21c96ae63efc4f21911c7831a1a19ff05d62b7a6dd9114e40ea086e2af1b30adf49a2a52c9a9ad2d
6
+ metadata.gz: 91d37722dac921ad657cfdf2c4018378b27b7ec1005fe66e53ea47b7fbc6c42b73628241ef480c7380421e8a513142f040344f00bebbde19435ed967fde81eb3
7
+ data.tar.gz: d668d3cbe4bd343e85afbf98ee044aed515d807c7c8c462c727ca37e773ce4efdaff5bec14719e86eff023d80fed028d7cba8886c7e68f612678d811cc719b75
data/Rakefile CHANGED
@@ -11,4 +11,4 @@ Rake::TestTask.new(:test) do |t|
11
11
  end
12
12
  end
13
13
 
14
- task :default => :spec
14
+ task :default => :test
@@ -11,6 +11,8 @@ class AntColonyOptimizer
11
11
  @q = q
12
12
  @ants = ants
13
13
  initialize_pheromone
14
+ @min_cost = Float::MAX
15
+ @best_path = nil
14
16
  end
15
17
 
16
18
  def self.greedy_path_cost(matrix, start)
@@ -44,7 +46,7 @@ class AntColonyOptimizer
44
46
  initial_phero = @ants / avg
45
47
  @pheromone = @node_num.times.map{|i|
46
48
  @node_num.times.map{|j|
47
- if i == j
49
+ if i == j or @matrix[i][j] == 0
48
50
  0
49
51
  else
50
52
  initial_phero
@@ -55,6 +57,7 @@ class AntColonyOptimizer
55
57
 
56
58
  def find_path
57
59
  # start from index 0
60
+ costs = []
58
61
  cost = 0.0
59
62
  i = 0
60
63
  visited = [0]
@@ -62,7 +65,11 @@ class AntColonyOptimizer
62
65
  until remain.empty?
63
66
  sum = 0.0
64
67
  probs = remain.map{|j|
65
- p = (@pheromone[i][j] ** @alpha) * (@matrix[i][j] ** -@beta)
68
+ if @matrix[i][j] == 0
69
+ p = 0.0
70
+ else
71
+ p = (@pheromone[i][j] ** @alpha) * (@matrix[i][j] ** -@beta)
72
+ end
66
73
  sum += p
67
74
  sum
68
75
  }
@@ -72,17 +79,23 @@ class AntColonyOptimizer
72
79
  remain.delete(next_i)
73
80
  visited << next_i
74
81
  cost += @matrix[i][next_i]
82
+ if @matrix[0][next_i] == 0
83
+ costs << cost
84
+ cost = 0.0
85
+ end
75
86
  i = next_i
76
87
  end
77
88
  visited << 0
78
89
  cost += @matrix[i][0]
79
- [visited, cost]
90
+ costs << cost
91
+ [visited, costs.max]
80
92
  end
81
93
 
82
94
  def update_pheromone(paths)
83
95
  @node_num.times do |i|
84
96
  @node_num.times do |j|
85
97
  next if i == j
98
+ next if @matrix[i][j] == 0.0
86
99
  curr = @pheromone[i][j]
87
100
  new = curr * @ro + paths.inject(0.0){|sum, (path, cost)|
88
101
  if path.each_cons(2).find{|cons| cons == [i,j] or cons == [j,i] }
@@ -115,16 +128,20 @@ class AntColonyOptimizer
115
128
  [best_path, min_cost]
116
129
  end
117
130
 
118
- def optimize(iteration)
119
- min_cost = Float::MAX
120
- best_path = nil
121
- iteration.times do
131
+ def optimize(iteration, best_path_callback: nil)
132
+ iteration.times do |i|
122
133
  path, cost = step
123
- if cost < min_cost
124
- min_cost = cost
125
- best_path = path
134
+ if block_given?
135
+ yield [i, path, cost]
136
+ end
137
+ if cost < @min_cost
138
+ @min_cost = cost
139
+ @best_path = path
140
+ if best_path_callback.respond_to?(:call)
141
+ best_path_callback.call(i, path, cost)
142
+ end
126
143
  end
127
144
  end
128
- [best_path, min_cost]
145
+ [@best_path, @min_cost]
129
146
  end
130
147
  end
@@ -1,3 +1,3 @@
1
1
  class AntColonyOptimizer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ant_colony_optimizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - nagachika
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-11 00:00:00.000000000 Z
11
+ date: 2016-10-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ant Colony Optimizer Library
14
14
  email: