-
Notifications
You must be signed in to change notification settings - Fork 6
/
astar.go
58 lines (51 loc) · 1.46 KB
/
astar.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
package main
import (
"container/heap"
"math"
)
// aStarSearch performs A* search on Weighed Grid, looking for the least costly path from start to goal location.
// Return least costly path from start to goal in slice of Locations.
func aStarSearch(grid GridWithWeights, start, goal Location) []Location {
frontier := make(PriorityQueue, 0)
heap.Init(&frontier)
heap.Push(&frontier, &Item{
value: start,
priority: 0,
})
cameFrom := map[Location]Location{start: start}
costSoFar := map[Location]int{start: 0}
for frontier.Len() > 0 {
current := heap.Pop(&frontier).(*Item).value.(Location)
if current.Equals(goal) {
break
}
for _, next := range grid.Neighbors(current) {
newCost := costSoFar[current] + grid.Cost(current, next)
if _, exists := costSoFar[next]; !exists || newCost < costSoFar[next] {
costSoFar[next] = newCost
priority := newCost + heuristic(next, goal)
heap.Push(&frontier, &Item{
value: next,
priority: priority,
})
cameFrom[next] = current
}
}
}
//Now we are done, we found the path. Go backwards from the goal node to beggining and return nice list
current := goal
path := make([]Location, 0)
for !current.Equals(start) {
path = append(path, current)
current = cameFrom[current]
}
path = append(path, start)
path = reverse(path)
return path
}
func heuristic(a, b Location) int {
dx := float64(a.x - b.x)
dy := float64(a.y - b.y)
h := math.Abs(dx) + math.Abs(dy)
return int(h)
}