class Tsp

Implement a TSP problem

Public Class Methods

new() click to toggle source
# File lib/gimuby/problem/tsp/tsp.rb, line 6
def initialize
  ensure_distance_matrix
end

Public Instance Methods

get_distance(from, to) click to toggle source
# File lib/gimuby/problem/tsp/tsp.rb, line 25
def get_distance(from, to)
  @distance_matrix[from][to]
end
get_number_of_points() click to toggle source
# File lib/gimuby/problem/tsp/tsp.rb, line 10
def get_number_of_points
  @distance_matrix.length
end
get_permutation_distance(permutation) click to toggle source
# 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

Protected Instance Methods

ensure_distance_matrix() click to toggle source
# File lib/gimuby/problem/tsp/tsp.rb, line 31
def ensure_distance_matrix
  path = $config.persistence_dir_path + '/TSP_distances_' +
      $config.tsp_number_points.to_s + '.data'
  load_distance_matrix(path)
  if @distance_matrix.nil?
    init_distance_matrix()
    persist_distance_matrix(path)
  end
end
get_random_distance() click to toggle source
# File lib/gimuby/problem/tsp/tsp.rb, line 71
def get_random_distance
  min_value = -10000
  max_value = 10000
  rand(max_value - min_value) + min_value
end
init_distance_matrix() click to toggle source
# File lib/gimuby/problem/tsp/tsp.rb, line 55
def init_distance_matrix
  @distance_matrix = []
  max_index = $config.tsp_number_points - 1
  (0..max_index).each do |city_index1|
    @distance_matrix[city_index1] = []
    (0..max_index).each do |city_index2|
      if city_index1 != city_index2
        distance = get_random_distance
      else
        distance = 0
      end
      @distance_matrix[city_index1][city_index2] = distance
    end
  end
end
load_distance_matrix(path) click to toggle source
# File lib/gimuby/problem/tsp/tsp.rb, line 41
def load_distance_matrix(path)
  if File::exists? path
    f = File.new(path, 'r')
    @distance_matrix = Marshal.load(f.read())
    f.close()
  end
end
persist_distance_matrix(path) click to toggle source
# File lib/gimuby/problem/tsp/tsp.rb, line 49
def persist_distance_matrix(path)
  f = File.new(path, 'w')
  f.write(Marshal.dump(@distance_matrix))
  f.close()
end