Sha256: 05912e13973b7975863446a2b060b37b19f4b934027f12859a9578afb708558b

Contents?: true

Size: 932 Bytes

Versions: 1

Compression:

Stored size: 932 Bytes

Contents

require 'timeout'

module TspRunner
  class Runner
    class BaseError < StandardError; end
    class TimeoutError < BaseError; end
    class InvalidError < BaseError; end

    attr_reader :cmd, :input_filename, :output

    def initialize(cmd, input_filename)
      @cmd, @input_filename  = cmd, input_filename
    end

    def run(time_limit)
      @output = Timeout.timeout(time_limit) { `#{cmd} #{input_filename}` }
    rescue Timeout::Error
      raise TimeoutError, 'Failed: timeout'
    end

    def validate!(initial_location_name = nil)
      solution = TspRunner::Solution.from_string(output, location_hash)
      if solution.valid?(initial_location_name)
        solution.total_distance / 1000.0
      else
        raise InvalidError, ['Failed: invalid', output].join("\n")
      end
    end

    private

    def location_hash
      @location_hash ||= TspRunner::LocationHash.from_file(input_filename)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tsp_runner-0.1.0 lib/tsp_runner/runner.rb