Beispiel #1
0
        private static void MeasuringAverageAlgorithmSolvingTime()
        {
            const int REPEATS = 2;

            System.Console.WriteLine("Measuring average algorithm solving time:");
            System.Console.WriteLine("");

            FileInfo[] files = new DirectoryInfo(Directories.Solutions).GetFiles(FileExtensions.XmlZipMask);

            var solutions1 = from file in files.AsParallel()
                             select new
            {
                intermediate_solution = SudokuIntermediateSolution.LoadFromFile(file.FullName),
                fileName = file.FullName
            };

            var solutions2 = (from solution in solutions1
                              group solution by solution.intermediate_solution.Solution.Type into g
                              select(from obj in g
                                     orderby obj.fileName
                                     select obj).ToArray()).ToArray();

            foreach (var sols_gr in solutions2)
            {
                var sols = SudokuSolutionNode.FilterByOptions(sols_gr.Select(gr => gr.intermediate_solution.Solution).ToList());

                if (sols_gr.Length == 20)
                {
                    if (SudokuOptions.Current.IncludeBoxes)
                    {
                        sols = sols.Take(10).ToList();
                    }
                    else
                    {
                        sols = sols.Skip(10).Take(10).ToList();
                    }
                }

                var sols_gr2 = (from gr in sols_gr
                                where sols.Contains(gr.intermediate_solution.Solution)
                                select gr).ToList();

                System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start();

                foreach (var solution in sols_gr2)
                {
                    SudokuIntermediateSolution intermediate_solution = solution.intermediate_solution;

                    bool bError = false;

                    for (int i = 0; i < REPEATS; i++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            if (!intermediate_solution.Test(false))
                            {
                                bError = true;
                            }

                            intermediate_solution = intermediate_solution.Rotate();
                        }
                    }

                    if (bError)
                    {
                        System.Console.WriteLine("Can't solve: {0}", solution.fileName);
                    }
                }

                stopwatch.Stop();

                double time = stopwatch.ElapsedMilliseconds * 1.0 / sols_gr.Length / REPEATS / 4;

                s_algorithmToTime[sols_gr.First().intermediate_solution.Solution.Type] = time;

                System.Console.WriteLine("{0} ({1}): {2} [ms]", sols_gr.First().intermediate_solution.Solution.Type, sols_gr.Length, time.ToString("G4"));
            }
        }
        private bool LoadFile(string a_fileName)
        {
            if (a_fileName.Equals(""))
            {
                return(false);
            }

            solutionsCheckedListBox.SelectedSolution = null;

            SudokuBoard board = SudokuBoard.LoadFromFile(a_fileName);

            if (board != null)
            {
                Cursor.Current = Cursors.WaitCursor;

                try
                {
                    solutionsTree.InitTree(board);
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }
            }
            else
            {
                SudokuIntermediateSolution intermediate_solution = SudokuIntermediateSolution.LoadFromFile(a_fileName);

                if (intermediate_solution == null)
                {
                    MessageBox.Show("Loaded file is invalid", "Sudoku", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                else
                {
                    Cursor.Current = Cursors.WaitCursor;

                    try
                    {
                        solutionsTree.InitTree(intermediate_solution.Before);
                        solutionsCheckedListBox.SelectedSolution =
                            solutionsCheckedListBox.Node.Nodes.Select(n => n.Solution).FirstOrDefault(s => s.Equals(intermediate_solution.Solution));
                    }
                    finally
                    {
                        Cursor.Current = Cursors.Default;
                    }
                }
            }

            LastOpenFiles.Instance.Opened(a_fileName);
            openFileDialog.FileName      = new FileInfo(a_fileName).Name;
            saveBoardFileDialog.FileName = new FileInfo(a_fileName).Name;
            saveIntermediateSolutionFileDialog.FileName = new FileInfo(a_fileName).Name;

            Text = "Sudoku - " + a_fileName;

            UndoRedoManager.Instance.Clear();

            return(true);
        }
        public void Test_Solutions_Solvable()
        {
            SudokuOptions.Current.ShowAllSolutions = false;
            SudokuOptions.Current.IncludeBoxes     = true;

            FileInfo[] files = new DirectoryInfo(Directories.Solutions).GetFiles(FileExtensions.XmlZipMask);

            ProgressIndicator pi = new ProgressIndicator(System.Reflection.MethodBase.GetCurrentMethod().Name);

            var solutions1 = (from file in files.AsParallel()
                              select new
            {
                intermediate_solution = SudokuIntermediateSolution.LoadFromFile(file.FullName),
                fileName = file.FullName
            }).ToArray();

            var solutions2 = (from sol in solutions1.AsParallel()
                              select new[]
            {
                new
                {
                    intermediate_solution = sol.intermediate_solution,
                    fileName = sol.fileName,
                    rotate = 0
                },
                new
                {
                    intermediate_solution = sol.intermediate_solution.Rotate(),
                    fileName = sol.fileName,
                    rotate = 1
                },
                new
                {
                    intermediate_solution = sol.intermediate_solution.Rotate().Rotate(),
                    fileName = sol.fileName,
                    rotate = 2
                },
                new
                {
                    intermediate_solution = sol.intermediate_solution.Rotate().Rotate().Rotate(),
                    fileName = sol.fileName,
                    rotate = 3
                }
            }).SelectMany().ToArray();

            var solutions3 = (from sol in solutions2
                              group sol by sol.intermediate_solution.Solution.Type into gr
                              select gr.ToArray()).ToArray();

            ConcurrentBag <string> unsolvable = new ConcurrentBag <string>();

            int count = solutions3.Sum(gr => gr.Count());
            ConcurrentCounter counter = new ConcurrentCounter();

            Parallel.ForEach(ChunkPartitioner.Create(solutions3.SelectMany(), 10), (solution) =>
            {
                if (!solution.intermediate_solution.Test(true))
                {
                    unsolvable.Add("unsolvable; rotate_" + solution.rotate + "; " + solution.fileName);
                }

                counter.Increment();

                pi.AddLine((counter.Value * 100 / count).ToString());
            });

            if (unsolvable.Count > 0)
            {
                foreach (var file in unsolvable)
                {
                    TestContext.WriteLine(file);
                }

                Assert.Fail();
            }
        }
        public void Test_Solutions_Unique()
        {
            ProgressIndicator pi = new ProgressIndicator(System.Reflection.MethodBase.GetCurrentMethod().Name);

            FileInfo[] files = new DirectoryInfo(Directories.Solutions).GetFiles(FileExtensions.XmlZipMask);

            var solutions1 = (from file in files.AsParallel()
                              select new
            {
                intermediate_solution = SudokuIntermediateSolution.LoadFromFile(file.FullName),
                fileName = file.FullName,
            }).ToArray();

            solutions1.ForEach(example => Assert.IsNotNull(example.intermediate_solution, example.fileName));

            var solutions2 = (from sol in solutions1.AsParallel()
                              select new[]
            {
                new
                {
                    intermediate_solution = sol.intermediate_solution,
                    fileName = sol.fileName,
                    rotate = 0
                },
                new
                {
                    intermediate_solution = sol.intermediate_solution.Rotate(),
                    fileName = sol.fileName,
                    rotate = 1
                },
                new
                {
                    intermediate_solution = sol.intermediate_solution.Rotate().Rotate(),
                    fileName = sol.fileName,
                    rotate = 2
                },
                new
                {
                    intermediate_solution = sol.intermediate_solution.Rotate().Rotate().Rotate(),
                    fileName = sol.fileName,
                    rotate = 3
                }
            }).SelectMany().ToArray();

            var solutions_gr = (from obj in solutions2
                                group obj by obj.intermediate_solution.Solution.Type into gr
                                select(from obj in gr
                                       orderby obj.fileName
                                       select obj).ToArray()).ToArray();

            solutions_gr = (from gr in solutions_gr
                            select(gr.Length == 80) ? new[] { gr.Take(40).ToArray(), gr.Skip(40).Take(40).ToArray() } :
                            new[] { gr }).SelectMany(gr => gr.ToArray()).ToArray();

            ConcurrentBag <string> not_uniques = new ConcurrentBag <string>();

            ConcurrentCounter counter = new ConcurrentCounter();
            int count = solutions_gr.Sum(gr => gr.Length);

            Parallel.ForEach(solutions_gr, (solution_gr) =>
            {
                var sols = solution_gr;

                foreach (var solution1 in sols)
                {
                    foreach (var solution2 in sols.Except(solution1))
                    {
                        if (sols.IndexOf(solution1) >= sols.IndexOf(solution2))
                        {
                            continue;
                        }
                        if ((solution1.fileName == solution2.fileName))
                        {
                            continue;
                        }

                        if (solution1.intermediate_solution.Equals(solution2.intermediate_solution))
                        {
                            not_uniques.Add("are the same");
                            not_uniques.Add("solution1; " + solution1.rotate + " - " + solution1.fileName);
                            not_uniques.Add("solution2; " + solution2.rotate + " - " + solution2.fileName);
                        }
                    }

                    counter.Increment();
                    pi.AddLine((counter.Value * 100 / count).ToString());
                }
            });

            if (not_uniques.Count != 0)
            {
                foreach (var line in not_uniques)
                {
                    TestContext.WriteLine(line);
                }

                Assert.Fail();
            }
        }
        public void Test_Solutions_Quantity()
        {
            SudokuOptions.Current.ShowAllSolutions = false;
            SudokuOptions.Current.IncludeBoxes     = true;

            ProgressIndicator pi = new ProgressIndicator(System.Reflection.MethodBase.GetCurrentMethod().Name);

            FileInfo[] files = new DirectoryInfo(Directories.Solutions).GetFiles(FileExtensions.XmlZipMask);

            var solutions = (from file in files.AsParallel()
                             select new
            {
                intermediate_solution = SudokuIntermediateSolution.LoadFromFile(file.FullName),
                fileName = file.FullName,
            }).ToArray();

            solutions.ForEach(example => Assert.IsNotNull(example.intermediate_solution, example.fileName));

            var solutions_gr = (from obj in solutions
                                group obj by obj.intermediate_solution.Solution.Type into gr
                                select(from obj in gr
                                       orderby obj.fileName
                                       select obj).ToArray()).ToArray();

            ConcurrentBag <string> bad_quantities = new ConcurrentBag <string>();

            Parallel.ForEach(solutions_gr, (solution_gr) =>
            {
                pi.AddLine((solutions_gr.IndexOf(solution_gr) * 100 / solutions_gr.Count()).ToString());

                if (new[] { SudokuSolutionType.XWing, SudokuSolutionType.JellyFish,
                            SudokuSolutionType.SwordFish }.Contains(
                        solution_gr.First().intermediate_solution.Solution.Type))
                {
                    if (solution_gr.Count() < 20)
                    {
                        bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - less then 20");
                    }
                    else if (solution_gr.Count() > 20)
                    {
                        bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - more then 20");
                    }
                    else
                    {
                        SudokuOptions.Current.IncludeBoxes = false;

                        var gr1 = (from obj in solution_gr.Take(10)
                                   select obj.intermediate_solution.Solution).ToList();
                        int c1 = SudokuSolutionNode.FilterByOptions(gr1).Count();
                        if (c1 == 0)
                        {
                            bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type +
                                               " - solutions from 1 to 10 doesn't contains any boxes");
                        }
                        if (c1 == 10)
                        {
                            bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type +
                                               " - solutions from 1 to 10 all contains boxes");
                        }

                        SudokuOptions.Current.IncludeBoxes = false;
                        var gr2 = (from obj in solution_gr.Skip(10).Take(10)
                                   select obj.intermediate_solution.Solution).ToList();
                        int c2 = SudokuSolutionNode.FilterByOptions(gr2).Count();
                        if (c2 != 10)
                        {
                            bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type +
                                               " - solutions from 11 to 20 some contains boxes");
                        }
                    }
                }
                else
                {
                    if (solution_gr.Count() < 10)
                    {
                        bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - less then 10");
                    }
                    if (solution_gr.Count() > 10)
                    {
                        bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - more then 10");
                    }
                }
            });

            if (solutions_gr.Count() != Enum.GetValues(typeof(SudokuSolutionType)).Length)
            {
                bad_quantities.Add("Not all algorithms was tested");
            }

            if (bad_quantities.Count != 0)
            {
                foreach (var line in bad_quantities)
                {
                    TestContext.WriteLine(line);
                }
            }
        }