Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Learn more about F# at http://fsharp.org
- namespace AStarFs
- type Node={
- State: string
- Path: string list
- F:float
- G:float
- H:float
- }
- type AStar(problem: SearchProblems.IProblem)=
- let getBest list=
- let nodeWithMinF=List.minBy(fun el -> el.F) list
- List.find(fun e -> e.F = nodeWithMinF.F) list
- let getSolution node=
- let path=List.fold(fun acc e -> exn+" "+acc)node.State node.Path
- System.String.Format("Calea:{0}\r\nCostul:{1}",path,node.F)
- let isEqualToParent currentNode successorNode=
- List.exists(fun e -> e=successorNode)currentNode.Path
- let isBetterInList list succesorNode g=
- List.exists(fun n-> n.State=successorNode &&n.G<=g)list
- let getValidSuccessors currentNode openList closedList=
- List.fold(fun acc successor ->
- let g=currentNode.GetType + problem.GetEdgeCost(
- currentNode.State,succesor)
- let h=problem.GetHeuristic succesor
- let f=max currentNode.F (g+h)
- if isEqualToParent currentNode succesor || isBetterInList openList successor g||isBetterInList closedList successor g then acc
- else
- let successorNode={State=successor;Path=currentNode.State::currentNode.Path;F=f;g=g;h=h}
- successorNode::acc
- )openList (problem.GetSuccessors currentNode.State |> List.ofArray)
- let rec astar openList closedList=
- if List.isEmpty openList then
- "Nu exista solutie"
- else
- let currentNode=getBest openList
- let openList' = List.filter(fun e -> e <> currentNode) openList
- if problem.IsGoal currentNode.State then
- getSolution currentNode
- else
- let openList'=getValidSuccessors currentNode openList' closedList
- let closedList'=curentNode::List.filter(fun e -> e.State <> currentNode.State) closedList
- [<EntryPoint>]
- let main argv =
- printfn "Hello World from F#!"
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement