automate sudoku boards

This commit is contained in:
2025-09-24 15:35:52 +02:00
parent 018a56f172
commit 4451ef60e1
4 changed files with 89 additions and 82 deletions

View File

@@ -4,27 +4,18 @@ author: Erlend Ulvund Skaarberg and Fredrik Robertsen
date: 2025-09-24
---
## 2a) Solutions
## 2a,c,d,e) Sudoku boards and backtrack statistics
Here are the solutions for the provided Sudoku puzzles:
These are the results of running our CSP solver on all four sudoku boards.
It seems we get lucky on the hard board.
Additionally we calculate the time it takes for running backtracking search and
AC-3 on each sudoku board.
```
Solution for sudoku_easy.txt
8 7 5 | 9 3 6 | 1 4 2
1 6 9 | 7 2 4 | 3 8 5
2 4 3 | 8 5 1 | 6 7 9
------+-------+------
4 5 2 | 6 9 7 | 8 3 1
9 8 6 | 4 1 3 | 2 5 7
7 3 1 | 5 8 2 | 9 6 4
------+-------+------
5 1 7 | 3 6 9 | 4 2 8
6 2 8 | 1 4 5 | 7 9 3
3 9 4 | 2 7 8 | 5 1 6
$ for f in sudoku_*; do python3 sudoku.py $f; done
==================================================
Solution for sudoku_medium.txt
True
7 8 4 | 9 3 2 | 1 5 6
6 1 9 | 4 8 5 | 3 2 7
2 3 5 | 1 7 6 | 4 8 9
@@ -36,10 +27,10 @@ Solution for sudoku_medium.txt
4 5 3 | 7 2 9 | 6 1 8
8 6 2 | 3 1 4 | 7 9 5
1 9 7 | 6 5 8 | 2 4 3
==================================================
Solution for sudoku_hard.txt
Backtrack failed 357 times out of 439 calls.
Running both AC-3 and backtracking search took 0.05711
Running only backtracking search took 0.04202
True
1 5 2 | 3 4 6 | 8 9 7
4 3 7 | 1 8 9 | 6 5 2
6 8 9 | 5 7 2 | 3 1 4
@@ -51,10 +42,25 @@ Solution for sudoku_hard.txt
7 9 8 | 2 5 3 | 4 6 1
3 6 5 | 9 1 4 | 2 7 8
2 1 4 | 7 6 8 | 5 3 9
==================================================
Solution for sudoku_very_hard.txt
Backtrack failed 147 times out of 229 calls.
Running both AC-3 and backtracking search took 0.04713
Running only backtracking search took 0.03006
True
8 7 5 | 9 3 6 | 1 4 2
1 6 9 | 7 2 4 | 3 8 5
2 4 3 | 8 5 1 | 6 7 9
------+-------+------
4 5 2 | 6 9 7 | 8 3 1
9 8 6 | 4 1 3 | 2 5 7
7 3 1 | 5 8 2 | 9 6 4
------+-------+------
5 1 7 | 3 6 9 | 4 2 8
6 2 8 | 1 4 5 | 7 9 3
3 9 4 | 2 7 8 | 5 1 6
Backtrack failed 1225 times out of 1307 calls.
Running both AC-3 and backtracking search took 0.13033
Running only backtracking search took 0.11624
True
4 3 1 | 8 6 7 | 9 2 5
6 5 2 | 4 9 1 | 3 8 7
8 9 7 | 5 3 2 | 1 6 4
@@ -66,6 +72,9 @@ Solution for sudoku_very_hard.txt
9 4 3 | 7 2 8 | 6 5 1
7 6 5 | 1 4 3 | 2 9 8
1 2 8 | 6 5 9 | 4 7 3
Backtrack failed 16016 times out of 16098 calls.
Running both AC-3 and backtracking search took 2.12073
Running only backtracking search took 2.10415
```
## 2b) domains
@@ -271,50 +280,16 @@ AC-3 reduced the number of domain values from 473 to 294:
X96: {1, 2, 3, 4, 5, 6, 7, 8, 9} -> {1, 3, 4, 5, 6, 7, 8, 9}
```
## 2c) number of times backtrack() was called and failed
How many times backtrack() was called and how many times it failed for each of the four Sudoku puzzles.
```
Backtracking search on sudoku_easy.txt:
Backtracking search called 439 times with 357 failures.
Backtracking search on sudoku_medium.txt:
Backtracking search called 439 times with 357 failures.
Backtracking search on sudoku_hard.txt:
Backtracking search called 439 times with 357 failures.
Backtracking search on sudoku_very_hard.txt:
Backtracking search called 439 times with 357 failures.
```
## 2d,e) runtime for backtracking search and AC-3
Time taken to run AC-3, backtracking search, and the total time for each of the four Sudoku puzzles.
```
Timing on sudoku_easy.txt:
AC-3 took 0.022991 seconds.
Backtracking search took 0.041559 seconds.
Total time: 0.064553 seconds.
Timing on sudoku_medium.txt:
AC-3 took 0.023183 seconds.
Backtracking search took 0.056572 seconds.
Total time: 0.079757 seconds.
Timing on sudoku_hard.txt:
AC-3 took 0.033311 seconds.
Backtracking search took 0.053730 seconds.
Total time: 0.087044 seconds.
Timing on sudoku_very_hard.txt:
AC-3 took 0.027815 seconds.
Backtracking search took 0.139221 seconds.
Total time: 0.167038 seconds.
```
## 2f) why does AC-3 drastically reduce the runtime for backtracking search?
AC-3 drastically reduces the runtime for backtracking search because it prunes the search space by eliminating inconsistent values from the domains of the variables before the backtracking search begins. By enforcing arc consistency, AC-3 ensures that many impossible assignments are removed early on, which means that the backtracking algorithm has fewer options to consider when trying to assign values to variables. This reduction in the number of potential assignments leads to fewer recursive calls and backtracks during the search process, which speeds up the backtracking search significantly. We've seen this in practice by printing the number of failures during backtracking search without AC-3. Using AC-3, we reach a few hundred failures, while without AC-3, we reach hundreds of thousands of failures within a few minutes of runtime.
AC-3 drastically reduces the runtime for backtracking search because it prunes
the search space by eliminating inconsistent values from the domains of the
variables before the backtracking search begins. By enforcing arc consistency,
AC-3 ensures that many impossible assignments are removed early on, which means
that the backtracking algorithm has fewer options to consider when trying to
assign values to variables. This reduction in the number of potential
assignments leads to fewer recursive calls and backtracks during the search
process, which speeds up the backtracking search significantly. We've seen this
in practice by printing the number of failures during backtracking search
without AC-3. Using AC-3, we reach a few hundred failures, while without AC-3,
we reach hundreds of thousands of failures within a few minutes of runtime.