Ejemplo n.º 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"));
            }
        }
Ejemplo n.º 2
0
        public void Test_Solution_Trees_Consolidate()
        {
            SudokuOptions.Current.ShowAllSolutions = false;

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

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

            string dir = Directories.SolutionTrees + Path.DirectorySeparatorChar + "Consolidate";

            new DirectoryInfo(dir).CreateOrEmpty();

            int index = 0;
            ConcurrentCounter counter = new ConcurrentCounter();

            Parallel.ForEach(files, (file) =>
            {
                counter.Increment();
                pi.AddLine((counter.Value * 100 / files.Length).ToString());

                SudokuSolutionNode root = SudokuSolutionNode.LoadFromFile(file.FullName);

                foreach (SudokuSolutionNode node in root.GetAllNodesEnumerator())
                {
                    if (node.State != SudokuSolutionNodeState.State)
                    {
                        continue;
                    }

                    var filtered_sols = SudokuSolutionNode.FilterByOptions(node.Nodes.Select(n => n.Solution).ToList());

                    var filtered_nodes = (from n in node.Nodes
                                          where filtered_sols.Contains(n.Solution)
                                          select n).ToList();

                    var new_includes = (from n1 in filtered_nodes
                                        from n2 in filtered_nodes
                                        where SudokuSolutionNode.IsInclude(n1.Solution, n2.Solution, false)
                                        select new { n1, n2 }).ToList();

                    var ignore_pairs = new[]
                    {
                        new [] { SudokuSolutionType.NakedPair, SudokuSolutionType.BoxLineReduction },
                        new [] { SudokuSolutionType.NakedPair, SudokuSolutionType.PointingPair },
                        new [] { SudokuSolutionType.NakedPair, SudokuSolutionType.NakedTriple },
                        new [] { SudokuSolutionType.NakedPair, SudokuSolutionType.NakedQuad },
                        new [] { SudokuSolutionType.NakedTriple, SudokuSolutionType.BoxLineReduction },
                        new [] { SudokuSolutionType.NakedTriple, SudokuSolutionType.NakedQuad },
                        new [] { SudokuSolutionType.NakedTriple, SudokuSolutionType.PointingPair },
                        new [] { SudokuSolutionType.NakedTriple, SudokuSolutionType.PointingTriple },
                        new [] { SudokuSolutionType.NakedQuad, SudokuSolutionType.PointingPair },
                        new [] { SudokuSolutionType.HiddenPair, SudokuSolutionType.HiddenTriple },
                        new [] { SudokuSolutionType.HiddenPair, SudokuSolutionType.HiddenQuad },
                        new [] { SudokuSolutionType.HiddenTriple, SudokuSolutionType.HiddenQuad },
                    };

                    var ignore = from pair in new_includes
                                 from ignore_pair in ignore_pairs
                                 where (((pair.n1.Solution.Type == ignore_pair.First()) &&
                                         (pair.n2.Solution.Type == ignore_pair.Last())) ||
                                        ((pair.n1.Solution.Type == ignore_pair.Last()) &&
                                         (pair.n2.Solution.Type == ignore_pair.First())))
                                 select pair;

                    new_includes = new_includes.Except(ignore).ToList();

                    var doubles = from p1 in new_includes
                                  from p2 in new_includes
                                  where (p1.n1 == p2.n2) &&
                                  (p1.n2 == p2.n1)
                                  select p1;

                    new_includes = new_includes.Except(doubles).ToList();

                    foreach (var pair in new_includes)
                    {
                        SudokuIntermediateSolution intermediate_solution_1 =
                            new SudokuIntermediateSolution(pair.n1.Board, pair.n1.NextBoard, pair.n1.Solution);

                        intermediate_solution_1.SaveToFile(dir + Path.DirectorySeparatorChar + index + "_" +
                                                           Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(file.FullName)) + "_" +
                                                           pair.n1.Solution.Type + FileExtensions.XmlZipExt);

                        SudokuIntermediateSolution intermediate_solution_2 =
                            new SudokuIntermediateSolution(pair.n2.Board, pair.n2.NextBoard, pair.n2.Solution);

                        intermediate_solution_2.SaveToFile(dir + Path.DirectorySeparatorChar + index + "_" +
                                                           Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(file.FullName)) + "_" +
                                                           pair.n2.Solution.Type + FileExtensions.XmlZipExt);

                        index++;
                    }
                }
            });

            Assert.IsTrue(index == 0);
        }
Ejemplo n.º 3
0
        private static void GenerateAllSolutions(GenerationMethod a_method)
        {
            System.Console.WriteLine();
            System.Console.WriteLine("Generating solutions...");
            System.Console.WriteLine();

            string dir = Directories.SolutionTrees + Path.DirectorySeparatorChar + "All";

            new DirectoryInfo(dir).CreateOrEmpty();

            FileInfo[] files1 = new DirectoryInfo(Directories.Examples).GetFiles(FileExtensions.XmlZipMask);
            FileInfo[] files2 = new DirectoryInfo(Directories.SolutionTrees).GetFiles(FileExtensions.XmlZipMask);

            var not_all = (from file1 in files1
                           where files2.Count(fi => Directories.ConvertName(fi.FullName) == file1.Name) < 4
                           select file1).ToArray();

            var too_much = (from file2 in files2
                            where files1.All(fi => fi.Name != Directories.ConvertName(file2.Name))
                            select file2).ToArray();


            if (not_all.Count() > 0)
            {
                System.Console.WriteLine("Is in 'Examples', but not in 'SolutionTrees' (run project 'SudokuGenerator'): " + not_all.First().FullName);
                System.Console.ReadKey(true);
                return;
            }

            if (too_much.Count() > 0)
            {
                System.Console.WriteLine("Is in 'SolutionTrees', but not in 'Examples': " + too_much.First().FullName);
                System.Console.ReadKey(true);
                return;
            }

            FileInfo[] files = (from file in new DirectoryInfo(Directories.SolutionTrees).GetFiles(FileExtensions.XmlZipMask)
                                group file by Directories.ConvertName(file.Name) into gr
                                select gr.First()).ToArray();

            ShowProgress(0, 70);

            ConcurrentCounter counter = new ConcurrentCounter();

            Parallel.ForEach(files, (file) =>
            {
                SudokuSolutionNode root = SudokuSolutionNode.LoadFromFile(file.FullName);

                string file_name = Path.GetFileNameWithoutExtension(Directories.ConvertName(file.Name));

                Dictionary <SudokuSolutionType, int> counters = new Dictionary <SudokuSolutionType, int>();
                Dictionary <SudokuSolutionType, List <SudokuSolutionNode> > lists = new Dictionary <SudokuSolutionType, List <SudokuSolutionNode> >();

                foreach (var type in Enum.GetValues(typeof(SudokuSolutionType)).Cast <SudokuSolutionType>())
                {
                    if (type == SudokuSolutionType.MarkImpossibles)
                    {
                        continue;
                    }
                    if (type == SudokuSolutionType.MarkSolved)
                    {
                        continue;
                    }
                    if (type == SudokuSolutionType.SinglesInUnit)
                    {
                        continue;
                    }

                    new DirectoryInfo(dir + Path.DirectorySeparatorChar + type).CreateOrEmpty();
                    lists[type]    = new List <SudokuSolutionNode>();
                    counters[type] = 1;
                }

                foreach (var list in lists.Values)
                {
                    list.Clear();
                }

                foreach (SudokuSolutionNode node in root.GetAllNodesEnumerator())
                {
                    if (node.State != SudokuSolutionNodeState.State)
                    {
                        continue;
                    }

                    var nodes = (from n in node.Nodes
                                 where (n.Solution.Type != SudokuSolutionType.MarkImpossibles) &&
                                 (n.Solution.Type != SudokuSolutionType.MarkSolved) &&
                                 (n.Solution.Type != SudokuSolutionType.SinglesInUnit)
                                 select n).ToList();

                    var sols = SudokuSolutionNode.FilterByOptions(nodes.Select(n => n.Solution).ToList());

                    var nodes_gr = (from n in nodes
                                    where sols.Contains(n.Solution)
                                    group n by n.Solution.Type into gr
                                    select gr.ToList()).ToList();


                    if ((a_method == GenerationMethod.OneType) || (a_method == GenerationMethod.OneSolution))
                    {
                        bool one_type = nodes_gr.Count(gr => gr.Any()) == 1;

                        if (!one_type)
                        {
                            continue;
                        }
                    }

                    if (a_method == GenerationMethod.OneSolution)
                    {
                        bool one_solution = nodes_gr.Count(gr => gr.Count == 1) == 1;

                        if (!one_solution)
                        {
                            continue;
                        }
                    }

                    foreach (var n in nodes)
                    {
                        lists[n.Solution.Type].Add(n);
                    }
                }

                foreach (var list in lists.Values)
                {
                    var uniques = from n in list
                                  group n by n.Solution into gr
                                  select gr.First();

                    var exclude = from n1 in uniques
                                  from n2 in uniques
                                  where !Object.ReferenceEquals(n1, n2) &&
                                  n1.Solution.Removed.Contains(n2.Solution.Removed, Comparators.SudokuNumberRowColComparer.Instance) &&
                                  n1.Solution.Stayed.Contains(n2.Solution.Stayed, Comparators.SudokuNumberRowColComparer.Instance) &&
                                  n1.Solution.ColorUnits.Equals(n2.Solution.ColorUnits)
                                  select n2;

                    var sols = uniques.Except(exclude);

                    foreach (var n in sols)
                    {
                        SudokuIntermediateSolution intermediate_solution = new SudokuIntermediateSolution(n.Board, n.NextBoard, n.Solution);

                        intermediate_solution.SaveToFile(dir + Path.DirectorySeparatorChar + n.Solution.Type + Path.DirectorySeparatorChar +
                                                         file_name + " - " + counters[n.Solution.Type]++ + FileExtensions.XmlZipExt);
                    }
                }

                counter.Increment();
                lock (counter)
                {
                    ShowProgress(counter.Value * 70 / files.Length, 70);
                }
            });
        }
Ejemplo n.º 4
0
        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);
                }
            }
        }