Beispiel #1
0
        // returns a plan for each of the given states, if all plans are not null, else null
        private static IPlanNode <TState, TAction> AndSearch(
            IEnumerable <TState> states,
            INonDeterministicSearchProblem <TState, TAction> problem,
            IReadOnlyDictionary <TState, IPlanNode <TState, TAction> > explored)
        {
            var thisPlan   = new AndNode <TState, TAction>();
            var statesList = states.ToList();

            // all child states have already been explored - path to goal is not this way (?)
            if (statesList.All(explored.ContainsKey))
            {
                return(null);
            }

            foreach (var state in statesList)
            {
                var plan = OrSearch(state, problem, explored);

                // an and node's children must all lead to the goal
                if (plan == null)
                {
                    return(null);
                }

                thisPlan.AddPlanForState(state, plan);
            }

            return(thisPlan);
        }
Beispiel #2
0
        // returns the first action that yields a non-null plan, else null
        private static IPlanNode <TState, TAction> OrSearch(
            TState state,
            INonDeterministicSearchProblem <TState, TAction> problem,
            IReadOnlyDictionary <TState, IPlanNode <TState, TAction> > explored)
        {
            if (problem.IsGoal(state))
            {
                return(EmptyPlan);
            }
            if (explored.ContainsKey(state))
            {
                return(explored[state]);
            }

            foreach (var action in problem.GetActions(state))
            {
                var orNode       = new OrNode <TState, TAction>(action);
                var exploredCopy = explored.ToDictionary(e => e.Key, e => e.Value);
                exploredCopy[state] = orNode;
                var plan = AndSearch(problem.DoAction(state, action), problem, exploredCopy);
                if (plan == null)
                {
                    continue;
                }
                orNode.Child = plan;
                return(orNode);
            }

            return(null);
        }
Beispiel #3
0
        private static INonDeterministicSearchSolution <TState, TAction> Search(
            INonDeterministicSearchProblem <TState, TAction> problem,
            TState initialState)
        {
            var plan = OrSearch(initialState, problem, new Dictionary <TState, IPlanNode <TState, TAction> >());

            return(new NonDeterministicDfsSearchSolution <TState, TAction>(plan));
        }
Beispiel #4
0
 public NonDeterministicDfsSearch(INonDeterministicSearchProblem <TState, TAction> problem, TState initialState)
 {
     _problem      = problem;
     _initialState = initialState;
 }
Beispiel #5
0
 private INonDeterministicSearchAlgorithm<StateMock, ActionMock> CreateSearchAlgorithm(
     INonDeterministicSearchProblem<StateMock, ActionMock> problem,
     StateMock initialState)
 {
     return new NonDeterministicDfsSearch<StateMock, ActionMock>(problem, initialState);
 }
Beispiel #6
0
 public void Setup()
 {
     _problem = A.Fake<INonDeterministicSearchProblem<StateMock, ActionMock>>();
     _initialState = new StateMock("initial state");
 }