Пример #1
0
        internal static void Solve(PuzzleSource source, SolvingMethod method, DirectoryInfo directory, int id, int threads, int timeout, bool verbose)
        {
            try
            {
                GameState gameState = GetPuzzle(source, directory, id, verbose);

                Solver solver;
                double timeElapsed;

                switch (method)
                {
                case SolvingMethod.Sequential:
                    solver = new SequentialSolver(gameState, timeout);
                    Logging.Message($"Attempting to solve using {method} solver...", verbose);
                    break;

                case SolvingMethod.Parallel:
                    solver = new ThreadPoolSolver(gameState, timeout, threads);
                    Logging.Message($"Attempting to solve puzzle #{id} using {method} solver ({threads} threads)...");
                    break;

                default:
                    throw new NotImplementedException();
                }

                timeElapsed = TimeSolverAverage(solver, 1);

                Logging.Message($"{solver.Solutions.Count( x => true )} solution(s) found in {timeElapsed:0.00E+00} seconds:");

                foreach (GameState solution in solver.Solutions)
                {
                    Console.WriteLine();
                    solution.Print();
                }

                Console.WriteLine();
            }
            catch (NonogramException exception)
            {
                Logging.Error(exception.Message);
            }
            catch (TimeoutException exception)
            {
                Logging.Error(exception.Message);
            }
            catch (NotImplementedException)
            {
                Logging.Error("Feature not implemented.");
            }
            catch (System.Exception exception)
            {
                #if DEBUG
                Logging.Error("Unexpected exception");
                Console.WriteLine(exception);
                #else
                Logging.Error("Unknown");
                #endif
            }
        }
Пример #2
0
        internal static void Benchmark(PuzzleSource source, SolvingMethod[] methods, DirectoryInfo directory, string idSet, int trials, int timeout, bool verbose)
        {
            List <BenchmarkData> benchmarkData = new List <BenchmarkData>();

            foreach (int id in ParseID(idSet))
            {
                GameState gameState;

                try
                {
                    gameState = GetPuzzle(source, directory, id, verbose);

                    foreach (SolvingMethod method in methods)
                    {
                        Solver solver;
                        double time;

                        switch (method)
                        {
                        case SolvingMethod.Sequential:
                            solver = new SequentialSolver(gameState, timeout);

                            Logging.Message($"Attempting to solve puzzle #{id} using {method} solver...");

                            time = TimeSolverAverage(solver, trials);
                            benchmarkData.Add(new BenchmarkData(id, gameState.Width, gameState.Height, method, 1, time));
                            break;

                        case SolvingMethod.Parallel:
                            for (int threads = 1; threads <= Environment.ProcessorCount; threads++)
                            {
                                solver = new SequentialSolver(gameState, timeout);

                                Logging.Message($"Attempting to solve puzzle #{id} using {method} solver ({threads} threads)...");

                                time = TimeSolverAverage(solver, trials);
                                benchmarkData.Add(new BenchmarkData(id, gameState.Width, gameState.Height, method, threads, time));
                            }
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                    }
                }
                catch (NonogramException exception)
                {
                    Logging.Error(exception.Message);
                }
                catch (TimeoutException exception)
                {
                    Logging.Error(exception.Message);
                }
                catch (NotImplementedException)
                {
                    Logging.Error("Feature not implemented.");
                }
                catch (System.Exception exception)
                {
                    #if DEBUG
                    Logging.Error("Unexpected exception");
                    Console.WriteLine(exception);
                    #else
                    Logging.Error("Unknown");
                    #endif
                }
            }

            DateTime dateTime = DateTime.Now;

            FileInfo benchmarkFile = new FileInfo($"{directory.FullName}benchmark_{DateTime.Now:dd-MM-yyyy_hh-mm}.csv");

            Logging.Message($"Saving benchmark data to file: {benchmarkFile.Name}");

            using (StreamWriter writer = new StreamWriter(benchmarkFile.FullName))
            {
                writer.WriteLine(BenchmarkData.HeaderString(trials));
                foreach (BenchmarkData data in benchmarkData)
                {
                    writer.WriteLine(data);
                }
            }
        }