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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
// Copyright ©2014 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 (
"math"
"reflect"
"testing"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/path/internal/testgraphs"
"gonum.org/v1/gonum/graph/simple"
"gonum.org/v1/gonum/graph/topo"
)
var aStarTests = []struct {
name string
g graph.Graph
s, t int64
heuristic Heuristic
wantPath []int64
}{
{
name: "simple path",
g: func() graph.Graph {
return testgraphs.NewGridFrom(
"*..*",
"**.*",
"**.*",
"**.*",
)
}(),
s: 1, t: 14,
wantPath: []int64{1, 2, 6, 10, 14},
},
{
name: "small open graph",
g: testgraphs.NewGrid(3, 3, true),
s: 0, t: 8,
},
{
name: "large open graph",
g: testgraphs.NewGrid(1000, 1000, true),
s: 0, t: 999*1000 + 999,
},
{
name: "no path",
g: func() graph.Graph {
tg := testgraphs.NewGrid(5, 5, true)
// Create a complete "wall" across the middle row.
tg.Set(2, 0, false)
tg.Set(2, 1, false)
tg.Set(2, 2, false)
tg.Set(2, 3, false)
tg.Set(2, 4, false)
return tg
}(),
s: 2, t: 22,
},
{
name: "partially obstructed",
g: func() graph.Graph {
tg := testgraphs.NewGrid(10, 10, true)
// Create a partial "wall" across the middle
// row with a gap at the left-hand end.
tg.Set(4, 1, false)
tg.Set(4, 2, false)
tg.Set(4, 3, false)
tg.Set(4, 4, false)
tg.Set(4, 5, false)
tg.Set(4, 6, false)
tg.Set(4, 7, false)
tg.Set(4, 8, false)
tg.Set(4, 9, false)
return tg
}(),
s: 5, t: 9*10 + 9,
},
{
name: "partially obstructed with heuristic",
g: func() graph.Graph {
tg := testgraphs.NewGrid(10, 10, true)
// Create a partial "wall" across the middle
// row with a gap at the left-hand end.
tg.Set(4, 1, false)
tg.Set(4, 2, false)
tg.Set(4, 3, false)
tg.Set(4, 4, false)
tg.Set(4, 5, false)
tg.Set(4, 6, false)
tg.Set(4, 7, false)
tg.Set(4, 8, false)
tg.Set(4, 9, false)
return tg
}(),
s: 5, t: 9*10 + 9,
// Manhattan Heuristic
heuristic: func(u, v graph.Node) float64 {
uid := u.ID()
cu := (uid % 10)
ru := (uid - cu) / 10
vid := v.ID()
cv := (vid % 10)
rv := (vid - cv) / 10
return math.Abs(float64(ru-rv)) + math.Abs(float64(cu-cv))
},
},
}
func TestAStar(t *testing.T) {
t.Parallel()
for _, test := range aStarTests {
pt, _ := AStar(simple.Node(test.s), simple.Node(test.t), test.g, test.heuristic)
p, cost := pt.To(test.t)
if !topo.IsPathIn(test.g, p) {
t.Errorf("got path that is not path in input graph for %q", test.name)
}
bfp, ok := BellmanFordFrom(simple.Node(test.s), test.g)
if !ok {
t.Fatalf("unexpected negative cycle in %q", test.name)
}
if want := bfp.WeightTo(test.t); cost != want {
t.Errorf("unexpected cost for %q: got:%v want:%v", test.name, cost, want)
}
var got = make([]int64, 0, len(p))
for _, n := range p {
got = append(got, n.ID())
}
if test.wantPath != nil && !reflect.DeepEqual(got, test.wantPath) {
t.Errorf("unexpected result for %q:\ngot: %v\nwant:%v", test.name, got, test.wantPath)
}
}
}
func TestExhaustiveAStar(t *testing.T) {
t.Parallel()
g := simple.NewWeightedUndirectedGraph(0, math.Inf(1))
nodes := []locatedNode{
{id: 1, x: 0, y: 6},
{id: 2, x: 1, y: 0},
{id: 3, x: 8, y: 7},
{id: 4, x: 16, y: 0},
{id: 5, x: 17, y: 6},
{id: 6, x: 9, y: 8},
}
for _, n := range nodes {
g.AddNode(n)
}
edges := []weightedEdge{
{from: g.Node(1), to: g.Node(2), cost: 7},
{from: g.Node(1), to: g.Node(3), cost: 9},
{from: g.Node(1), to: g.Node(6), cost: 14},
{from: g.Node(2), to: g.Node(3), cost: 10},
{from: g.Node(2), to: g.Node(4), cost: 15},
{from: g.Node(3), to: g.Node(4), cost: 11},
{from: g.Node(3), to: g.Node(6), cost: 2},
{from: g.Node(4), to: g.Node(5), cost: 7},
{from: g.Node(5), to: g.Node(6), cost: 9},
}
for _, e := range edges {
g.SetWeightedEdge(e)
}
heuristic := func(u, v graph.Node) float64 {
lu := u.(locatedNode)
lv := v.(locatedNode)
return math.Hypot(lu.x-lv.x, lu.y-lv.y)
}
if ok, edge, goal := isMonotonic(g, heuristic); !ok {
t.Fatalf("non-monotonic heuristic at edge:%v for goal:%v", edge, goal)
}
ps := DijkstraAllPaths(g)
ends := graph.NodesOf(g.Nodes())
for _, start := range ends {
for _, goal := range ends {
pt, _ := AStar(start, goal, g, heuristic)
gotPath, gotWeight := pt.To(goal.ID())
wantPath, wantWeight, _ := ps.Between(start.ID(), goal.ID())
if gotWeight != wantWeight {
t.Errorf("unexpected path weight from %v to %v result: got:%f want:%f",
start, goal, gotWeight, wantWeight)
}
if !reflect.DeepEqual(gotPath, wantPath) {
t.Errorf("unexpected path from %v to %v result:\ngot: %v\nwant:%v",
start, goal, gotPath, wantPath)
}
}
}
}
type locatedNode struct {
id int64
x, y float64
}
func (n locatedNode) ID() int64 { return n.id }
type weightedEdge struct {
from, to graph.Node
cost float64
}
func (e weightedEdge) From() graph.Node { return e.from }
func (e weightedEdge) To() graph.Node { return e.to }
func (e weightedEdge) ReversedEdge() graph.Edge { e.from, e.to = e.to, e.from; return e }
func (e weightedEdge) Weight() float64 { return e.cost }
func isMonotonic(g UndirectedWeightLister, h Heuristic) (ok bool, at graph.Edge, goal graph.Node) {
for _, goal := range graph.NodesOf(g.Nodes()) {
for _, edge := range graph.WeightedEdgesOf(g.WeightedEdges()) {
from := edge.From()
to := edge.To()
w, ok := g.Weight(from.ID(), to.ID())
if !ok {
panic("A*: unexpected invalid weight")
}
if h(from, goal) > w+h(to, goal) {
return false, edge, goal
}
}
}
return true, nil, nil
}
func TestAStarNullHeuristic(t *testing.T) {
t.Parallel()
for _, test := range testgraphs.ShortestPathTests {
g := test.Graph()
for _, e := range test.Edges {
g.SetWeightedEdge(e)
}
var (
pt Shortest
panicked bool
)
func() {
defer func() {
panicked = recover() != nil
}()
pt, _ = AStar(test.Query.From(), test.Query.To(), g.(graph.Graph), nil)
}()
if panicked || test.HasNegativeWeight {
if !test.HasNegativeWeight {
t.Errorf("%q: unexpected panic", test.Name)
}
if !panicked {
t.Errorf("%q: expected panic for negative edge weight", test.Name)
}
continue
}
if pt.From().ID() != test.Query.From().ID() {
t.Fatalf("%q: unexpected from node ID: got:%d want:%d", test.Name, pt.From().ID(), test.Query.From().ID())
}
p, weight := pt.To(test.Query.To().ID())
if weight != test.Weight {
t.Errorf("%q: unexpected weight from To: got:%f want:%f",
test.Name, weight, test.Weight)
}
if weight := pt.WeightTo(test.Query.To().ID()); weight != test.Weight {
t.Errorf("%q: unexpected weight from Weight: got:%f want:%f",
test.Name, weight, test.Weight)
}
var got []int64
for _, n := range p {
got = append(got, n.ID())
}
ok := len(got) == 0 && len(test.WantPaths) == 0
for _, sp := range test.WantPaths {
if reflect.DeepEqual(got, sp) {
ok = true
break
}
}
if !ok {
t.Errorf("%q: unexpected shortest path:\ngot: %v\nwant from:%v",
test.Name, p, test.WantPaths)
}
np, weight := pt.To(test.NoPathFor.To().ID())
if pt.From().ID() == test.NoPathFor.From().ID() && (np != nil || !math.IsInf(weight, 1)) {
t.Errorf("%q: unexpected path:\ngot: path=%v weight=%f\nwant:path=<nil> weight=+Inf",
test.Name, np, weight)
}
}
}
|