示例#1
0
        public void NoSolutions()
        {
            var command = new SolverCommand
            {
                Puzzle = Puzzle.Builder.FromLines(new[]
                {
                    "##########",
                    "#O....X..#",
                    "#O..P..X.#",
                    "#O....X..#",
                    "##########"
                }),
                Report         = new XUnitOutput(outp),
                ExitConditions = ExitConditions.OneMinute()
            };

            var solver = new SingleThreadedForwardSolver(new SolverNodeFactoryTrivial());
            var state  = solver.Init(command) as SolverBaseState;
            var result = solver.Solve(state);

            Assert.Empty(state.Solutions);
            Assert.NotEmpty(state.Root.Children);

            foreach (var n in state.Root.Recurse())
            {
                outp.WriteLine(n.ToString());
            }
            Assert.Equal(4, state.Root.CountRecursive()); // NOTE: Should this not be 5 = 2 valid pushes, then 3 dead
            Assert.True(state.Root.Recurse().All(x => ((SolverNode)x).IsClosed));
            Assert.Equal(ExitConditions.Conditions.ExhaustedTree, result);
        }
示例#2
0
        public void Exhause_Default()
        {
            var command = new SolverCommand
            {
                Puzzle         = Puzzle.Builder.DefaultTestPuzzle(),
                Report         = new XUnitOutput(outp),
                ExitConditions = new ExitConditions()
                {
                    StopOnSolution = false,
                    Duration       = TimeSpan.FromMinutes(5)
                }
            };

            var solver = new SingleThreadedForwardSolver(new SolverNodeFactoryTrivial());
            var state  = solver.Init(command) as SolverBaseState;
            var result = solver.Solve(state);

            Assert.NotEmpty(state.Root.Children);

            Assert.True(state.Root.Recurse().All(x => ((SolverNode)x).IsClosed ||
                                                 x.Status == SolverNodeStatus.Solution ||
                                                 x.Status == SolverNodeStatus.SolutionPath));


            Assert.True(state.Queue is SolverQueue);
            if (state.Queue is SolverQueue qq)
            {
                Assert.Equal(0, qq.Count);
            }
        }
        public void NoSolutions()
        {
            var command = new SolverCommand
            {
                Puzzle = Puzzle.Builder.FromLines(new[]
                {
                    "##########",
                    "#O....X..#",
                    "#O..P..X.#",
                    "#O....X..#",
                    "##########"
                }),
                Report         = TextWriter.Null,
                ExitConditions = ExitConditions.OneMinute()
            };

            var solver = new SingleThreadedForwardSolver(new SolverNodeFactoryTrivial());
            var state  = solver.Init(command) as SolverBaseState;
            var result = solver.Solve(state);

            Assert.Empty(state.Solutions);
            Assert.NotEmpty(state.Root.Children);
            Assert.Equal(2, state.Root.CountRecursive());
            Assert.True(state.Root.All(x => ((SolverNode)x).IsClosed));
            Assert.Equal(ExitConditions.Conditions.ExhaustedTree, result);
        }
        public void Assemble()
        {
            var exit = new ExitConditions
            {
                Duration       = TimeSpan.FromSeconds(1),
                StopOnSolution = true,
                TotalNodes     = int.MaxValue,
                TotalDead      = int.MaxValue
            };
            var command = new SolverCommand
            {
                Puzzle = Puzzle.Builder.DefaultTestPuzzle(),

                ExitConditions = exit
            };

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

            solver.Solve(result);
            result.ThrowErrors();

            var root     = ((SolverBaseState)result).Root;
            var allNodes = root.Recurse().ToArray();

            var mem    = new MemoryStream();
            var writer = new BinaryNodeSerializer();

            using (var sw = new BinaryWriter(mem, Encoding.Unicode, true))
            {
                writer.Write(sw, allNodes);
            }

            outp.WriteLine($"Memory Stream Size = {allNodes.Length}nodes => {mem.Length}b");

            Assert.Equal(allNodes.Length, root.CountRecursive());

            mem.Seek(0, SeekOrigin.Begin);

            using (var sr = new BinaryReader(mem))
            {
                var t = writer.AssembleTree(sr);

                Assert.True(t.RecursiveAll().Any(x => x.Status != SolverNodeStatus.UnEval));
                Assert.Equal(root.CountRecursive(), t.CountRecursive());
            }
        }
示例#5
0
        public void ForwardSingle()
        {
            var solverCommand = new SolverCommand()
            {
                Puzzle         = Puzzle.Builder.DefaultTestPuzzle(),
                ExitConditions = ExitConditions.Default3Min(),
            };
            var solver      = new SingleThreadedForwardSolver(new SolverNodeFactoryTrivial());
            var solverState = solver.Init(solverCommand);

            solver.Solve(solverState);
            if (!solverState.HasSolution)
            {
                throw new Exception();
            }
        }
        public void WriteDefaultForwardSolution()
        {
            var exit = new ExitConditions
            {
                Duration       = TimeSpan.FromSeconds(10),
                StopOnSolution = true,
                TotalNodes     = int.MaxValue,
                TotalDead      = int.MaxValue
            };
            var command = new SolverCommand
            {
                Puzzle = Puzzle.Builder.DefaultTestPuzzle(),

                ExitConditions = exit,
                Inspector      = (s) =>
                {
                    if (s.GetHashCode() == 30759)
                    {
                        outp.WriteLine(s.ToString());
                        return(true);
                    }
                    return(false);
                }
            };

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

            solver.Solve(result);
            result.ThrowErrors();
            Assert.True(result.HasSolution);

            var root = ((SolverBaseState)result).Root;

            using (var f = File.Create(Path.Combine(TestHelper.GetDataPath(), "./SavedState/SQ1~P1-default.ssbn")))
            {
                var writer = new BinaryNodeSerializer();
                writer.WriteTree(new BinaryWriter(f), root);
            }
        }
示例#7
0
        public void Exhause()
        {
            var command = new SolverCommand
            {
                Puzzle = Puzzle.Builder.FromLines(new[]
                {
                    "##########",
                    "#O....XP.#",
                    "#O.....X.#",
                    "#O....X..#",
                    "##########"
                }),
                Report         = new XUnitOutput(outp),
                ExitConditions = new ExitConditions()
                {
                    StopOnSolution = false,
                    Duration       = TimeSpan.FromHours(1)
                }
            };

            var solver = new SingleThreadedForwardSolver(new SolverNodeFactoryTrivial());
            var state  = solver.Init(command) as SolverBaseState;
            var result = solver.Solve(state);

            //
            //
            // using (var f = File.CreateText(nameof(Exhause) + ".dot"))
            // {
            //     // dot .\Exhause.dot -o file.svg -T svg
            //     new GraphVisRender().Render(state.Root.Recurse(), f);
            // }
            //

            Assert.NotEmpty(state.Solutions);
            Assert.NotEmpty(state.Root.Children);
            Assert.True(state.Root.Recurse().All(x => ((SolverNode)x).IsClosed ||
                                                 x.Status == SolverNodeStatus.Solution ||
                                                 x.Status == SolverNodeStatus.SolutionPath));

            Assert.Equal(ExitConditions.Conditions.ExhaustedTree, result);
        }
示例#8
0
        public void NoSolutions_InvalidPuzzle()
        {
            var command = new SolverCommand
            {
                Puzzle = Puzzle.Builder.FromLines(new[]
                {
                    // More goals than crates - strictly not valid
                    "##########",
                    "#O...X..O#",
                    "#O..XPX.O#",
                    "#O..X.X.O#",
                    "##########"
                }),
                Report         = new XUnitOutput(outp),
                ExitConditions = ExitConditions.OneMinute()
            };

            var solver = new SingleThreadedForwardSolver(new SolverNodeFactoryTrivial());

            Assert.Throws <InvalidDataException>(() =>
            {
                var state = solver.Init(command) as SolverBaseState;
            });
        }