mirror of
https://github.com/gonum/gonum.git
synced 2025-10-06 07:37:03 +08:00
185 lines
5.4 KiB
Go
185 lines
5.4 KiB
Go
// 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 (
|
|
"testing"
|
|
|
|
"gonum.org/v1/gonum/graph"
|
|
"gonum.org/v1/gonum/graph/graphs/gen"
|
|
"gonum.org/v1/gonum/graph/simple"
|
|
)
|
|
|
|
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)
|
|
)
|
|
|
|
func gnpUndirected(n int, p float64) graph.Undirected {
|
|
g := simple.NewUndirectedGraph()
|
|
gen.Gnp(g, n, p, nil)
|
|
return g
|
|
}
|
|
|
|
func benchmarkAStarNilHeuristic(b *testing.B, g graph.Undirected) {
|
|
var expanded int
|
|
for i := 0; i < b.N; i++ {
|
|
_, expanded = AStar(simple.Node(0), simple.Node(1), g, nil)
|
|
}
|
|
if expanded == 0 {
|
|
b.Fatal("unexpected number of expanded nodes")
|
|
}
|
|
}
|
|
|
|
func BenchmarkAStarGnp_10_tenth(b *testing.B) {
|
|
benchmarkAStarNilHeuristic(b, gnpUndirected_10_tenth)
|
|
}
|
|
func BenchmarkAStarGnp_100_tenth(b *testing.B) {
|
|
benchmarkAStarNilHeuristic(b, gnpUndirected_100_tenth)
|
|
}
|
|
func BenchmarkAStarGnp_1000_tenth(b *testing.B) {
|
|
benchmarkAStarNilHeuristic(b, gnpUndirected_1000_tenth)
|
|
}
|
|
func BenchmarkAStarGnp_10_half(b *testing.B) {
|
|
benchmarkAStarNilHeuristic(b, gnpUndirected_10_half)
|
|
}
|
|
func BenchmarkAStarGnp_100_half(b *testing.B) {
|
|
benchmarkAStarNilHeuristic(b, gnpUndirected_100_half)
|
|
}
|
|
func BenchmarkAStarGnp_1000_half(b *testing.B) {
|
|
benchmarkAStarNilHeuristic(b, gnpUndirected_1000_half)
|
|
}
|
|
|
|
var (
|
|
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 navigableSmallWorldUndirected(n, p, q int, r float64) graph.Undirected {
|
|
g := simple.NewUndirectedGraph()
|
|
gen.NavigableSmallWorld(g, []int{n, n}, p, q, r, nil)
|
|
return g
|
|
}
|
|
|
|
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 benchmarkAStarHeuristic(b *testing.B, g graph.Undirected, h Heuristic) {
|
|
var expanded int
|
|
for i := 0; i < b.N; i++ {
|
|
_, expanded = AStar(simple.Node(0), simple.Node(1), g, h)
|
|
}
|
|
if expanded == 0 {
|
|
b.Fatal("unexpected number of expanded nodes")
|
|
}
|
|
}
|
|
|
|
func BenchmarkAStarUndirectedmallWorld_10_2_2_2(b *testing.B) {
|
|
benchmarkAStarHeuristic(b, nswUndirected_10_2_2_2, nil)
|
|
}
|
|
func BenchmarkAStarUndirectedmallWorld_10_2_2_2_Heur(b *testing.B) {
|
|
h := func(x, y graph.Node) float64 {
|
|
return manhattanBetween(coordinatesForID(x, 10, 10), coordinatesForID(y, 10, 10))
|
|
}
|
|
benchmarkAStarHeuristic(b, nswUndirected_10_2_2_2, h)
|
|
}
|
|
func BenchmarkAStarUndirectedmallWorld_10_2_5_2(b *testing.B) {
|
|
benchmarkAStarHeuristic(b, nswUndirected_10_2_5_2, nil)
|
|
}
|
|
func BenchmarkAStarUndirectedmallWorld_10_2_5_2_Heur(b *testing.B) {
|
|
h := func(x, y graph.Node) float64 {
|
|
return manhattanBetween(coordinatesForID(x, 10, 10), coordinatesForID(y, 10, 10))
|
|
}
|
|
benchmarkAStarHeuristic(b, nswUndirected_10_2_5_2, h)
|
|
}
|
|
func BenchmarkAStarUndirectedmallWorld_100_5_10_2(b *testing.B) {
|
|
benchmarkAStarHeuristic(b, nswUndirected_100_5_10_2, nil)
|
|
}
|
|
func BenchmarkAStarUndirectedmallWorld_100_5_10_2_Heur(b *testing.B) {
|
|
h := func(x, y graph.Node) float64 {
|
|
return manhattanBetween(coordinatesForID(x, 100, 100), coordinatesForID(y, 100, 100))
|
|
}
|
|
benchmarkAStarHeuristic(b, nswUndirected_100_5_10_2, h)
|
|
}
|
|
func BenchmarkAStarUndirectedmallWorld_100_5_20_2(b *testing.B) {
|
|
benchmarkAStarHeuristic(b, nswUndirected_100_5_20_2, nil)
|
|
}
|
|
func BenchmarkAStarUndirectedmallWorld_100_5_20_2_Heur(b *testing.B) {
|
|
h := func(x, y graph.Node) float64 {
|
|
return manhattanBetween(coordinatesForID(x, 100, 100), coordinatesForID(y, 100, 100))
|
|
}
|
|
benchmarkAStarHeuristic(b, nswUndirected_100_5_20_2, h)
|
|
}
|
|
|
|
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) graph.Directed {
|
|
g := simple.NewDirectedGraph()
|
|
gen.Gnp(g, n, p, nil)
|
|
return g
|
|
}
|
|
|
|
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},
|
|
}
|
|
|
|
for _, bm := range benchmarks {
|
|
b.Run(bm.name, func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
BellmanFordFrom(bm.graph.Node(0), bm.graph)
|
|
}
|
|
})
|
|
}
|
|
}
|