Documentation
¶
Overview ¶
Package astar implements the A* search algorithm for finding least-cost paths.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CostFunc ¶
A CostFunc is a function that returns a cost for the transition from node a to node b.
type Graph ¶
type Graph[Node any] interface { // Neighbours returns the neighbour nodes of node n in the graph. Neighbours(n Node) iter.Seq[Node] }
The Graph interface is the minimal interface a graph data structure must satisfy to be suitable for the A* algorithm.
type Path ¶
type Path[Node any] []Node
A Path is a sequence of nodes in a graph.
func FindPath ¶
func FindPath[Node comparable](g Graph[Node], start, dest Node, d, h CostFunc[Node]) Path[Node]
FindPath finds the least-cost path between start and dest in graph g using the cost function d and the cost heuristic function h. Returns nil if no path was found.
Example ¶
package main import ( "fmt" "image" "iter" "math" "slices" "github.com/fzipp/astar" ) func main() { // Create a graph with 2D points as nodes p1 := image.Pt(3, 1) p2 := image.Pt(1, 2) p3 := image.Pt(2, 4) p4 := image.Pt(4, 5) p5 := image.Pt(4, 3) p6 := image.Pt(5, 1) p7 := image.Pt(8, 4) p8 := image.Pt(8, 3) p9 := image.Pt(6, 3) g := newGraph[image.Point](). link(p1, p2).link(p1, p3). link(p2, p3).link(p2, p4). link(p3, p4).link(p3, p5). link(p4, p6).link(p4, p7). link(p5, p7). link(p6, p9). link(p7, p8). link(p8, p9) // Find the shortest path from p1 to p9 p := astar.FindPath[image.Point](g, p1, p9, nodeDist, nodeDist) // Output the result if p == nil { fmt.Println("No path found.") return } for i, n := range p { fmt.Printf("%d: %s\n", i, n) } } // nodeDist is our cost function. We use points as nodes, so we // calculate their Euclidean distance. func nodeDist(p, q image.Point) float64 { d := q.Sub(p) return math.Sqrt(float64(d.X*d.X + d.Y*d.Y)) } // graph is represented by an adjacency list. type graph[Node comparable] map[Node][]Node func newGraph[Node comparable]() graph[Node] { return make(map[Node][]Node) } // link creates a bi-directed edge between nodes a and b. func (g graph[Node]) link(a, b Node) graph[Node] { g[a] = append(g[a], b) g[b] = append(g[b], a) return g } // Neighbours returns the neighbour nodes of node n in the graph. func (g graph[Node]) Neighbours(n Node) iter.Seq[Node] { return slices.Values(g[n]) }
Output: 0: (3,1) 1: (2,4) 2: (4,5) 3: (5,1) 4: (6,3)
Example (Maze) ¶
package main import ( "fmt" "image" "iter" "math" "github.com/fzipp/astar" ) func main() { maze := floorPlan{ "###############", "# # # # #", "# ### ### ### #", "# # # # # #", "### # # # ### #", "# # # #", "# # ### ### ###", "# # # # # #", "### # # # # ###", "# # # # #", "# # ######### #", "# # #", "# ### # # ### #", "# # # # #", "###############", } start := image.Pt(1, 13) // Bottom left corner dest := image.Pt(13, 1) // Top right corner // Find the shortest path path := astar.FindPath[image.Point](maze, start, dest, distance, distance) // Mark the path with dots before printing for _, p := range path { maze.put(p, '.') } maze.print() } // distance is our cost function. We use points as nodes, so we // calculate their Euclidean distance. func distance(p, q image.Point) float64 { d := q.Sub(p) return math.Sqrt(float64(d.X*d.X + d.Y*d.Y)) } type floorPlan []string var offsets = [...]image.Point{ image.Pt(0, -1), // North image.Pt(1, 0), // East image.Pt(0, 1), // South image.Pt(-1, 0), // West } // Neighbours implements the astar.Graph[Node] interface (with Node = image.Point). func (f floorPlan) Neighbours(p image.Point) iter.Seq[image.Point] { return func(yield func(image.Point) bool) { for _, off := range offsets { q := p.Add(off) if f.isFreeAt(q) { if !yield(q) { return } } } } } func (f floorPlan) isFreeAt(p image.Point) bool { return f.isInBounds(p) && f[p.Y][p.X] == ' ' } func (f floorPlan) isInBounds(p image.Point) bool { return (0 <= p.X && p.X < len(f[p.Y])) && (0 <= p.Y && p.Y < len(f)) } func (f floorPlan) put(p image.Point, c rune) { f[p.Y] = f[p.Y][:p.X] + string(c) + f[p.Y][p.X+1:] } func (f floorPlan) print() { for _, row := range f { fmt.Println(row) } }
Output: ############### # # # #.# # ### ### ###.# # # # # #.# ### # # # ###.# # # # .......# # # ###.### ### # # #.# # # ### # #.# # ### # #..... # # # # #.######### # #... # # #.### # # ### # #. # # # # ###############
Click to show internal directories.
Click to hide internal directories.