public hasAssignmentFor ( |
||
var | ||
return | bool |
/** : 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); }
// ////////////////////////////////////////////////////////////// // 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); }
/** * 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, 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; }
/** * 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); }
// ////////////////////////////////////////////////////////////// // 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); }
/** * 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; }
// ////////////////////////////////////////////////////////////// // 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; }
// ////////////////////////////////////////////////////////////// // 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; }