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 IEnumerable <Node <TState, TAction> > Expand(ISearchProblem <TState, TAction> problem) { var actions = problem.Actions(State); foreach (var action in actions) { yield return(ChildNode(problem, action)); } }
public static IEnumerable <Node <S, A> > GetSuccessors <S, A>(Node <S, A> node, ISearchProblem <S, A> problem) where A : class { var successors = new List <Node <S, A> >(); foreach (var action in problem.Actions(node.State)) { S successorState = problem.Result(node.State, action); double stepCost = problem.StepCost(node.State, action, successorState); successors.Add(CreateNode(successorState, node, action, stepCost)); } return(successors); }
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>()); } } }