示例#1
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);
        }
示例#2
0
        public void GivenOnlyActionLeadsToGoal_FirstSolutionActionShouldGoToGoal()
        {
            var goalState = new StateMock("goal state");
            var goToGoal = new ActionMock("go to goal");

            A.CallTo(() => _problem.IsGoal(_initialState)).Returns(false);
            A.CallTo(() => _problem.IsGoal(goalState)).Returns(true);
            A.CallTo(() => _problem.GetActions(_initialState)).Returns(new [] {goToGoal});
            A.CallTo(() => _problem.DoAction(_initialState, goToGoal)).Returns(new[] {goalState});
            
            // act
            var solution = CreateSearchAlgorithm(_problem, _initialState).GetSolution();
            
            // assert
            Assert.AreEqual(goToGoal, solution.NextAction(_initialState));
        }