讲解:EBS2002、Solving Sudokus、java、javaProcessing|C/C++

COURSE MANUALSecond Year Project IEBS2002Operations Research Assignment -- Solving SudokusAcademic Year 2018/2019 Contents1 Sudoku 32 Data structures and a very simple logic rule 53 A generalization 74 Enumeration 95 Deliverables 116 Presentations 137 File format of Sudoku instances 152Chapter 1SudokuYou probably have heard of, or even solved, a puzzle commonly known as Sudoku. If not, have alook at the game on Wikipedia or www.sudoku.org.uk. A Sudoku is simply a game, but it sharesquite a lot of characteristics with scheduling problems as they occur in practice. That the problemis not trivial can be seen by going to scholar.google.com. Submitting ‘Sudoku’ as the search termyou can find quite a few relatively recent publications.So what would be some practical applications of solving a Sudoku? Consider a sports competitionwhere 9 teams play each other over the course of 9 weekends. Each team plays one home gameand one away game against each opposing team. Additionally, each team plays two matches eachweekend, except for one weekend where the team has a day off.Now take a solved Sudoku and compare it to the above sports scheduling problem: Let the rowscorrespond to the home games for each team, and the columns to the away games. Each entry ina cell now tells us the weekend of the match, the row number tells us which team is playing thehome game, and the column number which other team is visiting the match as an away game. Thediagonal has equal row and column numbers; this gives us the weekend on which the particularteam is not playing. The 3 × 3 blocks can be interpreted as follows: Assume we can categorizethe teams in groups of three teams: high ranking teams, average ranking teams and low rankingteams. Then the requirement that a number between 1 and 9 occurs exactly once tells that eachweekend, we want to have exactly one game between teams of any combination of strengths andhome/away games.Your solution procedures will combine two principles that can be found in many optimizationalgorithms. The first is pre-processing: you will implement a collection of logic rules that iterativelyreduce the set of possible solutions. This is indispensable e.g. when solving large integer linearprogramming problems. The second is branching: when logic rules allow no further reduction,one can try all possible values for a specific variable (in our case, the content of one cell in thematrix). For each value, one gets a so-called branch in the search for a solution. The idea is thatnew opportunities for pre-processing arise after fixing one variable to a particular value. However,it may happen that the chosen value was not feasible in the sense that one cannot find any feasiblesolution in that branch. In this case, one has to try another branch. Branching will be discussedsome more later on.The goal of the project is to implement a Java program with classes with appropriate methods to3EBS2002 –Second Year Project 1 – Operations Research Assignmentsolve Sudoku puzzles of as high a difficulty as possible. For simplicity, we will restrict ourselves tothe classical Sudokus of order 3, that is consisting of a 9 × 9 square. As a computer is fast, thereis a straight forward approach: try all combinations. But there might be too many combinationsto do so. For every cell without a hint, there are 9 possibilities, and given e.g. an instance with 60free cells there will be 960 combinations to be tested!Therefore we need to apply some logic reasoning, as we do when solving a Sudoku by hand. Acollection of ideas is presented inhttp://www.sudoku.org.uk/PDF/Solving_Sudoku.pdf.In this project, the focus will be on two basic rules explained in the following two sections, butyou are free to choose to implement any additional rules. You are also required to implement acomplete enumeration method which finds a solution, should the rules that you have implementedfail to solve an instance. These approaches, their implementations and the advisory data structureswill be discussed in the following sections. Implementing the two basic logic rules will already allowyou to solve simple Sudokus.Page 4 of 15Chapter 2Data structures and a very simplelogic ruleGiven an instance, you will have some hints. A hint is a cell which already contains a number.Create a class cell which captures the state of a cell. In cells without hints, this data structurewill initially contain all numbers from 1 to 9 as candidates for a solution. In cells with a hint, thisdata structure will contain the number. Our logic rules will reduce the set of candidates step bystep, until exactly one is left. We refer to a cell as solved if we have reached this status.The next class that you need is one that combines 81 cells to a Sudoku instance. Obviously, thisclass consists of some kind of 9 × 9 matrix of cells, plus member functions to operate on thesecells in order to solve the Sudoku, plus some private data members that support these membersfunctions.The first logic rule is based on the following simple observation: Every cell belongs to exactly onerow, one column and one block. Consider an unsolved cell. The only candidates for an unsolvedcell are those numbers which are neither contained in a solved cell in this row, nor in a solvedcell in this column, nor in a solved cell in this block. Therefore, all candidates in a cell which aresolutions to cells in the same row, column or block can be removed from the set of candidates. Ifone is lucky, one ends up with exactly one remaining candidate, which means that this cell is alsosolved.The first logic rule works as follows. Start with all hints and use them to reduce the set of candidatesof all other cells in the same row, column and block. While doing this, memorize whenever a cell issolved and use it as an additional hint. A good way to implement this is to organize a queue of hints(using for instance java.util class PriorityQueue or the class from Programming, IntegerQueue),initially fill it with all hints, process the queue (until it is empty), during which you add any solvedcell as a new hint to the end of the queue. Do this until the queue is empty.If implemented correctly, this logic rule solves the instance number 1 found on Eleum.5EBS2002 –Second Year Project 1 – Operations Research AssignmentPage 6 of 15Chapter 3A generalizationThis first rule can be generalized by taking approaching the problem from the following perspective:Consider a solution of a Sudoku. For each cell it tells you which number to write in this cell. Acell is thereby characterized by its row index and its column index. Now, instead of providing thenumber to be written for every combination of a row index and column index, you may also thinkof a solution in the following way: for every number (between 1 and 9) and every row, the solutiontells you in which column of this row to write this number. A Sudoku is solved if for each numberand each row, the number of column candidates is exactly one. Similarly, for every number andeach block, the solution tells you in which of the nine positions of this block to write the number.Or, for every number and column, the solutions specifies in which row to put this number.The key obseEBS2002作业代写、Solving Sudokus作业代做、代写java程序作业、代做java语言作业 代做留学生Prvation here is that the data structure as described before does not make transparentthat for a particular combination of a row and number (or a column and a number, or a block anda number), there is only a single column (or row, or position) left where this number can go! Inother words, we might reach a state where several cells in the same row contain more than onecandidate, but some of these candidates appear only once in those cells, and therefore have to bewritten to this cell in order to find a solution. In this case, they will no longer be candidates forother cells in the same column, or for other cells in the same block.This generalization of the first reduction method organizes a systematic search for such situations.Whenever it detects these, it can solve an additional cell. Newly solved cells can then be used toperform further reductions using the simple logic rules and this generalization.7EBS2002 –Second Year Project 1 – Operations Research AssignmentPage 8 of 15Chapter 4EnumerationOnce the above logic rules are not sufficient to solve a problem instance, one needs to use abranching algorithm to enumerate possible solutions. Such a branching algorithm works as follows:Take any cell for which the list of candidates has a size larger than 1. Take any number n in thislist, delete all elements except n, and try to solve this simplified instance (using all the solved cellsthat have already been found at this point). Usually this guess will allow you to again apply thelogic rules as described above to further reduce the problem. If this still does not help you furtheryour search for a solution, you need to make a new guess in another cell. If you always guesscorrectly, this will solve the problem. But it can also happen that you guess wrongly, and theproblem in a branch has no solution. Your algorithm will detect this when the logic rules suddenlycreate an empty cell. In this case, the search in this branch should be stopped, the algorithmshould backtrack, and a new branch guessing a different number should be started. As soon asyour enumeration has found a complete solution, it should stop.The difficulty in implementing such an enumeration using branching lies in backtracking. That is,one might have to reverse some guesses - and all implications of these guesses - when a branch turnsout to lead to infeasibility. The best way to deal with this difficulty is to enter every branch witha complete copy of the current situation. If the branch does not solve the problem, your algorithmwill still have the original situation as it was before branching. From this you can make a new copyand enter another branch. If your implementation is sound, Java will use the Garbage Collection inorder to free up memory slots which were used for branches which have been completely traversedand will no longer be used, thus making your implementation effective.9EBS2002 –Second Year Project 1 – Operations Research AssignmentPage 10 of 15Chapter 5DeliverablesThe following is a list of tasks for this project. For each task, there is a maximum number of pointswhich can be obtained. You will need at least 55 points in order to get a passing grade for thispart of the skills.1. (20 points) Construct a collection of classes which stores the necessary data in appropriatedata structures in order to capture all aspects of the Sudoku. Use the data structures asdescribed in the sections above.2. (15 points) Implement the simple logic rules. After this, your program should be able tosolve Sudoku number 1.3. (25 points) Implement the generalization as described in Section 3.4. (25 points) Implement the enumeration algorithm from Section 4.5. (15 points) Describe your implementation in a report of maximum 2000 words. You shouldshortly introduce the problem (max 500 words) and explain how you implemented the problem:describe the structure of your data structures and discuss your solution methods andtheir usage of the data structures. Close with a short concluding section. Do not add appendicesor your code!The quality of the program will be evaluated in terms of1. readability and documentation,2. organization of data structures and methods, and their interaction, clever usage of thejava.util class library (do not reinvent the wheel!), and a clear distinction between the highlevellogic of your solution procedure and the methods which are necessary to implement thislogic.3. efficiency in terms of omitting unnecessary computational work.Your program has to make good use of Java classes and its libraries. In particular the logic rulesshould be implemented as methods of appropriate classes. Your algorithms should also keep track ofsome performance information, for example how often branching is used, or how often a particular11EBS2002 –Second Year Project 1 – Operations Research Assignmentlogic rule helped to reduce the size of a list in a cell. If you implement several rules, count howoften each of the rules helped to reduce the problem. This provides more information about thespeed of your implementation than keeping track of the time; different computers yields differentrunning times, but deterministic logic rules will always yield the same amount of rule-usage andbranches, making a fair comparison possible. Include this performance information in your report.Additional training instances can be found athttp://www.sudoku.org.uk/PDF/Solving_Sudoku.pdf.Submit your source files (.java files) and the report as a PDF file on StudentPortal. Pleasecompress all your files into a single .zip file! The deadline for your submission is Friday,January 25th, 2019 at 20:00. Points will be deducted for late submissions! Tutorials will beorganized on Monday, 21.1.2019, and on Wednesday, 23.1.2019. The details can be found on theStudentPortal.Page 12 of 15Chapter 6PresentationsOn January 25th each team will give a 15 minute presentation on either the Game Theory or onthe Operations Research assignment.If you are scheduled for an OR presentation, please heed the following guidelines:Use 1 minute to shortly introduce the problem (everyone should know this by now).Use 5 minutes to explain your general implementation: which data structures and which methodsfor solving the Sudoku did you implement?Use 5 minutes to elaborate on one of your implementations. DO NOT JUST PRESENT THECODE as slides containing source code are not helpful to understand what you did! Instead,describe and/or visualise your implementations (e.g. draw your data structures as lists of lists).Use 1 minute for concluding remarks (e.g. did you solve everything? do you have ideas forimprovement?)Then there will be about 3 minutes left for questions and potential swapping between sessions.13EBS2002 –Second Year Project 1 – Operations Research AssignmentPage 14 of 15Chapter 7File format of Sudoku instancesA file with an instance of a Sudoku starts with the number of hints, followed by as many rows asthere are cells with hints. Each row in the file first gives you the row number of the Sudoku (anumber between 0 and 8), then the column number of the Sudoku (a number between 0 and 8),and finally the hint (a number between 1 and 9). You can find 6 example files on Eleum.转自:http://ass.3daixie.com/2019012368012125.html

你可能感兴趣的:(讲解:EBS2002、Solving Sudokus、java、javaProcessing|C/C++)