Homework Three

Constraint Problem Solving

out Wednesday 3/01, due Friday 3/17

(1) Heuristics for CSP (25)

We covered several heuristics for CSP search. Explain why when selecting a variable to assign a value next, it is a good to choose the variable that is most constrained, but the value that is least constraining.

(2) Magic Squares (50)

In class we showed examples of using the python constraint package to generate magic squares of size three. In general, there is always a magic square of size N whose magic sum is equal to N*(N**2+1)/2 for n>2. (Others may also exist.)

(a) (20) Complete a file ms.py that defines a subclass named MS of the constraints Problem class to find a magic square of a given size with a given solver. You can test this directly like this:

  myhw3> python
  >>> import ms
  >>> ms.MS(3).getSolution()
  {0: 6, 1: 7, 2: 2, 3: 1, 4: 5, 5: 9, 6: 8, 7: 3, 8: 4}

(b ) (20) Note that the MinConflicts() solver fails to solve even the smallest problem. Think about how this strategy works and explain why MinConflicts() does not work well for this problem.

(c) (10) Can you get a solution for a magic square of size six using any of the solvers and if so, how long does it take? Do you think that using CSP is a good approach for generating magic squares? Why or why not.


(3) N Queens (25)

Complete another file nq.py that defines a subclass named NQ of the constraints Problem class for the n-queens problem for a n-by-n board with a given solver. You can test this directly like this:
  >>> import nq
  >>> nq.NQ(8)
  {0: 7, 1: 3, 2: 0, 3: 2, 4: 5, 5: 1, 6: 6, 7: 4}
You can find starter files for ms.py and nq.py and a test file test.py in your starter github repository (you can find a link to create this in our Piazza forum). Note that this directory includes the python-constraints package, so you need not install it if you use this directory. Of course, if you have already installed it that's fine also. You can run use the test.py file to see the results of solving both problems for a variety of sizes. See test.txt for an example of the output. Your output need not match this exactly.

You will probably discover that some of the solvers takes an inordinately long time to find a solution for a magic square of size five or larger. The test.py script takes an optional argument that specifies a timeout value. If an attempt to solve the problem exceeds this value, the solution is abandoned.

(4) Solitaire Battleship (extra credit: 25)

Use the python constraint package to develop a program that can solve instances of the Battleship Puzzle for a 6x6 grid with three submarines, two destroyers and one cruiser given the row and column sums and zero or more hints. Start by reading the paper by which describes on approach to solving the problem. You can adapt this approach to exploit the capabilities of the python constraint package, or develop your own.

Note that the python constraint package is much more flexible in the kinds of constraints you can define. For example, you can add to problem instance p the constraint over three Boolean variables ('A', 'B', and 'C') that A is True iff B and C are both true as follows:

p.addConstraint(lambda x,y,z: (x and y and z)
                            or (not x and not y)
                            or (not x and not z),
              ['A', 'B', 'C']) 
The constraint that 'A' must equal the sum of 'B' and 'C' is also easy:
p.addConstraint(lambda x,y,z: x == y + z, ['A', 'B', 'C'])
You can also define a number of constraints on a set of variables, including that they sum to a particular value or to at most a value or that a subset of them (all, some, exactly n) have a value in or not in a certain set, etc. Read the minimal documentation and examine the code for more information.

Demonstrate your program solving the available 6x6 Battleship puzzles found on the Conceptis Puzzles site. You are allowed to collaborate on approaches to solving this problem, but please identify all of your collaborators for this part of the homework. Collaboration groups should not have more than four people.

Background reading

What and how to submit

Commit and push your repository by the end of the day on Monday, March 13 with updated versions of four files:

  • hw3.pdf with answers to the questions for problems 1 and 2 . If you do problem four, also include a brief description of your battleships approach, any collaborators and examples you tested it on.
  • ms.py -- your code for problem one
  • nq.py -- your code for problem two
  • bs.py -- your code for problem three