Sudoku solver: Difference between revisions

From Opasnet
Jump to navigation Jump to search
Line 206: Line 206:
out$dataResult <- as.character(out$dataResult)
out$dataResult <- as.character(out$dataResult)
out$Result <- ifelse(out$dataResult == "", out$Result, (as.character(out$dataResult) == as.character(out$Hypothesis)))
out$Result <- ifelse(out$dataResult == "", out$Result, (as.character(out$dataResult) == as.character(out$Hypothesis)))
out[1:19, ]
check <- out
 
temp <- out["Result"]
levels(out[[1]])
for(m in 1:nrow(out)) {
levels(out[[2]])
a <- out[m , ]
levels(out[[3]])
temp[[m]] <- ifelse(a$SudCell != out$SudCell & a$SudRow == out$SudRow & a$Hypothesis == out$Hypothesis, FALSE, out$Result)
levels(out[[4]])
temp[[m]] <- ifelse(a$SudCell != out$SudCell & a$SudCol == out$SudCol & a$Hypothesis == out$Hypothesis, FALSE, out$Result)
levels(out[[5]])
temp[[m]] <- ifelse(a$SudCell != out$SudCell & a$SudArea == out$SudArea & a$Hypothesis == out$Hypothesis, FALSE, out$Result)
levels(out[[6]])
}
levels(out[[7]])


for(m in 1:(nrow(out) - 1)) {
temp <- t(temp)
for(n in (m + 1):nrow(out)) {
temp2 <- data.frame()
a <- out[levels(out$SudCell) == m , ]
temp
b <- out[levels(out$SudCell) == n , ]
out$SudCell
btemp <- list(b)
for(m in 1:nrow(out)) {temp2[m] <- tapply(temp[[m]], out$SudCell, any)}
for(h in levels(out$Hypothesis)) {
for(m in 1:nrow(out)) {temp2[m] <- all(temp2[[m]])}
atemp <- a[levels(out$Hypothesis) == h , ]
temp2 <- as.vector(t(temp2))
if(atemp$SudRow == b$SudRow[1]) b$Result <- ifelse(atemp$Hypothesis == b$Hypothesis, FALSE, b$Result)
temp2
if(atemp$SudCol == b$SudCol[1]) b$Result <- ifelse(atemp$Hypothesis == b$Hypothesis, FALSE, b$Result)
check$Result
if(atemp$SudRow == b$SudRow[1]) b$Result <- ifelse(atemp$Hypothesis == b$Hypothesis, FALSE, b$Result)
}
}
}


#    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.  
#    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.  
Line 252: Line 247:


</rcode>
</rcode>
{{attack|# |Not all the loops are needed. One row can be compared to all rows simultaneously, and then plausible hypotheses can be searched for using tapply.|--[[User:Jouni|Jouni]] 23:57, 2 May 2013 (EEST)}}


==See also==
==See also==

Revision as of 20:57, 2 May 2013



Question

How to describe a sudoku and the sudoku rules in Opasnet so that it can be solved automatically?

Answer

You need the following tables.

Hypotheses
Row Column Result Description
All All 1,2,3,4,5,6,7,8,9 For all row and column locations it applies that the plausible hypotheses are a single integer between 1 and 9 (unless more information is available).


Area descriptions
Row Column Area
1 1 A
1 2 A
1 3 A
1 4 B
2 1 A
4 1 D
9 9 I


Rules of exclusion when comparing two cells.
Property1 Condition1 Property2 Condition2 Rule Description
Row Same Column Different Same integer not allowed Two cells with the same row and different column are not allowed to have the same integer.
Row Different Column Same Same integer not allowed Two cells with the different row and same column are not allowed to have the same integer.
Area Same Column Different Same integer not allowed Two cells with the same area and different column are not allowed to have the same integer.
Area Same Row Different Same integer not allowed Two cells with the same area and different row are not allowed to have the same integer.
The sudoku data (this example is "the most difficult sudoku in the world")
Row Column
1 2 3 4 5 6 7 8 9
1 8
2 3 6
3 7 9 2
4 5 7
5 4 5 7
6 1 3
7 1 6 8
8 8 5 1
9 9 4

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.


  1. Expand the missing index values of the Hypotheses table to create the full A.
  2. Take the sudoku data table and replace hypotheses with data, if available.
  3. Compare two cells a and b in the sudoku. Make a for-loop for the first cell a: for(m in 1:nrow(l))).
    1. Make another for-loop for the second cell b: for(n in (m+1):nrow(l)).
      1. Make a third for-loop for the hypotheses of a: for(h in 1:nrow(a))
        1. Make a fourth for-loop for the rules: for(r in 1:nrow(rules)).
          1. Test for the rule with the pair of cells, creating a set of plausible hypothesis for the other cell b conditional on the first cell a.
          2. If the set of plausible hypotheses for b is empty, the condition is implausible; remove the condition and thus that hypothesis from the first cell a.
          3. Take the union of plausible hypothesis for b (which then covers all plausible hypotheses unconditionally) and replace the current hypotheses of b with the new hypotheses.
      2. Do the third for-loop again but now with the other cell b.
  4. Do the second and first loops for all values of m and n.
  5. Do another set of loops for each row, column, and area to test whether there is exactly one cell where each hypothesis is plausible. (Remember, that in sudoku, unlike in most parts of the world, we know that each hypothesis is correct at exactly one cell). If a unique cell is found, remove all other hypotheses from that cell.
    1. Make a loop for each row and then hypothesis.
    2. Make a loop for each column and then hypothesis.
    3. Make a loop for each area and then hypothesis.
  6. If a unique solution was not found and if the current set of hypotheses is not the same as the previous set, save the current set as "previous set" and go to number 2.
  7. Calculate the number of different solutions still plausible and print it.
  8. If the number is smaller than 100, print also the solutions.

Rationale

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.

Hypotheses(Boolean)
ObsSudRowSudColHypothesisResult
11TRUE
22TRUE
33TRUE
44TRUE
55TRUE
66TRUE
77TRUE
88TRUE
99TRUE
101TRUE
112TRUE
123TRUE
134TRUE
145TRUE
156TRUE
167TRUE
178TRUE
189TRUE
191TRUE
202TRUE
213TRUE
224TRUE
235TRUE
246TRUE
257TRUE
268TRUE
279TRUE
Sudoku data
Sudoku(-)
ObsSudRow123456789
118
2236
33792
4457
55457
6613
77168
88851
9994

Formula

+ Show code

⇤--#: . Not all the loops are needed. One row can be compared to all rows simultaneously, and then plausible hypotheses can be searched for using tapply. --Jouni 23:57, 2 May 2013 (EEST) (type: truth; paradigms: science: attack)

See also

Keywords

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>