Skip to content
FutureBlitz

    Install

    Import std.algo.search

    AStar

    std.algo.search

    function

    @PanicsAtCaller Public Function AStar<N, C>(start: N, successors: Func(N): (N, C)[], heuristic: Func(N): C, goal: Func(N): Bool): Path<N, C>?

    The cheapest path from start to the first node that goal says is one, by A*, which goes on first from where the cost so far, plus heuristic's guess of what's left to the goal, is least. For the path to be the cheapest, the guess can't be more than what's left, as a straight line's length isn't. Null when no node it reaches is the goal. A negative or NaN weight, or a NaN guess, panics.

    Examples

    // Steps right and down on an open plane, guessed by the Manhattan distance to the goal.
    Local goal = Vec2i(3, 2)
    Local steps = Function(at: Vec2i): (Vec2i, Int)[]
        Return [(at + Vec2i(1, 0), 1), (at + Vec2i(0, 1), 1)]
    End Function
    Local guess = Function(at: Vec2i): Int => Abs(goal.x - at.x) + Abs(goal.y - at.y)
    Local path = AStar(Vec2i(0, 0), steps, guess, Function(at: Vec2i) => at == goal)
    Expect(path?.Cost == 5)

    std/algo/search.fb, line 412

    Report an issue with this page or with FutureBlitz on GitHub.