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); }
private void RunSolverWith(ISolver solver) { SolverException = null; SolverState = null; Solver = solver; SolverCommand = new SolverCommand() { Puzzle = Puzzle, ExitConditions = ExitConditions.Default3Min(), }; SolverTask = Task.Run(() => { try { SolverState = Solver.Init(SolverCommand); Solver.Solve(SolverState); } catch (Exception e) { SolverException = e; } }); }
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); }
/// <summary> /// Executes loop with getting messages from other roles /// </summary> /// <param name="exitConditions">List of delegates that return true if there is finish</param> public void Run(ExitConditions exitConditions = null) { bool finish = false; while (!finish) //particular scene merging { if (exitConditions != null && exitConditions.IsExit()) { return; } CloudQueueMessage msg = m_listenedQueue.GetMessage(); if (msg == null) { System.Threading.Thread.Sleep(1000); continue; } m_listenedQueue.DeleteMessage(msg); T merMes = m_listenedQueue.ConvertMessage(msg); if (m_messageResponses.ContainsKey(merMes.GetType().Name)) { MessageResponse actionForMessage = m_messageResponses[merMes.GetType().Name]; finish = actionForMessage(merMes); } else { m_log.Warning("Incorrect type of message"); } } }
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); }
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()); } }
public void ForwardReverseSingle() { var solverCommand = new SolverCommand() { Puzzle = Puzzle.Builder.DefaultTestPuzzle(), ExitConditions = ExitConditions.Default3Min(), }; var solver = new SingleThreadedForwardReverseSolver(new SolverNodeFactoryTrivial()); var solverState = solver.Init(solverCommand); solver.Solve(solverState); if (!solverState.HasSolution) { throw new Exception(); } }
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); } }
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); } }
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; }); }