-
Notifications
You must be signed in to change notification settings - Fork 5
/
bidirectional_ch_n_to_n_test.go
203 lines (190 loc) · 5.92 KB
/
bidirectional_ch_n_to_n_test.go
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package ch
import (
"fmt"
"math"
"math/rand"
"testing"
)
func TestManyToManyShortestPath(t *testing.T) {
g := Graph{}
err := graphFromCSV(&g, "./data/pgrouting_osm.csv")
if err != nil {
t.Error(err)
return
}
t.Log("Please wait until contraction hierarchy is prepared")
g.PrepareContractionHierarchies()
t.Log("TestShortestPath is starting...")
u := []int64{106600, 106600, 69618}
v := []int64{5924, 81611, 69618, 68427, 68490}
correctAns := [][]float64{
{61089.42195558673, 94961.78959757874, 78692.8292369651, 61212.00481622628, 71101.1080090782},
{61089.42195558673, 94961.78959757874, 78692.8292369651, 61212.00481622628, 71101.1080090782},
{19135.6581215226, -2, -2, -2, -2},
}
correctPath := [][]int{
{418, 866, 591, 314, 353},
{418, 866, 591, 314, 353},
{160, -2, -2, -2, -2},
}
ans, path := g.ShortestPathManyToMany(u, v)
// t.Log("ShortestPathManyToMany returned", ans, path)
for sourceIdx := range u {
for targetIdx := range v {
if correctPath[sourceIdx][targetIdx] != -2 && len(path[sourceIdx][targetIdx]) != correctPath[sourceIdx][targetIdx] {
t.Errorf("Num of vertices in path should be %d, but got %d", correctPath[sourceIdx][targetIdx], len(path[sourceIdx][targetIdx]))
return
}
if correctAns[sourceIdx][targetIdx] != -2 && math.Abs(ans[sourceIdx][targetIdx]-correctAns[sourceIdx][targetIdx]) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctAns[sourceIdx][targetIdx], ans[sourceIdx][targetIdx])
return
}
}
}
t.Log("TestShortestPath is Ok!")
}
func BenchmarkShortestPathManyToMany(b *testing.B) {
b.Log("BenchmarkShortestPathManyToMany is starting...")
rand.Seed(1337)
for k := 2.0; k <= 8; k++ {
n := int(math.Pow(2, k))
g, err := generateSyntheticGraph(n)
if err != nil {
b.Error(err)
return
}
b.ResetTimer()
b.Run(fmt.Sprintf("%s/%d/vertices-%d-edges-%d-shortcuts-%d", "CH shortest path", n, len(g.Vertices), g.GetEdgesNum(), g.GetShortcutsNum()), func(b *testing.B) {
for i := 0; i < b.N; i++ {
u := []int64{int64(rand.Intn(len(g.Vertices)))}
v := []int64{
int64(rand.Intn(len(g.Vertices))),
int64(rand.Intn(len(g.Vertices))),
int64(rand.Intn(len(g.Vertices))),
int64(rand.Intn(len(g.Vertices))),
int64(rand.Intn(len(g.Vertices))),
}
ans, path := g.ShortestPathManyToMany(u, v)
_, _ = ans, path
}
})
}
}
func BenchmarkOldWayShortestPathManyToMany(b *testing.B) {
b.Log("BenchmarkOldWayShortestPathManyToMany is starting...")
rand.Seed(1337)
for k := 2.0; k <= 8; k++ {
n := int(math.Pow(2, k))
g, err := generateSyntheticGraph(n)
if err != nil {
b.Error(err)
return
}
b.ResetTimer()
b.Run(fmt.Sprintf("%s/%d/vertices-%d-edges-%d-shortcuts-%d", "CH shortest path", n, len(g.Vertices), g.GetEdgesNum(), g.GetShortcutsNum()), func(b *testing.B) {
for i := 0; i < b.N; i++ {
u := int64(rand.Intn(len(g.Vertices)))
v := []int64{
int64(rand.Intn(len(g.Vertices))),
int64(rand.Intn(len(g.Vertices))),
int64(rand.Intn(len(g.Vertices))),
int64(rand.Intn(len(g.Vertices))),
int64(rand.Intn(len(g.Vertices))),
}
for vv := range v {
ans, path := g.ShortestPath(u, v[vv])
_, _ = ans, path
}
}
})
}
}
func BenchmarkStaticCaseShortestPathManyToMany(b *testing.B) {
g := Graph{}
err := graphFromCSV(&g, "./data/pgrouting_osm.csv")
if err != nil {
b.Error(err)
}
b.Log("Please wait until contraction hierarchy is prepared")
g.PrepareContractionHierarchies()
b.Log("BenchmarkStaticCaseShortestPathManyToMany is starting...")
b.ResetTimer()
b.Run(fmt.Sprintf("%s/vertices-%d", "CH shortest path (many to many)", len(g.Vertices)), func(b *testing.B) {
for i := 0; i < b.N; i++ {
u := []int64{106600}
v := []int64{5924, 81611, 69618, 68427, 68490}
ans, path := g.ShortestPathManyToMany(u, v)
_, _ = ans, path
}
})
}
func BenchmarkStaticCaseOldWayShortestPathManyToMany(b *testing.B) {
g := Graph{}
err := graphFromCSV(&g, "data/pgrouting_osm.csv")
if err != nil {
b.Error(err)
}
b.Log("Please wait until contraction hierarchy is prepared")
g.PrepareContractionHierarchies()
b.Log("BenchmarkStaticCaseOldWayShortestPathManyToMany is starting...")
b.ResetTimer()
b.Run(fmt.Sprintf("%s/vertices-%d", "CH shortest path (many to many)", len(g.Vertices)), func(b *testing.B) {
for i := 0; i < b.N; i++ {
u := int64(106600)
v := []int64{5924, 81611, 69618, 68427, 68490}
for vv := range v {
ans, path := g.ShortestPath(u, v[vv])
_, _ = ans, path
}
}
})
}
func TestManyToManyAlternatives(t *testing.T) {
// S-(1)-0-(1)-1-(1)-2
// | | | |
// (2) (1) (2) (2)
// | | | |
// 3-(1)-4-(1)-5-(1)-T
g := Graph{}
g.CreateVertex(0)
g.CreateVertex(1)
g.CreateVertex(2)
g.CreateVertex(3)
g.CreateVertex(4)
g.CreateVertex(5)
g.CreateVertex(6)
g.AddEdge(0, 1, 1.0)
g.AddEdge(0, 4, 1.0)
g.AddEdge(1, 2, 1.0)
g.AddEdge(1, 5, 2.0)
g.AddEdge(3, 4, 1.0)
g.AddEdge(4, 5, 1.0)
expectedPath := []int64{0, 4, 5}
g.PrepareContractionHierarchies()
t.Log("TestManyToManyAlternatives is starting...")
sources := []VertexAlternative{
{Label: 0, AdditionalDistance: 1.0},
{Label: 3, AdditionalDistance: 2.0},
}
targets := []VertexAlternative{
{Label: 2, AdditionalDistance: 2.0},
{Label: 5, AdditionalDistance: 1.0},
}
ans, paths := g.ShortestPathManyToManyWithAlternatives([][]VertexAlternative{sources}, [][]VertexAlternative{targets})
t.Log("ShortestPathManyToManyWithAlternatives returned", ans, paths)
path := paths[0][0]
if len(path) != len(expectedPath) {
t.Errorf("Num of vertices in path should be %d, but got %d", len(expectedPath), len(path))
}
for i := range expectedPath {
if path[i] != expectedPath[i] {
t.Errorf("Path item %d should be %d, but got %d", i, expectedPath[i], path[i])
}
}
correctCost := 4.0
if math.Abs(ans[0][0]-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans[0][0])
return
}
t.Log("TestManyToManyAlternatives is Ok!")
}