// //////////////////////////////////////////////////////////////
        // inference algorithms

        /** : forward checking. */
        private DomainRestoreInfo doForwardChecking(Variable var,
                                                    Assignment assignment, CSP csp)
        {
            DomainRestoreInfo result = new DomainRestoreInfo();

            foreach (Constraint constraint in csp.getConstraints(var))
            {
                List <Variable> scope = constraint.getScope();
                if (scope.Count == 2)
                {
                    foreach (Variable neighbor in constraint.getScope())
                    {
                        if (!assignment.hasAssignmentFor(neighbor))
                        {
                            if (revise(neighbor, constraint, assignment, csp,
                                       result))
                            {
                                if (csp.getDomain(neighbor).isEmpty())
                                {
                                    result.setEmptyDomainFound(true);
                                    return(result);
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        private bool revise(Variable xi, Variable xj, Constraint constraint,
                            CSP csp, DomainRestoreInfo info)
        {
            bool       revised    = false;
            Assignment assignment = new Assignment();

            foreach (Object iValue in csp.getDomain(xi))
            {
                assignment.setAssignment(xi, iValue);
                bool consistentExtensionFound = false;
                foreach (Object jValue in csp.getDomain(xj))
                {
                    assignment.setAssignment(xj, jValue);
                    if (constraint.isSatisfiedWith(assignment))
                    {
                        consistentExtensionFound = true;
                        break;
                    }
                }
                if (!consistentExtensionFound)
                {
                    info.storeDomainFor(xi, csp.getDomain(xi));
                    csp.removeValueFromDomain(xi, iValue);
                    revised = true;
                }
            }
            return(revised);
        }
Exemplo n.º 3
0
 /** 
  * Makes a CSP consisting of binary constraints arc-consistent.
  * @return An object which indicates success/failure and contains
  * data to undo the operation.
  */
 public DomainRestoreInfo reduceDomains(CSP csp)
 {
     DomainRestoreInfo result = new DomainRestoreInfo();
     FIFOQueue<Variable> queue = new FIFOQueue<Variable>();
     foreach (Variable var in csp.getVariables())
         queue.Add(var);
     reduceDomains(queue, csp, result);
     return result.compactify();
 }
Exemplo n.º 4
0
        /**
         * Makes a CSP consisting of binary constraints arc-consistent.
         * @return An object which indicates success/failure and contains
         * data to undo the operation.
         */
        public DomainRestoreInfo reduceDomains(CSP csp)
        {
            DomainRestoreInfo    result = new DomainRestoreInfo();
            FIFOQueue <Variable> queue  = new FIFOQueue <Variable>();

            foreach (Variable var in csp.getVariables())
            {
                queue.Add(var);
            }
            reduceDomains(queue, csp, result);
            return(result.compactify());
        }
        private bool revise(Variable var, Constraint constraint,
                            Assignment assignment, CSP csp, DomainRestoreInfo info)
        {
            bool revised = false;

            foreach (Object value in csp.getDomain(var))
            {
                assignment.setAssignment(var, value);
                if (!constraint.isSatisfiedWith(assignment))
                {
                    info.storeDomainFor(var, csp.getDomain(var));
                    csp.removeValueFromDomain(var, value);
                    revised = true;
                }
                assignment.removeAssignment(var);
            }
            return(revised);
        }
Exemplo n.º 6
0
 /** 
  * Reduces the domain of the specified variable to the specified
  * value and reestablishes arc-consistency. It is assumed that the
  * provided CSP is arc-consistent before the call.
  * @return An object which indicates success/failure and contains
  * data to undo the operation.
  */
 public DomainRestoreInfo reduceDomains(Variable var, Object value, CSP csp)
 {
     DomainRestoreInfo result = new DomainRestoreInfo();
     Domain domain = csp.getDomain(var);
     if (domain.contains(value))
     {
         if (domain.Count > 1)
         {
             FIFOQueue<Variable> queue = new FIFOQueue<Variable>();
             queue.Add(var);
             result.storeDomainFor(var, domain);
             csp.setDomain(var, new Domain(new Object[] { value }));
             reduceDomains(queue, csp, result);
         }
     }
     else
     {
         result.setEmptyDomainFound(true);
     }
     return result.compactify();
 }
Exemplo n.º 7
0
        /**
         * Reduces the domain of the specified variable to the specified
         * value and reestablishes arc-consistency. It is assumed that the
         * provided CSP is arc-consistent before the call.
         * @return An object which indicates success/failure and contains
         * data to undo the operation.
         */
        public DomainRestoreInfo reduceDomains(Variable var, Object value, CSP csp)
        {
            DomainRestoreInfo result = new DomainRestoreInfo();
            Domain            domain = csp.getDomain(var);

            if (domain.contains(value))
            {
                if (domain.Count > 1)
                {
                    FIFOQueue <Variable> queue = new FIFOQueue <Variable>();
                    queue.Add(var);
                    result.storeDomainFor(var, domain);
                    csp.setDomain(var, new Domain(new Object[] { value }));
                    reduceDomains(queue, csp, result);
                }
            }
            else
            {
                result.setEmptyDomainFound(true);
            }
            return(result.compactify());
        }
Exemplo n.º 8
0
        /**
         * Template method, which can be configured by overriding the three
         * primitive operations below.
         */
        private Assignment recursiveBackTrackingSearch(CSP csp,
                                                       Assignment assignment)
        {
            Assignment result = null;

            if (assignment.isComplete(csp.getVariables()))
            {
                result = assignment;
            }
            else
            {
                Variable var = selectUnassignedVariable(assignment, csp);
                foreach (Object value in orderDomainValues(var, assignment, csp))
                {
                    assignment.setAssignment(var, value);
                    fireStateChanged(assignment, csp);
                    if (assignment.isConsistent(csp.getConstraints(var)))
                    {
                        DomainRestoreInfo info = inference(var, assignment, csp);
                        if (!info.isEmpty())
                        {
                            fireStateChanged(csp);
                        }
                        if (!info.isEmptyDomainFound())
                        {
                            result = recursiveBackTrackingSearch(csp, assignment);
                            if (result != null)
                            {
                                break;
                            }
                        }
                        info.restoreDomains(csp);
                    }
                    assignment.removeAssignment(var);
                }
            }
            return(result);
        }
Exemplo n.º 9
0
 private void reduceDomains(FIFOQueue <Variable> queue, CSP csp,
                            DomainRestoreInfo info)
 {
     while (!queue.isEmpty())
     {
         Variable var = queue.pop();
         foreach (Constraint constraint in csp.getConstraints(var))
         {
             if (constraint.getScope().Count == 2)
             {
                 Variable neighbor = csp.getNeighbor(var, constraint);
                 if (revise(neighbor, var, constraint, csp, info))
                 {
                     if (csp.getDomain(neighbor).isEmpty())
                     {
                         info.setEmptyDomainFound(true);
                         return;
                     }
                     queue.push(neighbor);
                 }
             }
         }
     }
 }
Exemplo n.º 10
0
 private void reduceDomains(FIFOQueue<Variable> queue, CSP csp,
         DomainRestoreInfo info)
 {
     while (!queue.isEmpty())
     {
         Variable var = queue.pop();
         foreach (Constraint constraint in csp.getConstraints(var))
         {
             if (constraint.getScope().Count == 2)
             {
                 Variable neighbor = csp.getNeighbor(var, constraint);
                 if (revise(neighbor, var, constraint, csp, info))
                 {
                     if (csp.getDomain(neighbor).isEmpty())
                     {
                         info.setEmptyDomainFound(true);
                         return;
                     }
                     queue.push(neighbor);
                 }
             }
         }
     }
 }
Exemplo n.º 11
0
 private bool revise(Variable xi, Variable xj, Constraint constraint,
         CSP csp, DomainRestoreInfo info)
 {
     bool revised = false;
     Assignment assignment = new Assignment();
     foreach (Object iValue in csp.getDomain(xi))
     {
         assignment.setAssignment(xi, iValue);
         bool consistentExtensionFound = false;
         foreach (Object jValue in csp.getDomain(xj))
         {
             assignment.setAssignment(xj, jValue);
             if (constraint.isSatisfiedWith(assignment))
             {
                 consistentExtensionFound = true;
                 break;
             }
         }
         if (!consistentExtensionFound)
         {
             info.storeDomainFor(xi, csp.getDomain(xi));
             csp.removeValueFromDomain(xi, iValue);
             revised = true;
         }
     }
     return revised;
 }
        private bool revise(Variable var, Constraint constraint,
                Assignment assignment, CSP csp, DomainRestoreInfo info)
        {

            bool revised = false;
            foreach (Object value in csp.getDomain(var))
            {
                assignment.setAssignment(var, value);
                if (!constraint.isSatisfiedWith(assignment))
                {
                    info.storeDomainFor(var, csp.getDomain(var));
                    csp.removeValueFromDomain(var, value);
                    revised = true;
                }
                assignment.removeAssignment(var);
            }
            return revised;
        }
        // //////////////////////////////////////////////////////////////
        // inference algorithms

        /** : forward checking. */
        private DomainRestoreInfo doForwardChecking(Variable var,
                Assignment assignment, CSP csp)
        {
            DomainRestoreInfo result = new DomainRestoreInfo();
            foreach (Constraint constraint in csp.getConstraints(var))
            {
                List<Variable> scope = constraint.getScope();
                if (scope.Count == 2)
                {
                    foreach (Variable neighbor in constraint.getScope())
                    {
                        if (!assignment.hasAssignmentFor(neighbor))
                        {
                            if (revise(neighbor, constraint, assignment, csp,
                                    result))
                            {
                                if (csp.getDomain(neighbor).isEmpty())
                                {
                                    result.setEmptyDomainFound(true);
                                    return result;
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }