class TspRunner::Solution
Attributes
location_hash[R]
location_names[R]
Public Class Methods
from_file(filename, location_hash)
click to toggle source
# File lib/tsp_runner/solution.rb, line 5 def self.from_file(filename, location_hash) new(location_hash).tap do |solution| File.open(filename).each do |line| solution << line.chomp end end end
from_string(str, location_hash)
click to toggle source
# File lib/tsp_runner/solution.rb, line 13 def self.from_string(str, location_hash) new(location_hash).tap do |solution| str.split("\n").each do |line| solution << line.chomp end end end
new(location_hash)
click to toggle source
# File lib/tsp_runner/solution.rb, line 21 def initialize(location_hash) @location_hash = location_hash @location_names = [] end
Public Instance Methods
<<(location_name)
click to toggle source
# File lib/tsp_runner/solution.rb, line 26 def <<(location_name) location_names << location_name end
total_distance()
click to toggle source
# File lib/tsp_runner/solution.rb, line 37 def total_distance distance = 0 location_names.each.with_index do |location_name, index| location = location_hash[location_name] next_index = (index + 1) % location_names.length next_location_name = location_names[next_index] next_location = location_hash[next_location_name] distance += location.distance_from(next_location) end distance end
valid?(initial_location_name = nil)
click to toggle source
# File lib/tsp_runner/solution.rb, line 30 def valid?(initial_location_name = nil) if initial_location_name return false if initial_location_name != location_names.first end location_hash.location_names.sort == location_names.sort end