File: bench_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (192 lines) | stat: -rw-r--r-- 5,366 bytes parent folder | download
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
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package path

import (
	"fmt"
	"sync"
	"testing"

	"gonum.org/v1/gonum/graph"
	"gonum.org/v1/gonum/graph/graphs/gen"
	"gonum.org/v1/gonum/graph/simple"
	"gonum.org/v1/gonum/graph/traverse"
)

var (
	gnpUndirected_10_tenth   = gnpUndirected(10, 0.1)
	gnpUndirected_100_tenth  = gnpUndirected(100, 0.1)
	gnpUndirected_1000_tenth = gnpUndirected(1000, 0.1)
	gnpUndirected_10_half    = gnpUndirected(10, 0.5)
	gnpUndirected_100_half   = gnpUndirected(100, 0.5)
	gnpUndirected_1000_half  = gnpUndirected(1000, 0.5)

	nswUndirected_10_2_2_2   = navigableSmallWorldUndirected(10, 2, 2, 2)
	nswUndirected_10_2_5_2   = navigableSmallWorldUndirected(10, 2, 5, 2)
	nswUndirected_100_5_10_2 = navigableSmallWorldUndirected(100, 5, 10, 2)
	nswUndirected_100_5_20_2 = navigableSmallWorldUndirected(100, 5, 20, 2)
)

func gnpUndirected(n int, p float64) func() graph.Undirected {
	var once sync.Once
	var cache graph.Undirected
	return func() graph.Undirected {
		once.Do(func() {
			g := simple.NewUndirectedGraph()
			err := gen.Gnp(g, n, p, nil)
			if err != nil {
				panic(fmt.Sprintf("path: bad test: %v", err))
			}
			cache = g
		})
		return cache
	}
}

func navigableSmallWorldUndirected(n, p, q int, r float64) func() graph.Undirected {
	var once sync.Once
	var cache graph.Undirected
	return func() graph.Undirected {
		once.Do(func() {
			g := simple.NewUndirectedGraph()
			err := gen.NavigableSmallWorld(g, []int{n, n}, p, q, r, nil)
			if err != nil {
				panic(fmt.Sprintf("path: bad test: %v", err))
			}
			cache = g
		})
		return cache
	}
}

func manhattan(size int) func(x, y graph.Node) float64 {
	return func(x, y graph.Node) float64 {
		return manhattanBetween(coordinatesForID(x, size, size), coordinatesForID(y, size, size))
	}
}

func coordinatesForID(n graph.Node, c, r int) [2]int {
	id := n.ID()
	if id >= int64(c*r) {
		panic("out of range")
	}
	return [2]int{int(id) / r, int(id) % r}
}

// manhattanBetween returns the Manhattan distance between a and b.
func manhattanBetween(a, b [2]int) float64 {
	var d int
	for i, v := range a {
		d += abs(v - b[i])
	}
	return float64(d)
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func BenchmarkAStarUndirected(b *testing.B) {
	benchmarks := []struct {
		name  string
		graph graph.Undirected
		h     Heuristic
	}{
		{"GNP Undirected 10 tenth", gnpUndirected_10_tenth(), nil},
		{"GNP Undirected 100 tenth", gnpUndirected_100_tenth(), nil},
		{"GNP Undirected 1000 tenth", gnpUndirected_1000_tenth(), nil},
		{"GNP Undirected 10 half", gnpUndirected_10_half(), nil},
		{"GNP Undirected 100 half", gnpUndirected_100_half(), nil},
		{"GNP Undirected 1000 half", gnpUndirected_1000_half(), nil},

		{"NSW Undirected 10 2 2 2", nswUndirected_10_2_2_2(), nil},
		{"NSW Undirected 10 2 2 2 heuristic", nswUndirected_10_2_2_2(), manhattan(10)},
		{"NSW Undirected 10 2 5 2", nswUndirected_10_2_5_2(), nil},
		{"NSW Undirected 10 2 5 2 heuristic", nswUndirected_10_2_5_2(), manhattan(10)},
		{"NSW Undirected 100 5 10 2", nswUndirected_100_5_10_2(), nil},
		{"NSW Undirected 100 5 10 2 heuristic", nswUndirected_100_5_10_2(), manhattan(100)},
		{"NSW Undirected 100 5 20 2", nswUndirected_100_5_20_2(), nil},
		{"NSW Undirected 100 5 20 2 heuristic", nswUndirected_100_5_20_2(), manhattan(100)},
	}

	for _, bm := range benchmarks {
		b.Run(bm.name, func(b *testing.B) {
			var expanded int
			for i := 0; i < b.N; i++ {
				_, expanded = AStar(simple.Node(0), simple.Node(1), bm.graph, bm.h)
			}
			if expanded == 0 {
				b.Fatal("unexpected number of expanded nodes")
			}
		})
	}
}

var (
	gnpDirected_500_tenth  = gnpDirected(500, 0.1)
	gnpDirected_1000_tenth = gnpDirected(1000, 0.1)
	gnpDirected_2000_tenth = gnpDirected(2000, 0.1)
	gnpDirected_500_half   = gnpDirected(500, 0.5)
	gnpDirected_1000_half  = gnpDirected(1000, 0.5)
	gnpDirected_2000_half  = gnpDirected(2000, 0.5)
	gnpDirected_500_full   = gnpDirected(500, 1)
	gnpDirected_1000_full  = gnpDirected(1000, 1)
	gnpDirected_2000_full  = gnpDirected(2000, 1)
)

func gnpDirected(n int, p float64) func() graph.Directed {
	var once sync.Once
	var cache graph.Directed
	return func() graph.Directed {
		once.Do(func() {
			g := simple.NewDirectedGraph()
			err := gen.Gnp(g, n, p, nil)
			if err != nil {
				panic(fmt.Sprintf("path: bad test: %v", err))
			}
			cache = g
		})
		return cache
	}
}

func BenchmarkBellmanFordFrom(b *testing.B) {
	benchmarks := []struct {
		name  string
		graph graph.Directed
	}{
		{"500 tenth", gnpDirected_500_tenth()},
		{"1000 tenth", gnpDirected_1000_tenth()},
		{"2000 tenth", gnpDirected_2000_tenth()},
		{"500 half", gnpDirected_500_half()},
		{"1000 half", gnpDirected_1000_half()},
		{"2000 half", gnpDirected_2000_half()},
		{"500 full", gnpDirected_500_full()},
		{"1000 full", gnpDirected_1000_full()},
		{"2000 full", gnpDirected_2000_full()},
	}

	type incremental struct {
		traverse.Graph
	}
	for _, bm := range benchmarks {
		for _, tg := range []struct {
			typ string
			g   traverse.Graph
		}{
			{g: bm.graph},
			{typ: " incremental", g: incremental{bm.graph}},
		} {
			b.Run(bm.name+tg.typ, func(b *testing.B) {
				for i := 0; i < b.N; i++ {
					BellmanFordFrom(bm.graph.Node(0), tg.g)
				}
			})
		}
	}
}