Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.04 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.org
  2. namespace AStarFs
  3. type Node={
  4.     State: string
  5.     Path: string list
  6.     F:float
  7.     G:float
  8.     H:float
  9. }
  10.  
  11. type AStar(problem: SearchProblems.IProblem)=
  12.     let getBest list=
  13.         let nodeWithMinF=List.minBy(fun el -> el.F) list
  14.         List.find(fun e -> e.F = nodeWithMinF.F) list
  15.  
  16.     let getSolution node=
  17.         let path=List.fold(fun acc e -> exn+" "+acc)node.State node.Path
  18.         System.String.Format("Calea:{0}\r\nCostul:{1}",path,node.F)
  19.  
  20.     let isEqualToParent currentNode successorNode=
  21.         List.exists(fun e -> e=successorNode)currentNode.Path
  22.  
  23.     let isBetterInList list succesorNode g=
  24.         List.exists(fun n-> n.State=successorNode &&n.G<=g)list
  25.  
  26.     let getValidSuccessors currentNode openList closedList=
  27.         List.fold(fun acc successor ->
  28.             let g=currentNode.GetType + problem.GetEdgeCost(
  29.                 currentNode.State,succesor)
  30.             let h=problem.GetHeuristic succesor
  31.             let f=max currentNode.F (g+h)
  32.             if isEqualToParent currentNode succesor || isBetterInList openList successor g||isBetterInList closedList successor g then acc
  33.             else
  34.                 let successorNode={State=successor;Path=currentNode.State::currentNode.Path;F=f;g=g;h=h}
  35.             successorNode::acc
  36.         )openList (problem.GetSuccessors currentNode.State |> List.ofArray)
  37.  
  38.     let rec astar openList closedList=
  39.         if List.isEmpty openList then
  40.             "Nu exista solutie"
  41.         else
  42.             let currentNode=getBest openList
  43.             let openList' = List.filter(fun e -> e <> currentNode) openList
  44.            if problem.IsGoal currentNode.State then
  45.                getSolution currentNode
  46.            else
  47.                let openList'=getValidSuccessors currentNode openList' closedList
  48.                let closedList'=curentNode::List.filter(fun e -> e.State <> currentNode.State) closedList
  49.  
  50.  
  51.  
  52.  
  53. [<EntryPoint>]
  54. let main argv =
  55.     printfn "Hello World from F#!"
  56.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement