/** * Ignores constraints which are not binary. */ private int countLostValues(CSP <VAR, VAL> csp, Assignment <VAR, VAL> assignment, VAR var, VAL value) { int result = 0; Assignment <VAR, VAL> assign = new Assignment <VAR, VAL>(); assign.add(var, value); foreach (IConstraint <VAR, VAL> constraint in csp.getConstraints(var)) { if (constraint.getScope().Size() == 2) { VAR neighbor = csp.getNeighbor(var, constraint); if (!assignment.contains(neighbor)) { foreach (VAL nValue in csp.getDomain(neighbor)) { assign.add(neighbor, nValue); if (!constraint.isSatisfiedWith(assign)) { ++result; } } } } } return(result); }
public override Assignment <VAR, VAL> solve(CSP <VAR, VAL> csp) { Assignment <VAR, VAL> assignment = new Assignment <VAR, VAL>(); // Select a root from the List of Variables VAR root = _useRandom ? Util.selectRandomlyFromList(csp.getVariables()) : csp.getVariables().Get(0); // Sort the variables in topological order ICollection <VAR> orderedVars = CollectionFactory.CreateQueue <VAR>(); IMap <VAR, IConstraint <VAR, VAL> > parentConstraints = CollectionFactory.CreateInsertionOrderedMap <VAR, IConstraint <VAR, VAL> >(); topologicalSort(csp, root, orderedVars, parentConstraints); if (csp.getDomain(root).isEmpty()) { return(null); // CSP has no solution! (needed if orderedVars.size() == 1) } // Establish arc consistency from top to bottom (starting at the bottom). csp = csp.copyDomains(); // do not change the original CSP! for (int i = orderedVars.Size() - 1; i > 0; i--) { VAR var = orderedVars.Get(i); IConstraint <VAR, VAL> constraint = parentConstraints.Get(var); VAR parent = csp.getNeighbor(var, constraint); if (makeArcConsistent(parent, var, constraint, csp)) { fireStateChanged(csp, null, parent); if (csp.getDomain(parent).isEmpty()) { return(null); // CSP has no solution! } } } // Assign values to variables from top to bottom. for (int i = 0; i < orderedVars.Size(); ++i) { VAR var = orderedVars.Get(i); foreach (VAL value in csp.getDomain(var)) { assignment.add(var, value); if (assignment.isConsistent(csp.getConstraints(var))) { fireStateChanged(csp, assignment, var); break; } } } return(assignment); }
private ISet <VAR> getConflictedVariables(Assignment <VAR, VAL> assignment, CSP <VAR, VAL> csp) { ISet <VAR> result = CollectionFactory.CreateSet <VAR>(); foreach (IConstraint <VAR, VAL> constraint in csp.getConstraints()) { if (!constraint.isSatisfiedWith(assignment)) { foreach (VAR var in constraint.getScope()) { if (!result.Contains(var)) { result.Add(var); } } } } return(result); }
/** Returns variables from <code>vars</code> which are the best with respect to DEG. */ public ICollection <VAR> apply(CSP <VAR, VAL> csp, ICollection <VAR> vars) { ICollection <VAR> result = CollectionFactory.CreateQueue <VAR>(); int maxDegree = -1; foreach (VAR var in vars) { int degree = csp.getConstraints(var).Size(); if (degree >= maxDegree) { if (degree > maxDegree) { result.Clear(); maxDegree = degree; } result.Add(var); } } return(result); }
/** * Computes an explicit representation of the tree structure and a total order which is consistent with the * parent-child relations. If the provided CSP has not the required properties (CSP contains only binary * constraints, constraint graph is tree-structured and connected), an exception is thrown. * * @param csp A CSP * @param root A root variable * @param orderedVars The computed total order (initially empty) * @param parentConstraints The tree structure, maps a variable to the constraint representing the arc to the parent * variable (initially empty) */ private void topologicalSort(CSP <VAR, VAL> csp, VAR root, ICollection <VAR> orderedVars, IMap <VAR, IConstraint <VAR, VAL> > parentConstraints) { orderedVars.Add(root); parentConstraints.Put(root, null); int currParentIdx = -1; while (currParentIdx < orderedVars.Size() - 1) { currParentIdx++; VAR currParent = orderedVars.Get(currParentIdx); int arcsPointingUpwards = 0; foreach (IConstraint <VAR, VAL> constraint in csp.getConstraints(currParent)) { VAR neighbor = csp.getNeighbor(currParent, constraint); if (neighbor == null) { throw new IllegalArgumentException("Constraint " + constraint + " is not binary."); } if (parentConstraints.ContainsKey(neighbor)) { // faster than orderedVars.contains(neighbor)! arcsPointingUpwards++; if (arcsPointingUpwards > 1) { throw new IllegalArgumentException("CSP is not tree-structured."); } } else { orderedVars.Add(neighbor); parentConstraints.Put(neighbor, constraint); } } } if (orderedVars.Size() < csp.getVariables().Size()) { throw new IllegalArgumentException("Constraint graph is not connected."); } }
private VAL getMinConflictValueFor(VAR var, Assignment <VAR, VAL> assignment, CSP <VAR, VAL> csp) { ICollection <IConstraint <VAR, VAL> > constraints = csp.getConstraints(var); Assignment <VAR, VAL> testAssignment = assignment.Clone(); int minConflict = int.MaxValue; ICollection <VAL> resultCandidates = CollectionFactory.CreateQueue <VAL>(); foreach (VAL value in csp.getDomain(var)) { testAssignment.add(var, value); int currConflict = countConflicts(testAssignment, constraints); if (currConflict <= minConflict) { if (currConflict < minConflict) { resultCandidates.Clear(); minConflict = currConflict; } resultCandidates.Add(value); } } return((!resultCandidates.IsEmpty()) ? Util.selectRandomlyFromList <VAL>(resultCandidates) : default(VAL)); }
/// <summary> /// Template method, which can be configured by overriding the three primitive operations below. /// </summary> /// <param name="csp"></param> /// <param name="assignment"></param> /// <returns>An assignment (possibly incomplete if task was cancelled) or null if no solution was found.</returns> private Assignment <VAR, VAL> backtrack(CSP <VAR, VAL> csp, Assignment <VAR, VAL> assignment) { Assignment <VAR, VAL> result = null; if (assignment.isComplete(csp.getVariables()) || currIsCancelled) { result = assignment; } else { VAR var = selectUnassignedVariable(csp, assignment); foreach (VAL value in orderDomainValues(csp, assignment, var)) { assignment.add(var, value); fireStateChanged(csp, assignment, var); if (assignment.isConsistent(csp.getConstraints(var))) { IInferenceLog <VAR, VAL> log = inference(csp, assignment, var); if (!log.isEmpty()) { fireStateChanged(csp, null, null); } if (!log.inconsistencyFound()) { result = backtrack(csp, assignment); if (result != null) { break; } } log.undo(csp); } assignment.remove(var); } } return(result); }
/// <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(CSP <VAR, VAL> csp) { return(isConsistent(csp.getConstraints()) && isComplete(csp.getVariables())); }