/**
  * 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;
 }
        private Assignment generateRandomAssignment(CSP csp) {
		Assignment assignment = new Assignment();
        foreach (Variable var in csp.getVariables())
        {
			Object randomValue = Util.selectRandomlyFromList(csp.getDomain(var)
					.asList());
			assignment.setAssignment(var, randomValue);
		}
		return assignment;
	}
        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;
	}
 /**
  * Primitive operation, selecting a not yet assigned variable.
  */
 protected override Variable selectUnassignedVariable(Assignment assignment, CSP csp)
 {
     switch (selectionStrategy)
     {
         case MRV:
             return applyMRVHeuristic(csp, assignment).get(0);
         case MRV_DEG:
             List<Variable> vars = applyMRVHeuristic(csp, assignment);
             return applyDegreeHeuristic(vars, assignment, csp).get(0);
         default:
             foreach (Variable var in csp.getVariables())
             {
                 if (!(assignment.hasAssignmentFor(var)))
                     return var;
             }
     }
     return null;
 }
 protected void fireStateChanged(Assignment assignment, CSP csp)
 {
     foreach (CSPStateListener listener in listeners)
         listener.stateChanged(assignment.copy(), csp.copyDomains());
 }
Beispiel #6
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;
 }
 /**
  * Primitive operation, selecting a not yet assigned variable. This default
  * implementation just selects the first in the ordered list of variables
  * provided by the CSP.
  */
 protected Variable selectUnassignedVariable(Assignment assignment, CSP csp)
 {
     foreach (Variable var in csp.getVariables())
     {
         if (!(assignment.hasAssignmentFor(var)))
             return var;
     }
     return null;
 }
 /**
  * Primitive operation, which tries to prune out values from the CSP which
  * are not possible anymore when extending the given assignment to a
  * solution. This default implementation just leaves the original CSP as it
  * is.
  * 
  * @return An object which provides informations about (1) whether changes
  *         have been performed, (2) possibly inferred empty domains , and
  *         (3) how to restore the domains.
  */
 protected DomainRestoreInfo inference(Variable var, Assignment assignment,
         CSP csp)
 {
     return new DomainRestoreInfo().compactify();
 }
        // //////////////////////////////////////////////////////////////
        // heuristics for selecting the next unassigned variable and domain ordering

        /** : the minimum-remaining-values heuristic. */
        private List<Variable> applyMRVHeuristic(CSP csp, Assignment assignment)
        {
            List<Variable> result = new List<Variable>();
            int mrv = int.MAX_VALUE;
            foreach (Variable var in csp.getVariables())
            {
                if (!assignment.hasAssignmentFor(var))
                {
                    int num = csp.getDomain(var).Count;
                    if (num <= mrv)
                    {
                        if (num < mrv)
                        {
                            result.clear();
                            mrv = num;
                        }
                        result.Add(var);
                    }
                }
            }
            return result;
        }
 public override bool isSatisfiedWith(Assignment assignment)
 {
     Object value1 = assignment.getAssignment(var1);
     return value1 == null || !value1.Equals(assignment.getAssignment(var2));
 }
 /**
  * Primitive operation, ordering the domain values of the specified
  * variable.
  */
 protected override Iterable? orderDomainValues(Variable var,
         Assignment assignment, CSP csp)
 {
     if (!isLCVHeuristicEnabled)
     {
         return csp.getDomain(var);
     }
     else
     {
         return applyLeastConstrainingValueHeuristic(var, csp);
     }
 }
        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;
        }
 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;
 }
        private int countConflicts(Assignment assignment,
                List<Constraint> constraints) {
		int result = 0;
		foreach (Constraint constraint in constraints)
			if (!constraint.isSatisfiedWith(assignment))
				result++;
		return result;
	}
Beispiel #17
0
 public Assignment copy()
 {
     Assignment copy = new Assignment();
     foreach (Variable var in variables)
     {
         copy.setAssignment(var, variableToValue.get(var));
     }
     return copy;
 }
 /**
  * Primitive operation, ordering the domain values of the specified
  * variable. This default implementation just takes the default order
  * provided by the CSP.
  */
 protected Iterable? orderDomainValues(Variable var,
         Assignment assignment, CSP csp)
 {
     return csp.getDomain(var);
 }
        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;
	}
 /**
  * Primitive operation, which tries to prune out values from the CSP which
  * are not possible anymore when extending the given assignment to a
  * solution.
  * 
  * @return An object which provides informations about (1) whether changes
  *         have been performed, (2) possibly inferred empty domains , and
  *         (3) how to restore the domains.
  */
 protected override DomainRestoreInfo inference(Variable var, Assignment assignment,
         CSP csp)
 {
     switch (inferenceStrategy)
     {
         case FORWARD_CHECKING:
             return doForwardChecking(var, assignment, csp);
         case AC3:
             return new AC3Strategy().reduceDomains(var, assignment
                     .getAssignment(var), csp);
         default:
             return new DomainRestoreInfo().compactify();
     }
 }