Skip to content

Commit 6f17809

Browse files
bibengabibenga
authored andcommitted
updates example
1 parent 038fe6f commit 6f17809

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

examples/tsp.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99

1010
module TSP
1111
def self.solve
12+
nodes = make_nodes(COORDS)
1213
dists = make_dists(COORDS)
1314

14-
nodes = Array.new(COORDS.length)
15-
COORDS.each_with_index do |_, i|
16-
nodes[i] = i
17-
end
18-
nodes = nodes.freeze
19-
2015
solver = ALNS::Solver.new(Random.new(1234))
2116

2217
init_sol = TspState.new(nodes, {}, dists)
@@ -220,17 +215,25 @@ def self.operator_stats_to_s(counter)
220215
[107, 27]
221216
].freeze
222217

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+
223226
def self.make_dists(coords)
224227
n = coords.length
225-
m = Array.new(n, 0)
228+
dists = Array.new(n, 0)
226229
coords.each_with_index do |coord1, row|
227-
m[row] = Array.new(n, 0)
230+
dists_row = Array.new(n, 0)
228231
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])
230233
end
231-
m[row] = m[row].freeze
234+
dists[row] = dists_row.freeze
232235
end
233-
m.freeze
236+
dists.freeze
234237
end
235238

236239
def self.euclidean(x1, y1, x2, y2) # rubocop:disable Naming/MethodParameterName

0 commit comments

Comments
 (0)