[pypy-svn] r25493 - in pypy/dist/pypy/objspace: . constraint constraint/test
auc at codespeak.net
auc at codespeak.net
Fri Apr 7 13:47:01 CEST 2006
Author: auc
Date: Fri Apr 7 13:47:00 2006
New Revision: 25493
Modified:
pypy/dist/pypy/objspace/constraint/constraint.py
pypy/dist/pypy/objspace/constraint/test/test_constraint.py
pypy/dist/pypy/objspace/constraint/test/test_fd.py
pypy/dist/pypy/objspace/logic.py
Log:
estimated_cost and revise
Modified: pypy/dist/pypy/objspace/constraint/constraint.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/constraint.py (original)
+++ pypy/dist/pypy/objspace/constraint/constraint.py Fri Apr 7 13:47:00 2006
@@ -116,7 +116,7 @@
return variable == self._variable
def w_estimate_cost(self):
- return 0 # get in the first place in the queue
+ return self._space.newint(0) # get in the first place in the queue
def w_affected_variables(self):
return [self._variable]
@@ -168,14 +168,14 @@
# worst case complexity
self.__cost = len(w_variables.wrappeditems) * (len(w_variables.wrappeditems) - 1) / 2
- def __repr__(self):
- return '<AllDistinct %s>' % str(self._variables)
+ def w__repr__(self):
+ return self._space.newstring('<AllDistinct %s>' % str(self._variables))
def w_copy_to(self, w_computation_space):
return self.__class__(space, self._variables)
def w_estimate_cost(self):
- return self.__cost
+ return self._space.newint(self.__cost)
def test_solution(self, sol):
"""test a solution against this constraint
@@ -185,20 +185,20 @@
return len(value_set) == len(sol)
def w_revise(self):
- variables = [(self.cs.w_dom(variable).size(),
+ variables = [(self._space.int_w(self.cs.w_dom(variable).w_size()),
variable, self.cs.w_dom(variable))
- for variable in self._variables]
-
+ for variable in self._variables.wrappeditems]
+
variables.sort()
# 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 dom.size() == 1:
+ if self._space.eq_w(dom.w_size(), self._space.newint(1)):
print "AllDistinct removes values"
for _siz, _var, _dom in variables:
- if _var != var:
+ if not self._space.eq_w(_var, var):
try:
- _dom.remove_value(dom.get_values()[0])
+ _dom.w_remove_value(dom.w_get_values().wrappeditems[0])
except KeyError:
# we ignore errors caused by the removal of
# non existing values
@@ -207,26 +207,29 @@
# if there are less values than variables, the constraint fails
values = {}
for size, var, dom in variables:
- for val in dom:
+ for val in dom.w_get_values().wrappeditems:
values[val] = 0
if len(values) < len(variables):
print "AllDistinct failed"
- raise ConsistencyFailure()
+ raise OperationError(self._space.w_RuntimeError,
+ self._space.wrap("Consistency Failure"))
+# raise ConsistencyFailure()
# the constraint is entailed if all domains have a size of 1
for variable in variables:
- if variable[2].size() != 1:
- return 0
+ if self._space.is_true(self._space.ne(variable[2].w_size(), self._space.newint(1))):
+ return self._space.newint(0)
# Question : did we *really* completely check
# our own alldistinctness predicate ?
- return 1
+ return self._space.newint(1)
W_AllDistinct.typedef = typedef.TypeDef(
"W_AllDistinct", W_AbstractConstraint.typedef,
estimate_cost = interp2app(W_AllDistinct.w_estimate_cost),
- revise = interp2app(W_Constraint.w_revise)
+ revise = interp2app(W_AllDistinct.w_revise),
+ __repr__ = interp2app(W_AllDistinct.w__repr__)
)
# function bolted into the space to serve as constructor
Modified: pypy/dist/pypy/objspace/constraint/test/test_constraint.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/test/test_constraint.py (original)
+++ pypy/dist/pypy/objspace/constraint/test/test_constraint.py Fri Apr 7 13:47:00 2006
@@ -13,7 +13,46 @@
cstr = AllDistinct(cspace, [v1, v2])
variables = cstr.affected_variables()
assert variables is not None
- print variables
assert len(variables) == 2
assert v1 in variables
assert v2 in variables
+
+ def test_estimated_cost(self):
+ csp = newspace()
+ v1 = csp.var('v1', FiniteDomain([1, 2]))
+ v2 = csp.var('v2', FiniteDomain([1, 2]))
+ cstr = AllDistinct(csp, [v1, v2])
+ assert cstr.estimate_cost() == 1
+
+ def notest_repr(self):
+ csp = newspace()
+ v1 = csp.var('v1', FiniteDomain([1, 2]))
+ v2 = csp.var('v2', FiniteDomain([1, 2]))
+ cstr = AllDistinct(csp, [v1, v2])
+ print cstr
+
+ def test_revise(self):
+ csp = newspace()
+ v1 = csp.var('v1', FiniteDomain([1, 2]))
+ v2 = csp.var('v2', FiniteDomain([1, 2]))
+ cstr = AllDistinct(csp, [v1, v2])
+ assert cstr.revise() == 0 # not entailed
+
+ v3 = csp.var('v3', FiniteDomain([1]))
+ v4 = csp.var('v4', FiniteDomain([2]))
+ cstr = AllDistinct(csp, [v3, v4])
+ assert cstr.revise() == 1 # entailed
+
+ v5 = csp.var('v5', FiniteDomain([1]))
+ v6 = csp.var('v6', FiniteDomain([1]))
+ cstr = AllDistinct(csp, [v5, v6])
+ raises(Exception, cstr.revise)
+
+ cstr = AllDistinct(csp, [v1, v5])
+ assert cstr.revise() == 1
+ assert csp.dom(v1).get_values() == [2]
+
+ v7 = csp.var('v7', FiniteDomain([1, 2]))
+ v8 = csp.var('v8', FiniteDomain([1, 2]))
+ cstr = AllDistinct(csp, [v2, v7, v8])
+ raises(Exception, cstr.revise)
Modified: pypy/dist/pypy/objspace/constraint/test/test_fd.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/test/test_fd.py (original)
+++ pypy/dist/pypy/objspace/constraint/test/test_fd.py Fri Apr 7 13:47:00 2006
@@ -9,7 +9,6 @@
fd = FiniteDomain([1, 2, 3])
assert fd.size() == 3
assert set(fd.get_values()) == set([1, 2, 3])
- #fd2 = fd.copy()
def test_copy(self):
fd = FiniteDomain([1, 2, 3])
@@ -28,7 +27,6 @@
# FIXME: check this is a ConsistencyFailure
raises(Exception, fd.remove_value, 3)
-
def test_remove_values(self):
fd = FiniteDomain([1, 2, 3])
fd.remove_values([1, 2])
Modified: pypy/dist/pypy/objspace/logic.py
==============================================================================
--- pypy/dist/pypy/objspace/logic.py (original)
+++ pypy/dist/pypy/objspace/logic.py Fri Apr 7 13:47:00 2006
@@ -786,6 +786,8 @@
# multimethods hack
space.model.typeorder[W_Var] = [(W_Var, None), (W_Root, None)] # None means no conversion
space.model.typeorder[W_FiniteDomain] = [(W_FiniteDomain, None), (W_Root, None)]
+
+
for name in all_mms.keys():
exprargs, expr, miniglobals, fallback = (
all_mms[name].install_not_sliced(space.model.typeorder, baked_perform_call=False))
More information about the Pypy-commit
mailing list