/** * Stores the specified domain for the specified variable if a domain has * not yet been stored for the variable. */ public void storeDomainFor(Variable var, Domain domain) { if (!affectedVariables.contains(var)) { savedDomains.Add(new Pair<Variable, Domain>(var, domain)); affectedVariables.Add(var); } }
public NotEqualConstraint(Variable var1, Variable var2) { this.var1 = var1; this.var2 = var2; scope = new List<Variable>(2); scope.Add(var1); scope.Add(var2); }
public void removeAssignment(Variable var) { if (hasAssignmentFor(var)) { variables.remove(var); variableToValue.remove(var); } }
/** * Replaces the domain of the specified variable by new domain, * which contains all values of the old domain except the specified * value. */ public void removeValueFromDomain(Variable var, Object value) { Domain currDomain = getDomain(var); List<Object> values = new List<Object>(currDomain.Count); foreach (Object v in currDomain) if (!v.Equals(value)) values.Add(v); setDomain(var, new Domain(values)); }
/** * 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(); }
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; }
/** : the least constraining value heuristic. */ private List<Object> applyLeastConstrainingValueHeuristic(Variable var, CSP csp) { List<Pair<Object, int>> pairs = new List<Pair<Object, int>>(); foreach (Object value in csp.getDomain(var)) { int num = countLostValues(var, value, csp); pairs.Add(new Pair<Object, int>(value, num)); } // TODO //Collections.sort(pairs, new Comparator<Pair<Object, int>>() { // public int compare(Pair<Object, int> o1, // Pair<Object, int> o2) { // return o1.getSecond() < o2.getSecond() ? -1 // : o1.getSecond() > o2.getSecond() ? 1 : 0; // } //}); List<Object> result = new List<Object>(); foreach (Pair<Object, int> pair in pairs) result.Add(pair.getFirst()); return result; }
public Domain getDomain(Variable var) { return domains.get(varIndexHash.get(var)); }
/** * Returns for binary constraints the other variable from the scope. * * @return a variable or null for non-binary constraints. */ public Variable getNeighbor(Variable var, Constraint constraint) { List<Variable> scope = constraint.getScope(); if (scope.Count == 2) { if (var == scope.get(0)) return scope.get(1); else if (var == scope.get(1)) return scope.get(0); } return null; }
public bool hasAssignmentFor(Variable var) { return(variableToValue.get(var) != null); }
public void setAssignment(Variable var, Object value) { if (!variableToValue.containsKey(var)) variables.Add(var); variableToValue.put(var, value); }
public Object getAssignment(Variable var) { return variableToValue.get(var); }
/** * 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; }
public Object getAssignment(Variable var) { return(variableToValue.get(var)); }
public bool hasAssignmentFor(Variable var) { return variableToValue.get(var) != null; }
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; }
/** * Returns true if this assignment assigns values to every variable of * <code>vars</code>. */ public bool isComplete(Variable[] vars) { foreach (Variable var in vars) { if (!hasAssignmentFor(var)) return false; } return true; }
public int indexOf(Variable var) { return varIndexHash.get(var); }
/** * 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); }
public void setDomain(Variable var, Domain domain) { domains.set(indexOf(var), domain); }
/** * 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(); }
/** * Returns all constraints in which the specified variable participates. */ public List<Constraint> getConstraints(Variable var) { return cnet.get(var); }
/** * 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(); } }