public void TestSolve()
        {
            var cpb = new CPBoard(bSolvableByCP);
            var sr  = CPSolver.Solve(cpb);

            Assert.True(sr.DidSolve);
            Assert.True(CPSolver.IsSolved(sr.CPB));

            cpb = new CPBoard(bSolved);
            sr  = CPSolver.Solve(cpb);
            Assert.True(sr.DidSolve);
            Assert.Equal("7", cpb.Get(0));
            Assert.True(CPSolver.IsSolved(sr.CPB));

            // Double check results
            cpb = new CPBoard(bSolvable);
            sr  = CPSolver.Solve(cpb);
            Assert.True(sr.DidSolve);
            Assert.True(CPSolver.IsSolved(sr.CPB));

            cpb = new CPBoard(bEmpty);
            sr  = CPSolver.Solve(cpb);
            Assert.True(sr.DidSolve);
            Assert.True(CPSolver.IsSolved(sr.CPB));
        }
        public void TestInitializeConstraints()
        {
            // This board is solvable from the constraints alone
            var cpb = new CPBoard(bSolvableByCP);

            Assert.True(CPSolver.InitializeConstraints(cpb));
            Assert.True(CPSolver.IsSolved(cpb));

            cpb = new CPBoard(bInvalid);
            Assert.False(CPSolver.InitializeConstraints(cpb));

            cpb = new CPBoard(bEmpty);
            Assert.True(CPSolver.InitializeConstraints(cpb));
            for (int i = 0; i < Const.N2; i++)
            {
                Assert.Equal("123456789", cpb.Get(i));
            }

            cpb = new CPBoard("1234" + bEmpty.Remove(0, 4));
            CPSolver.InitializeConstraints(cpb);
            Assert.Equal("1", cpb.Get(0));
            Assert.Equal("2", cpb.Get(1));
            Assert.Equal("3", cpb.Get(2));
            Assert.Equal("4", cpb.Get(3));
            Assert.Equal("56789", cpb.Get(4));

            // TODO: Double check results
            cpb = new CPBoard(bSolvable);
            CPSolver.InitializeConstraints(cpb);
            Assert.Equal("1458", cpb.Get(2));
        }
        public void TestBasicEliminate()
        {
            var cpb = new CPBoard(bEmpty);

            CPSolver.Eliminate(cpb, 0, "1");
            Assert.Equal("23456789", cpb.Get(0));
        }
        public void TestIsSolved()
        {
            var cpb = new CPBoard(bSolvableByCP);

            Assert.False(CPSolver.IsSolved(cpb));

            cpb = new CPBoard(bSolved);
            Assert.True(CPSolver.IsSolved(cpb));

            cpb = new CPBoard(bInvalid);
            Assert.False(CPSolver.IsSolved(cpb));
        }
        public void TestInvalidAssign()
        {
            var cpb = new CPBoard(bEmpty);

            CPSolver.Assign(cpb, 0, "1");

            Assert.False(CPSolver.Assign(cpb, 1, "1"), "Made invalid assignment!");
            Assert.Equal("23456789", cpb.Get(1));   // If assignment fails do not mutate CPBoard

            Assert.False(CPSolver.Assign(cpb, 0, "2"), "Assigned an impossible value");
            // Check that invalid assignment did not propagates
            foreach (var peerIndex in CPSolver.Peers[0])
            {
                Assert.Equal("23456789", cpb.Get(peerIndex));
            }
        }
        public void TestAssignEliminate()
        {
            var cpb = new CPBoard(bEmpty);

            CPSolver.Assign(cpb, 0, "1");
            Assert.Equal("1", cpb.Get(0));

            // Check assignment propagates
            foreach (var peerIndex in CPSolver.Peers[0])
            {
                Assert.Equal("23456789", cpb.Get(peerIndex));
            }
            // Check overlap elimination
            CPSolver.Assign(cpb, Const.N2 - 1, "9");                       // Last tile
            Assert.Equal("2345678", cpb.Get(Const.N - 1));                 // 8
            Assert.Equal("2345678", cpb.Get(Const.N * Const.N - Const.N)); // 72
        }
Exemple #7
0
        static void FakeMain(string[] args)
        {
            Console.WriteLine(string.Join(',', args));
            if (args.Length > 0)
            {
                IList <string> gridStrings = new List <string> ();
                string         line;

                string fileName             = @"puzzles.txt";
                System.IO.StreamReader file = new System.IO.StreamReader(fileName);
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("Grid"))
                    {
                        continue;
                    }
                    else
                    {
                        gridStrings.Add(line.Trim());
                    }
                    System.Console.WriteLine(line);
                }

                file.Close();
                System.Console.WriteLine("{0} in {1}", gridStrings.Count, fileName);

                // TODO: Do initial solve to reduce jitter
                Console.WriteLine("Starting benchmarking");
                var solveTimes = new List <double> (new double[gridStrings.Count]);

                Stopwatch sw            = new Stopwatch();
                int       numIterations = 10;

                for (int i = 0; i < numIterations; i++)
                {
                    for (int j = 0; j < gridStrings.Count; j++)
                    {
                        var cpb = new CPBoard(gridStrings[j]);
                        cpb.Print();
                        Console.WriteLine(new String('-', 20));

                        sw.Restart();
                        var sr = CPSolver.Solve(cpb);
                        sw.Stop();

                        double ticks        = sw.ElapsedTicks;
                        double seconds      = ticks / Stopwatch.Frequency;
                        double milliseconds = (ticks / Stopwatch.Frequency) * 1000;
                        solveTimes[j] += milliseconds;

                        if (!sr.DidSolve)
                        {
                            throw new Exception($"Could not solve:\n {cpb.ToString()}");
                        }

                        sr.CPB.Print();
                    }
                }

                var averageTimes = solveTimes.Select(st => st / numIterations).ToList();
                var averageTime  = averageTimes.Average();
                Console.WriteLine($"Average solve time was: {averageTime}");
                for (int i = 0; i < averageTimes.Count; i++)
                {
                    Console.WriteLine($"Grid {i} : {averageTimes[i]}");
                }
            }
            else
            {
                string line;
                // Handle piped input
                Console.SetIn(new System.IO.StreamReader(Console.OpenStandardInput(8192)));    // This will allow input >256 chars
                while (Console.In.Peek() != -1)
                {
                    line = Console.In.ReadLine();

                    if (line.Contains("Grid"))
                    {
                        var puzzleName = line;
                        Console.WriteLine($"Attempting ${puzzleName}");
                    }
                    else
                    {
                        var cpb = new CPBoard(line);
                        cpb.Print();
                        Console.WriteLine(new String('-', 20));

                        var sr = CPSolver.Solve(cpb);
                        if (!sr.DidSolve)
                        {
                            throw new Exception($"Could not solve:\n {cpb.ToString()}");
                        }
                        sr.CPB.Print();
                    }
                }
            }
        }