[pypy-svn] r25805 - in pypy/dist/pypy: lib/logic/computation_space objspace/constraint objspace/constraint/applevel objspace/constraint/test
afayolle at codespeak.net
afayolle at codespeak.net
Thu Apr 13 17:27:37 CEST 2006
Author: afayolle
Date: Thu Apr 13 17:27:33 2006
New Revision: 25805
Modified:
pypy/dist/pypy/lib/logic/computation_space/computationspace.py
pypy/dist/pypy/lib/logic/computation_space/variable.py
pypy/dist/pypy/objspace/constraint/applevel/problems.py
pypy/dist/pypy/objspace/constraint/applevel/solver.py
pypy/dist/pypy/objspace/constraint/constraint.py
pypy/dist/pypy/objspace/constraint/test/test_solver.py
Log:
small enhancements
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 Thu Apr 13 17:27:33 2006
@@ -180,7 +180,7 @@
def commit(self, choice):
"""if self is distributable, causes the Choose call in the
space to complete and return some_number as a result. This
- may cause the spzce to resume execution.
+ may cause the space to resume execution.
some_number must satisfy 1=<I=<N where N is the first arg
of the Choose call.
"""
Modified: pypy/dist/pypy/lib/logic/computation_space/variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/variable.py (original)
+++ pypy/dist/pypy/lib/logic/computation_space/variable.py Thu Apr 13 17:27:33 2006
@@ -76,7 +76,6 @@
return self.__str__()
# public interface
-
val = property(_get_val, _set_val)
def is_bound(self):
Modified: pypy/dist/pypy/objspace/constraint/applevel/problems.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/applevel/problems.py (original)
+++ pypy/dist/pypy/objspace/constraint/applevel/problems.py Thu Apr 13 17:27:33 2006
@@ -59,30 +59,14 @@
('c03','c05','c06','c07'),
('c01','c03','c07','c08'))
-## for group in groups:
-## cs.tell(AllDistinct([cs.find_var(v) for v in group]))
-## cs.add_expression(AllDistinct(cs, tuple([cs.find_var(v)
-## for v in group])))
-
for group in groups:
for conf1 in group:
for conf2 in group:
- v1, v2 = cs.find_vars((conf1, conf2))
if conf2 > conf1:
+ v1, v2 = cs.find_vars((conf1, conf2))
cs.tell(make_expression([v1, v2], '%s[1] != %s[1]'% (v1.name(),v2.name())))
+ cs.tell(AllDistinct(variables))
-## for g in groups:
-## for conf1 in g:
-## for conf2 in g:
-## v1, v2 = cs.find_vars(conf1, conf2)
-## if conf2 > conf1:
-## cs.add_constraint([v1,v2], '%s[1] != %s[1]'% (v1.name,v2.name))
-
- for conf1 in variables:
- for conf2 in variables:
- if conf2 > conf1:
- cs.tell(make_expression([conf1,conf2],
- '%s != %s'%(conf1.name(),conf2.name())))
return variables
def sudoku(computation_space):
Modified: pypy/dist/pypy/objspace/constraint/applevel/solver.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/applevel/solver.py (original)
+++ pypy/dist/pypy/objspace/constraint/applevel/solver.py Thu Apr 13 17:27:33 2006
@@ -23,7 +23,7 @@
sp_stack.appendleft(space)
print "ready to find solution ..."
- while len(sp_stack):
+ while sp_stack:
space = sp_stack.pop()
print ' '*len(sp_stack), "ask [depth = %s]" % len(sp_stack)
status = space.ask()
@@ -32,7 +32,7 @@
yield space.merge()
elif status > 1:
print ' '*len(sp_stack), "%s branches ..." % status
- for i in range(status):
+ for i in xrange(status):
clone = space.clone()
clone.commit(status-i)
collect(clone)
Modified: pypy/dist/pypy/objspace/constraint/constraint.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/constraint.py (original)
+++ pypy/dist/pypy/objspace/constraint/constraint.py Thu Apr 13 17:27:33 2006
@@ -93,8 +93,9 @@
return len(value_set) == len(sol)
def revise(self, w_cs):
+ _spc = self._space
assert isinstance(w_cs, W_ComputationSpace)
- variables = [(self._space.int_w(w_cs.w_dom(variable).w_size()),
+ variables = [(_spc.int_w(w_cs.w_dom(variable).w_size()),
variable, w_cs.w_dom(variable))
for variable in self._variables]
@@ -102,10 +103,10 @@
# if a domain has a size of 1,
# then the value must be removed from the other domains
for size, var, dom in variables:
- if self._space.eq_w(dom.w_size(), self._space.newint(1)):
+ if _spc.eq_w(dom.w_size(), _spc.newint(1)):
#print "AllDistinct removes values"
for _siz, _var, _dom in variables:
- if not self._space.eq_w(_var, var):
+ if not _spc.eq_w(_var, var):
try:
_dom.w_remove_value(dom.w_get_values().wrappeditems[0])
except KeyError:
@@ -118,19 +119,21 @@
for size, var, dom in variables:
for val in dom.w_get_values().wrappeditems:
values[val] = 0
+
if len(values) < len(variables):
#print "AllDistinct failed"
- raise OperationError(self._space.w_RuntimeError,
- self._space.wrap("Consistency Failure"))
+ raise OperationError(_spc.w_RuntimeError,
+ _spc.wrap("ConsistencyFailure"))
# the constraint is entailed if all domains have a size of 1
for variable in variables:
- if self._space.is_true(self._space.ne(variable[2].w_size(), self._space.newint(1))):
+ if not _spc.eq_w(variable[2].w_size(),
+ _spc.newint(1)):
return False
# Question : did we *really* completely check
# our own alldistinctness predicate ?
-
+ #print "All distinct entailed"
return True
W_AllDistinct.typedef = typedef.TypeDef(
Modified: pypy/dist/pypy/objspace/constraint/test/test_solver.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/test/test_solver.py (original)
+++ pypy/dist/pypy/objspace/constraint/test/test_solver.py Thu Apr 13 17:27:33 2006
@@ -22,6 +22,9 @@
sols = solver.solve(spc)
count = 0
+ solutions = set()
for sol in sols:
+ assert sol not in solutions
+ solutions.add(sol)
count += 1
assert count == 64
More information about the Pypy-commit
mailing list