コード例 #1
0
        public void Test()
        {
            var configurator = new BacktrackingConfigurator<int, BitArray> (Size,
                                                                            PartialCheck,
                                                                            TotalCheck,
                                                                            GeneratePartiallyBacktrack,
                                                                            CreateArgument,
                                                                            ResetArgument/*,
                                                                            new [] { 3, 1, 2, 4 },
                                                                            new [] { 4, 1, 2, 3 }*/);
            var backtracking = new Backtracking<int, BitArray> (configurator);
            int total = 0;

            foreach (var solution in backtracking)
            {
                ++total;
            }
            Console.WriteLine ($"Serial Total = {total}");

            total = 0;

            var sums = new int[Size];

            Parallel.For (1, Size + 1, (int i) => {
            //for (int i = 1; i < Size + 1; i++) {
                var init = new int[Size];
                var last = new int[Size];
                init [0] = last [0] = i;

                int v = 1, u = Size;

                for (int p = 1; p < Size; ++p) {
                    if (v == i)
                        ++v;
                    init [p] = v++;

                    if (u == i)
                        --u;
                    last [p] = u--;
                }
                var configurator2 = new BacktrackingConfigurator<int, BitArray> (Size,
                                                                                 PartialCheck,
                                                                                 TotalCheck,
                                                                                 GeneratePartiallyBacktrack,
                                                                                 CreateArgument,
                                                                                 ResetArgument,
                                                                                 init,
                                                                                 last);
                var backtracking2 = new Backtracking<int, BitArray> (configurator2);
                int tot = 0;

                foreach (var solution in backtracking2)
                {
                    ++tot;
                }
                sums [i - 1] = tot;
            //}
            });

            Array.ForEach (sums, (o) => total += o);
            Console.WriteLine ($"Parallel Total = {total}");
            /*var e1 = backtracking.GetEnumerator ();

            while (e1.MoveNext ())
            {
                foreach (var part in e1.Current)
                {
                    Console.Write ($"{part}");
                }
                Console.WriteLine ();
                var e2 = backtracking.GetEnumerator ();

                while (e2.MoveNext ())
                {
                    foreach (var par in e2.Current)
                    {
                        Console.Write ($"{par}");
                    }
                    Console.Write (" ");
                }
                Console.WriteLine ();
                Console.WriteLine ();
            }*/

            /*foreach (var solution in backtracking)
            {
                foreach (var part in solution)
                {
                    Console.Write ($"{part}");
                }
                Console.WriteLine ();
                foreach (var sol in backtracking)
                {
                    foreach (var part2 in sol)
                    {
                        Console.Write ($"{part2}");
                    }
                    Console.Write (" ");
                }
                Console.WriteLine ();
                Console.WriteLine ();
            }*/
        }
コード例 #2
0
        public void Test()
        {
            var configurator = new BacktrackingConfigurator<Point, char[,], bool[,]> (SearchSpace,
                                                                                      PartialCheck,
                                                                                      TotalCheck,
                                                                                      Generate,
                                                                                      CreateArgument,
                                                                                      ResetArgument,
                                                                                      new Point[] { new Point (1, 1), new Point (1, 2)/*, new Point (2, 2), new Point (2, 3) */},
                                                                                      new Point[] { new Point (1, 1), new Point (1, 2), new Point (2, 2), new Point (2, 3), new Point (3, 3), new Point (3, 4), new Point (4, 4), new Point (4, 5) });
            var backtracking = new Backtracking<Point, char[,], bool[,]> (configurator);
            int total = 0;

            foreach (var solution in backtracking)
            {
                ++total;
                foreach (var part in solution)
                {
                    Console.Write ($"({part.X}, {part.Y}) ");
                }
                Console.WriteLine ();
            }
            Console.WriteLine ($"Serial total: {total}");
        }