|
9 | 9 |
|
10 | 10 | module TSP |
11 | 11 | def self.solve |
| 12 | + nodes = make_nodes(COORDS) |
12 | 13 | dists = make_dists(COORDS) |
13 | 14 |
|
14 | | - nodes = Array.new(COORDS.length) |
15 | | - COORDS.each_with_index do |_, i| |
16 | | - nodes[i] = i |
17 | | - end |
18 | | - nodes = nodes.freeze |
19 | | - |
20 | 15 | solver = ALNS::Solver.new(Random.new(1234)) |
21 | 16 |
|
22 | 17 | init_sol = TspState.new(nodes, {}, dists) |
@@ -220,17 +215,25 @@ def self.operator_stats_to_s(counter) |
220 | 215 | [107, 27] |
221 | 216 | ].freeze |
222 | 217 |
|
| 218 | + def self.make_nodes(coords) |
| 219 | + nodes = Array.new(coords.length) |
| 220 | + coords.each_with_index do |_, i| |
| 221 | + nodes[i] = i |
| 222 | + end |
| 223 | + nodes.freeze |
| 224 | + end |
| 225 | + |
223 | 226 | def self.make_dists(coords) |
224 | 227 | n = coords.length |
225 | | - m = Array.new(n, 0) |
| 228 | + dists = Array.new(n, 0) |
226 | 229 | coords.each_with_index do |coord1, row| |
227 | | - m[row] = Array.new(n, 0) |
| 230 | + dists_row = Array.new(n, 0) |
228 | 231 | coords.each_with_index do |coord2, col| |
229 | | - m[row][col] = euclidean(coord1[0], coord1[1], coord2[0], coord2[1]) |
| 232 | + dists_row[col] = euclidean(coord1[0], coord1[1], coord2[0], coord2[1]) |
230 | 233 | end |
231 | | - m[row] = m[row].freeze |
| 234 | + dists[row] = dists_row.freeze |
232 | 235 | end |
233 | | - m.freeze |
| 236 | + dists.freeze |
234 | 237 | end |
235 | 238 |
|
236 | 239 | def self.euclidean(x1, y1, x2, y2) # rubocop:disable Naming/MethodParameterName |
|
0 commit comments