示例#1
0
        private static void PrintGrid(SudokuGrid grid)
        {
            for (int i = 0; i < grid.Rows; i++)
            {
                for (int j = 0; j < grid.Columns; j++)
                {
                    Node thisNode = grid.GetNode(i, j);
                    if (j == 5) // last column, go onto a newline
                    {
                        Console.WriteLine(thisNode.Value);

                        if (i == 1 || i == 3) // 2nd or 4th row, write an extra newline so we can visualize the 2x3 boxes
                        {
                            Console.WriteLine();
                        }
                    }
                    else
                    {
                        Console.Write(thisNode.Value);
                    }

                    if (j == 2) // space after 3rd column to help us visualize the 2x3 boxes
                    {
                        Console.Write(" ");
                    }
                }
            }
        }
        private Queue <Arc> GetAllArcs()
        {
            Queue <Arc> arcs = new Queue <Arc>();

            foreach (Node node1 in _grid.Grid)
            {
                foreach (Node node2 in _grid.Grid)
                {
                    if (node1 != node2 && // it's the same node
                        SudokuGrid.NodesAreRelated(node1, node2) && // nodes have to be related to be considered an arc
                        (node1.Editable || node2.Editable))         // at least one of them should be editable, otherwise why bother?
                    {
                        Arc arc = new Arc()
                        {
                            Node1 = node1,
                            Node2 = node2
                        };

                        if (!arcs.Contains(arc))
                        {
                            arcs.Enqueue(arc);
                        }
                    }
                }
            }
            return(arcs);
        }
示例#3
0
        protected CSPSolution CompleteSolve(string name, SudokuGrid grid, TimeSpan elapsed, int processed, int iterations)
        {
            CSPSolution solution = new CSPSolution();

            solution.AlgorithmName = name;
            solution.SolutionGrid  = grid;
            solution.TimeElapsed   = elapsed;
            solution.Processed     = processed;
            solution.Iterations    = iterations;

            return(solution);
        }
        // How many constraints are being violated by the current guess?
        private int PerformEvaluationFunction()
        {
            int violations = 0;

            foreach (Node node in _solvedGrid.Grid)
            {
                // The valid values for this node
                List <int> validValues = SudokuGrid.GetNodeDomain(node);

                // If the current value is not one of the valid ones, we have a violation
                if (!validValues.Contains(node.Value))
                {
                    violations++;
                }
            }

            return(violations);
        }
示例#5
0
 public BacktrackingSolver(SudokuGrid initialGrid)
     : base(initialGrid)
 {
     // Use default constructor
 }
 public ArcConsistencySolver(SudokuGrid initialGrid) : base(initialGrid)
 {
     // Use default constructor
 }
示例#7
0
        private static SudokuGrid LoadGridFromFile(string filePath)
        {
            SudokuGrid grid = new SudokuGrid(6, 6, File.ReadAllText(filePath));

            return(grid);
        }
示例#8
0
        static void Main(string[] args)
        {
            Console.WriteLine($"{args[0]} loaded");

            bool quit = false;

            while (!quit)
            {
                SudokuGrid initialProblem = LoadGridFromFile(args[0]);

                Console.WriteLine();
                Console.WriteLine("Choose an algorithm: ArcConsistency (ac), BacktrackingSearch (bts), or GradientDescent (gd)");
                Console.WriteLine("Press Q to quit, or P to print the initial grid");
                string choice = Console.ReadLine();

                List <CSPSolver> problemSolvers = new List <CSPSolver>();

                switch (choice.ToLower().Trim())
                {
                case "print":
                case "p":
                    PrintGrid(initialProblem);
                    break;

                case "q":
                case "quit":
                    quit = true;
                    break;

                case "ac":
                case "arcconsistency":
                case "arc":
                    problemSolvers.Add(new ArcConsistencySolver(initialProblem));
                    break;

                case "backtracking":
                case "bts":
                case "bt":
                case "backtrackingsearch":
                    problemSolvers.Add(new BacktrackingSolver(initialProblem));
                    break;

                case "gradientdescent":
                case "gd":
                case "gradient":
                    problemSolvers.Add(new GradientDescentSolver(initialProblem));
                    break;

                default:        // just do the solution for all of them
                    problemSolvers.Add(new ArcConsistencySolver(initialProblem));
                    problemSolvers.Add(new BacktrackingSolver(initialProblem));
                    problemSolvers.Add(new GradientDescentSolver(initialProblem));
                    break;
                }


                foreach (CSPSolver solver in problemSolvers)
                {
                    CSPSolution solution = solver.Solve();
                    PrintSolution(solution);
                }
            }
        }
示例#9
0
 // Default constructor for all solvers. They all use a SudokuGrid
 protected CSPSolver(SudokuGrid initialGrid)
 {
     _grid = initialGrid;
 }
 public GradientDescentSolver(SudokuGrid initialGrid)
     : base(initialGrid)
 {
     _solvedGrid = initialGrid; // start with the initial grid
     _solvedGrid.Randomize();
 }