# [pypy-svn] r24026 - pypy/dist/pypy/lib/logic/computation_space

ale at codespeak.net ale at codespeak.net
Mon Mar 6 17:14:48 CET 2006

```Author: ale
Date: Mon Mar  6 17:14:47 2006
New Revision: 24026

Modified:
pypy/dist/pypy/lib/logic/computation_space/problems.py
pypy/dist/pypy/lib/logic/computation_space/test_computationspace.py
Log:
(George Paci, ale) A not finished Sudoku solver, and a not working test

Modified: pypy/dist/pypy/lib/logic/computation_space/problems.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/problems.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/problems.py	Mon Mar  6 17:14:47 2006
@@ -114,4 +114,35 @@
for conf2 in variables:
if conf2 > conf1:
+
return tuple(variables)
+
+def sudoku(computation_space):
+    cs = computation_space
+    import constraint as c
+
+    variables = [cs.var('v%i%i'%(x,y)) for x in range(1,10) for y in range(1,10)]
+
+    # Make the variables
+    for v in variables:
+        cs.set_dom(v, c.FiniteDomain(range(1,10)))
+    # Add constraints for rows (sum should be 45)
+    for i in range(1,10):
+        row = [ v for v in variables if v.name[1] == str(i)]
+        cs.add_constraint(row, 'sum([%s]) == 45' % ', '.join([v.name for v in row]))
+    # Add constraints for columns (sum should be 45)
+    for i in range(1,10):
+        row = [ v for v in variables if v.name[2] == str(i)]
+        cs.add_constraint(row, 'sum([%s]) == 45' % ', '.join([v.name for v in row]))
+    # Add constraints for subsquares (sum should be 45)
+    offsets = [(r,c) for r in [-1,0,1] for c in [-1,0,1]]
+    subsquares = [(r,c) for r in [2,5,8] for c in [2,5,8]]
+    for rc in subsquares:
+        sub = [cs.get_var_by_name('v%d%d'% (rc[0] + off[0],rc[1] + off[1])) for off in offsets]
+        cs.add_constraint(sub, 'sum([%s]) == 45' % ', '.join([v.name for v in sub]))
+        for v in sub:
+            for m in sub[sub.index(v)+1:]:
+                cs.add_constraint([v,m], '%s != %s' % (v.name, m.name))
+    #print cs.constraints
+    return tuple(variables)
+

Modified: pypy/dist/pypy/lib/logic/computation_space/test_computationspace.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/test_computationspace.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/test_computationspace.py	Mon Mar  6 17:14:47 2006
@@ -596,4 +596,38 @@
assert len(sols) == 64
print sols

+    def no_test_sudoku(self):
+        #spc = newspace(problems.sudoku)
+        #print spc.constraints
+        def more_constraints(space):
+            f = 'puzzle1.su'
+
+            file = open(f)
+            c = []
+            row = 1
+                for col in range(1,10):
+                    if line[col-1] != ' ':
+                        tup = ('v%d%d' % (col, row), int(line[col-1]))
+                        space.add_constraint([space.get_var_by_name(tup[0])],'%s == %d' % tup)
+                row += 1
+
+        #nspc = spc.clone()
+        #nspc.inject(more_constraints)
+        #print nspc.constraints
+        sol2 = strategies.dfs_one(strategies.sudoku)
+        print "done dfs"
+        #sol2 = [var.val for var in sol]
+        assert sol2 == [('room A', 'day 1 PM'),
+                        ('room B', 'day 2 PM'),
+                        ('room C', 'day 2 AM'),
+                        ('room C', 'day 2 PM'),
+                        ('room C', 'day 1 AM'),
+                        ('room C', 'day 1 PM'),
+                        ('room A', 'day 2 PM'),
+                        ('room B', 'day 1 AM'),
+                        ('room A', 'day 2 AM'),
+                        ('room A', 'day 1 AM')]
+
+

```