public SolutionSearchBase <TState, TAction> Search( ISearchProblem <TState, TAction> problem) { var rootNode = Node <TState, TAction> .Root(problem.InitialState); if (problem.GoalTest(rootNode.State)) { return(new SolutionFound <TState, TAction>(rootNode)); } var frontier = new BasicPriorityQueue <double, FrontierItem <TState, TAction> >( x => x.Evaluation); var explored = new HashSet <TState>(sComparer); frontier.Enqueue(new FrontierItem <TState, TAction>(rootNode, EvaluationFuntion(problem, rootNode))); while (frontier.Count > 0) { var element = frontier.Dequeue(); if (problem.GoalTest(element.Node.State)) { return(new SolutionFound <TState, TAction>(element.Node)); } explored.Add(element.Node.State); var actions = problem.Actions(element.Node.State); foreach (var action in actions) { var child = NodeExtensions.ChildNode(problem, element.Node, action); var childElement = new FrontierItem <TState, TAction>(child, EvaluationFuntion(problem, child)); var comparedChild = frontier.CherryPeek( x => sComparer.Equals(x.Node.State, childElement.Node.State)); bool stateInFrontier = comparedChild != default; bool containsState = explored.Contains(child.State) || stateInFrontier; if (!containsState) { frontier.Enqueue(childElement); } else if (stateInFrontier && comparedChild.Evaluation > childElement.Evaluation) { frontier.ReplaceWith(comparedChild, childElement); } } } return(new SolutionFailure <TState, TAction>()); }
public SolutionSearchBase <TState, TAction> Search(ISearchProblem <TState, TAction> problem) { var rootNode = Node <TState, TAction> .Root(problem.InitialState); if (problem.GoalTest(rootNode.State)) { return(new SolutionFound <TState, TAction>(rootNode)); } var frontier = new Stack <Node <TState, TAction> >(); var explored = new HashSet <TState>(sComparer); frontier.Push(rootNode); while (frontier.Count > 0) { var node = frontier.Pop(); explored.Add(node.State); var actions = problem.Actions(node.State); foreach (var action in actions) { var child = NodeExtensions.ChildNode(problem, node, action); bool containsState = explored.Contains(child.State) || frontier.Contains(child, nComparer); if (!containsState) { if (problem.GoalTest(child.State)) { return(new SolutionFound <TState, TAction>(child)); } frontier.Push(child); } } } return(new SolutionFailure <TState, TAction>()); }
private SolutionSearchBase <TState, TAction> RecursiveDLS( Node <TState, TAction> node, ISearchProblem <TState, TAction> problem, int limit) { if (problem.GoalTest(node.State)) { return(new SolutionFound <TState, TAction>(node)); } else if (limit == 0) { return(new SolutionCutoff <TState, TAction>()); } else { var cutoffOccurred = false; var actions = problem.Actions(node.State); foreach (var action in actions) { var child = NodeExtensions.ChildNode(problem, node, action); var result = RecursiveDLS(child, problem, limit - 1); if (result.GetType() == typeof(SolutionCutoff <TState, TAction>)) { cutoffOccurred = true; } else if (result.GetType() != typeof(SolutionFailure <TState, TAction>)) { return(result); } } if (cutoffOccurred) { return(new SolutionCutoff <TState, TAction>()); } else { return(new SolutionFailure <TState, TAction>()); } } }
public override Node <S, A> FindNode(ISearchProblem <S, A> problem, InOutCollection <Node <S, A> > frontier) { _bytes = GC.GetTotalMemory(true); var startTime = Stopwatch.StartNew(); startTime.Start(); Frontier = frontier; var root = NodeFactory.CreateNode <S, A>(problem.InitState); AddToFrontier(root); long maxMemory = _bytes; while (!IsFrontierEmpty()) { maxMemory = Math.Max(maxMemory, GC.GetTotalMemory(false)); _count++; var node = RemoveFromFrontier(); if (problem.GoalTest(node.State)) { startTime.Stop(); _times = startTime.Elapsed; _bytes -= maxMemory; return(node); } foreach (var successor in NodeFactory.GetSuccessors(node, problem)) { AddToFrontier(successor); } } _bytes -= maxMemory; startTime.Stop(); _times = startTime.Elapsed; return(null); }