Example #1
0
        public void GivenNonDeterministicAction_ShouldReachGoal()
        {
            var state1 = new StateMock("state 1");
            var state2 = new StateMock("state 2");
            var goalState = new StateMock("goal state");
            var action = new ActionMock("action");

            A.CallTo(() => _problem.IsGoal(goalState)).Returns(true);
            // available actions
            A.CallTo(() => _problem.GetActions(_initialState)).Returns(new [] {action});
            A.CallTo(() => _problem.GetActions(state1)).Returns(new [] {action});
            A.CallTo(() => _problem.GetActions(state2)).Returns(new [] {action});
            // action outcomes
            A.CallTo(() => _problem.DoAction(_initialState, action)).Returns(new[]
            {
                state1,
                state2
            });
            A.CallTo(() => _problem.DoAction(state1, action)).Returns(new[] {goalState});
            A.CallTo(() => _problem.DoAction(state2, action)).Returns(new[] {goalState});

            // act
            var solution = CreateSearchAlgorithm(_problem, _initialState).GetSolution();
            
            // assert
            Assert.AreEqual(action, solution.NextAction(_initialState));
            Assert.AreEqual(action, solution.NextAction(state1));
        }
Example #2
0
        public void Solve_should_solve_solvable_problem()
        {
            var action1      = new ActionMock("action 1");
            var action2      = new ActionMock("action 2");
            var initialState = new StateMock("initial state");
            var state1       = new StateMock("state 1");
            var state2       = new StateMock("state 2");
            var problem      = A.Fake <ISearchProblem <StateMock, ActionMock> >();

            A.CallTo(() => problem.InitialState).Returns(initialState);
            A.CallTo(() => problem.GetActions(initialState)).Returns(new [] { action1 });
            A.CallTo(() => problem.DoAction(initialState, action1)).Returns(state1);
            A.CallTo(() => problem.GetActions(state1)).Returns(new [] { action2 });
            A.CallTo(() => problem.DoAction(state1, action2)).Returns(state2);
            A.CallTo(() => problem.IsGoal(state2)).Returns(true);

            var bfs = new BreadthFirstSearch <StateMock, ActionMock>(problem);

            bfs.Solve();

            Assert.IsTrue(bfs.IsFinished);
            Assert.IsTrue(bfs.IsSolved);

            var solution = bfs.GetSolutionTo(state2).ToList();

            Assert.AreEqual(new[] { action1, action2 }, solution);
        }
        public void Execute_empty_from_does_nothing()
        {
            StateMock.Setup(s => s.IsEmpty(It.IsAny <BoardLocation>()))
            .Returns(true);

            Action.Execute(EnPassantMove);

            VerifyEntityWasNOTRetrieved(EnPassantMove.From);
        }
Example #4
0
        public void Init()
        {
            var problem      = A.Fake <ISearchProblem <StateMock, ActionMock> >();
            var initialState = new StateMock("initial state");

            A.CallTo(() => problem.InitialState).Returns(initialState);
            A.CallTo(() => problem.GetActions(A <StateMock> ._)).Returns(new List <ActionMock>());
            A.CallTo(() => problem.IsGoal(A <StateMock> ._)).Returns(false);

            var bfs = new BreadthFirstSearch <StateMock, ActionMock>(problem);

            Assert.IsFalse(bfs.IsFinished);
            Assert.IsFalse(bfs.IsSolved);
            Assert.AreEqual(initialState, bfs.CurrentState);
        }
Example #5
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));
        }
Example #6
0
        public void Solve_problem_with_no_solution_should_finish_and_be_not_solved()
        {
            var problem      = A.Fake <ISearchProblem <StateMock, ActionMock> >();
            var initialState = new StateMock("initial state");

            A.CallTo(() => problem.InitialState).Returns(initialState);
            A.CallTo(() => problem.GetActions(A <StateMock> ._)).Returns(new List <ActionMock>());
            A.CallTo(() => problem.IsGoal(A <StateMock> ._)).Returns(false);

            var bfs = new BreadthFirstSearch <StateMock, ActionMock>(problem);

            bfs.Solve();

            Assert.IsTrue(bfs.IsFinished);
            Assert.IsFalse(bfs.IsSolved);
        }
Example #7
0
        public void GivenOneActionRepeatsAndOneGoesToGoal_ShouldReturnPlanToGoal()
        {
            var goalState = new StateMock("goal state");
            var repeat = new ActionMock("repeat");
            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 [] {repeat, goToGoal});
            A.CallTo(() => _problem.DoAction(_initialState, repeat)).Returns(new[] {_initialState});
            A.CallTo(() => _problem.DoAction(_initialState, goToGoal)).Returns(new[] {goalState});
            
            // act
            var solution = CreateSearchAlgorithm(_problem, _initialState).GetSolution();
            
            // assert
            Assert.AreEqual(goToGoal, solution.NextAction(_initialState));
        }
Example #8
0
        public void GivenGoalIsTwoActionsAway_SolutionShouldLeadToGoal()
        {
            var nextState = new StateMock("next state");
            var goalState = new StateMock("goal state");
            var goToNext = new ActionMock("go to next");
            var goToGoal = new ActionMock("go to goal");

            A.CallTo(() => _problem.IsGoal(goalState)).Returns(true);
            A.CallTo(() => _problem.GetActions(_initialState)).Returns(new [] {goToNext});
            A.CallTo(() => _problem.GetActions(nextState)).Returns(new [] {goToGoal});
            A.CallTo(() => _problem.DoAction(_initialState, goToNext)).Returns(new[] {nextState});
            A.CallTo(() => _problem.DoAction(nextState, goToGoal)).Returns(new[] {goalState});

            // act
            var solution = CreateSearchAlgorithm(_problem, _initialState).GetSolution();
            
            // assert
            Assert.AreEqual(goToNext, solution.NextAction(_initialState));
            Assert.AreEqual(goToGoal, solution.NextAction(nextState));
        }
Example #9
0
        public void First_step()
        {
            var action1      = new ActionMock("action 1");
            var action2      = new ActionMock("action 2");
            var initialState = new StateMock("initial state");
            var state1       = new StateMock("state 1");
            var state2       = new StateMock("state 2");
            var problem      = A.Fake <ISearchProblem <StateMock, ActionMock> >();

            A.CallTo(() => problem.InitialState).Returns(initialState);
            A.CallTo(() => problem.GetActions(A <StateMock> ._)).Returns(new [] { action1, action2 });
            A.CallTo(() => problem.DoAction(initialState, action1)).Returns(state1);
            A.CallTo(() => problem.DoAction(initialState, action2)).Returns(state2);

            var bfs = new BreadthFirstSearch <StateMock, ActionMock>(problem);

            bfs.Step();

            Assert.IsFalse(bfs.IsFinished);
        }
Example #10
0
        public void GivenNonDeterministicActionAndRepeatedState_SolutionShouldKeepTryingToGoToGoal()
        {
            var goalState = new StateMock("goal state");
            var goToGoal = new ActionMock("go to goal");

            A.CallTo(() => _problem.IsGoal(goalState)).Returns(true);
            A.CallTo(() => _problem.GetActions(_initialState)).Returns(new [] {goToGoal});
            A.CallTo(() => _problem.DoAction(_initialState, goToGoal)).Returns(new[]
            {
                _initialState,
                goalState
            });

            // act
            var solution = CreateSearchAlgorithm(_problem, _initialState).GetSolution();
            
            // assert
            Assert.AreEqual(goToGoal, solution.NextAction(_initialState));
            Assert.AreEqual(goToGoal, solution.NextAction(_initialState));
            Assert.AreEqual(goToGoal, solution.NextAction(_initialState));
        }
Example #11
0
        public void Step_ShouldGoToBetterState()
        {
            var state3 = new StateMock("3");
            var state2 = new StateMock("2");
            var state1 = new StateMock("1");

            var action1 = new ActionMock("1");
            var action3 = new ActionMock("3");

            A.CallTo(() => _problem.InitialState).Returns(state2);
            A.CallTo(() => _problem.GetActions(state2)).Returns(new[] { action1, action3 });
            A.CallTo(() => _problem.DoAction(state2, action1)).Returns(state1);
            A.CallTo(() => _problem.DoAction(state2, action3)).Returns(state3);

            var search = new AStarSearch <StateMock, ActionMock>(_problem, state => int.Parse(state.Value));

            // act
            search.Step(); // step 1 sets current state to initial state
            search.Step();

            // assert
            Assert.AreEqual(state1, search.CurrentState);
        }
Example #12
0
 private INonDeterministicSearchAlgorithm<StateMock, ActionMock> CreateSearchAlgorithm(
     INonDeterministicSearchProblem<StateMock, ActionMock> problem,
     StateMock initialState)
 {
     return new NonDeterministicDfsSearch<StateMock, ActionMock>(problem, initialState);
 }
Example #13
0
 public void Setup()
 {
     _problem = A.Fake<INonDeterministicSearchProblem<StateMock, ActionMock>>();
     _initialState = new StateMock("initial state");
 }