forked from iwky911/onedayinparis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.rb
164 lines (134 loc) · 3.61 KB
/
route.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require 'csv'
require 'fc'
require 'pry'
def read_edges
edges = []
CSV.open("edges.csv").each do |csv|
edges << Edge.new(csv[0], csv[1], csv[2].to_f, csv[3])
end
Graph.new(edges)
end
Edge = Struct.new(:source, :destination, :cost, :line)
class Edge2
attr_accessor :source, :destination, :cost, :line
def initialize
@line = []
end
end
class Graph
attr_reader :edge_table, :max_id
def initialize(edges)
@edges = edges
build_edge_table
end
def find_edges(node)
@edge_table[node]
end
def build_edge_table
builder = Hash.new { |h, k| h[k] = Edge2.new }
@max_id = 0
@edges.each do |e|
builder["#{e.source}-#{e.destination}"].source = e.source
builder["#{e.source}-#{e.destination}"].destination = e.destination
builder["#{e.source}-#{e.destination}"].cost = e.cost
builder["#{e.source}-#{e.destination}"].line << e.line
@max_id = e.source.to_i if @max_id < e.source.to_i
@max_id = e.destination.to_i if @max_id < e.destination.to_i
end
@edge_table = Hash.new { |h, k| h[k] = [] }
# binding.pry
builder.each do |_, e|
@edge_table[e.source] << e
end
end
end
class Route
attr_reader :cost, :path
def intitialize(cost:, path:)
@cost = cost
@path = path
end
end
# dijkstra(read_edges, "1", "260")
# crazy example, it's cheaper to swith to line 6 than to go straight from 1 to 12
# should go straight from line 1 to line 12
# at node 93, there are two paths with the same cost to get there
# we end up needing the other path
# to carry forward with the lowest cost
def dijkstra(graph, source, destination)
dist = {}
dist[source] = 0
visited = {}
path = {}
q = FastContainers::PriorityQueue.new(:min)
q.push(source, 0)
path[source] = []
q.pop_each do |point, cost|
if point == destination
return [cost, path[point]]
end
if visited.key?(point) && cost >= dist[point]
next
end
visited[point] = 1
dist[point] = cost
current_path = path[point]
graph.find_edges(point).map do |next_e|
path[next_e.destination] = current_path + [next_e]
cp_line = source != point ? current_path.last.line : []
line_switched = source != point && (cp_line & next_e.line).length == 0
line_switch = line_switched ? 15 : 0
q.push(next_e.destination, next_e.cost + line_switch + cost)
end
end
return nil
end
def dijkstra_all(graph, source)
dist = {}
dist[source] = 0
visited = {}
path = {}
q = FastContainers::PriorityQueue.new(:min)
q.push(source, 0)
path[source] = []
q.pop_each do |point, cost|
if visited.key?(point) && cost >= dist[point]
next
end
visited[point] = 1
dist[point] = cost
current_path = path[point]
graph.find_edges(point).map do |next_e|
path[next_e.destination] = current_path + [next_e]
cp_line = source != point ? current_path.last.line : []
line_switched = source != point && (cp_line & next_e.line).length == 0
line_switch = line_switched ? 15 : 0
q.push(next_e.destination, next_e.cost + line_switch + cost)
end
end
return [dist, path]
end
def print_q(q)
q.each { |z, c| puts "#{z}=>#{c}" }
end
def run
full_matrix = []
graph = read_edges
0.upto(graph.max_id).each do |source|
source = source.to_s
dist, path = dijkstra_all(read_edges, source)
matrix_row = []
0.upto(graph.max_id).map do |dest|
dest = dest.to_s
matrix_row << dist[dest].to_i || 100000
end
full_matrix << matrix_row
end
CSV.open("full_matrix.csv", "w") do |csv|
full_matrix.each do |r|
csv << r
end
end
"done"
end
run