-draft- Homework Two -draft-

CSP Search

out 9/28, due 10/9

(1) Heuristics for CSP

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) Binary constraints

Binary constraints play an important role in constraint reasoning. We asserted that constraints involving more than two variables can always be rewritten as constraints involving only two. Some CSP systems software systems, in fact, can only constraints between a pair of variables.

Show how a single ternary constraint such as "A + B = C" can be turned into three binary constraints by using an auxiliary variable. You may assume finite domains. (Hint: Consider a new variable that takes on values that are pairs of other values, and consider constraints such as ?X is the first element of the pair Y .?) Next, show how constraints with more than three variables can be treated similarly. Finally, show how unary constraints can be eliminated by altering the domains of variables. This completes the demonstration that any CSP can be transformed into a CSP with only binary constraints.

(3) Magic Squares

In class we showed examples of using the python constraint package to generate magic squares of size three and four. 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) Write a function magic_square that takes a single integer argument and generates a magic square of that size. Use this function to measure how long it takes to generate magic squares of sizes 3, 4, and 5 using each of the three solvers that the constraint package supports: BacktrackingSolver, RecursiveBacktrackingSolver, and MinConflictsSolver. Show a solution for each size and print a table of the running times for each solver. Note: Use Python's time module to capture run times. For example:

import time
...
start = time.time()
foo(x, y, z)
stop =  time.time()
print "Calling foo took %.3f seconds." % (stop-start,)
...

You will probably discover that the RecursiveBacktracking() solver takes an inordinately long time to find a solution for a magic square of size 5. If so, feel free to skip that test. An elegant solution is to create a Problem subclass that includes a timeout property such that if getSolution() exceeds its allotted time to run it will stop and return None. You can use Python's signal library to do this. We'll give extra credit for those who do this.

(b) Note that the MinConflicts() solver fails to solve even the smallest problem. You can examine to code for this solver to see why it fails to find a solution. Explain why MinConflicts() does not work well for this problem.

(c) 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.

(4) Extra Credit Problem: Solitaire Battleship

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 this problem, but please identify all of your collaborators for this part of the homework.

What to hand in

Turn in your assignment by the end of the day on Friday, October 9 as three files: answers to questions one and two, ms.py for problem three and bs.py for the extra credit problem four. The method of submission is TBD.

Background reading