private SolverState PerformStandardTest(Puzzle puzzle, ExitConditions exit = null)
        {
            exit = exit ?? new ExitConditions
            {
                Duration       = TimeSpan.FromSeconds(60),
                StopOnSolution = true,
                TotalNodes     = int.MaxValue,
                TotalDead      = int.MaxValue
            };
            // arrange
            var solver  = new SingleThreadedReverseSolver(new SolverNodeFactoryTrivial());
            var command = new SolverCommand
            {
                Puzzle         = puzzle.Clone(),
                Report         = new XUnitOutput(outp),
                ExitConditions = exit,
                Inspector      = node =>
                {
                    if (node.GetHashCode() == 929793)
                    {
                        outp.WriteLine(node.ToString());
                        return(true);
                    }

                    return(false);
                }
            };

            // act
            var result = solver.Init(command);

            solver.Solve(result);
            // Console.WriteLine(result.ExitDescription);
            // Console.WriteLine(SolverHelper.GenerateSummary(result));
            result.ThrowErrors();

            // assert
            Assert.NotNull(result);

            foreach (var solution in result.SolutionsNodes)
            {
                var p = solution.PathToRoot().ToList();
                p.Reverse();
            }

            foreach (var sol in result.Solutions)
            {
                Assert.True(SolverHelper.CheckSolution(command.Puzzle, sol, out var error), "Solution is INVALID! " + error);
            }

            return(result);
        }
        public void R001_Regression_CheckPath()
        {
            var puzzle = Puzzle.Builder.FromLines(new[]
            {
                "##########",
                "#O...X...#",
                "#O..XPX.O#",
                "##########"
            });

            string desc = null;

            SolverHelper.CheckSolution(puzzle, new Path("RRLLLLLRRRRULLLL"), out desc);
            Assert.Null(desc);
        }
        private SolverState PerformStandardTest(
            Puzzle puzzle,
            ExitConditions exit = null,
            Func <SolverNode, bool>?inspector = null
            )
        {
            exit = exit ?? new ExitConditions
            {
                Duration       = TimeSpan.FromSeconds(60),
                StopOnSolution = true,
                TotalNodes     = int.MaxValue,
                TotalDead      = int.MaxValue
            };
            // arrange
            var solver  = new SingleThreadedForwardReverseSolver(new SolverNodeFactoryTrivial());
            var command = new SolverCommand
            {
                Puzzle         = puzzle.Clone(),
                Report         = new XUnitOutput(outp),
                ExitConditions = exit,
                Inspector      = inspector
            };

            // act
            var result = solver.Init(command);

            solver.Solve(result);
            Console.WriteLine(result.ExitDescription);
            Console.WriteLine(SolverHelper.GenerateSummary(result));
            result.ThrowErrors();

            // assert
            Assert.NotNull(result);

            Assert.True(result.HasSolution);
            Assert.NotNull(result.Solutions);
            Assert.NotEmpty(result.Solutions);

            foreach (var sol in result.Solutions)
            {
                Console.WriteLine("Path: {0}", sol);
                string error = null;
                Assert.True(SolverHelper.CheckSolution(command.Puzzle, sol, out error),
                            "Solution is INVALID! " + error);
            }

            return(result);
        }
Example #4
0
        private void PuzzleShouldHaveSolution(ISolver solver, Puzzle puzzle, ExitConditions exit = null,
                                              bool verbose = false)
        {
            if (exit == null)
            {
                exit = new ExitConditions
                {
                    Duration       = TimeSpan.FromSeconds(60),
                    StopOnSolution = true,
                    TotalNodes     = int.MaxValue,
                    TotalDead      = int.MaxValue
                }
            }
            ;
            var command = new SolverCommand
            {
                Puzzle         = puzzle,
                Report         = new XUnitOutput(outp),
                ExitConditions = exit
            };

            // act
            var result = solver.Init(command);

            solver.Solve(result);
            Console.WriteLine(result.ExitDescription);
            Console.WriteLine(SolverHelper.GenerateSummary(result));
            result.ThrowErrors();

            // assert
            Assert.NotNull(result);
            Assert.NotNull(result.Solutions);
            Assert.True(result.HasSolution);


            foreach (var sol in result.Solutions)
            {
                string error = null;
                Assert.True(SolverHelper.CheckSolution(command.Puzzle, sol, out error),
                            "Solution is INVALID! " + error);
            }
        }