Exemple #1
0
        // //////////////////////////////////////////////////////////////
        // inference algorithms

        /// <summary>
        /// Implements forward checking.
        /// </summary>
        /// <param name="var"></param>
        /// <param name="assignment"></param>
        /// <param name="csp"></param>
        /// <returns></returns>
        private DomainRestoreInfo DoForwardChecking(Variable var,
                                                    Assignment assignment, CSProblem csp)
        {
            DomainRestoreInfo result = new DomainRestoreInfo();

            foreach (IConstraint constraint in csp.GetConstraints(var))
            {
                IList <Variable> scope = constraint.GetScope();
                if (scope.Count == 2)
                {
                    foreach (Variable neighbor in constraint.GetScope())
                    {
                        if (!assignment.HasAssignmentFor(neighbor))
                        {
                            if (this.Revise(neighbor, constraint, assignment, csp,
                                            result))
                            {
                                if (csp.GetDomain(neighbor).IsEmpty())
                                {
                                    result.SetEmptyDomainFound(true);
                                    return(result);
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Implements the degree heuristic.
        /// </summary>
        /// <param name="vars"></param>
        /// <param name="assignment"></param>
        /// <param name="csp"></param>
        /// <returns></returns>
        private IList <Variable> applyDegreeHeuristic(IList <Variable> vars,
                                                      Assignment assignment, CSProblem csp)
        {
            IList <Variable> result = new List <Variable>();
            int maxDegree           = int.MinValue;

            foreach (Variable var in vars)
            {
                int degree = 0;
                foreach (IConstraint constraint in csp.GetConstraints(var))
                {
                    Variable neighbor = csp.GetNeighbor(var, constraint);
                    if (!assignment.HasAssignmentFor(neighbor) &&
                        csp.GetDomain(neighbor).Size() > 1)
                    {
                        ++degree;
                    }
                }
                if (degree >= maxDegree)
                {
                    if (degree > maxDegree)
                    {
                        result.Clear();
                        maxDegree = degree;
                    }
                    result.Add(var);
                }
            }
            return(result);
        }
        private IList <Variable> GetConflictedVariables(Assignment assignment, CSProblem csp)
        {
            IList <Variable> result = new List <Variable>();

            foreach (IConstraint constraint in csp.GetConstraints())
            {
                if (!constraint.IsSatisfiedWith(assignment))
                {
                    foreach (Variable var in constraint.GetScope())
                    {
                        if (!result.Contains(var))
                        {
                            result.Add(var);
                        }
                    }
                }
            }
            return(result);
        }
Exemple #4
0
        private int CountLostValues(Variable var, object value, CSProblem csp)
        {
            int        result     = 0;
            Assignment assignment = new Assignment();

            assignment.SetAssignment(var, value);
            foreach (IConstraint constraint in csp.GetConstraints(var))
            {
                Variable neighbor = csp.GetNeighbor(var, constraint);
                foreach (object nValue in csp.GetDomain(neighbor))
                {
                    assignment.SetAssignment(neighbor, nValue);
                    if (!constraint.IsSatisfiedWith(assignment))
                    {
                        ++result;
                    }
                }
            }
            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Template method, which can be configured by overriding the three
        /// primitive operations below.
        /// </summary>
        /// <param name="csp"></param>
        /// <param name="assignment"></param>
        /// <returns></returns>
        private Assignment RecursiveBackTrackingSearch(CSProblem csp,
                                                       Assignment assignment)
        {
            Assignment result = null;

            if (assignment.IsComplete(csp.Variables))
            {
                result = assignment;
            }
            else
            {
                Variable var = this.SelectUnassignedVariable(assignment, csp);
                foreach (object value in this.OrderDomainValues(var, assignment, csp))
                {
                    assignment.SetAssignment(var, value);
                    this.FireStateChanged(assignment, csp);
                    if (assignment.IsConsistent(csp.GetConstraints(var)))
                    {
                        DomainRestoreInfo info = this.Inference(var, assignment, csp);
                        if (!info.IsEmpty())
                        {
                            this.FireStateChanged(csp);
                        }
                        if (!info.IsEmptyDomainFound())
                        {
                            result = this.RecursiveBackTrackingSearch(csp, assignment);
                            if (result != null)
                            {
                                break;
                            }
                        }
                        csp.RestoreDomains(info);
                    }
                    assignment.RemoveAssignment(var);
                }
            }
            return(result);
        }
        private object GetMinConflictValueFor(Variable var, Assignment assignment, CSProblem csp)
        {
            IList <IConstraint> constraints = csp.GetConstraints(var);
            Assignment          duplicate   = assignment.Copy();
            int            minConflict      = int.MaxValue;
            IList <object> resultCandidates = new List <object>();

            foreach (object value in csp.GetDomain(var))
            {
                duplicate.SetAssignment(var, value);
                int currConflict = this.CountConflicts(duplicate, constraints);
                if (currConflict <= minConflict)
                {
                    if (currConflict < minConflict)
                    {
                        resultCandidates.Clear();
                        minConflict = currConflict;
                    }
                    resultCandidates.Add(value);
                }
            }
            return(resultCandidates.Count != 0 ? Util.Util.SelectRandomlyFromList(resultCandidates) : null);
        }
 protected void ReduceDomains(FIFOQueue <Variable> queue, CSProblem csp,
                              DomainRestoreInfo info)
 {
     while (!(queue.Count == 0))
     {
         Variable var = queue.Pop();
         foreach (IConstraint constraint in csp.GetConstraints(var))
         {
             if (constraint.GetScope().Count == 2)
             {
                 Variable neighbor = csp.GetNeighbor(var, constraint);
                 if (this.Revise(neighbor, var, constraint, csp, info))
                 {
                     if (csp.GetDomain(neighbor).IsEmpty())
                     {
                         info.SetEmptyDomainFound(true);
                         return;
                     }
                     queue.Push(neighbor);
                 }
             }
         }
     }
 }
Exemple #8
0
 /// <summary>
 /// Returns true if this assignment is consistent as well as complete with
 /// respect to the given CSP.
 /// </summary>
 /// <param name="csp"></param>
 /// <returns></returns>
 public bool IsSolution(CSProblem csp)
 {
     return(this.IsConsistent(csp.GetConstraints()) &&
            this.IsComplete(csp.Variables));
 }