Ejemplo n.º 1
0
        /**
         * Establishes arc-consistency for (xi, xj).
         * @return value true if the domain of xi was reduced.
         */
        private bool revise(VAR xi, VAR xj, IConstraint <VAR, VAL> constraint, CSP <VAR, VAL> csp, DomainLog <VAR, VAL> log)
        {
            Domain <VAL>          currDomain = csp.getDomain(xi);
            ICollection <VAL>     newValues  = CollectionFactory.CreateQueue <VAL>();
            Assignment <VAR, VAL> assignment = new Assignment <VAR, VAL>();

            foreach (VAL vi in currDomain)
            {
                assignment.add(xi, vi);
                foreach (VAL vj in csp.getDomain(xj))
                {
                    assignment.add(xj, vj);
                    if (constraint.isSatisfiedWith(assignment))
                    {
                        newValues.Add(vi);
                        break;
                    }
                }
            }
            if (newValues.Size() < currDomain.size())
            {
                log.storeDomainFor(xi, csp.getDomain(xi));
                csp.setDomain(xi, new Domain <VAL>(newValues));
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        /**
         * Removes all values from the domain of <code>var</code> which are not consistent with
         * <code>constraint</code> and <code>assignment</code>. Modifies the domain log accordingly so
         * that all changes can be undone later on.
         */
        private bool revise(VAR var, IConstraint <VAR, VAL> constraint, Assignment <VAR, VAL> assignment,
                            CSP <VAR, VAL> csp, DomainLog <VAR, VAL> log)
        {
            bool revised = false;

            foreach (VAL value in csp.getDomain(var))
            {
                assignment.add(var, value);
                if (!constraint.isSatisfiedWith(assignment))
                {
                    log.storeDomainFor(var, csp.getDomain(var));
                    csp.removeValueFromDomain(var, value);
                    revised = true;
                }
                assignment.remove(var);
            }
            return(revised);
        }
Ejemplo n.º 3
0
        /**
         * Reduces the domain of the specified variable to the specified value and
         * reestablishes arc-consistency. It is assumed that the provided CSP was
         * arc-consistent before the call.
         *
         * @return An object which indicates success/failure and contains data to
         *         undo the operation.
         */
        public IInferenceLog <VAR, VAL> apply(CSP <VAR, VAL> csp, Assignment <VAR, VAL> assignment, VAR var)
        {
            Domain <VAL> domain = csp.getDomain(var);
            VAL          value  = assignment.getValue(var);

            if (!domain.contains(value))
            {
                throw new Exception("domain does not contain value");
            }

            DomainLog <VAR, VAL> log = new DomainLog <VAR, VAL>();

            if (domain.size() > 1)
            {
                ICollection <VAR> queue = CollectionFactory.CreateFifoQueue <VAR>();
                queue.Add(var);
                log.storeDomainFor(var, domain);
                csp.setDomain(var, new Domain <VAL>(value));
                reduceDomains(queue, csp, log);
            }
            return(log.compactify());
        }