[pypy-svn] r22940 - pypy/dist/pypy/lib/logic/computation_space

auc at codespeak.net auc at codespeak.net
Thu Feb 2 10:59:00 CET 2006


Author: auc
Date: Thu Feb  2 10:58:55 2006
New Revision: 22940

Added:
   pypy/dist/pypy/lib/logic/computation_space/strategies.py
Log:
provide a single solution dfs using computation spaces


Added: pypy/dist/pypy/lib/logic/computation_space/strategies.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/logic/computation_space/strategies.py	Thu Feb  2 10:58:55 2006
@@ -0,0 +1,34 @@
+import computationspace as csp
+
+class StrategyDistributionMismatch(Exception):
+    pass
+
+def dfs_one_solution(problem):
+    """depth-first single-solution search"""
+
+    def do_dfs(space):
+        status = space.ask()
+        if status == csp.Failed:
+            return None
+        elif status == csp.Succeeded:
+            return [space]
+        elif status == csp.Alternatives(2):
+            new_space = space.clone()
+            space.commit(1)
+            outcome = do_dfs(space)
+            if outcome is None:
+                new_space.commit(2)
+                return do_dfs(new_space)
+            else:
+                return outcome
+        else:
+            raise StrategyDistributionMismatch()
+                                               
+    
+    space = csp.ComputationSpace(problem)
+    outcome = do_dfs(space)
+    if outcome == None: return None
+    space.merge()
+    return space
+
+



More information about the Pypy-commit mailing list