Difference between revisions of "N-Queens/Sudoku Assignment"

From CSE231 Wiki
Jump to navigation Jump to search
 
(145 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
Not everything in the world should be divided and conquered.  Backtracking is a powerful technique which can be readily parallelized.  We will gain experience with backtracking by solving the N-Queens problem and Sudoku in parallel.
 
Not everything in the world should be divided and conquered.  Backtracking is a powerful technique which can be readily parallelized.  We will gain experience with backtracking by solving the N-Queens problem and Sudoku in parallel.
  
N-Queens in particular can be used to explain the call stack as the chessboard *IS* the call stack.  Whoa.
+
N-Queens in particular can be used to explain the call stack as the chessboard *IS* the call stack.
  
In this assignment, you will implement solutions to both the n-queens and Sudoku problems.
+
In this assignment, you will implement solutions to both the N-Queens and Sudoku problems.
  
 
=N-Queens=
 
=N-Queens=
Line 11: Line 11:
  
 
==Background==
 
==Background==
The n-queens problem is a fundamental coding puzzle which asks: how can N queens be placed on an NxN chessboard so that they cannot attack each other? In chess, a queen can move horizontally, vertically, and diagonally across the board. Thus, to solve the n-queens problem, we must effectively figure out how to place the queens in such a way that no two of them occupy the same row, column, or diagonal. We will be building a method that finds the total number of solutions for n-queens for any given n.
+
The n-queens problem is a fundamental coding puzzle which asks: how can N queens be placed on an NxN chessboard so that they cannot attack each other? In chess, a [https://en.wikipedia.org/wiki/Queen_(chess)#Placement_and_movement queen can attack horizontally, vertically, and diagonally] across the board. Thus, to solve the n-queens problem, we must effectively figure out how to place the queens in such a way that no two of them occupy the same row, column, or diagonal. We will be building a method that finds the total number of solutions for n-queens for any given n.
  
 +
==Roadmap to Victory==
 +
# (Warm Up) SequentialNQueens
 +
# DefaultImmutableQueenLocations
 +
# FirstAvailableRowSearchAlgorithm
 +
# ParallelNQueens
  
 
+
==The Core Questions==
==Roadmap to Victory==
+
*What are the tasks?
#DefaultImmutableQueens
+
*What is the data?
#QueenLocationsUtils
+
*Is the data mutable?
#SequentialNQueens
+
*If so, how is it shared?
#ParallelNQueens
 
  
 
==Code To Implement==
 
==Code To Implement==
Before coding anything, take a look at the javadocs to see what everything does and what you will need to implement.
+
===Sequential Warm Up===
===Sequential Solution===
+
  <nowiki> public static int countSolutions(int boardSize) {
  <nowiki> public static int countSolutions(MutableQueenLocations queenLocations) {
+
MutableInt count = new MutableInt();
MutableInt count = new MutableInt(0);
+
int[] board = new int[boardSize];
placeQueenInRow(count, queenLocations, 0);
+
Arrays.fill(board, EMPTY);
 +
search(count, board, 0);
 
return count.intValue();
 
return count.intValue();
 
}</nowiki>
 
}</nowiki>
  
{{CodeToImplement|SequentialNQueens|placeQueenInRow|nqueens.lab}}
+
{{CodeToImplement|SequentialNQueens|search|nqueens.warmup}}
  
{{Sequential|private static void placeQueenInRow(MutableInt count, MutableQueenLocations queenLocations, int row)}}
+
{{Sequential|private static void search(MutableInt count, int[] board, int row)}}
  
===Parallel Solution===
+
===Parallel Studio===
====Board State====
+
====Board State: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/nqueens/lab/DefaultImmutableQueenLocations.html DefaultImmutableQueenLocations]====
Investigate <code>DefaultMutableQueenLocations</code> and <code>AbstractQueenLocations</code> for clues on how to implement <code>DefaultImmutableQueenLocations</code>.
+
{{CodeToImplement|DefaultQueenLocations|createNext<br>getBoardSize<br>getColumnOfQueenInRow<br>getCandidateColumnsInRow|nqueens.lab}}
  
{{CodeToImplement|DefaultImmutableQueenLocations|createNext<br>getColumnOfQueenInRow<br>getRowCount<br>getBoardSize|nqueens.lab}}
+
=====[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/nqueens/core/ImmutableQueenLocations.html#createNext(int,int) createNext(row,col)]=====
 +
{{Sequential|public DefaultQueenLocations createNext(int row, int col)}}
  
{{Sequential|public DefaultImmutableQueenLocations createNext(int column)}}
+
There are two constructors for this class.  [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/nqueens/lab/DefaultImmutableQueenLocations.html#DefaultImmutableQueenLocations-int- A public one which creates a fresh new board state with no queens yet placed.] and a private one which creates a new board with the state of a given board which is further constrained by a new queen in the next row.  You need to create a new instance using one of these two constructors.  Which one is it?
  
There are two constructors for this class.  [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/nqueens/lab/DefaultImmutableQueenLocations.html#DefaultImmutableQueenLocations-int- A public one which creates a fresh new board state with no queens yet placed.] and a private one which creates a new board with the state of a given board which is further constrained by a new queen in the next row.  You need to create a new instance using one of these two constructors.  Which one is it?
+
Consider this example program which creates a valid 4-queens solution:
  
{{Sequential|public int getColumnOfQueenInRow(int row)}}
+
<pre> int boardSize = 4;
 +
QueenLocations board0 = new DefaultQueenLocations(boardSize);
 +
QueenLocations board1 = board0.createNext(0, 1);
 +
QueenLocations board2 = board1.createNext(1, 3);
 +
QueenLocations board3 = board2.createNext(2, 0);
 +
QueenLocations board4 = board3.createNext(3, 2);
 +
System.out.println(board4);</pre>
  
{{Sequential|public int getRowCount()}}
 
  
 +
Which board is used to create the next board?
 +
 +
=====[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/nqueens/core/ImmutableQueenLocations.html#getBoardSize() getBoardSize()]=====
 
{{Sequential|public int getBoardSize()}}
 
{{Sequential|public int getBoardSize()}}
  
The three methods above can all be done in just one line. Don't make things too complicated! Note that we will refer to the standard 8x8 chessboard's size as 8 and not 64.
+
Note that we will refer to the standard 8x8 chessboard's size as 8 and not 64.
  
{{Sequential|public boolean isNextRowThreatFree(int column)}}
+
=====[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/nqueens/core/ImmutableQueenLocations.html#getColumnOfQueenInRow(int) getColumnOfQueenInRow(row)]=====
 +
{{Sequential|public Optional<Integer> getColumnOfQueenInRow(int row)}}
  
Do not feel compelled to build this method from scratch.  Investigate your super class for a utility method that will be helpful.
+
For an 8x8 board with queens placed in (row=0, col=1), (row=1, col=6), and (row=2, col=4)  
<spoiler show="spoiler" hide="spoiler">[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/nqueens/core/AbstractQueenLocations.html#isCandidateThreatFree-int-int- isCandidateThreatFree(row, candidateColumn)]</spoiler>
 
  
====Board Utils====
+
[[File:Queens in rows 012.png|350px]]
This class will provide methods that will speed up our implementation of the parallel solution in the final step.
 
  
{{CodeToImplement|QueenLocationsUtils|getCandidateColumns<br>getCandidateColumnsForNextRow|nqueens.lab}}
+
* getColumnOfQueenInRow(0) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- Optional.of](1)
 +
* getColumnOfQueenInRow(1) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- Optional.of](6)
 +
* getColumnOfQueenInRow(2) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- Optional.of](4)
 +
* getColumnOfQueenInRow(3) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]
 +
* getColumnOfQueenInRow(4) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]
 +
* getColumnOfQueenInRow(5) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]
 +
* getColumnOfQueenInRow(6) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]
 +
* getColumnOfQueenInRow(7) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]
  
{{Sequential| public static Collection<Integer> getCandidateColumns(QueenLocations queenLocations, int row)}}
+
=====[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/nqueens/core/ImmutableQueenLocations.html#getCandidateColumnsInRow(int) getCandidateColumnsInRow(row)]=====
 +
{{Sequential|public List<Integer> getCandidateColumnsInRow(int row)}}
  
This method should find all the columns in the given row that a queen could be placed, and return them in a single collection (ex. LinkedList). Don't forget to utilize some of the methods you just completed in <code>DefaultImmutableQueensLocation</code>
+
For an 8x8 board with a single queen placed in (row=0, col=4)
  
{{Sequential| public static Collection<Integer> getCandidateColumnsForNextRow(ImmutableQueenLocations queenLocations)}}
+
[[File:Queen_r0_c4.png|350px]]
  
Nice and simple, don't overthink it! Just use methods you've already written to do all the work.
+
* getCandidateColumnsInRow(0) returns []
 +
* getCandidateColumnsInRow(1) returns [0,1,2,6,7]
 +
* getCandidateColumnsInRow(2) returns [0,1,3,5,7]
 +
* getCandidateColumnsInRow(3) returns [0,2,3,5,6]
 +
* getCandidateColumnsInRow(4) returns [1,2,3,5,6,7]
 +
* getCandidateColumnsInRow(5) returns [0,1,2,3,5,6,7]
 +
* getCandidateColumnsInRow(6) returns [0,1,2,3,5,6,7]
 +
* getCandidateColumnsInRow(7) returns [0,1,2,3,5,6,7]
  
====ParallelNQueens====
+
The provided [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/nqueens/core/ImmutableQueenLocations.html#isLocationThreatFree(int,int) isLocationThreatFree(row, col)] method should be helpful.
 +
 
 +
====Search Order: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/nqueens/lab/FirstAvailableRowSearchAlgorithm.html FirstAvailableRowSearchOrder]====
 +
This class will provide methods that will allow us to implement a clean and efficient parallel solution in the final step.
 +
 
 +
{{CodeToImplement|FirstAvailableRowSearchOrder|selectedNextUnplacedRow|nqueens.lab}}
 +
 
 +
{{Sequential|public Optional<Integer> selectedNextUnplacedRow(QueenLocations queenLocations)}}
 +
 
 +
For an 8x8 board with queens placed at (row=0, col=0), (row=1, col=3), (row=2, col=6), and (row=6, col=7):
 +
 
 +
[[File:Queen missing in row3.png|350px]]
 +
 
 +
* selectedNextUnplacedRow(queenLocations) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- Optional.of](3)
 +
 
 +
<hr>
 +
 
 +
For a board with no unplaced rows, for example, a solution:
 +
 
 +
[[File:8queens solution0.png|350px]]
 +
 
 +
* selectedNextUnplacedRow(queenLocations) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]
 +
 
 +
<hr>
 +
{{Warning|Do NOT skip empty rows simply because they have no candidate columns}}
 +
 
 +
In cases where a row does not have a queen placed in it, but has no valid candidate columns, for example a 3x3 board with a queen placed at (row=0, col=1):
 +
 
 +
[[File:Queen 3x3 eliminates next row.png|200px]]
 +
 
 +
It is critical that
 +
 
 +
* selectedNextUnplacedRow(queenLocations) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- Optional.of](1)
 +
 
 +
When searching for solutions we do not want to avoid dead rows.  If anything, we want to move them to the front of the line, so that search can cease the current fruitless path.
 +
 
 +
====[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/nqueens/lab/ParallelNQueens.html ParallelNQueens]====
 
Searching for solutions like n-queens can be done in parallel without the need to finish at each level.  As such, <code>forasync</code> is preferable to <code>forall</code>.  However:
 
Searching for solutions like n-queens can be done in parallel without the need to finish at each level.  As such, <code>forasync</code> is preferable to <code>forall</code>.  However:
  
{{Warning|Ensure that you complete all of your tasks by enclosing them all in a <code>finish</code>.}}
+
{{Warning|Ensure that you complete all of your tasks by enclosing them a single <code>finish</code>.}}
  
{{CodeToImplement|ParallelNQueens|placeQueenInRow<br>countSolutions|nqueens.lab}}
+
{{CodeToImplement|ParallelNQueens|searchForSolutions<br>countSolutions|nqueens.lab}}
  
{{Parallel|public static int countSolutions(ImmutableQueenLocations queenLocations)}}
+
{{Parallel|public static int countSolutions(QueenLocations queenLocations, RowSearchOrder rowSearchOrder)}}
  
 
{{Warning|FinishAccumulators must be registered with their finish statement}}
 
{{Warning|FinishAccumulators must be registered with their finish statement}}
  
Instead of using a MutableInt in order to count the number of solutions we have found, we want to use a Finish Accumulator. The slide below shows syntax for setting one up, and it is also listed in the [[#Tips]] section.
+
Instead of using a MutableInt in order to count the number of solutions we have found, we want to use a Finish Accumulator.  
  
[https://docs.google.com/presentation/d/15loIp6XhMWI2OCAYgswLm3MLD6vqJcRUtYJqhUJrves/pub?start=false&loop=false&delayms=3000&slide=id.g35c5a72286_0_19 slide]
+
Creating a new instance of FinishAccumulator is done via on of the many static methods on the V5 class (the same class we get async and finish from).
  
{{Parallel|private static void placeQueenInRow(FinishAccumulator<Integer> acc, ImmutableQueenLocations queenLocations)}}
+
Refer to the [[Syntax_of_231#Finish_Accumulators|syntax page]] in order to see the syntax for properly setting up the accumulator.
  
Make sure you look at the Sequential N-Queens solution if you need ideas on how to approach the algorithm.
+
{{Parallel|private static void searchForSolutions(FinishAccumulator<Integer> accumulator, QueenLocations queenLocations, RowSearchOrder rowSearchOrder)}}
  
 +
<!--
 
==Tips==
 
==Tips==
*As <code>ImmutableQueenLocations</code> is immutable, you will need to create a new instance of the object whenever you move on from one row to the next. This is where <code>createNext</code> comes in, along with the private constructor of this class.
+
*As <code>QueenLocations</code> is immutable, you will need to create a new instance of the object whenever you move on from one row to the next. This is where <code>createNext</code> comes in, along with the private constructor of this class.
 
*The <code>isNextRowThreatFree</code> method can easily be completed with a method in <code>AbstractQueenLocations</code>. Refer to that for help.
 
*The <code>isNextRowThreatFree</code> method can easily be completed with a method in <code>AbstractQueenLocations</code>. Refer to that for help.
 
*The sequential solution uses <code>MutableQueenLocations</code> while the parallel solution uses your implementation of <code>ImmutableQueenLocations</code>. Be careful to use the correct <code>QueenLocations</code> implementation.
 
*The sequential solution uses <code>MutableQueenLocations</code> while the parallel solution uses your implementation of <code>ImmutableQueenLocations</code>. Be careful to use the correct <code>QueenLocations</code> implementation.
 
*As the name suggests, <code>placeQueenInRow</code> will go through the columns of the given row to check if a queen can fit in that location. If it can, it will set that value in <code>MutableQueenLocations</code>. If the examined row is that last row of the board, you have found one valid solution to the n-queens problem. Update the correct parameter accordingly. Otherwise, recurse and keep going until you reach the last row.
 
*As the name suggests, <code>placeQueenInRow</code> will go through the columns of the given row to check if a queen can fit in that location. If it can, it will set that value in <code>MutableQueenLocations</code>. If the examined row is that last row of the board, you have found one valid solution to the n-queens problem. Update the correct parameter accordingly. Otherwise, recurse and keep going until you reach the last row.
 
*For the parallel implementation of <code>placeQueenInRow</code>, we are using the "one-finish" pattern. Do not call <code>finish</code> in the recursive method.
 
*For the parallel implementation of <code>placeQueenInRow</code>, we are using the "one-finish" pattern. Do not call <code>finish</code> in the recursive method.
*The syntax for instantiating a <code>FinishAccumulator</code> of <code>Integer</code> is: <code>FinishAccumulator<Integer> acc = newIntegerFinishAccumulator(NumberReductionOperator.SUM);</code>
+
*Go check out the [[Syntax_of_231#Finish_Accumulators|syntax page]] if you have questions on how to set up the Finish Accumulator
*The syntax for using a <code>FinishAccumulator</code> is: <code>finish(register(acc), () -> { //body });</code>
+
-->
  
 
=Sudoku=
 
=Sudoku=
Line 103: Line 168:
 
We will be using a similar algorithm to solve a Sudoku puzzle. For those not familiar, a Sudoku puzzle is composed of a 9-by-9 grid of squares. This grid is also divided into 9 large boxes, each of which is a 3-by-3 of the smaller squares. In a completed puzzle, each of the smaller squares contains a single number from 1 to 9 (inclusive). However, if a square contains a given number, that same number cannot be anywhere else in the same row, column, or box. Thus, for Sudoku, we are given an incomplete board and must fill in the remaining squares while meeting these requirements.
 
We will be using a similar algorithm to solve a Sudoku puzzle. For those not familiar, a Sudoku puzzle is composed of a 9-by-9 grid of squares. This grid is also divided into 9 large boxes, each of which is a 3-by-3 of the smaller squares. In a completed puzzle, each of the smaller squares contains a single number from 1 to 9 (inclusive). However, if a square contains a given number, that same number cannot be anywhere else in the same row, column, or box. Thus, for Sudoku, we are given an incomplete board and must fill in the remaining squares while meeting these requirements.
  
Sudoku is another problem well solved by backtracking.  Check the understanding you gained of backtracking with N-Queens by challenging yourself to solve Sudoku's solver without assistance. The game of Sudoku is bit more complex though than N-Queens, and there are more strategies we can do than just backtracking in order to speed up our solution. To make this assignment more compelling, you will also do search orderings and constraint propagation.  
+
Sudoku is another problem well solved by backtracking.  Check the understanding you gained of backtracking with N-Queens by challenging yourself to solve Sudoku's solver without assistance. The game of Sudoku is bit more complex though than N-Queens, and there are more strategies we can do than just backtracking in order to speed up our solution. To make this assignment more compelling, you will implement alternate search orderings and constraint propagation.  
  
 
Read [http://norvig.com/sudoku.html Peter Norvig's Essay] before you begin coding. It will cover everything related to the Sudoku problem itself and how one can design a solution for it.
 
Read [http://norvig.com/sudoku.html Peter Norvig's Essay] before you begin coding. It will cover everything related to the Sudoku problem itself and how one can design a solution for it.
  
 
==Roadmap to Victory==
 
==Roadmap to Victory==
There isn't one easiest path through the required files. Some classes utilize methods written in other files, so some students may take a the path such that they will only call methods that are provided or have already implemented. For other students, it might be conceptually easier to start with <code>ParallelSudoku.java</code>, since this class closest resembles the work you just did in n-queens. Below is one path that we recommend. In this path, you will have to call the constraint propagator in the Immutable Puzzle before completing the actual propagator, but aside from that most methods build on top of one another. In summary, you can work on these classes in whatever order makes the most sense for you personally.
+
<!--
#DefaultImmutableSudokuPuzzle
+
There isn't one easiest path through the required files. Some classes utilize methods written in other files, so some students may take a the path such that they will only call methods that are provided or have already implemented. For other students, it might be conceptually easier to start with ParallelSudoku and RowMajorSquareSearchAlgorithm, since these classes closest resembles the work you just did in n-queens. Below is one path that we recommend. In this path, you will build the methods before other methods use them. Many find DefaultConstraintPropagator to be the most challenging part, however. In summary, you can work on these classes in whatever order makes the most sense for you personally.
#RowMajorSquareSearchAlgorithm
+
-->
#FewestOptionsFirstSquareSearchAlgorithm
+
#PeerEliminationOnlySudokuPuzzle
 +
#RowMajorSearchOrder
 +
#FewestOptionsFirstSearchOrder
 
#ParallelSudoku
 
#ParallelSudoku
#DefaultConstraintPropagator
+
#(Optional Challenge) Add Unit and Twins Constraint Propagation to DefaultConstraintPropagator
#(Optional Challenge) Add Unit Constraint Propagation to DefaultConstraintPropagator
+
 
 +
==The Core Questions==
 +
*What are the tasks?
 +
*What is the data?
 +
*Is the data mutable?
 +
*If so, how is it shared?
  
 
==Code To Investigate==
 
==Code To Investigate==
 +
===Square===
 
enum [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Square.html Square]
 
enum [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Square.html Square]
:Collection<Square> [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Square.html#getPeers-- getPeers()]
+
:Collection<Square> [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Square.html#getPeers() getPeers()]
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Square.html#valueOf-int-int- valueOf(row, column)]
+
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Square.html#valueOf(int,int) valueOf(row, column)]
:all [https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html enums] have a [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Square.html#values-- values()] method
+
:all [https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html enums] have a [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Square.html#values() values()] method
 
 
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Units.html Units]
 
:public static Iterable<Collection<Square>> [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Units.html#allUnits-- allUnits()]
 
 
 
interface [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/ConstraintPropagator.html ConstraintPropagator] extends AbstractSet<Integer> implements SortedSet<Integer>
 
  
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/OptionSet.html OptionSet]
+
===SudokuUtils===
:public static OptionSet [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/OptionSet.html#createAllOptions-- createAllOptions()]
+
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/SudokuUtils.html SudokuUtils]
:public static OptionSet [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/OptionSet.html#createSingleOption-- createSingleOption(int option)]
+
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/SudokuUtils.html#deepCopyOf(java.util.Map) deepCopyOf(Map<Square, SortedSet<Integer>> other)]
 +
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/SudokuUtils.html#allUnits() allUnits()]
 +
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/SudokuUtils.html#getRowUnit(int) getRowUnit(row)]
 +
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/SudokuUtils.html#getColumnUnit(int) getColumnUnit(col)]
 +
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/SudokuUtils.html#getBoxUnit(int,int) getColumnUnit(row,col)]
 +
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/SudokuUtils.html#getUnitsForSquare(sudoku.core.Square) getUnitsForSquare(square)]
  
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/SquareToOptionSetMapUtils.html SquareToOptionSetMapUtils]
+
===CandidateSet===
:public static Map<Square, SortedSet<Integer>> deepCopyOf(Map<Square, SortedSet<Integer>> other)
+
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/candidate/core/CandidateSet.html CandidateSet<E>] implements [https://docs.oracle.com/javase/8/docs/api/java/util/SortedSet.html SortedSet<E>]
 +
:public static CandidateSet [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/candidate/core/CandidateSet.html#createAllCandidates()-- createAllCandidates()]
 +
<!--
 +
:public static CandidateSet [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/candidate/core/CandidateSet.html#createSingleOption-- createSingleOption(int option)]
 +
-->
  
 
==Code To Implement==
 
==Code To Implement==
 +
===PeerEliminationOnlySudokuPuzzle===
 +
As the name suggests, <code>DefaultImmutableSudokuPuzzle</code> is immutable, and you will need to create a new instance of the object whenever you move on from one square to the next. This is analogous to the work you did for [[#NQueens]].
  
 +
{{CodeToImplement|PeerEliminationOnlySudokuPuzzle|constructors<br>createNext<br>getValue<br>getOptions|sudoku.lab}}
  
===Puzzle===
+
====constructors====
As the name suggests, <code>ImmutableSudokuPuzzle</code> is immutable, and you will need to create a new instance of the object whenever you move on from one square to the next. This is analogous to the work you did for [[#NQueens]].
+
The constructors for PeerEliminationOnlySudokuPuzzle have been provided:
 
 
{{CodeToImplement|DefaultImmutableSudokuPuzzle|constructors<br>createNext<br>getValue<br>getOptions|sudoku.lab}}
 
  
{{Sequential|public DefaultImmutableSudokuPuzzle(ConstraintPropagator constraintPropagator, String givens)}}
+
=====DefaultImmutableSudokuPuzzle(givens)=====
 +
{{Sequential|public PeerEliminationOnlySudokuPuzzle(String givens)}}
  
 
This constructor creates a puzzle constrained to an initial set of givens.  You can think of the givens as the original values provided by the newspaper or airline magazine or puzzle book or whatever.
 
This constructor creates a puzzle constrained to an initial set of givens.  You can think of the givens as the original values provided by the newspaper or airline magazine or puzzle book or whatever.
  
You will leverage the constraintPropagator to build your map of option sets.
+
=====PeerEliminationOnlySudokuPuzzle(other,square,value)=====
 
+
{{Sequential|private PeerEliminationOnlySudokuPuzzle(PeerEliminationOnlySudokuPuzzle other, Square square, int value)}}
{{Sequential|private DefaultImmutableSudokuPuzzle(DefaultImmutableSudokuPuzzle other, Square square, int value)}}
 
  
This constructor takes a given previous puzzle and a square value to create a new further constrained puzzle.
+
This constructor takes a given previous puzzle and a square value to create a new further constrained puzzle.  This will be invoked via a public method on PeerEliminationOnlySudokuPuzzle during the search process.
  
 +
====createNext(square,value)====
 
{{Sequential|public ImmutableSudokuPuzzle createNext(Square square, int value)}}
 
{{Sequential|public ImmutableSudokuPuzzle createNext(Square square, int value)}}
  
 
This method should create a new puzzle instance using one of the constructors.  Which one is it?
 
This method should create a new puzzle instance using one of the constructors.  Which one is it?
  
{{Sequential|public int getValue(Square square)}}
+
====getValue(square)====
 +
{{Sequential|public Optional<Integer> getValue(Square square)}}
 +
 
 +
{{Warning|Ignore any documentation which reports this method should return 0 if it is unfilled.}}
  
Based on the state of the board, return the value of a given square if it is known.  Otherwise, return 0.
+
Based on the state of the board, return the value of a given square if it is known.  Otherwise, return empty.
  
 
How do we determine if a value for a given square is "known"?
 
How do we determine if a value for a given square is "known"?
  
{{Sequential|public SortedSet<Integer> getOptions(Square square)}}
+
====getCandidates(square)====
 +
{{Sequential|public SortedSet<Integer> getCandidates(Square square)}}
  
 
Based on the state of the board, return the candidate values for a given square.
 
Based on the state of the board, return the candidate values for a given square.
  
 
===Search Order===
 
===Search Order===
Simply by changing the search order, a great reduction of work can be achieved. The class names sadly give away the approaches. 
+
Simply by changing the search order, a great reduction of work can be achieved.
 +
 
 +
To implement them, ask yourself:
 +
* How do I determine if a square is filled?
 +
* How do I find the unfilled square with the minimum number of candidates?
  
Ask yourself:  
+
When you have completed them, ask yourself:  
 
* Which algorithm will perform better and why?
 
* Which algorithm will perform better and why?
 
* What properties make a square "filled"?
 
* What properties make a square "filled"?
  
{{CodeToImplement|RowMajorSquareSearchAlgorithm|selectNextUnfilledSquare|sudoku.lab}}
+
{{Warning|Do NOT omit squares with 0 candidates.}}
{{Sequential|public Square selectNextUnfilledSquare(SudokuPuzzle puzzle)}}
+
 
 +
====RowMajorSearchOrder====
 +
{{CodeToImplement|RowMajorSearchOrder|selectNextUnfilledSquare|sudoku.lab}}
 +
{{Sequential|Optional<Square> selectNextUnfilledSquare(ImmutableSudokuPuzzle puzzle)}}
 +
 
 +
{{Warning|<br>Ignore any documentation which reports this method should return null if it is completely filled.<br>This method should return Optional.empty() for a completely filled board.}}
  
 
Simply run through the <code>Square.values()</code> which will iterate through squares going down the row (A1, A2, A3, ...). Make sure not to return squares that have already been filled.
 
Simply run through the <code>Square.values()</code> which will iterate through squares going down the row (A1, A2, A3, ...). Make sure not to return squares that have already been filled.
  
{{CodeToImplement|FewestOptionsFirstSquareSearchAlgorithm|selectNextUnfilledSquare|sudoku.lab}}
+
====FewestOptionsFirstSearchOrder====
{{Sequential|public Square selectNextUnfilledSquare(SudokuPuzzle puzzle)}}
+
{{CodeToImplement|FewestOptionsFirstSearchOrder|selectNextUnfilledSquare|sudoku.lab}}
 +
{{Sequential|Optional<Square> selectNextUnfilledSquare(ImmutableSudokuPuzzle puzzle)}}
 +
 
 +
{{Warning|<br>Ignore any documentation which reports this method should return null if it is completely filled.<br>This method should return Optional.empty() for a completely filled board.}}
  
 
Go through every square by calling <code>Square.values()</code>, find a square that is (1) not already filled, and (2) has the minimal number of possible options among all the squares, and return that square.
 
Go through every square by calling <code>Square.values()</code>, find a square that is (1) not already filled, and (2) has the minimal number of possible options among all the squares, and return that square.
  
 
===Solver===
 
===Solver===
This part of the assignment is also similar to its n-queens counterpart. Searching for solutions like sudoku can be done in parallel without the need to finish at each level.  As such, <code>forasync</code> is preferable to <code>forall</code>. However:
+
This part of the assignment is also similar to its n-queens counterpart. Searching for solutions like sudoku can be done in parallel without the need to finish at each level.  As such, <code>forasync</code> is preferable to <code>forall</code> and is, in fact, required by the test.  
 +
 
 +
{{Warning|Ensure that you complete all of your tasks by enclosing them all in a single <code>finish</code>.}}
  
{{Warning|Ensure that you complete all of your tasks by enclosing them all in a <code>finish</code>.}}
+
{{Tip|Use the iterable version of forasync}}
  
 
{{CodeToImplement|ParallelSudoku|solve<br>solveKernel|sudoku.lab}}
 
{{CodeToImplement|ParallelSudoku|solve<br>solveKernel|sudoku.lab}}
Line 193: Line 290:
  
 
{{Parallel|private static void solveKernel(MutableObject<ImmutableSudokuPuzzle> solution, ImmutableSudokuPuzzle puzzle, SquareSearchAlgorithm squareSearchAlgorithm)}}
 
{{Parallel|private static void solveKernel(MutableObject<ImmutableSudokuPuzzle> solution, ImmutableSudokuPuzzle puzzle, SquareSearchAlgorithm squareSearchAlgorithm)}}
 
====Tips====
 
*Make sure your solver uses the generic SquareSearchAlgorithm so that it works with both versions of the search ordering.
 
*Just like n-queens, your <code>solveKernel</code> is the recursive method that will be called in the solve method. Again, watch where you put your <code>finish</code>.
 
*In your <code>solveKernel</code> method, you should use the given search algorithm (row major or fewest options first) to select which square you will fill. After selecting a square, you should recursively call the kernel to check every viable option in the context of the puzzle. If there are no more unfilled squares, you should set the value of the solution to the finished puzzle and exit out of the recursion.
 
 
===Constraint Propagator===
 
 
{{CodeToImplement|DefaultConstraintPropagator|createOptionSetsFromGivens<br>createNextOptionSets<br>assign<br>eliminate|sudoku.lab}}
 
 
You will need to implement two public methods to satisfy the ConstraintPropagator interface: <code>createOptionSetsFromGivens</code> and <code>createNextOptionSets</code>.  Each of these two methods will be invoked from a different constructor in the [[#DefaultImmutableSudokuPuzzle]] class.  It should be relatively obvious which one goes with which based on the parameters.
 
 
{{Warning| Resist the temptation to perform the constraint propagation in these public methods.  Get things set up and then get your private method(s) to work for you.}}
 
 
====createOptionSetsFromGivens====
 
{{Sequential|public Map<Square, SortedSet<Integer>> createOptionSetsFromGivens(String givens)}}
 
 
This method will be invoked when you have loaded a new puzzle.  Whether it is from the newspaper, or your airline magazine, or Dr. Arto Inkala there is a set of givens the puzzle creator has provided. That string of givens is parsed into a double array of integers that aligns with the Sudoku board (values[0][0] is the top left corner of the board). If a specific spot of the board wasn't given in the input, it will be put into values[][] as 0. 
 
 
A good approach here is to start of by initializing all of the squares to all the options (1 through 9) whether or not the square has a given value. 
 
 
Once that is complete go through the givens and get your private method(s) to work for you.  Note: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/sudoku/core/Square.html#valueOf-int-int- Square.valueOf(row, column)] should be useful here.
 
 
====createNextOptionSets====
 
{{Sequential|public Map<Square, SortedSet<Integer>> createNextOptionSets(Map<Square, SortedSet<Integer>> otherOptionSets, Square square, int value)}}
 
 
This method should be invoked when you are searching for the solution in your solver.  Much like when you need to create a new copy of the board every time you make a decision in the n-queens search so too will you need to create a new copy of your sudoku board every time you make a decision in your backtracking search.
 
 
Make sure to use the <code>SquareToOptionSetMapUtils.deepCopyOf</code> method and refrain from mutating the incoming parameter <code>otherOptionSets</code>.
 
 
As warned above, do not actually do any of the constraint propagation in this method. Simply get the copy of the options set, then send it through your private <code>assign</code> and <code>eliminate</code> to update the copy to be correct before returning it.
 
 
{{Tip |If you have two Lists, <code>a</code> and <code>b</code>, and you set <code>a <nowiki>=</nowiki> b</code>, then any changes you make to <code>a</code> will also be made to <code>b</code>. Both variables reference the same objects; they are not copies.}}
 
 
====Assign and Eliminate====
 
 
[http://norvig.com/sudoku.html Peter Norvig's Essay] is very helpful here.  We have adopted his terms, but challenge yourself to complete this section without simply translating his pseudocode.
 
 
To simplify things a bit, we have elected to not short circuit when a 0 option square is found (relying on the search ordering to take care of that for us).
 
 
It is up to you how to utilize these two methods. The goal is to update the map that is passed in to reflect the "solving" of a square. For example, if assign is called on square A1 for the value 1, the map that is returned should link the square A1 to a list containing just the value 1 ([1]). Furthermore, any square that is a peer of A1 (ex. B1), should not have the value 1 in its set in the map.
 
 
But in solving Sudoku, there are some strategies that we can rely on to more efficiently complete the puzzle. The first one, and required in order to finish this lab, is ''Peer Elimination Constraint Propagation'' (rule 1 in the Norvig essay). The rule is as follows: "If a square has only one possible value, then eliminate that value from the square's peers."
 
 
Let's very slowly walk through a example of this propagation in detail to make it more clear what we are looking for. Start by looking at this nearly finished board:
 
 
{| class="wikitable"
 
|-
 
! Board !! Info
 
|-
 
| [[File:PeerConstraint1.PNG|200px]]  || We can see that the red square (D1) has only one available option. At this point, let's say we call assign on the red square D1 for the value 1.
 
|-
 
| [[File:PeerConstraint3.PNG|200px]]  || As part of the assignment process, all the peers of red square D1 have the value 1 removed from their possibilities. In this example, yellow square, A1, is directly affected! After eliminating the alternative option, there is only one possible value yellow square A1 could be. This is where our constraint propagator needs to take the next step and treat yellow square A1 as assigned.
 
                |-
 
                | [[File:PeerConstraint4.PNG|200px]]  || This step right here (going from a small 7 to a big 7) is not actually something our program will do. In our implementation, '''having only one option in a square's set is equivalent to that square being "assigned" to that value'''. Because of this, you actually do not need to call assign again for yellow square A1.
 
                |-
 
                | [[File:PeerConstraint5.PNG|200px]]  || Now that yellow square A1 is known, the final step is to go through all of A1's peers (including green square C3) and remove the value 7. Note that now there is only one possible option left for green square C3. Your code should keep going, looking at all the peers of C3 and eliminating the value 1, and so on. Some puzzles can be completely solved using only Peer Elimination constraint propagation!
 
|}
 
 
 
{{Sequential|private void assign(Map<Square, SortedSet<Integer>> resultOptionSets, Square square, int value)}}
 
 
{{Warning|If you are hitting a ConcurrentModificationException here, make sure to make a copy of the optionsSet you are iterating over.  Use your local utility copyOf method. }}
 
 
<nowiki> SortedSet<Integer> copy = copyOf(optionSet);
 
for (int otherValue : copy) {
 
...</nowiki>
 
 
{{Sequential|private void eliminate(Map<Square, SortedSet<Integer>> resultOptionSets, Square square, int value)}}
 
 
====(Optional) Unit Constraint Propagation====
 
The second rule in [http://norvig.com/sudoku.html Peter Norvig's Essay] is Unit Constraint Propagation: "If a unit has only one possible place for a value, then put the value there". This propagation rule is it bit more challenging to put into code, but doing so can make even the hardest of Sudoku puzzles fall nice and quickly. To complete this bonus part of the lab, simply update your <code>assign</code> and <code>eliminate</code> methods to do unit constraint propagation (don't remove the old propagation rule!). After assigning a square, you'll have to look at the new options set to see if any unit (a single row, column, or box) has only one unassigned square that is a possibility for any value. We encourage you to think of a solution on your own! Below is a example of unit constraint propagation.
 
 
{| class="wikitable"
 
|-
 
! Board !! Info
 
|-
 
| [[File:UnitConstraint1.PNG|200px]]  || We can see that the red square (E1) has only one available option. At this point, let's say we call assign on the red square E1 for the value 6.
 
|-
 
| [[File:UnitConstraint2.PNG|200px]]  || Due to the assignment of red square E1, the value of 6 is eliminated from its peer, yellow square A1.
 
                |-
 
                | [[File:UnitConstraint3.PNG|200px]]  || Our Peer Elimination constraint propagation will treat yellow square A1 as if it has been assigned. Unfortunately, eliminating the value 9 from the peers doesn't open up any new opportunities for solving squares. This is where unit constraint propagation kicks in. Of the three unassigned squares in the top left box unit, 6 is only a valid option in the green square, C3. Even though green square C3 has multiple possible options remaining, we know that it '''must''' be 6, and should assign it as such
 
                |-
 
                | [[File:UnitConstraint4.PNG|200px]]  || Once green square C3 has been assigned (all other options are removed), we should make sure that all of C3's peers have 6 removed from their options. And keep an eye out for any new squares that we now know due to the two propagation rules! With both peer elimination and unit constraint propagation, we can solve a large quantity of Sudoku puzzles without needing any backtracking recursion
 
|}
 
  
 
=Testing Your Solution=
 
=Testing Your Solution=
Line 284: Line 296:
 
{{Viz|NQueensVizApp|nqueens.viz.solution}}
 
{{Viz|NQueensVizApp|nqueens.viz.solution}}
  
[[File:NQueensViz.png]]
+
[[File:NQueensViz.png|400px]]
  
 
===Sudoku===
 
===Sudoku===
{{Viz|SudokuSolutionApp|sudoku.viz.solution}}
+
====Propogate====
 +
{{Viz|SudokuApp|sudoku.viz.solution}}
 +
 
 +
[[File:SudokuPropagateViz.png|600px]]
 +
 
 +
====Solve====
 +
{{Viz|FxSudokuSolutionApp|sudoku.viz.solution}}
  
[[File:SudokuViz.png]]
+
[[File:SudokuSolutionViz.png|400px]]
  
 
==Correctness==
 
==Correctness==
 +
===Warm Up===
 +
{{TestSuite|SequentialNQueensWarmUpTestSuite|nqueens.warmup}}
 +
 +
===Lab===
 
There is a top-level test suite comprised of sub test suites which can be invoked separately when you want to focus on one part of the assignment.
 
There is a top-level test suite comprised of sub test suites which can be invoked separately when you want to focus on one part of the assignment.
===top level===
 
 
{{TestSuite|BacktrackTestSuite|backtrack.lab}}
 
{{TestSuite|BacktrackTestSuite|backtrack.lab}}
===sub===
+
 
 +
====NQueens====
 
{{TestSuite|NQueensTestSuite|nqueens.lab}}
 
{{TestSuite|NQueensTestSuite|nqueens.lab}}
 +
=====ImmutableQueenLocations=====
 +
{{TestSuite|ImmutableQueenLocationsTestSuite|nqueens.lab}}
 +
=====FirstAvailableRowSearchAlgorithm=====
 +
{{TestSuite|FirstAvailableRowSearchTestSuite|nqueens.lab}}
 +
=====ParallelNQueens=====
 +
{{TestSuite|ParallelNQueensSolutionCountTestSuite|nqueens.lab}}
 +
 +
====Sudoku====
 
{{TestSuite|SudokuTestSuite|sudoku.lab}}
 
{{TestSuite|SudokuTestSuite|sudoku.lab}}
 +
=====DefaultConstraintPropagator=====
 +
{{TestSuite|DefaultConstraintPropagatorTestSuite|sudoku.lab}}
 +
=====DefaultImmutableSudokuPuzzle=====
 +
{{TestSuite|DefaultImmutableSudokuPuzzleTestSuite|sudoku.lab}}
 +
=====RowMajorSearchOrder=====
 +
{{TestSuite|RowMajorSearchOrderTestSuite|sudoku.lab}}
 +
=====FewestOptionsFirstOrder=====
 +
{{TestSuite|FewestOptionsFirstOrderTestSuite|sudoku.lab}}
 +
=====ParallelSudokuSolve=====
 +
{{TestSuite|ParallelSudokuSolveTestSuite|sudoku.lab}}
 +
=====Holistic=====
 +
{{TestSuite|HolisticTestSuite|sudoku.lab}}
  
===(Optional) Challenge Unit Constraint Propagation===
+
=== Extra Credit Challenge Unit Constraint Propagation ===
 
{{TestSuite|ChallengeSudokuTestSuite|sudoku.challenge}}
 
{{TestSuite|ChallengeSudokuTestSuite|sudoku.challenge}}
  
Line 309: Line 351:
  
 
N-Queens subtotal: 35
 
N-Queens subtotal: 35
* Correct DefaultImmutableQueenLocations (5)
+
* Correct DefaultImmutableQueenLocations (10)
* Correct SequentialNQueens (10)
+
* Correct FirstAvailableRowSearchAlgorithm (5)
 
* Correct ParallelNQueens (10)
 
* Correct ParallelNQueens (10)
 
* Parallel ParallelNQueens (10)
 
* Parallel ParallelNQueens (10)
 
+
Sudoku subtotal: 65
Sudoku subtotal: 55
+
* Correct ImmutableSudokuPuzzle (5)
* Correct DefaultImmutableSudokuPuzzle (5)
+
* Correct ContraintPropagator (20)
* Correct ContraintPropagator (15)
+
* Correct RowMajorSearch (10)
* Correct RowMajorSquareSearchAlgorithm (5)
+
* Correct FewestOptionsFirstSearch (10)
* Correct FewestOtionsFirstSquareSearchAlgorithm (10)
 
 
* Correct ParallelSudoku (10)
 
* Correct ParallelSudoku (10)
 
* Parallel ParallelSudoku (10)
 
* Parallel ParallelSudoku (10)
  
Whole project:
+
Penalties may be assessed for clarity and efficiency.
* Clarity and efficiency (10)
 

Latest revision as of 20:11, 4 October 2021

Motivation

Not everything in the world should be divided and conquered. Backtracking is a powerful technique which can be readily parallelized. We will gain experience with backtracking by solving the N-Queens problem and Sudoku in parallel.

N-Queens in particular can be used to explain the call stack as the chessboard *IS* the call stack.

In this assignment, you will implement solutions to both the N-Queens and Sudoku problems.

N-Queens

Example solution of N-Queens when n equals 8

Background

The n-queens problem is a fundamental coding puzzle which asks: how can N queens be placed on an NxN chessboard so that they cannot attack each other? In chess, a queen can attack horizontally, vertically, and diagonally across the board. Thus, to solve the n-queens problem, we must effectively figure out how to place the queens in such a way that no two of them occupy the same row, column, or diagonal. We will be building a method that finds the total number of solutions for n-queens for any given n.

Roadmap to Victory

  1. (Warm Up) SequentialNQueens
  2. DefaultImmutableQueenLocations
  3. FirstAvailableRowSearchAlgorithm
  4. ParallelNQueens

The Core Questions

  • What are the tasks?
  • What is the data?
  • Is the data mutable?
  • If so, how is it shared?

Code To Implement

Sequential Warm Up

	public static int countSolutions(int boardSize) {
		MutableInt count = new MutableInt();
		int[] board = new int[boardSize];
		Arrays.fill(board, EMPTY);
		search(count, board, 0);
		return count.intValue();
	}
class: SequentialNQueens.java Java.png
methods: search
package: nqueens.warmup
source folder: student/src/main/java

method: private static void search(MutableInt count, int[] board, int row) Sequential.svg (sequential implementation only)

Parallel Studio

Board State: DefaultImmutableQueenLocations

class: DefaultQueenLocations.java Java.png
methods: createNext
getBoardSize
getColumnOfQueenInRow
getCandidateColumnsInRow
package: nqueens.lab
source folder: student/src/main/java
createNext(row,col)

method: public DefaultQueenLocations createNext(int row, int col) Sequential.svg (sequential implementation only)

There are two constructors for this class. A public one which creates a fresh new board state with no queens yet placed. and a private one which creates a new board with the state of a given board which is further constrained by a new queen in the next row. You need to create a new instance using one of these two constructors. Which one is it?

Consider this example program which creates a valid 4-queens solution:

		int boardSize = 4;
		QueenLocations board0 = new DefaultQueenLocations(boardSize);
		QueenLocations board1 = board0.createNext(0, 1);
		QueenLocations board2 = board1.createNext(1, 3);
		QueenLocations board3 = board2.createNext(2, 0);
		QueenLocations board4 = board3.createNext(3, 2);
		System.out.println(board4);


Which board is used to create the next board?

getBoardSize()

method: public int getBoardSize() Sequential.svg (sequential implementation only)

Note that we will refer to the standard 8x8 chessboard's size as 8 and not 64.

getColumnOfQueenInRow(row)

method: public Optional<Integer> getColumnOfQueenInRow(int row) Sequential.svg (sequential implementation only)

For an 8x8 board with queens placed in (row=0, col=1), (row=1, col=6), and (row=2, col=4)

Queens in rows 012.png

getCandidateColumnsInRow(row)

method: public List<Integer> getCandidateColumnsInRow(int row) Sequential.svg (sequential implementation only)

For an 8x8 board with a single queen placed in (row=0, col=4)

Queen r0 c4.png

  • getCandidateColumnsInRow(0) returns []
  • getCandidateColumnsInRow(1) returns [0,1,2,6,7]
  • getCandidateColumnsInRow(2) returns [0,1,3,5,7]
  • getCandidateColumnsInRow(3) returns [0,2,3,5,6]
  • getCandidateColumnsInRow(4) returns [1,2,3,5,6,7]
  • getCandidateColumnsInRow(5) returns [0,1,2,3,5,6,7]
  • getCandidateColumnsInRow(6) returns [0,1,2,3,5,6,7]
  • getCandidateColumnsInRow(7) returns [0,1,2,3,5,6,7]

The provided isLocationThreatFree(row, col) method should be helpful.

Search Order: FirstAvailableRowSearchOrder

This class will provide methods that will allow us to implement a clean and efficient parallel solution in the final step.

class: FirstAvailableRowSearchOrder.java Java.png
methods: selectedNextUnplacedRow
package: nqueens.lab
source folder: student/src/main/java

method: public Optional<Integer> selectedNextUnplacedRow(QueenLocations queenLocations) Sequential.svg (sequential implementation only)

For an 8x8 board with queens placed at (row=0, col=0), (row=1, col=3), (row=2, col=6), and (row=6, col=7):

Queen missing in row3.png

  • selectedNextUnplacedRow(queenLocations) returns Optional.of(3)

For a board with no unplaced rows, for example, a solution:

8queens solution0.png


Attention niels epting.svg Warning:Do NOT skip empty rows simply because they have no candidate columns

In cases where a row does not have a queen placed in it, but has no valid candidate columns, for example a 3x3 board with a queen placed at (row=0, col=1):

Queen 3x3 eliminates next row.png

It is critical that

  • selectedNextUnplacedRow(queenLocations) returns Optional.of(1)

When searching for solutions we do not want to avoid dead rows. If anything, we want to move them to the front of the line, so that search can cease the current fruitless path.

ParallelNQueens

Searching for solutions like n-queens can be done in parallel without the need to finish at each level. As such, forasync is preferable to forall. However:

Attention niels epting.svg Warning:Ensure that you complete all of your tasks by enclosing them a single finish.
class: ParallelNQueens.java Java.png
methods: searchForSolutions
countSolutions
package: nqueens.lab
source folder: student/src/main/java

method: public static int countSolutions(QueenLocations queenLocations, RowSearchOrder rowSearchOrder) Parallel.svg (parallel implementation required)

Attention niels epting.svg Warning:FinishAccumulators must be registered with their finish statement

Instead of using a MutableInt in order to count the number of solutions we have found, we want to use a Finish Accumulator.

Creating a new instance of FinishAccumulator is done via on of the many static methods on the V5 class (the same class we get async and finish from).

Refer to the syntax page in order to see the syntax for properly setting up the accumulator.

method: private static void searchForSolutions(FinishAccumulator<Integer> accumulator, QueenLocations queenLocations, RowSearchOrder rowSearchOrder) Parallel.svg (parallel implementation required)


Sudoku

Background

BasicSudoku.PNG

We will be using a similar algorithm to solve a Sudoku puzzle. For those not familiar, a Sudoku puzzle is composed of a 9-by-9 grid of squares. This grid is also divided into 9 large boxes, each of which is a 3-by-3 of the smaller squares. In a completed puzzle, each of the smaller squares contains a single number from 1 to 9 (inclusive). However, if a square contains a given number, that same number cannot be anywhere else in the same row, column, or box. Thus, for Sudoku, we are given an incomplete board and must fill in the remaining squares while meeting these requirements.

Sudoku is another problem well solved by backtracking. Check the understanding you gained of backtracking with N-Queens by challenging yourself to solve Sudoku's solver without assistance. The game of Sudoku is bit more complex though than N-Queens, and there are more strategies we can do than just backtracking in order to speed up our solution. To make this assignment more compelling, you will implement alternate search orderings and constraint propagation.

Read Peter Norvig's Essay before you begin coding. It will cover everything related to the Sudoku problem itself and how one can design a solution for it.

Roadmap to Victory

  1. PeerEliminationOnlySudokuPuzzle
  2. RowMajorSearchOrder
  3. FewestOptionsFirstSearchOrder
  4. ParallelSudoku
  5. (Optional Challenge) Add Unit and Twins Constraint Propagation to DefaultConstraintPropagator

The Core Questions

  • What are the tasks?
  • What is the data?
  • Is the data mutable?
  • If so, how is it shared?

Code To Investigate

Square

enum Square

Collection<Square> getPeers()
valueOf(row, column)
all enums have a values() method

SudokuUtils

class SudokuUtils

deepCopyOf(Map<Square, SortedSet<Integer>> other)
allUnits()
getRowUnit(row)
getColumnUnit(col)
getColumnUnit(row,col)
getUnitsForSquare(square)

CandidateSet

class CandidateSet<E> implements SortedSet<E>

public static CandidateSet createAllCandidates()

Code To Implement

PeerEliminationOnlySudokuPuzzle

As the name suggests, DefaultImmutableSudokuPuzzle is immutable, and you will need to create a new instance of the object whenever you move on from one square to the next. This is analogous to the work you did for #NQueens.

class: PeerEliminationOnlySudokuPuzzle.java Java.png
methods: constructors
createNext
getValue
getOptions
package: sudoku.lab
source folder: student/src/main/java

constructors

The constructors for PeerEliminationOnlySudokuPuzzle have been provided:

DefaultImmutableSudokuPuzzle(givens)

method: public PeerEliminationOnlySudokuPuzzle(String givens) Sequential.svg (sequential implementation only)

This constructor creates a puzzle constrained to an initial set of givens. You can think of the givens as the original values provided by the newspaper or airline magazine or puzzle book or whatever.

PeerEliminationOnlySudokuPuzzle(other,square,value)

method: private PeerEliminationOnlySudokuPuzzle(PeerEliminationOnlySudokuPuzzle other, Square square, int value) Sequential.svg (sequential implementation only)

This constructor takes a given previous puzzle and a square value to create a new further constrained puzzle. This will be invoked via a public method on PeerEliminationOnlySudokuPuzzle during the search process.

createNext(square,value)

method: public ImmutableSudokuPuzzle createNext(Square square, int value) Sequential.svg (sequential implementation only)

This method should create a new puzzle instance using one of the constructors. Which one is it?

getValue(square)

method: public Optional<Integer> getValue(Square square) Sequential.svg (sequential implementation only)

Attention niels epting.svg Warning:Ignore any documentation which reports this method should return 0 if it is unfilled.

Based on the state of the board, return the value of a given square if it is known. Otherwise, return empty.

How do we determine if a value for a given square is "known"?

getCandidates(square)

method: public SortedSet<Integer> getCandidates(Square square) Sequential.svg (sequential implementation only)

Based on the state of the board, return the candidate values for a given square.

Search Order

Simply by changing the search order, a great reduction of work can be achieved.

To implement them, ask yourself:

  • How do I determine if a square is filled?
  • How do I find the unfilled square with the minimum number of candidates?

When you have completed them, ask yourself:

  • Which algorithm will perform better and why?
  • What properties make a square "filled"?
Attention niels epting.svg Warning:Do NOT omit squares with 0 candidates.

RowMajorSearchOrder

class: RowMajorSearchOrder.java Java.png
methods: selectNextUnfilledSquare
package: sudoku.lab
source folder: student/src/main/java

method: Optional<Square> selectNextUnfilledSquare(ImmutableSudokuPuzzle puzzle) Sequential.svg (sequential implementation only)

Attention niels epting.svg Warning:
Ignore any documentation which reports this method should return null if it is completely filled.
This method should return Optional.empty() for a completely filled board.

Simply run through the Square.values() which will iterate through squares going down the row (A1, A2, A3, ...). Make sure not to return squares that have already been filled.

FewestOptionsFirstSearchOrder

class: FewestOptionsFirstSearchOrder.java Java.png
methods: selectNextUnfilledSquare
package: sudoku.lab
source folder: student/src/main/java

method: Optional<Square> selectNextUnfilledSquare(ImmutableSudokuPuzzle puzzle) Sequential.svg (sequential implementation only)

Attention niels epting.svg Warning:
Ignore any documentation which reports this method should return null if it is completely filled.
This method should return Optional.empty() for a completely filled board.

Go through every square by calling Square.values(), find a square that is (1) not already filled, and (2) has the minimal number of possible options among all the squares, and return that square.

Solver

This part of the assignment is also similar to its n-queens counterpart. Searching for solutions like sudoku can be done in parallel without the need to finish at each level. As such, forasync is preferable to forall and is, in fact, required by the test.

Attention niels epting.svg Warning:Ensure that you complete all of your tasks by enclosing them all in a single finish.
Circle-information.svg Tip:Use the iterable version of forasync
class: ParallelSudoku.java Java.png
methods: solve
solveKernel
package: sudoku.lab
source folder: student/src/main/java

method: public static ImmutableSudokuPuzzle solve(ImmutableSudokuPuzzle puzzle, SquareSearchAlgorithm squareSearchAlgorithm) Parallel.svg (parallel implementation required)

method: private static void solveKernel(MutableObject<ImmutableSudokuPuzzle> solution, ImmutableSudokuPuzzle puzzle, SquareSearchAlgorithm squareSearchAlgorithm) Parallel.svg (parallel implementation required)

Testing Your Solution

Visualization

N-Queens

class: NQueensVizApp.java VIZ
package: nqueens.viz.solution
source folder: student/src//java

NQueensViz.png

Sudoku

Propogate

class: SudokuApp.java VIZ
package: sudoku.viz.solution
source folder: student/src//java

SudokuPropagateViz.png

Solve

class: FxSudokuSolutionApp.java VIZ
package: sudoku.viz.solution
source folder: student/src//java

SudokuSolutionViz.png

Correctness

Warm Up

class: SequentialNQueensWarmUpTestSuite.java Junit.png
package: nqueens.warmup
source folder: testing/src/test/java

Lab

There is a top-level test suite comprised of sub test suites which can be invoked separately when you want to focus on one part of the assignment.

class: BacktrackTestSuite.java Junit.png
package: backtrack.lab
source folder: testing/src/test/java

NQueens

class: NQueensTestSuite.java Junit.png
package: nqueens.lab
source folder: testing/src/test/java
ImmutableQueenLocations
class: ImmutableQueenLocationsTestSuite.java Junit.png
package: nqueens.lab
source folder: testing/src/test/java
FirstAvailableRowSearchAlgorithm
class: FirstAvailableRowSearchTestSuite.java Junit.png
package: nqueens.lab
source folder: testing/src/test/java
ParallelNQueens
class: ParallelNQueensSolutionCountTestSuite.java Junit.png
package: nqueens.lab
source folder: testing/src/test/java

Sudoku

class: SudokuTestSuite.java Junit.png
package: sudoku.lab
source folder: testing/src/test/java
DefaultConstraintPropagator
class: DefaultConstraintPropagatorTestSuite.java Junit.png
package: sudoku.lab
source folder: testing/src/test/java
DefaultImmutableSudokuPuzzle
class: DefaultImmutableSudokuPuzzleTestSuite.java Junit.png
package: sudoku.lab
source folder: testing/src/test/java
RowMajorSearchOrder
class: RowMajorSearchOrderTestSuite.java Junit.png
package: sudoku.lab
source folder: testing/src/test/java
FewestOptionsFirstOrder
class: FewestOptionsFirstOrderTestSuite.java Junit.png
package: sudoku.lab
source folder: testing/src/test/java
ParallelSudokuSolve
class: ParallelSudokuSolveTestSuite.java Junit.png
package: sudoku.lab
source folder: testing/src/test/java
Holistic
class: HolisticTestSuite.java Junit.png
package: sudoku.lab
source folder: testing/src/test/java

Extra Credit Challenge Unit Constraint Propagation

class: ChallengeSudokuTestSuite.java Junit.png
package: sudoku.challenge
source folder: testing/src/test/java

Rubric

As always, please make sure to cite your work appropriately.

Total points: 100

N-Queens subtotal: 35

  • Correct DefaultImmutableQueenLocations (10)
  • Correct FirstAvailableRowSearchAlgorithm (5)
  • Correct ParallelNQueens (10)
  • Parallel ParallelNQueens (10)

Sudoku subtotal: 65

  • Correct ImmutableSudokuPuzzle (5)
  • Correct ContraintPropagator (20)
  • Correct RowMajorSearch (10)
  • Correct FewestOptionsFirstSearch (10)
  • Correct ParallelSudoku (10)
  • Parallel ParallelSudoku (10)

Penalties may be assessed for clarity and efficiency.