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

auc at codespeak.net auc at codespeak.net
Tue Jan 31 18:05:23 CET 2006

```Author: auc
Date: Tue Jan 31 18:05:22 2006
New Revision: 22903

Modified:
pypy/dist/pypy/lib/logic/computation_space/computationspace.py
pypy/dist/pypy/lib/logic/computation_space/distributor.py
pypy/dist/pypy/lib/logic/computation_space/test_computationspace.py
Log:
* add inject (to further constrain a space)
* fix space/distributor relationship
* remove dead code

Modified: pypy/dist/pypy/lib/logic/computation_space/computationspace.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/computationspace.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/computationspace.py	Tue Jan 31 18:05:22 2006
@@ -160,23 +160,6 @@
## then it returns alternatives(N), where N is the number of
## alternatives.

-## An example specifying how to use a computation space :
-
-## def my_problem(store):
-##     #declare variables, their domain
-##     x, y, z = var('x'), var('y'), var('z')
-##     #declare constraints
-##     set_domain(x, FiniteDomain([1, 2]))
-##     set_domain(y, FiniteDomain([2, 3]))
-##     set_domain(z, FiniteDomain([42, 43]))
-##     add_constraint(c.Expression([x, y, z], 'x == y + z'))
-##     add_constraint(c.Expression([z, w], 'z < w'))
-##     #set up a distribution strategy
-##     ????
-##     return (x, y, z)
-
-## space = ComputationSpace(fun=my_problem)
-
from threading import Thread, Condition, RLock, local

from state import Succeeded, Distributable, Failed, Merged
@@ -249,9 +232,10 @@
* variables bound to a number, record or procedure
(also called determined variables)."""

+    # we have to enforce only one distributor
+    # thread running in one space at the same time
_nb_choices = 0

-
def __init__(self, problem, parent=None):
# consistency-preserving stuff
self.in_transaction = False
@@ -644,15 +628,15 @@
spc = ComputationSpace(NoProblem, parent=self)
for var in spc.vars:
var.cs_set_dom(spc, var.cs_get_dom(self).copy())
-        # check satisfiability of the space
-        spc._process()
+        spc.distributor.set_space(spc)
if spc.status == Distributable:
spc.distributor.start()
return spc

def inject(self, restricting_problem):
-        """add additional stuff into a space"""
-        pass
+        """add additional entities into a space"""
+        restricting_problem(self)
+        self._process()

def commit(self, choice):
"""if self is distributable, causes the Choose call in the
@@ -682,39 +666,6 @@
ComputationSpace._nb_choices += 1
return self.var('__choice__'+str(self._nb_choices))

-    def make_children(self):
-        for dommap in self.distributor.distribute():
-            cs = ComputationSpace(lambda cs : True,
-                                  parent=self)
-            self.children.add(cs)
-            for var, dom in dommap.items():
-                var.cs_set_dom(cs, dom)
-
-    def solve_all(self):
-        """recursively solves the problem
-        """
-        if self.status == Unprocessed:
-            self.process()
-            if self.status == Succeeded: return self.root
-            if self.status == Failed: raise Failed
-            self.make_children()
-            results = set() # to be merged/committed ?
-            for cs in self.children:
-                try:
-                    results.add(cs.solve_all())
-                except Failed:
-                    pass
-            for result in results:
-                # Q: do we (a) merge our children results right now
-                #    or (b) do we pass results up the call chain ?
-                # (b) makes sense for a SolveAll kind of method on cs's
-                # (a) might be more Oz-ish, maybe allowing some very fancy
-                #     stuff with distribution or whatever
-                self.do_something_with(result)
-
-
-
-
#-- Unifiability checks---------------------------------------
#--
#-- quite costly & could be merged back in unify
@@ -778,36 +729,3 @@
def _both_are_bound(v1, v2):
return v1._is_bound() and v2._is_bound()

-
-
-#--
-#-- the global store
-#from problems import dummy_problem
-#_cs = ComputationSpace(dummy_problem)
-
-#-- global accessor functions
-## def var(name):
-##     v = Var(name, _cs)
-##     _cs.add_unbound(v)
-##     return v
-
-## def set_domain(var, dom):
-##     return _cs.set_domain(var, dom)
-
-## def add_constraint(constraint):
-##     return _cs.add_constraint(constraint)
-
-## def satisfiable(constraint):
-##     return _cs.satisfiable(constraint)
-
-## def get_satisfying_domains(constraint):
-##     return _cs.get_satisfying_domains(constraint)
-
-## def satisfy(constraint):
-##     return _cs.satisfy(constraint)
-
-## def bind(var, val):
-##     return _cs.bind(var, val)
-
-## def unify(var1, var2):
-##     return _cs.unify(var1, var2)

Modified: pypy/dist/pypy/lib/logic/computation_space/distributor.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/distributor.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/distributor.py	Tue Jan 31 18:05:22 2006
@@ -18,6 +18,9 @@
self.nb_subspaces = nb_subspaces
self.cs = c_space
self.verbose = 0
+
+    def set_space(self, space):
+        self.cs = space

def findSmallestDomain(self):
"""returns the variable having the smallest domain.
@@ -132,8 +135,6 @@
def run(self):
while self.cs.status == Distributable:
choice = self.cs.choose(self.nb_subdomains())
-            if self.cs.status == Failed:
-                return
self.new_distribute(choice)
self.cs._process()

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	Tue Jan 31 18:05:22 2006
@@ -489,7 +489,7 @@
spc = space.ComputationSpace(problems.satisfiable_problem)
assert spc.ask() == space.Alternatives(2)

-    def test_distribute(self):
+    def test_old_distribute(self):
spc = space.ComputationSpace(problems.satisfiable_problem)
new_domains = [tuple(d.items()) for d in
spc.distributor.distribute()]
@@ -512,13 +512,8 @@
for (e1, e2) in zip(d1, d2):
print e1, '=?', e2
assert e1 == e2
-        # the following assertion fails for mysterious reasons
-        # have we discovered a bug in CPython ?
-        #print hash(new_domains[0]), hash(new_domains[1])
-        #print hash(expected_domains[0]), hash(expected_domains[1])
-        #assert set(new_domains) == set(expected_domains)

-    def test_clone(self):
+    def test_clone_and_distribute(self):
spc = space.ComputationSpace(problems.satisfiable_problem)
w = spc.get_var_by_name('w')
assert spc.ask() == space.Alternatives(2)
@@ -529,5 +524,17 @@
assert new_spc.ask() == space.Succeeded
assert w.cs_get_dom(spc) == c.FiniteDomain([5, 6, 7])
assert w.cs_get_dom(new_spc) == c.FiniteDomain([5])
-        spc.commit(0)
-        new_spc.commit(0)
+
+    def test_inject(self):
+        def more_constraints(space):
+            space.add_constraint(c.Expression(space, [w], 'w == 5'))
+        spc = space.ComputationSpace(problems.satisfiable_problem)
+        w = spc.get_var_by_name('w')
+        assert spc.ask() == space.Alternatives(2)
+        new_spc = spc.clone()
+        new_spc.inject(more_constraints)
+        assert spc.ask() == space.Alternatives(2)
+        assert new_spc.ask() == space.Succeeded
+        assert w.cs_get_dom(spc) == c.FiniteDomain([5, 6, 7])
+        assert w.cs_get_dom(new_spc) == c.FiniteDomain([5])
+

```

More information about the Pypy-commit mailing list