or-tools 0.1.4 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/README.md +430 -195
- data/ext/or-tools/ext.cpp +5 -0
- data/ext/or-tools/extconf.rb +21 -63
- data/ext/or-tools/vendor.rb +98 -0
- data/lib/or-tools.rb +6 -0
- data/lib/or_tools/basic_scheduler.rb +86 -0
- data/lib/or_tools/cp_solver.rb +5 -1
- data/lib/or_tools/seating.rb +115 -0
- data/lib/or_tools/sudoku.rb +132 -0
- data/lib/or_tools/tsp.rb +60 -0
- data/lib/or_tools/version.rb +1 -1
- metadata +7 -3
- data/lib/or_tools/ext.bundle +0 -0
data/lib/or_tools/tsp.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module ORTools
|
2
|
+
class TSP
|
3
|
+
attr_reader :route, :route_indexes, :distances, :total_distance
|
4
|
+
|
5
|
+
DISTANCE_SCALE = 1000
|
6
|
+
DEGREES_TO_RADIANS = Math::PI / 180
|
7
|
+
|
8
|
+
def initialize(locations)
|
9
|
+
raise ArgumentError, "Locations must have latitude and longitude" unless locations.all? { |l| l[:latitude] && l[:longitude] }
|
10
|
+
raise ArgumentError, "Latitude must be between -90 and 90" unless locations.all? { |l| l[:latitude] >= -90 && l[:latitude] <= 90 }
|
11
|
+
raise ArgumentError, "Longitude must be between -180 and 180" unless locations.all? { |l| l[:longitude] >= -180 && l[:longitude] <= 180 }
|
12
|
+
raise ArgumentError, "Must be at least two locations" unless locations.size >= 2
|
13
|
+
|
14
|
+
distance_matrix =
|
15
|
+
locations.map do |from|
|
16
|
+
locations.map do |to|
|
17
|
+
# must be integers
|
18
|
+
(distance(from, to) * DISTANCE_SCALE).to_i
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
manager = ORTools::RoutingIndexManager.new(locations.size, 1, 0)
|
23
|
+
routing = ORTools::RoutingModel.new(manager)
|
24
|
+
|
25
|
+
distance_callback = lambda do |from_index, to_index|
|
26
|
+
from_node = manager.index_to_node(from_index)
|
27
|
+
to_node = manager.index_to_node(to_index)
|
28
|
+
distance_matrix[from_node][to_node]
|
29
|
+
end
|
30
|
+
|
31
|
+
transit_callback_index = routing.register_transit_callback(distance_callback)
|
32
|
+
routing.set_arc_cost_evaluator_of_all_vehicles(transit_callback_index)
|
33
|
+
assignment = routing.solve(first_solution_strategy: :path_cheapest_arc)
|
34
|
+
|
35
|
+
@route_indexes = []
|
36
|
+
@distances = []
|
37
|
+
|
38
|
+
index = routing.start(0)
|
39
|
+
while !routing.end?(index)
|
40
|
+
@route_indexes << manager.index_to_node(index)
|
41
|
+
previous_index = index
|
42
|
+
index = assignment.value(routing.next_var(index))
|
43
|
+
@distances << routing.arc_cost_for_vehicle(previous_index, index, 0) / DISTANCE_SCALE.to_f
|
44
|
+
end
|
45
|
+
@route_indexes << manager.index_to_node(index)
|
46
|
+
@route = locations.values_at(*@route_indexes)
|
47
|
+
@total_distance = @distances.sum
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def distance(from, to)
|
53
|
+
from_lat = from[:latitude] * DEGREES_TO_RADIANS
|
54
|
+
from_lng = from[:longitude] * DEGREES_TO_RADIANS
|
55
|
+
to_lat = to[:latitude] * DEGREES_TO_RADIANS
|
56
|
+
to_lng = to[:longitude] * DEGREES_TO_RADIANS
|
57
|
+
2 * 6371 * Math.asin(Math.sqrt(Math.sin((to_lat - from_lat) / 2.0)**2 + Math.cos(from_lat) * Math.cos(to_lat) * Math.sin((from_lng - to_lng) / 2.0)**2))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/or_tools/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: or-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-04
|
11
|
+
date: 2020-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rice
|
@@ -93,21 +93,25 @@ files:
|
|
93
93
|
- README.md
|
94
94
|
- ext/or-tools/ext.cpp
|
95
95
|
- ext/or-tools/extconf.rb
|
96
|
+
- ext/or-tools/vendor.rb
|
96
97
|
- lib/or-tools.rb
|
98
|
+
- lib/or_tools/basic_scheduler.rb
|
97
99
|
- lib/or_tools/bool_var.rb
|
98
100
|
- lib/or_tools/comparison.rb
|
99
101
|
- lib/or_tools/comparison_operators.rb
|
100
102
|
- lib/or_tools/cp_model.rb
|
101
103
|
- lib/or_tools/cp_solver.rb
|
102
104
|
- lib/or_tools/cp_solver_solution_callback.rb
|
103
|
-
- lib/or_tools/ext.bundle
|
104
105
|
- lib/or_tools/int_var.rb
|
105
106
|
- lib/or_tools/knapsack_solver.rb
|
106
107
|
- lib/or_tools/linear_expr.rb
|
107
108
|
- lib/or_tools/routing_model.rb
|
108
109
|
- lib/or_tools/sat_int_var.rb
|
109
110
|
- lib/or_tools/sat_linear_expr.rb
|
111
|
+
- lib/or_tools/seating.rb
|
110
112
|
- lib/or_tools/solver.rb
|
113
|
+
- lib/or_tools/sudoku.rb
|
114
|
+
- lib/or_tools/tsp.rb
|
111
115
|
- lib/or_tools/version.rb
|
112
116
|
homepage: https://github.com/ankane/or-tools
|
113
117
|
licenses:
|
data/lib/or_tools/ext.bundle
DELETED
Binary file
|