Sudoku solver
Moderator:Jouni (see all) |
This page is a stub. You may improve it into a full page. |
Upload data
|
Question
How to describe a sudoku and the sudoku rules in Opasnet so that it can be solved automatically?
Answer
Procedure
The following terms are used:
- The possible space of solutions is described by a logical vector A, which contains all possible values given the current information. A is indexed by h, i, j, k, and l. A develops in time, when more information occurs or is processed. In the beginning, all hypotheses are considered as potentially TRUE.
- h = {1, 2, ..., 9}, i = {1, 2, ..., 9}, j = {1, 2, ..., 9}, k = {1, 2, ..., 9}, l = {1, 2, ..., 81} are indices for hypothesis, row, column, area, and cell, respectively. Note l is just another way to say (i,j), as l = i + (j-1)*9. Also, k is known if (i,j) is known, as k = ceiling(i/3) + (ceiling(j/3)-1)*3. However, k does not contain all information that (i,j) or l contains.
- ah and bh are sub-vectors of A in such a way that ah = Ah,m and bh = Ah,n, where m and n (m < n) are values from index l.
- Expand the missing index values of the Hypotheses table to create the full A.
- Take the sudoku data table and replace hypotheses with data, if available.
- Compare all two-cell pairs at once using matrices.
- Create matrices of all critical properties: SudRow, SudCol, SudArea, and Hypothesis. These are compared pairwise, two cells at a time.
- Use rules to deduce if a pair is incompatible or not.
- Aggregate plausible hypotheses in the second cell across all plausible values in the first cell. This gives a set of hypotheses that are plausible in at least some conditions.
- Aggregate along the first cell: each second cell must be compatible with all other (first) cells.
- Do not apply these rules if the first and second cells are the same.
- Go through every row, column, and area and find hypotheses where there is exactly one cell where it is plausible.
- Remove all other hypotheses from these cells.
- Do steps 3 and 4 until the sudoku does not improve (i.e., further hypotheses are not falsified).
- Take a user-defined list of cells for which a random sample from plausible hypotheses is taken. It would be elegant to do this stepwise, but for simplicity, let's do it at once. Therefore, the cells that the user selects is critical, and a wise user will not select cells where uncertainties are clearly interdependent.
- Solve all sudokus that are created in the sampling.
- If a sudoku results in cells with zero plausible hypotheses, remove that iteration.
- Calculate the number of different solutions still plausible and print it.
- If the number is smaller than 100, print also the solutions.
Rationale
←--#: . Development needs to improve the solver:
- Change sudoku input string in a way that it can have also other possibilities than a single number or any number. For example, syntax [3578] would mean that any of those four numbers could be in a particular position. This makes the interpretation of the sudoku string a bit problematic, because then its length is no longer fixed to 81. However, this way it is possible to restrict possibilities iterativerly.
- When guessing, a new rule should be used: if a particular hypothesis is falsified in ALL scenarios, it can be falsified always. This rule can be extended in a way that when no unique solution is found, the solver would make guesses automatically and gradually falsify all wrong hypotheses. --Jouni (talk) 09:36, 31 January 2016 (UTC) (type: truth; paradigms: science: defence)
Data
This table will be expanded by fillna to be a 9*9*9 array (formatted as data.frame). As default, each hypothesis is assumed to be true unless shown otherwise.
- List of all possible hypotheses, which are a priori assumed to be true.
Obs | SudRow | SudCol | Hypothesis | Result |
---|---|---|---|---|
1 | 1 | TRUE | ||
2 | 2 | TRUE | ||
3 | 3 | TRUE | ||
4 | 4 | TRUE | ||
5 | 5 | TRUE | ||
6 | 6 | TRUE | ||
7 | 7 | TRUE | ||
8 | 8 | TRUE | ||
9 | 9 | TRUE | ||
10 | 1 | TRUE | ||
11 | 2 | TRUE | ||
12 | 3 | TRUE | ||
13 | 4 | TRUE | ||
14 | 5 | TRUE | ||
15 | 6 | TRUE | ||
16 | 7 | TRUE | ||
17 | 8 | TRUE | ||
18 | 9 | TRUE | ||
19 | 1 | TRUE | ||
20 | 2 | TRUE | ||
21 | 3 | TRUE | ||
22 | 4 | TRUE | ||
23 | 5 | TRUE | ||
24 | 6 | TRUE | ||
25 | 7 | TRUE | ||
26 | 8 | TRUE | ||
27 | 9 | TRUE |
- Rules of inference
- the table is actually for illustration only because the code is too complex to implement from a table entry. Rules 1-4 come directly from the rules of the sudoku game. All other rules are logically derived from them.
Obs | Rule name | Rule | Description |
---|---|---|---|
1 | rule1 | If a hypothesis is TRUE in cell A, that hypothesis must be FALSE in cell B, if B is on the same row as A. | |
2 | rule2 | If a hypothesis is TRUE in cell A, that hypothesis must be FALSE in cell B, if B is on the same column as A. | |
3 | rule3 | If a hypothesis is TRUE in cell A, that hypothesis must be FALSE in cell B, if B is on the same area as A. | |
4 | rule4 | If cells A and B are actually the same cell, no inferences are made. | This is a stronger rule than others. |
5 | rule5 | If a hypothesis in cell B is TRUE at least once given all hypotheses in cell A, the hypothesis in B is considered TRUE. | This rule loses a lot of information about dependencies between cells, but it saves huge amounts of memory. In any case, even then, the set of rules effectively narrow down the potential hypothesis space. |
6 | rule6 | Rules 1-5 apply to all pairs of cells A and B. | |
7 | rule7 | If a hypothesis in cell B is FALSE after applying rule 5 for even one cell A, it is FALSE always. | |
8 | rule8 | If a hypothesis is TRUE in exactly one cell in a particular row, all other hypothesis in that cell are FALSE. | |
9 | rule9 | If a hypothesis is TRUE in exactly one cell in a particular column, all other hypothesis in that cell are FALSE. | |
10 | rule10 | If a hypothesis is TRUE in exactly one cell in a particular area, all other hypothesis in that cell are FALSE. | |
11 | rule11 | If two cells on the same row contain exactly two TRUE hypotheses that are the same in both cells, these hypotheses cannot be TRUE in any other cell on that row. | This rule is analogous to rule1 but with two cells. However, this rule (and the respective rules for columns and areas) are not implemented in the code. |
- Sudoku data (this is "the most difficult sudoku in thr world").
Obs | SudRow | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|---|
1 | 1 | 8 | ||||||||
2 | 2 | 3 | 6 | |||||||
3 | 3 | 7 | 9 | 2 | ||||||
4 | 4 | 5 | 7 | |||||||
5 | 5 | 4 | 5 | 7 | ||||||
6 | 6 | 1 | 3 | |||||||
7 | 7 | 1 | 6 | 8 | ||||||
8 | 8 | 8 | 5 | 1 | ||||||
9 | 9 | 9 | 4 |
Formula
See also
- file:Sudoku solver.ANA (a draft solver using Analytica)
Keywords
References
Related files
<mfanonymousfilelist></mfanonymousfilelist>