tsp_runner 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4de20bb835ad9abb7ca9b8200d3900881d9445d9
4
+ data.tar.gz: 4b8b15b499659263bf014641bc2f8b855cc2572d
5
+ SHA512:
6
+ metadata.gz: deba5646b3df24fca5c236dd821e766ac71b84a64522d622157299b59b64bbdc416f36df6c2bddffb4603b567cc2d3da309db9164e1f9a53138d6600a2113fa0
7
+ data.tar.gz: 5667cf34f1385ad59ddb692270ad743af61b569462949cedec938231750436e1083222e8e6714ccc91da3db8a2780b77039946ac6c2892014c6d4cce929e3545
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require 'spec_helper'
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tsp_runner.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Tom Collier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # TspRunner
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tsp_runner`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tsp_runner'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tsp_runner
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/tsp_runner/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'tsp_runner'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ Philadelphia,40.00763286606546,-75.13417473522492
2
+ San Antonio,29.472503828830956,-98.52479802769332
3
+ San Diego,32.83530987648001,-117.11948127009282
4
+ Dallas,32.79406551342133,-96.76562905345834
5
+ San Francisco,37.75624104468897,-122.44148308888244
6
+ Milwaukee,43.0642012492431,-87.96704550921461
7
+ Tampa,27.99679690746927,-82.44546311098391
8
+ Kansas City,39.123770613135896,-94.5541998664069
9
+ Cleveland,41.47718512840605,-81.68089809554883
10
+ New Orleans,30.068751467455936,-89.93079810833594
@@ -0,0 +1,10 @@
1
+ San Francisco
2
+ Philadelphia
3
+ San Antonio
4
+ San Diego
5
+ Dallas
6
+ Milwaukee
7
+ Tampa
8
+ Kansas City
9
+ Cleveland
10
+ New Orleans
data/exe/tsp_runner ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup'
3
+ require 'tsp_runner'
4
+
5
+ filename = File.join(File.dirname(__FILE__), '..', 'data', 'input', 'sample_10.csv')
6
+
7
+ begin
8
+ runner = TspRunner::Runner.new(ARGV.join(' '), filename)
9
+ runner.run(5)
10
+ distance = runner.validate!('San Francisco')
11
+ $stdout.puts("Success: #{distance} km")
12
+ rescue TspRunner::Runner::BaseError => error
13
+ $stderr.puts error.message
14
+ exit(1)
15
+ end
@@ -0,0 +1,49 @@
1
+ module TspRunner
2
+ class Location
3
+ RADIANS_PER_DEGREE = Math::PI / 180
4
+ EARTH_RADIUS = 6371 * 1000 # in meters
5
+
6
+ attr_reader :name, :lat, :lon
7
+
8
+ # @param name [String] the name of the location
9
+ # @param lat [Fixnum] the latitude
10
+ # @param lon [Fixnum] the longitude
11
+ def initialize(name, lat, lon)
12
+ unless lat.in?(-90..90)
13
+ raise ArgumentError, 'Latitudue should be between -90 and 90'
14
+ end
15
+
16
+ unless lon.in?(-180..180)
17
+ raise ArgumentError, 'Longitudue should be between -180 and 180'
18
+ end
19
+
20
+ @name, @lat, @lon = name, lat, lon
21
+ end
22
+
23
+ # @return [Fixnum] the latitude in radians
24
+ def lat_rad
25
+ lat * RADIANS_PER_DEGREE
26
+ end
27
+
28
+ # @return [Fixnum] the longitude in radians
29
+ def lon_rad
30
+ lon * RADIANS_PER_DEGREE
31
+ end
32
+
33
+ # Gleefully stolen from:
34
+ # http://stackoverflow.com/questions/12966638/how-to-calculate-the-distance-between-two-gps-coordinates-without-using-google-m
35
+ #
36
+ # @return [Fixnum] the Haversine distance of the location (from self) as
37
+ # measured in meters
38
+ def distance_from(location)
39
+ delta_lat = location.lat_rad - lat_rad
40
+ delta_lon = location.lon_rad - lon_rad
41
+
42
+ a = Math.sin(delta_lat / 2) ** 2 + Math.cos(lat_rad) *
43
+ Math.cos(location.lat_rad) * Math.sin(delta_lon / 2) ** 2
44
+ c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1 - a))
45
+
46
+ EARTH_RADIUS * c
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,30 @@
1
+ module TspRunner
2
+ class LocationHash
3
+ attr_reader :locations
4
+
5
+ def self.from_file(filename)
6
+ new.tap do |location_hash|
7
+ File.open(filename).each do |line|
8
+ name, lat_str, lon_str = *line.chomp.split(',')
9
+ location_hash << Location.new(name, Float(lat_str), Float(lon_str))
10
+ end
11
+ end
12
+ end
13
+
14
+ def initialize
15
+ @locations = {}
16
+ end
17
+
18
+ def <<(location)
19
+ locations[location.name] = location
20
+ end
21
+
22
+ def [](location_name)
23
+ locations[location_name]
24
+ end
25
+
26
+ def location_names
27
+ locations.keys
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ require 'timeout'
2
+
3
+ module TspRunner
4
+ class Runner
5
+ class BaseError < StandardError; end
6
+ class TimeoutError < BaseError; end
7
+ class InvalidError < BaseError; end
8
+
9
+ attr_reader :cmd, :input_filename, :output
10
+
11
+ def initialize(cmd, input_filename)
12
+ @cmd, @input_filename = cmd, input_filename
13
+ end
14
+
15
+ def run(time_limit)
16
+ @output = Timeout.timeout(time_limit) { `#{cmd} #{input_filename}` }
17
+ rescue Timeout::Error
18
+ raise TimeoutError, 'Failed: timeout'
19
+ end
20
+
21
+ def validate!(initial_location_name = nil)
22
+ solution = TspRunner::Solution.from_string(output, location_hash)
23
+ if solution.valid?(initial_location_name)
24
+ solution.total_distance / 1000.0
25
+ else
26
+ raise InvalidError, ['Failed: invalid', output].join("\n")
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def location_hash
33
+ @location_hash ||= TspRunner::LocationHash.from_file(input_filename)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,49 @@
1
+ module TspRunner
2
+ class Solution
3
+ attr_reader :location_hash, :location_names
4
+
5
+ def self.from_file(filename, location_hash)
6
+ new(location_hash).tap do |solution|
7
+ File.open(filename).each do |line|
8
+ solution << line.chomp
9
+ end
10
+ end
11
+ end
12
+
13
+ def self.from_string(str, location_hash)
14
+ new(location_hash).tap do |solution|
15
+ str.split("\n").each do |line|
16
+ solution << line.chomp
17
+ end
18
+ end
19
+ end
20
+
21
+ def initialize(location_hash)
22
+ @location_hash = location_hash
23
+ @location_names = []
24
+ end
25
+
26
+ def <<(location_name)
27
+ location_names << location_name
28
+ end
29
+
30
+ def valid?(initial_location_name = nil)
31
+ if initial_location_name
32
+ return false if initial_location_name != location_names.first
33
+ end
34
+ location_hash.location_names.sort == location_names.sort
35
+ end
36
+
37
+ def total_distance
38
+ distance = 0
39
+ location_names.each.with_index do |location_name, index|
40
+ location = location_hash[location_name]
41
+ next_index = (index + 1) % location_names.length
42
+ next_location_name = location_names[next_index]
43
+ next_location = location_hash[next_location_name]
44
+ distance += location.distance_from(next_location)
45
+ end
46
+ distance
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module TspRunner
2
+ VERSION = "0.1.0"
3
+ end
data/lib/tsp_runner.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'active_support/core_ext/object/inclusion'
2
+
3
+ require 'tsp_runner/location'
4
+ require 'tsp_runner/location_hash'
5
+ require 'tsp_runner/runner'
6
+ require 'tsp_runner/solution'
7
+ require 'tsp_runner/version'
8
+
9
+ module TspRunner
10
+ # https://gist.github.com/tcollier/3cb895f019b7c3729ba7
11
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tsp_runner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'tsp_runner'
8
+ spec.version = TspRunner::VERSION
9
+ spec.authors = ['Tom Collier']
10
+ spec.email = ['collier@apartmentlist.com']
11
+
12
+ spec.summary = %q{Script to run a TSP solver and validate/measure its output.}
13
+ spec.description = %{qScript to run a TSP solver and validate/measure its output.}
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'activesupport'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.8'
24
+ spec.add_development_dependency 'factory_girl'
25
+ spec.add_development_dependency 'faker'
26
+ spec.add_development_dependency 'mocha'
27
+ spec.add_development_dependency 'pry', '~> 0.10'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tsp_runner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Collier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_girl
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faker
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ description: qScript to run a TSP solver and validate/measure its output.
112
+ email:
113
+ - collier@apartmentlist.com
114
+ executables:
115
+ - tsp_runner
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - data/input/sample_10.csv
130
+ - data/solutions/solution_10_a.txt
131
+ - exe/tsp_runner
132
+ - lib/tsp_runner.rb
133
+ - lib/tsp_runner/location.rb
134
+ - lib/tsp_runner/location_hash.rb
135
+ - lib/tsp_runner/runner.rb
136
+ - lib/tsp_runner/solution.rb
137
+ - lib/tsp_runner/version.rb
138
+ - tsp_runner.gemspec
139
+ homepage:
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.4.5
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Script to run a TSP solver and validate/measure its output.
163
+ test_files: []
164
+ has_rdoc: