Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. findPath :: (Ord a, Ord n, Eq a, Num n) => Pos a -> Pos a -> g -> Cost g a n -> Heuristic a n -> Neighbours g a -> Maybe (Path a)
  2. findPath start end grid cost heuristic neighbours = findPath' (MinQueue.singleton (Node 0 0 start)) (Map.singleton start (start, 0))
  3.  where
  4.    findPath' open closed = do
  5.         minNode@(Node f g pos) <- MinQueue.getMin open
  6.         let neighbourNodes = filterNeighbours pos f closed
  7.             closed' = foldr (\p c -> Map.insert p (pos, f + cost pos p grid) c) closed neighbourNodes
  8.            open' = foldr (\p c -> MinQueue.insert (Node (f + cost pos p grid) (heuristic pos p) p) c) (MinQueue.deleteMin open) neighbourNodes
  9.         if pos == end then
  10.           return $ flattenPath closed' [end]
  11.        else
  12.          findPath' open' closed' -- Keep going till we find a solution.
  13.  
  14.     filterNeighbours pos f closed = filter filterFunc $ neighbours pos grid
  15.       where
  16.         filterFunc nPos = case Map.lookup nPos closed of
  17.                             Nothing -> True
  18.                             Just (cameFrom, score) -> f + cost pos nPos grid < score
  19.  
  20.     flattenPath closed path@(p:ps) = case Map.lookup p closed of
  21.                                       Nothing -> []
  22.                                       Just (cameFrom, _) -> if cameFrom == start then
  23.                                                               cameFrom : path
  24.                                                             else
  25.                                                               flattenPath closed (cameFrom : path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement