-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleo.rb
120 lines (88 loc) · 2.99 KB
/
leo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'hashie'
require 'terminal-table'
require 'benchmark'
require 'parallel'
require 'thor'
require_relative './station_loader.rb'
require_relative './planner.rb'
# station_hash.each do |k, station|
# puts "#{k} #{station.name} -> #{station.connecting_station_id}"
# end
stations = StationLoader.new(file: "stations.csv").load.station_hash
planner = Planner.set(stations: stations)
# loop do
# puts 'Elige actividad a realizar (ruta/matriz)'
# option = ARGF.readline.strip
# next calculate_route(station_names:) if option == 'ruta'
# next calculate_matrices if option == 'matriz'
# puts 'Continuar (si/no)'
# break if ARGF.readline.strip == 'no'
# end
# ARGF.readline
# ARGF.readline
# res.each do |matrix|
# matrix.each do |route, result|
# puts "#{route}: #{result.stations.reverse.map(&:id).join(',')}"
# end
# ARGF.readline
# end
class Runner < Thor
desc 'benchmark', 'benchmarks all matrix calculations'
def benchmark
res = []
Benchmark.bm do |benchmark|
benchmark.report('processes') { res << calculate_matrix_parallel_processes }
benchmark.report('threads') { res << calculate_matrix_parallel_threads }
benchmark.report('inline') { res << calculate_matrix }
end
res
end
desc 'calculate', 'calculates best route between stations'
def calculate_route(origin, destination)
station_names = StationLoader.new(file: "./stations.csv").load.station_names
puts station_names
puts 'Estación de Origen: '
origin = origin.strip
puts station_names[origin]
raise if station_names[origin].nil?
puts 'Estación de Destino: '
destination = destination.strip
puts station_names[destination]
raise if station_names[destination].nil?
res = Planner.get.travel(from: origin.to_s, to: destination.to_s)
puts res.stations.reverse.map { |s| "#{s.id} #{s.name} (#{s.line})"}.join("\n")
rescue StandardError => e
puts e.inspect
end
private
def prepare(filter: 0.04)
random = Random.new(100)
cropped_stations = stations.keys.select { |k| random.rand > (1 - filter) }
crop_station_zip = []
cropped_stations.each { |o| cropped_stations.each { |d| crop_station_zip << [o, d] } }
puts "#{crop_station_zip.length} stations tuples loaded for test"
@crop_station_zip
end
def calculate_matrix_parallel_threads
tuples = @crop_station_zip
Parallel.map(tuples, in_threads: 12) do |from, to|
["#{from}->#{to}", Planner.get.travel(from:, to:)]
end.to_h
end
def calculate_matrix_parallel_processes
tuples = @crop_station_zip
Parallel.map(tuples, in_processes: 12) do |from, to|
# puts "#{from} -> #{to}"
["#{from}->#{to}", Planner.get.travel(from:, to:)]
end.to_h
end
def calculate_matrix
distance_matrix = {}
@crop_station_zip.each do |from, to|
result = Planner.get.travel(from:, to:)
# puts "#{from} -> #{to} = #{result.cost}"
distance_matrix["#{from}->#{to}"] = result
end
distance_matrix
end
end