Advertisement
Guest User

Untitled

a guest
Dec 19th, 2021
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     -- Returns the cost of a node according to the SearchType currently used
  2.     getCost :: SearchType -> Distance -> Int -> Grid -> [Grid] -> Int
  3.     getCost st d l goal neighbors = let dist = calcDistance d goal (head neighbors) in case st of
  4.         Astar     -> dist + l  -- A* : h cost + g cost
  5.         Uniform   -> l         -- Uniform : g cost only
  6.         Greedy    -> dist      -- Greedy : h cost only
  7.  
  8.     -- Returns a PQueue containing the next nodes (value + cost)
  9.     getNextNodes :: Grid -> Distance -> SearchType -> Int -> [Grid] -> PQ.MinPQueue Int [Grid]
  10.     getNextNodes goal d st l xss = PQ.fromList $ zip costs neighbors where
  11.         costs       = parMap rseq (getCost st d l goal) neighbors
  12.         neighbors   = parMap rpar (:xss)  (getNeighbors $ head xss)
  13.  
  14.  
  15.  
  16.   -- Runs the search using a given SearchType. The SearchType will be used in nodes cost computation
  17.     -- goal : stage to reach ; xss : path from begining to current node ; os : open set ; cs : close set ; nn : nextNodes function ; n : time complexity ; m : space complexity ; l : xss length
  18.     runSearch :: Grid -> [Grid] -> PQ.MinPQueue Int [Grid] -> S.Set Grid -> NextNodesFunc -> Int -> Int -> Int -> IO ()
  19.     runSearch goal xss os cs nn n m l
  20.         | head xss == goal                          = putStrLn ("Solved with :\n- Time complexity  : " ++ show n ++ "\n- Space complexity : " ++ show m)
  21.         | suc /= PQ.empty                           = runSearch  goal  ((minim suc):xss)  (PQ.union os $ PQ.deleteMin suc)  cs'  nn  (n+1)  size (l+1)
  22.        | suc == PQ.empty && os /= PQ.empty         = runSearch  goal  (tail xss)         os                                cs'  nn  (n+1)  size (l-1)
  23.         | otherwise = putErr NotSolvable where
  24.             suc     = PQ.filter (\x -> S.notMember (head x) cs) $ nn l xss
  25.             cs'     = S.insert (head xss) cs
  26.            minim x = head . snd $ PQ.findMin x
  27.            size    = if PQ.size os > m then PQ.size os else m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement