Beispiel #1
0
    def _valueRemoved(self):
        if self.lowestMin >= self.highestMax:
            raise ConsistencyFailure("earliest start [%.2f] higher than latest end  [%.2f]" %
                                     (self.lowestMin, self.highestMax))
        if self._min_length > self._max_length:
            raise ConsistencyFailure("min length [%.2f] greater than max length [%.2f]" %
                                     (self._min_length, self._max_length))

        self._max_length = min(self._max_length, self.highestMax - self.lowestMin)
        AbstractDomain._valueRemoved(self)
Beispiel #2
0
    def narrow(self, domains):
        """narrowing algorithm for the constraint"""
        variables = [(domains[variable].size(), variable, domains[variable])
                     for variable in self._variables]

        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:
                for _siz, _var, _dom in variables:
                    if _var != var:
                        try:
                            _dom.removeValue(dom.getValues()[0])
                        except KeyError:
                            # we ignore errors caused by the removal of
                            # non existing values
                            pass

        # if there are less values than variables, the constraint fails
        values = {}
        for size, var, dom in variables:
            for val in dom:
                values[val] = 0
        if len(values) < len(variables):
            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
        return 1
Beispiel #3
0
    def narrow(self, domains):
        """generic narrowing algorithm for n-ary expressions"""
        maybe_entailed = 1
        ffunc = self.filterFunc
        result_cache = self._init_result_cache()
        for kwargs in self._assign_values(domains):
            if maybe_entailed:
                for var, val in kwargs.iteritems():
                    if val not in result_cache[var]:
                        break
                else:
                    continue
            if ffunc(**kwargs):
                for var, val in kwargs.items():
                    result_cache[var][val] = 1
            else:
                maybe_entailed = 0

        try:
            for var, keep in result_cache.iteritems():
                domain = domains[var]
                domain.removeValues([val for val in domain if val not in keep])

        except ConsistencyFailure:
            raise ConsistencyFailure('Inconsistency while applying %s' % \
                                     repr(self))
        except KeyError:
            # There are no more value in result_cache
            pass

        return maybe_entailed
Beispiel #4
0
 def narrow(self,domains):
     domain = domains[self._variable]
     try:
         for v in domain.getValues():
             s = str(v)
             for d in ('0','1','2','3','4','5','6','7','8','9'):
                 if s.count(d) not in (0,1):
                     domain.removeValue(v)
                     break
     except ConsistencyFailure, e:
         raise ConsistencyFailure('inconsistency while applying %s'%repr(self))
Beispiel #5
0
    def narrow(self, domains):
        """specialized narrowing algorithm for binary expressions
        Runs much faster than the generic version"""
        maybe_entailed = 1
        var1 = self._variables[0]
        dom1 = domains[var1]
        values1 = dom1.getValues()
        var2 = self._variables[1]
        dom2 = domains[var2]
        values2 = dom2.getValues()
        ffunc = self.filterFunc
        if dom2.size() < dom1.size():
            var1, var2 = var2, var1
            dom1, dom2 = dom2, dom1
            values1, values2 = values2, values1

        kwargs = {}
        keep1 = {}
        keep2 = {}
        maybe_entailed = 1
        try:
            # iterate for all values
            for val1 in values1:
                kwargs[var1] = val1
                for val2 in values2:
                    kwargs[var2] = val2
                    if val1 in keep1 and val2 in keep2 and maybe_entailed == 0:
                        continue
                    if ffunc(**kwargs):
                        keep1[val1] = 1
                        keep2[val2] = 1
                    else:
                        maybe_entailed = 0

            dom1.removeValues([val for val in values1 if val not in keep1])
            dom2.removeValues([val for val in values2 if val not in keep2])

        except ConsistencyFailure:
            raise ConsistencyFailure('Inconsistency while applying %s' % \
                                     repr(self))
        except Exception:
            print self, kwargs
            raise
        return maybe_entailed
Beispiel #6
0
    def narrow(self, domains):
        maybe_entailed = 1
        var1 = self._variables[0]
        dom1 = domains[var1]
        values1 = dom1.getValues()
        var2 = self._variables[1]
        dom2 = domains[var2]
        values2 = dom2.getValues()

        keep1 = {}
        keep2 = {}
        maybe_entailed = 1
        for val1 in values1:
            val1_0 = val1[0]
            val1_1 = val1[1]
            for val2 in values2:
                if val1 in keep1 and val2 in keep2 and maybe_entailed == 0:
                    continue
                val2_0 = val2[0]
                val2_1 = val2[1]
                if val1_0  < val2_0 and \
                   val1_1 != val2_1 and \
                   abs(val1_0-val2_0) != abs(val1_1-val2_1):
                    keep1[val1] = 1
                    keep2[val2] = 1
                else:
                    maybe_entailed = 0

        try:
            dom1.removeValues([val for val in values1 if val not in keep1])
            dom2.removeValues([val for val in values2 if val not in keep2])
        except ConsistencyFailure:
            raise ConsistencyFailure('Inconsistency while applying %s' % \
                                     repr(self))
        except Exception:
            print self, kwargs
            raise
        return maybe_entailed