public void GreedyGraphSearch() { // Arrange var problem = new TriForkTestProblem(); var search = new GraphSearch<TestState>(new GreedySearchFringe<TestState>(new TestStateHuristic()), new TestStateEqualityComparer()); // Act var result = search.Execute(problem); // Assert var expanded = problem.Expanded.Select(s => s.Node.Name); var resultPath = result.Path.Select(n => n.Name); Assert.Equal(new[] { "Start", "B" }, expanded); Assert.Equal(new[] { "Start", "B", "Goal" }, resultPath); }
public void Execute_AddsExpandedStatesToTheFringe() { // Arrange var stateBuilder = fixture.Build<TestState>(); var s1 = stateBuilder.Create(); var s2 = stateBuilder.Create(); var s3 = stateBuilder.Create(); strategy.SetupSequence(strat => strat.GetNext()) .Returns(startState) .Returns(s1) .Returns(s2) .Returns(s3) .Returns(goalState); problem.Setup(prob => prob.GetSuccessors(startState)).Returns(new[] { s1 }); problem.Setup(prob => prob.GetSuccessors(s1)).Returns(new[] { s2, s3 }); problem.Setup(prob => prob.GetSuccessors(s2)).Returns(fixture.CreateMany<TestState>()); problem.Setup(prob => prob.GetSuccessors(s3)).Returns(new[] { goalState }); using (Sequence.Create()) { strategy.Setup(strat => strat.Add(s1)).InSequence(); strategy.Setup(strat => strat.Add(s2)).InSequence(); strategy.Setup(strat => strat.Add(s3)).InSequence(); strategy.Setup(strat => strat.Add(goalState)).InSequence(); var subject = new GraphSearch<TestState>(strategy.Object); // Act subject.Execute(problem.Object); // Assert strategy.VerifyAll(); } }
public void Execute_WhenTheStartStateIsNotAGoalState_ExpandsTheStartState() { // Arrange var subject = new GraphSearch<TestState>(strategy.Object); strategy.SetupSequence(strat => strat.GetNext()) .Returns(startState) .Returns(goalState); // Act subject.Execute(problem.Object); // Assert problem.Verify(prob => prob.GetSuccessors(startState), Times.Once()); }
public void Execute_WhenTheStartStateIsNotAGoalState_AddsItsSucessorsToTheFringe() { // Arrange var succesors = fixture.CreateMany<TestState>(3).ToArray(); strategy.SetupSequence(strat => strat.GetNext()) .Returns(startState) .Returns(succesors[0]) .Returns(succesors[1]) .Returns(succesors[2]) .Returns(goalState); problem.Setup(prob => prob.GetSuccessors(startState)).Returns(succesors); var subject = new GraphSearch<TestState>(strategy.Object); // Act subject.Execute(problem.Object); // Assert strategy.Verify(strat => strat.Add(succesors[0]), Times.Once()); strategy.Verify(strat => strat.Add(succesors[1]), Times.Once()); strategy.Verify(strat => strat.Add(succesors[2]), Times.Once()); }
public void Execute_WhenTheStartStateIsAGoalState_DoesNotExpandAnyStates() { // Arrange var subject = new GraphSearch<TestState>(strategy.Object); strategy.Setup(strat => strat.GetNext()).Returns(startState); problem.Setup(prob => prob.IsGoalState(startState)).Returns(true); // Act subject.Execute(problem.Object); // Assert problem.Verify(prob => prob.GetSuccessors(It.IsAny<TestState>()), Times.Never()); }
public void Execute_WhenTheSameStateOccursMultipleTimes_OnlyExpandsItOnce() { // Arrange var someState = fixture.Create<TestState>(); strategy.SetupSequence(strat => strat.GetNext()) .Returns(startState) .Returns(someState) .Returns(someState) .Returns(goalState); var subject = new GraphSearch<TestState>(strategy.Object); // Act subject.Execute(problem.Object); // Assert problem.Verify(prob => prob.IsGoalState(someState), Times.Once); problem.Verify(prob => prob.GetSuccessors(someState), Times.Once); }
public void Execute_WhenTheSameStateOccursMultipleTimesAccordingToTheStateEqualityComparer_OnlyExpandsItOnce() { // Arrange var someState = fixture.Create<TestState>(); var someOtherState = fixture.Create<TestState>(); var equalToSomeState = fixture.Create<TestState>(); var equalityComparer = fixture.Create<Mock<IEqualityComparer<TestState>>>(); equalityComparer.Setup(comparer => comparer.Equals(It.IsAny<TestState>(), It.IsAny<TestState>())).Returns(false); equalityComparer.Setup(comparer => comparer.Equals(someState, equalToSomeState)).Returns(true); strategy.SetupSequence(strat => strat.GetNext()) .Returns(startState) .Returns(someState) .Returns(someOtherState) .Returns(equalToSomeState) .Returns(goalState); var subject = new GraphSearch<TestState>(strategy.Object, equalityComparer.Object); // Act subject.Execute(problem.Object); // Assert problem.Verify(prob => prob.IsGoalState(equalToSomeState), Times.Never); problem.Verify(prob => prob.GetSuccessors(equalToSomeState), Times.Never); }
public void Execute_WhenTheFringeBecomesEmptyWithNoGoalStateFound_ThrowsANoSolutionPossibleException() { // Arrange strategy.SetupSequence(strat => strat.IsEmpty()) .Returns(false) .Returns(false) .Returns(true); var subject = new GraphSearch<TestState>(strategy.Object); // Act Action act = () => subject.Execute(problem.Object); // Assert var ex = Assert.Throws<NoSolutionPossibleException>(act); Assert.NotNull(ex.Message); }
public void Execute_WhenProblemIsNull_ThrowsAnArgumentNullException() { // Arrange var subject = new GraphSearch<TestState>(strategy.Object); // Act Action act = () => subject.Execute(null); // Assert var ex = Assert.Throws<ArgumentNullException>(act); Assert.Equal(nameof(problem), ex.ParamName); }
public void Execute_ReturnsTheFirstGoalStateOffTheFringe() { // Arrange var stateBuilder = fixture.Build<TestState>(); var secondGoal = stateBuilder.Create(); strategy.SetupSequence(strat => strat.GetNext()) .Returns(stateBuilder.Create()) .Returns(stateBuilder.Create()) .Returns(goalState) .Returns(stateBuilder.Create()) .Returns(secondGoal); problem.Setup(prob => prob.IsGoalState(secondGoal)).Returns(true); var subject = new GraphSearch<TestState>(strategy.Object); // Act var result = subject.Execute(problem.Object); // Assert Assert.Equal(goalState, result); }
public void Execute_ExpandsStatesInOrderOfTheirAppearanceOffTheFringe() { // Arrange var stateBuilder = fixture.Build<TestState>(); var s1 = stateBuilder.Create(); var s2 = stateBuilder.Create(); var s3 = stateBuilder.Create(); strategy.SetupSequence(strat => strat.GetNext()) .Returns(startState) .Returns(s1) .Returns(s2) .Returns(s3) .Returns(goalState); using (Sequence.Create()) { problem.Setup(prob => prob.GetSuccessors(startState)).InSequence().Returns(fixture.CreateMany<TestState>()); problem.Setup(prob => prob.GetSuccessors(s1)).InSequence().Returns(fixture.CreateMany<TestState>()); problem.Setup(prob => prob.GetSuccessors(s2)).InSequence().Returns(fixture.CreateMany<TestState>()); problem.Setup(prob => prob.GetSuccessors(s3)).InSequence().Returns(fixture.CreateMany<TestState>()); var subject = new GraphSearch<TestState>(strategy.Object); // Act subject.Execute(problem.Object); // Assert problem.Verify(); } }
public void Execute_DoesNotExpandStatesAfterTheGoalStateIsFound() { // Arrange var afterGoal = fixture.Create<TestState>(); strategy.SetupSequence(strat => strat.GetNext()) .Returns(startState) .Returns(goalState) .Returns(afterGoal); var subject = new GraphSearch<TestState>(strategy.Object); // Act subject.Execute(problem.Object); // Assert problem.Verify(prob => prob.IsGoalState(afterGoal), Times.Never); problem.Verify(prob => prob.GetSuccessors(afterGoal), Times.Never); }
public void Execute_AddsTheStartStateToTheFringe() { // Arrange problem.Setup(prob => prob.IsGoalState(It.IsAny<TestState>())).Returns(true); // avoid infinite loop var subject = new GraphSearch<TestState>(strategy.Object); // Act subject.Execute(problem.Object); // Assert strategy.Verify(strat => strat.Add(startState), Times.Once()); }