Test Run - Solving Sudoku Puzzles Using the MSF Library. A Sudoku puzzle is an example of what. One way to tackle CSPs programmatically is to use the Microsoft Solver Foundation (MSF) library. Agency Program Google Developer Groups Google Developer Experts Startup Launchpad. First, the techniques presented here can be used to solve many realistic problems. Second, this article will introduce you to the MSF library and its capabilities. And third, you might just find that solving Sudoku puzzles programmatically is interesting and entertaining. To get an idea of where this article is headed, take a look at the demo program in Figure 1. The demo console application begins by setting up and displaying the data for a typical Sudoku puzzle. Next, it programmatically defines constraints (required conditions) that are common to all Sudoku puzzles, and then sets up the data constraints that are specific to the puzzle. Then, behind the scenes, the demo uses the MSF library to solve the puzzle. The demo concludes by displaying the solution. Figure 1 Sudoku Using the Microsoft Solver Foundation. This article assumes you have at least intermediate- level programming skills and a vague idea of what Sudoku puzzles are, but does not assume you know anything about constraint satisfaction problems or the MSF library. The demo program is coded using C# but you should be able to refactor the demo to other . NET languages without too much trouble. All the code is presented here and it is also available in the code download that accompanies this article at msdn. All normal error checking has been removed to keep the main ideas clear. The Microsoft Solver Foundation Library. The MSF version 3. The location of the download has tended to move around over time but I found it at bit. Gh. 1. I prefer to use 3. I clicked on that link, which gave me the option to run or save the Microsoft. Solver. Foundation. I selected the Run option. The installation wizard tells you you. MSF originally came in two versions, a free Express Edition and a for- purchase enterprise edition, but the enterprise edition has been discontinued. The MSF library is essentially no longer being actively developed, but the current 3. After the quick but somewhat clunky installation process finishes, the key library file Microsoft. Solver. Foundation. Games; Puzzle; Excel @ Sudoku. Excel @ Sudoku enables you to create and solve Sudoku puzzles using your. This example shows how to solve SUDOKU usin. Excel Macro to solve SUDOKU Puzzle. Solve Sudoku with Excel using VBA. Solve Sudoku with Excel. Excel @ Sudoku is an Excel Sudoku helper, not a solver. Windows (all), Excel Download Information; File Size: 69.31KB. How to solve a sudoku puzzle with MS Excel using VBA. Written as a video tutorial, this is a cut down version without all the delays and user interface dialog. C: \Program Files (x. Reference Assemblies\. The demo has no significant Microsoft . NET Framework version dependencies so any relatively recent version of Visual Studio should work. After the template code loaded, in the Solution Explorer window I renamed file Program. Sudoku. Program. cs and Visual Studio then automatically renamed class Program. The overall structure of the demo program, with a few minor edits to save space, is presented in Figure 2. Figure 2 Overall Program Structure. Microsoft. Solver. Foundation. Services. Sudoku. Solver. . Next, I added a reference to the MSF library DLL file and then added a using statement that references the library to bring it into scope. Almost all of the work is performed inside method Main. Two helper methods, Add. Data. Constraints and Number. Solutions, are just conveniences to keep the code inside Main a bit tidier. After a preliminary begin- message, the demo sets up the Sudoku puzzle data in an array- of- arrays style matrix. Unlike many languages, C# supports a true two- dimensional array, but as you. The demo data matrix is represented in Figure 3. Data can be accessed by individual cell, or by an entire row, but not by an entire column. The blank cells in Figure 3 actually have 0 values because C# automatically initializes integer- array cells to all 0s. Figure 3 The Problem Data Matrix. Setting up the Problem. After the problem data matrix has been created, the demo program displays the values to the shell. In my opinion, especially with demo programs, sometimes it. Next, the demo makes the first use of MSF code. Solver. Context problem = Solver. Context. Get. Context(). Model model = problem. Create. Model(). Decision. You can think of the first two lines as a magic incan. Rather than working with a single object, as you. You can think of a Decision object as an encapsulation of an answer. Or put another way, to solve a Sudoku puzzle you need to determine 9 x 9 = 8. Each of these values is represented by a Decision object and the demo stores them in a matrix. At this point, the grid matrix has uninstantiated Decision objects. The demo instantiates each cell like so. The first describes the data type of an answer. Here, each answer is an integer between 1 and 9. The second parameter is a non- optional name as a string. These names must be unique, so the demo programmatically assigns names . After the Decision objects have been instantiated, they must be added to the Model object. The Add. Decisions method can accept a single Decision object or an array of Decision objects, so the demo passes the nine rows of the grid matrix. An alternative would be to use two nested loops, like this. First, the values in each row must all be different. Second, the values in each column must all be different. And third, the values in each 3x. Taking care of the first set of constraints is easy. Console. Write. Line(. The demo uses the All. Different method to create a constraint. In words, for each of the nine rows, add a constraint that all the values in the row must be different. Adding the generic column constraint requires a little bit more effort. Console. Write. Line(. For column 0, the first time through the two inner nested loops sets the constraint named . The second iteration sets . To summarize, the All. Different method accepts a set of Decision objects as an array and behind the scenes generates explicit inequalities. In situations where your Decision objects aren. That code is presented in Figure 4. The necessary constraints for this sub- cube are. Now, it would be possible to list each one using copy- paste and some patience, but a programmatic approach is quicker. In the code, the two outer loops establish an upper- left corner of each sub- cube. In the four innermost loops, cells are represented as grid. If you look just at the indices in the example and image, which are just ordinary integers, you get. The innermost four loops iterate over a, b, x, and y to generate all possible pairs of indices and the if- then condition creates a constraint on grid. The demo puzzle has 2. Add. Constraint(. Library code written by developers for developers would likely have referenced the Decision (answer) objects along the lines of model. The unusual style of the MSF library took me about three examples to get comfortable with it. Solving the Puzzle. With everything in place, the demo program can solve the puzzle with this code. Console. Write. Line(. As a side effect, the Solution object has a Quality field that can be one of eight values, including Feasible and Infeasible. The Solution Get. Next method doesn. Recall that answers were defined to be integers in the range 1 to 9. Because they all end with point- zero, when displayed they appear as integers even though they are type double. Wrapping Up. The particular type of CSP described in this article is really a combinatorial optimization problem. That is, the goal is to find the combination of values that has the fewest constraint errors. The information presented here should allow you to use the MSF library to solve many practical combinatorial optimization problems you might come across. I have a confession. He consistently beats me whenever we compete for time on Sudoku or crossword puzzles or anything else. He has worked on several Microsoft products including Internet Explorer and Bing. Reach him at jammc@microsoft. Thanks to the following technical expert for reviewing this article: Kirk Olynyk (Microsoft Research).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |