public void SearchTest() { var problem = new EightPuzzleProblem(new Board(new byte[, ] { { 1, 2, 3 }, { 4, 5, 6 }, { 0, 7, 8 } })); var result = problem.CreateDefaultSolver().Search(problem).ToList(); Assert.NotEmpty(result); }
public SolutionSearchResult Solve(Board kInitialBoard, EAlgorithm eAlgorithmOption, EHeuristicFunction eHeuristicFunctionOption, CancellationToken kCancellationToken) { EightPuzzleProblem kProblem = new EightPuzzleProblem(kInitialBoard); ISearch <EightPuzzleState> kSearch = CreateSearch(kInitialBoard, eAlgorithmOption, eHeuristicFunctionOption); Stopwatch kStopwatch = new Stopwatch(); kStopwatch.Start(); List <EightPuzzleState> kResult = kSearch.Search(kProblem, kCancellationToken).ToList(); kStopwatch.Stop(); return(new SolutionSearchResult(kResult.Any(), kResult, kStopwatch.Elapsed)); }
public SolutionSearchResult Solve(Board initialBoard, Algorithm algorithm, HeuristicFunction heuristicFunction, CancellationToken cancellationToken) { var problem = new EightPuzzleProblem(initialBoard); var search = CreateSearch(initialBoard, algorithm, heuristicFunction); var stopwatch = new Stopwatch(); stopwatch.Start(); var result = search.Search(problem, cancellationToken).ToList(); stopwatch.Stop(); return(new SolutionSearchResult(result.Any(), result, stopwatch.Elapsed)); }
public void ShouldFindOptimal(Board board, int optimalSolution) { Assert.True(board.IsSolvable()); var problem = new EightPuzzleProblem(board); var h = new ManhattanHeuristicFunction(Board.CreateGoalBoard(board.RowCount, board.ColumnCount)); var algorithms = new ISearch <EightPuzzleState>[] { new AStarSearch <EightPuzzleState>(h), new RecursiveBestFirstSearch <EightPuzzleState>(h), }; foreach (var algorithm in algorithms) { var result = algorithm.Search(problem).ToList(); Assert.NotEmpty(result); Assert.Equal(optimalSolution, result.Count - 1); } }
public void RandomBoardSearchTest() { var sizes = new[] { new { RowCount = 2, ColumnCount = 2 }, new { RowCount = 2, ColumnCount = 3 }, new { RowCount = 3, ColumnCount = 2 }, new { RowCount = 3, ColumnCount = 3 }, new { RowCount = 4, ColumnCount = 2 }, new { RowCount = 2, ColumnCount = 4 }, //new { RowCount = 4, ColumnCount = 4 }, }; foreach (var size in sizes) { for (int i = 0; i < 3; i++) { var problem = new EightPuzzleProblem(Board.GenerateSolvableBoard(size.RowCount, size.ColumnCount)); var result = problem.CreateDefaultSolver().Search(problem).ToList(); Assert.NotEmpty(result); } } foreach (var size in sizes.Take(3)) { for (int i = 0; i < 3; i++) { var problem = new EightPuzzleProblem(Board.GenerateSolvableBoard(size.RowCount, size.ColumnCount)); var result = new IterativeDeepeningSearch <EightPuzzleState>().Search(problem).ToList(); Assert.NotEmpty(result); } } }