getConstraints() public method

public getConstraints ( ) : List
return List
Exemplo n.º 1
0
        private Object getMinConflictValueFor(Variable var, Assignment assignment,
                                              CSP csp)
        {
            List <Constraint> constraints  = csp.getConstraints(var);
            Assignment        duplicate    = assignment.copy();
            int           minConflict      = int.MAX_VALUE;
            List <Object> resultCandidates = new List <Object>();

            foreach (Object value in csp.getDomain(var))
            {
                duplicate.setAssignment(var, value);
                int currConflict = countConflicts(duplicate, constraints);
                if (currConflict <= minConflict)
                {
                    if (currConflict < minConflict)
                    {
                        resultCandidates.clear();
                        minConflict = currConflict;
                    }
                    resultCandidates.Add(value);
                }
            }
            if (!resultCandidates.isEmpty())
            {
                return(Util.selectRandomlyFromList(resultCandidates));
            }
            else
            {
                return(null);
            }
        }
        // //////////////////////////////////////////////////////////////
        // 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);
        }
        /** : the degree heuristic. */
        private List <Variable> applyDegreeHeuristic(List <Variable> vars,
                                                     Assignment assignment, CSP csp)
        {
            List <Variable> result    = new List <Variable>();
            int             maxDegree = int.MIN_VALUE;

            foreach (Variable var in vars)
            {
                int degree = 0;
                foreach (Constraint constraint in csp.getConstraints(var))
                {
                    Variable neighbor = csp.getNeighbor(var, constraint);
                    if (!assignment.hasAssignmentFor(neighbor) &&
                        csp.getDomain(neighbor).Count > 1)
                    {
                        ++degree;
                    }
                }
                if (degree >= maxDegree)
                {
                    if (degree > maxDegree)
                    {
                        result.clear();
                        maxDegree = degree;
                    }
                    result.Add(var);
                }
            }
            return(result);
        }
Exemplo n.º 4
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.º 5
0
        private List<Variable> getConflictedVariables(Assignment assignment, CSP csp) {
		List<Variable> result = new List<Variable>();
        foreach (Constraint constraint in csp.getConstraints())
        {
			if (!constraint.isSatisfiedWith(assignment))
				foreach (Variable var in constraint.getScope())
					if (!result.contains(var))
						result.Add(var);
		}
		return result;
	}
Exemplo n.º 6
0
        private List <Variable> getConflictedVariables(Assignment assignment, CSP csp)
        {
            List <Variable> result = new List <Variable>();

            foreach (Constraint constraint in csp.getConstraints())
            {
                if (!constraint.isSatisfiedWith(assignment))
                {
                    foreach (Variable var in constraint.getScope())
                    {
                        if (!result.contains(var))
                        {
                            result.Add(var);
                        }
                    }
                }
            }
            return(result);
        }
        private int countLostValues(Variable var, Object value, CSP csp)
        {
            int        result     = 0;
            Assignment assignment = new Assignment();

            assignment.setAssignment(var, value);
            foreach (Constraint 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);
        }
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 Object getMinConflictValueFor(Variable var, Assignment assignment,
                CSP csp) {
		List<Constraint> constraints = csp.getConstraints(var);
		Assignment duplicate = assignment.copy();
		int minConflict = int.MAX_VALUE;
		List<Object> resultCandidates = new List<Object>();
        foreach (Object value in csp.getDomain(var))
        {
			duplicate.setAssignment(var, value);
			int currConflict = countConflicts(duplicate, constraints);
			if (currConflict <= minConflict) {
				if (currConflict < minConflict) {
					resultCandidates.clear();
					minConflict = currConflict;
				}
				resultCandidates.Add(value);
			}
		}
		if (!resultCandidates.isEmpty())
			return Util.selectRandomlyFromList(resultCandidates);
		else
			return null;
	}
Exemplo n.º 11
0
 /**
  * Returns true if this assignment is consistent as well as complete with
  * respect to the given CSP.
  */
 public bool isSolution(CSP csp)
 {
     return(isConsistent(csp.getConstraints()) &&
            isComplete(csp.getVariables()));
 }
Exemplo n.º 12
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.º 13
0
 /**
  * Returns true if this assignment is consistent as well as complete with
  * respect to the given CSP.
  */
 public bool isSolution(CSP csp)
 {
     return isConsistent(csp.getConstraints())
             && isComplete(csp.getVariables());
 }
        // //////////////////////////////////////////////////////////////
        // 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;
        }
 private int countLostValues(Variable var, Object value, CSP csp)
 {
     int result = 0;
     Assignment assignment = new Assignment();
     assignment.setAssignment(var, value);
     foreach (Constraint 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;
 }
 /** : the degree heuristic. */
 private List<Variable> applyDegreeHeuristic(List<Variable> vars,
         Assignment assignment, CSP csp)
 {
     List<Variable> result = new List<Variable>();
     int maxDegree = int.MIN_VALUE;
     foreach (Variable var in vars)
     {
         int degree = 0;
         foreach (Constraint constraint in csp.getConstraints(var))
         {
             Variable neighbor = csp.getNeighbor(var, constraint);
             if (!assignment.hasAssignmentFor(neighbor)
                     && csp.getDomain(neighbor).Count > 1)
                 ++degree;
         }
         if (degree >= maxDegree)
         {
             if (degree > maxDegree)
             {
                 result.clear();
                 maxDegree = degree;
             }
             result.Add(var);
         }
     }
     return result;
 }