Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.27 KB | None | 0 0
  1.  
  2. let pathfinding (start: Position) (goal: Position) (grid: Grid<_>) =
  3.     let frontier = [ (start, 0) ]
  4.     let came_from = Map.singleton start None
  5.     let cost_so_far = Map.singleton start 0
  6.     let rec loop (frontier, came_from, cost_so_far) =
  7.         match frontier with
  8.         | [] -> came_from
  9.         | (current, _) :: frontier ->
  10.             if current = goal then came_from else
  11.             getWalkableNeighbord current grid
  12.             |> Array.fold (fun (frontier, came_from, cost_so_far) next ->
  13.                 let new_cost = (Map.find current cost_so_far) + costOfMovement current next
  14.                 if not (cost_so_far |> Map.containsKey next) || new_cost < (Map.find next cost_so_far) then
  15.                     let cost_so_far = cost_so_far |> Map.add next new_cost
  16.                     let priority = new_cost + distance goal next
  17.                     let frontier = List.Cons ((next, priority), frontier) |> List.sortBy snd
  18.                     let came_from = came_from |> Map.add next (Some current)
  19.                     (frontier, came_from, cost_so_far)
  20.                 else
  21.                     (frontier, came_from, cost_so_far)
  22.             ) (frontier, came_from, cost_so_far)
  23.             |> loop
  24.     let came_from = loop (frontier, came_from, cost_so_far)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement