讲解:159.271、Futoshiki Solver、Python、PythonSQL|Haskel

159.271 Computational ThinkingAssignment 1: Futoshiki SolverThis assignment is worth 10% of your final mark. It will be marked out of a total of 25marks. The due date for this assignment is Monday April 6th. You are expected to work onthis assignment individually and all work that you hand in is expected to be your own work.In this assignment your task is to write a Futoshiki solver. You will develop an animatedapplication that uses recursive backtracking as the basis of the implementation. A Futoshikipuzzle is similar to a Sudoku puzzle. The input to the problem is a square board having agiven fixed size (5x5 for example), with some of the board cells containing digits between 1and the boards size, and with some inequality constraints between adjacent pairs of cells onthe board provided. When complete, every cell on the board must be filled with a digitbetween 1 and the boards size. For each row and column, each digit must appear exactlyonce. In addition, the choice of digit for each cell must respect any inequality constraintsbetween adjacent pairs of cells. See http://www.futoshiki.org/.Go to Stream and download the fileFutoshiki.zip Futoshiki.zipThe unzipped folder contains a template program that implements the code needed to readFutoshiki puzzles from files and to display them, and to run the solver in animation mode.Set up a new project and add the folder src to it. The main game module isFutoshikiApp.py FutoshikiApp.py. If you run this, a screen will appear and clicking on one of the t,e or h keys will display a randomly chosen puzzle (trivial, easy or hard) from one ofthe three folders of puzzles included. Note that the trivial puzzles do not have any inequalityconstraints.You need to complete the module Solver.py Solver.py, so that a call to the solve function will doa bit more than just displaying the puzzle. You need to turn this function into a recursiveComputational Thinking 15/03/20, 1)09 PMfile:///Users/cmmccart/Documents/Teaching/159271/Notes/assignment1-2020.html Page 2 of 6a bit more than just displaying the puzzle. You need to turn this function into a recursivebacktracking solver, which, for animation purposes, displays a snapshot of the current stateof the puzzle at the start of each recursive call.The program uses two classes to represent a Futoshiki – Cell and Snapshot Snapshot. Cellrepresents a single cell on a Futoshiki board, and has methods to get and set the position ofthe cell and the value of the cell. For the purposes of this assignment, in any completesolution, the value must be between 1..5. If the value is 0 this means that the cell is stillempty. Snapshot Snapshot describes a state of the Futoshiki at a certain point in time – some cellshave values (other than zero), some may not. The Snapshot Snapshot class has methods that allowto clone a snapshot (this is required to produce the next level of snapshots in the recursiontree), to query the cells in various ways, and to get and set cell values. It also has a methodgetConstraints getConstraints that returns a list of the inequality constraints for the board. Each itemin the returned list has the format ((i,j),(k,l)) ((i,j),(k,l)) indicating that the value in cell (i,j)must be smaller than the value in cell (k,l).Futoshiki_IO.py Futoshiki_IO.py contains the code used to read Futoshiki puzzles from puzzle files andto display them. The puzzle files consist of 5 lines containing 5 digits each, encoding theinitial values for the board cells, followed by some number of lines containing 4 digits each,encoding the inequality constraints for the board. Board cells are indexed from 0 to 4, so theline 0 2 0 3 0 2 0 3, for example, encodes the inequality constraint cell(0,2) In general, to develop a recursive backtracking algorithm you need to consider:1. What question to ask? Thinking only about the top level of recursion, you need toformulate one small question about the solution that is being searched for. Rememberthat, in order for the final algorithm to be efficient, it is important that the number ofpossible answers is small.2. How to construct a subinstance? How to express the problem as a smaller instance ofthe same search problem?For your Futoshiki solver, an input instance for the algorithm will be a snapshot snapshotspecifying both the current state of the Futoshiki board and the provided inequalityconstraints. A solution is a valid way of filling the remaining empty cells. The simplequestion to ask is What number can go in the next empty cell?The brute force strategy is simply to find the next empty cell in the snapshot, working left toright, top to bottom, and then to try all possible numbers in that cell, with a recursive call tothe solve function for each possibility. Initial pruning of the recursion tree should includetwo strategies - 1. we dont continue on any branch that has already produced aninconsistent solution and 2. we stop once a complete solution has been found. Python codeexamples that implement this basic strategy for two straightforward problems have beengiven in the lecture slides.Computational Thinking 15/03/20, 1)09 PMfile:///Users/cmmccart/Documents/Teaching/159271/Notes/assignment1-2020.html Page 3 of 6Remember that the tree of recursive calls will be traversed in depth-first order. A branch ofthe tree will be recursively explored until either we establish that the branch cannot lead to acomplete solution - in which case we backtrack up the tree and try the next possible branch,or we find a complete solution. The brute force approach can lead to a very large tree ofrecursive calls. Suppose only a couple of the input board cells are already filled, and perhapsfour inequality constraints are provided (this can be enough to provide a unique solution), inthis case we might need to search through 52323 different combinations!You 159.271课程作业代做、代写Futoshiki Solver作业、Python程序设计作业代写、Python语言作业are going to need a smarter approach, so think about the following ideas and developpruning rules based on these.1. Column and row exclusions Column and row exclusionsIf a cells column and row already contain all possible digits, except one, then that cellmust contain the missing digit. In the example below, the green cell must be 4. This cellis a singleton. To find singletons, for each cell you need to keep a list of which digitsare allowed within it, and then check the relevant row and column to remove thosedigits that have already been used. Of course, if you end up in a situation where thereare no possible digits that can go in a cell then youve done something wrong, so youralgorithm should stop and backtrack. You will need to add attributes and methods tothe Cell class to implement this strategy. If you cant find any singletons, then it is agood idea to choose a doubleton as the next cell to try, that is, one that has only twopossible digit choices left.2. Forced min and max values Forced min and max valuesCells that are less than 2 must implicitly have the value 1 as it is the only admissiblevalue on the board which respects that condition. Similarly, cells that are greater thanthe board size minus 1 must be equal to the board size. In the example below, the onlyComputational Thinking 15/03/20, 1)09 PMfile:///Users/cmmccart/Documents/Teaching/159271/Notes/assignment1-2020.html Page 4 of 6the board size minus 1 must be equal to the board size. In the example below, the onlypossible value for the green cell (less than 2) is 1.3. Exclusion of min and max values Exclusion of min and max valuesCells that are greater than other cells cannot be 1, the lowest value allowed on theboard, as there is no value smaller than 1. Similarly, cells that are less than than othercells cannot contain the max allowed value, as there would be nothing greater to befilled on the other side of the inequality. In the example below, 1 cannot be filled in thered squares as they are all greater than other board squares, so the only possibleplacement for 1 on the first row of the board is the green square.To find forced cell values, or excluded cell values, you need to inspect the inequalityconstraints and remove digits from your possibles lists for certain cells, according towhat you find.4. (Harder to implement) Chains of inequalities Chains of inequalitiesIf you notice a chain of inequalities, be it either (all descending),equal in size with the boards size, then that chain must be a sequence from 1 up to thelength of the board. The length of the chain guarantees that this sequence is the onlypossible solution that satisfies the monotone condition imposed by the inequality chain.Computational Thinking 15/03/20, 1)09 PMfile:///Users/cmmccart/Documents/Teaching/159271/Notes/assignment1-2020.html Page 5 of 65. There are other intelligent things that can be done. You can cross-reference between thepossibles list for a cell, and the missing values in the relevant row and column. Ifthere is only one value that is common to all sets, then this value must go in the cell.Marking scheme1. 10 marks for a correct program that uses a recursive backtracking recursive backtracking strategywith basic pruning.2. 5 marks for adding the facility to choose singletons ahead of other cells, using anapproach that inspects cell values. With this approach, your solution should be able tosolve the trivial puzzles.3. 5 marks for finding forced and excluded cell values, using an approach that inspectsinequality constraints. With this approach, your solution should be able to solve theeasy puzzles.4. 5 marks for more sophisticated pruning strategies, either those described here or others,that allow for fast backtracking over hard puzzles.We will test your program using puzzles similar to those provided, for trivial, easy and hardcategories.SubmissionSubmit your project in Stream as a single zipped file containing your completed code,contained in the folder Futoshiki_YourID Futoshiki_YourID.PlagiarismThere are many Futoshiki solvers available on the internet (and many more Sudoku solvers).I am sure that many of these are recursive backtracking solutions implemented in Python.For this reason, and also so that the concepts taught in lectures are reinforced, you must useComputational Thinking 15/03/20, 1)09 PMfile:///Users/cmmccart/Documents/Teaching/159271/Notes/assignment1-2020.html Page 6 of 6For this reason, and also so that the concepts taught in lectures are reinforced, you must usethe template provided as the basis of your implementation. You can improve the display orthe animation however you like, you can add more classes or improve those provided (thispart you actually need to do), you can make the solver as sophisticated as you like. Whatyou cannot do is download a solution from the internet and submit it as your own work.If you try this, you will get no marks. Make an attempt to read the course notes, to followthe lecture slides and to apply the concepts discussed, in completing this assignment.As stated at the beginning of this document, you are expected to work on this assignmentindividually and all work that you hand in is expected to be your own work. You are allowedto discuss any aspect of this assignment with others, in person, or on the Stream forum.What you are not allowed to do is to submit someone elses solution as your own work. Ifit appears that this is the case, you will get no marks.Late submission:Late assignments will be penalised 10% for each weekday past the deadline, for up to five (5)days; after this no marks will be gained. In special circumstances an extension may beobtained from the paper co-ordinator, and these penalties will not apply. Workload will notbe considered a special circumstance – you must budget your time.转自:http://www.daixie0.com/contents/3/4937.html

你可能感兴趣的:(讲解:159.271、Futoshiki Solver、Python、PythonSQL|Haskel)