In this lab exercise, you’ll work with the AIMA Python implementations of Constraint Satisfaction Problems (CSPs). You start by applying specific algorithms to two specific problems and then reflect on the nature of CSPs and the algorithms used to solve them.
The implementation for sudoku provided in this lab includes calls to the following four algorithms:
The code also provides the following predefined problem specifications:
4 8 3 | 9 2 1 | 6 5 7 9 6 7 | 3 4 5 | 8 2 1 2 5 1 | 8 7 6 | 4 9 3 ------+-------+------ 5 4 8 | 1 3 2 | 9 7 6 7 2 9 | 5 6 4 | 1 3 8 1 3 6 | 7 9 8 | 2 4 5 ------+-------+------ 3 7 2 | 6 8 9 | 5 1 4 8 1 4 | 2 5 3 | 7 6 9 6 9 5 | 4 1 7 | 3 8 2 |
. . 3 | . 2 . | 6 . . 9 . . | 3 . 5 | . . 1 . . 1 | 8 . 6 | 4 . . ------+-------+------ . . 8 | 1 . 2 | 9 . . 7 . . | . . . | . . 8 . . 6 | 7 . 8 | 2 . . ------+-------+------ . . 2 | 6 . 9 | 5 . . 8 . . | 2 . 3 | . . 9 . . 5 | . 1 . | 3 . . |
4 1 7 | 3 6 9 | 8 . 5 . 3 . | . . . | . . . . . . | 7 . . | . . . ------+-------+------ . 2 . | . . . | . 6 . . . . | . 8 . | 4 . . . . . | . 1 . | . . . ------+-------+------ . . . | 6 . 3 | . 7 . 5 . . | 2 . . | . . . 1 . 4 | . . . | . . . |
1 . . | . . 7 | . 9 . . 3 . | . 2 . | . . 8 . . 9 | 6 . . | 5 . . ------+-------+------ . . 5 | 3 . . | 9 . . . 1 . | . 8 . | . . 2 6 . . | . . 4 | . . . ------+-------+------ 3 . . | . . . | . 1 . . 4 . | . . . | . . 7 . . 7 | . . . | 3 . . |
solved - This is a
solved puzzle taken from the text (see Figure 6.4.b). |
easy - This is an
unsolved but relatively easy puzzle, also taken from the text (see
Figure 6.4.a). |
harder - This is a
harder puzzle taken from csp.py . |
hardest - This the
hardest 9x9 puzzle according to this 2006 source![]() |
Download sudoku.py
and try
running each of the algorithms on each of the predefined sudoku
puzzles. Consider the following questions:
Which algorithms work (in a timely manner) and which don’t? Explain your results in terms of the capabilities (and implementations) of the algorithms and nature of the problems.
What effect does configuring the settings for backtracking have on the results? Try the following:
select_unassigned_variable
parameter) to first-unassigned-variable (the default) or
minimum-remaining-values (i.e., mrv
).
inference
parameter) to forward-checking (i.e., forward_checking
).
Which, if any, of these settings should work best for sudoku? What combination of settings actually works the best?
The implementation for n-queens provided in this lab includes calls to the same four algorithms given above and it allows you to specify n.
Download queens.py
and try
running each of the algorithms with various values for n. Consider
the following questions:
Take some time now to reflect on the nature of constraint-based problems and algorithms. Constraint satisfaction works by taking advantage of constraints stated in terms of parts of the state representation. As you’ve seen, traditional strategies can be applied as well as local search.
Review the AIMA Python implementation for constraint satisfaction problems (CSP) as needed to do the following:
csp.CSP
)
and traditional problems (i.e., search.Problem
). Be
sure to consider the nature of states, domains, actions, results and
goal tests.
Submit the files specified above in Moodle under lab 4.