Пример #1
0
        public void TestSolverReport()
        {
            XmlProvider xmlHelper = new XmlProvider();
            Library lib = xmlHelper.Load(MakePathUIContent("Libraries\\Sasquatch.ssx"));

            List<SolverResult> results = new List<SolverResult>();

            SolverController one = new SolverController(lib.Puzzles[0].MasterMap);
            results.Add(one.Solve());

            SolverController two = new SolverController(lib.Puzzles[1].MasterMap);
            results.Add(two.Solve());

            SolverResultHTML rpt = new SolverResultHTML(results, null);
            rpt.BuildReport();
            rpt.Save(@"solver.html");
        }
Пример #2
0
        protected SolverResult Solve(SokobanMap puzzle)
        {
            using (CodeTimer timer = new CodeTimer("TestSolver.Solve(...)"))
            {
                SolverController controller = new SolverController(puzzle);
                try
                {
                    System.Console.WriteLine(puzzle.ToString());

                    SolverResult results = controller.Solve();
                    if (results.Exception != null)
                    {
                        // Bubble up
                        throw new Exception("Solver Failed Iternally", results.Exception);
                    }

                    if (results.Status == SolverResult.CalculationResult.SolutionFound)
                    {
                        // Check that
                        Assert.IsTrue(results.HasSolution, "State says a solution was found, but none are listed in solutions list");
                    }

                    if (results.HasSolution)
                    {
                        int cc = 0;
                        foreach (Solution solution in results.Solutions)
                        {
                            string testRes = "Error";
                            Assert.IsTrue(solution.Test(puzzle, out testRes), testRes);
                            Console.WriteLine("Testing solution: {0} - {1}", cc, testRes);
                            cc++;
                        }
                    }

                    return results;
                }
                finally
                {
                    timer.Stop();
                    Console.WriteLine(controller.DebugReport.ToString(new DebugReportFormatter()));
                    System.Console.WriteLine("Total Time: " + timer.Duration(1));
                    System.Console.WriteLine("---");
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Start the solver (this will always to run with a non UI worker thread)
        /// </summary>
        private void Solve()
        {
            try
            {
                complete = false;
                solverEvalStatus = EvalStatus.InProgress;
                solver = new SolverController(map);

                solver.ExitConditions.StopOnSolution = exitConditions.cbStopOnSolution.Checked;
                solver.ExitConditions.MaxDepth = (int)exitConditions.upMaxDepth.Value;
                solver.ExitConditions.MaxNodes = (int)exitConditions.upMaxNodes.Value;
                solver.ExitConditions.MaxItterations = (int)exitConditions.upMaxItter.Value;
                solver.ExitConditions.MaxTimeSecs = (int)(exitConditions.upMaxTime.Value * 60);

                solverEvalStatus = solver.Solve();
                complete = true;
            }
            catch (Exception ex)
            {
                lastException = ex;
            }

            Invoke(OnComplete); // Set to SolverComplete
        }
Пример #4
0
        public override ReturnCodes Execute(ConsoleCommandController controller)
        {
            if (ArgReport != null)
            {
                reportFile = File.CreateText(ArgReport);
            }

            if (!string.IsNullOrEmpty(ArgSolverLibrary))
            {
                DateTime start = DateTime.Now;

                // MODE: Progress + Library
                ProgressComponent comp = new ProgressComponent();
                Library lib = null;

                // It is exists load...
                if (File.Exists(ArgSolverLibrary))
                {
                    comp.Load(ArgSolverLibrary);
                }

                // If a library is specified, merge/add it...
                if (!string.IsNullOrEmpty(ArgLibrary))
                {
                    XmlProvider xml = new XmlProvider();
                    lib = xml.Load(ArgLibrary);
                    comp.Add(lib);
                }

                int cc = 0;
                foreach (SolverPuzzle item in comp.Items)
                {
                    if (ArgNonSolutionOnly && item.HasSolution)
                    {
                        controller.DisplayLable("Skipping solution", item.Name);
                        cc++;
                        continue;
                    }

                    TimeSpan left = TimeSpan.FromSeconds((double) (comp.Items.Count - cc)*ArgMaxTime);
                    controller.DisplayLable("Est. Time Left", left.ToString());
                    controller.DisplayLable("Time Elapsed", (DateTime.Now-start).ToString());
                    controller.Display("==========================================================================");
                    controller.Display("");
                    controller.DisplayLable("Attempt", string.Format("{0}/{1} {2}%, maxtime={3}", cc, comp.Items.Count, cc*100/comp.Items.Count, TimeSpan.FromSeconds(ArgMaxTime)));
                    controller.DisplayLable("Name", item.Name);
                    controller.DisplayLable("Rating", string.Format("{0}, size=({1}, {2})", item.Rating, item.Width, item.Height));

                    controller.Display(StringHelper.Join(item.NormalisedMap, null, Environment.NewLine));

                    SokobanMap map = new SokobanMap();
                    map.SetFromStrings(item.NormalisedMap);
                    using (SolverController ctrl = new SolverController(map))
                    {
                        ctrl.ExitConditions.MaxDepth = 1000;
                        ctrl.ExitConditions.MaxItterations = 10000000;
                        ctrl.ExitConditions.MaxTimeSecs = (float) ArgMaxTime;

                        SolverResult res = ctrl.Solve();
                        if (res.Exception != null)
                        {
                            controller.Display(res.Exception);
                        }
                        controller.DisplayLable("Result", res.Summary);

                        comp.Update(res);
                    }

                    CheckForceGC();
                    controller.Display("---------------------------------------------------------------------------");

                    if (System.Console.KeyAvailable && System.Console.ReadKey().KeyChar == 'Q')
                    {
                        controller.Display("BREAK REQUESTED... Exiting");
                        break;
                    }

                    cc++;
                }

                comp.Save(ArgSolverLibrary);

            }
            else if (!string.IsNullOrEmpty(ArgLibrary))
            {
                // MODE: Only Library
                XmlProvider xml = new XmlProvider();
                Library lib = xml.Load(ArgLibrary);

                if (ArgPuzzle == "*")
                {
                    int cc = 0;
                    foreach (Puzzle puzzle in lib.Puzzles)
                    {
                        cc++;
                        controller.Display("");
                        controller.DisplayLable("Attempting", string.Format("{0:000} of {1:000}", cc, lib.Puzzles.Count));

                        SolvePuzzle(puzzle.MasterMap, puzzle.GetDetails().Name);

                        CheckForceGC();

                        if (System.Console.KeyAvailable && System.Console.ReadKey().KeyChar == 'Q')
                        {
                            controller.Display("BREAK REQUESTED... Exiting");
                            break;
                        }

                    }
                }
                else
                {
                    Puzzle pux = lib.GetPuzzleByID(ArgPuzzle);
                    SolvePuzzle(pux.MasterMap, pux.GetDetails().Name);
                }
            }

            if (reportFile != null)
            {
                reportFile.Close();
                reportFile.Dispose();
            }

            return ReturnCodes.OK;
        }
Пример #5
0
        private void SolvePuzzle(PuzzleMap puz, string Name)
        {
            using (SolverController ctrl = new SolverController(puz))
            {
                ctrl.ExitConditions.MaxDepth = 1000;
                ctrl.ExitConditions.MaxItterations = 10000000;
                ctrl.ExitConditions.MaxTimeSecs = (float) ArgMaxTime;

                SolverResult res = ctrl.Solve();
                if (res.Exception != null)
                {
                    controller.Display(res.Exception);
                }

                controller.DisplayLable("Name", Name);
                controller.DisplayLable("Result", res.StatusString);
                controller.DisplayLable("Summary", res.Summary);
                controller.DisplayLable("Exit Conditions", ctrl.ExitConditions.ToString());

                if (res.HasSolution)
                {
                    controller.DisplayLable("Solution", res.Solutions[0].Steps);
                }

                if (reportFile != null)
                {
                    reportFile.WriteLine("###########################################################################");
                    reportFile.WriteLine(string.Format("{0} ===> {1}", Name, res.Summary));
                    reportFile.WriteLine("###########################################################################");
                    reportFile.WriteLine(res.Info.ToString());
                    reportFile.WriteLine(res.DebugReport.ToString(new DebugReportFormatter()));
                    reportFile.WriteLine("---------------------------------------------------------------------------");
                    reportFile.WriteLine("");
                }
            }
        }