Class: Tsp

Inherits:
Object
  • Object
show all
Defined in:
lib/gimuby/problem/tsp/tsp.rb

Overview

Implement a TSP problem

Instance Method Summary collapse

Constructor Details

#initializeTsp

Returns a new instance of Tsp.



6
7
8
# File 'lib/gimuby/problem/tsp/tsp.rb', line 6

def initialize
  ensure_distance_matrix
end

Instance Method Details

#get_distance(from, to) ⇒ Object



25
26
27
# File 'lib/gimuby/problem/tsp/tsp.rb', line 25

def get_distance(from, to)
  @distance_matrix[from][to]
end

#get_number_of_pointsObject



10
11
12
# File 'lib/gimuby/problem/tsp/tsp.rb', line 10

def get_number_of_points
  @distance_matrix.length
end

#get_permutation_distance(permutation) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/gimuby/problem/tsp/tsp.rb', line 14

def get_permutation_distance(permutation)
  previous = permutation[-1]
  distance = 0
  permutation.each do |current|
    marginal_distance = get_distance(previous, current)
    distance += marginal_distance
    previous = current
  end
  distance
end