ant_colony_optimizer 0.1.0 → 0.1.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 +4 -4
- data/Rakefile +1 -1
- data/lib/ant_colony_optimizer.rb +28 -11
- data/lib/ant_colony_optimizer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2c50dc0a687d8571ee8dc1625a4e9b012b2a4de
|
4
|
+
data.tar.gz: faa09ae4dab05be3948524d2b35ecb1f4cf67079
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91d37722dac921ad657cfdf2c4018378b27b7ec1005fe66e53ea47b7fbc6c42b73628241ef480c7380421e8a513142f040344f00bebbde19435ed967fde81eb3
|
7
|
+
data.tar.gz: d668d3cbe4bd343e85afbf98ee044aed515d807c7c8c462c727ca37e773ce4efdaff5bec14719e86eff023d80fed028d7cba8886c7e68f612678d811cc719b75
|
data/Rakefile
CHANGED
data/lib/ant_colony_optimizer.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
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
|
124
|
-
|
125
|
-
|
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
|
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.
|
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
|
+
date: 2016-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ant Colony Optimizer Library
|
14
14
|
email:
|