private bool Revise(Variable xi, Variable xj, IConstraint constraint, CSProblem csp, DomainRestoreInfo info) { bool revised = false; var assignment = new Assignment(); foreach (object vValue in csp.GetDomain(xi)) { assignment.SetAssignment(xi, vValue); bool vValueOk = false; foreach (var nValue in csp.GetDomain(xj)) { assignment.SetAssignment(xj, nValue); if (constraint.IsSatisfiedWith(assignment)) { vValueOk = true; break; } } if (!vValueOk) { info.StoreDomainFor(xi, csp.GetDomain(xi)); csp.RemoveValueFromDomain(xi, vValue); revised = true; } } return(revised); }
// ////////////////////////////////////////////////////////////// // inference algorithms /// <summary> /// Implements forward checking. /// </summary> /// <param name="var"></param> /// <param name="assignment"></param> /// <param name="csp"></param> /// <returns></returns> private DomainRestoreInfo DoForwardChecking(Variable var, Assignment assignment, CSProblem csp) { DomainRestoreInfo result = new DomainRestoreInfo(); foreach (IConstraint constraint in csp.GetConstraints(var)) { IList <Variable> scope = constraint.GetScope(); if (scope.Count == 2) { foreach (Variable neighbor in constraint.GetScope()) { if (!assignment.HasAssignmentFor(neighbor)) { if (this.Revise(neighbor, constraint, assignment, csp, result)) { if (csp.GetDomain(neighbor).IsEmpty()) { result.SetEmptyDomainFound(true); return(result); } } } } } } return(result); }
/// <summary> /// Implements the degree heuristic. /// </summary> /// <param name="vars"></param> /// <param name="assignment"></param> /// <param name="csp"></param> /// <returns></returns> private IList <Variable> applyDegreeHeuristic(IList <Variable> vars, Assignment assignment, CSProblem csp) { IList <Variable> result = new List <Variable>(); int maxDegree = int.MinValue; foreach (Variable var in vars) { int degree = 0; foreach (IConstraint constraint in csp.GetConstraints(var)) { Variable neighbor = csp.GetNeighbor(var, constraint); if (!assignment.HasAssignmentFor(neighbor) && csp.GetDomain(neighbor).Size() > 1) { ++degree; } } if (degree >= maxDegree) { if (degree > maxDegree) { result.Clear(); maxDegree = degree; } result.Add(var); } } return(result); }
private bool Revise(Variable var, IConstraint constraint, Assignment assignment, CSProblem 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); }
private Assignment GenerateRandomAssignment(CSProblem csp) { Assignment assignment = new Assignment(); foreach (Variable var in csp.Variables) { object randomValue = Util.Util.SelectRandomlyFromList(csp.GetDomain(var).ToList()); assignment.SetAssignment(var, randomValue); } return(assignment); }
/// <summary> /// Primitive operation, ordering the domain values of the specified /// variable. /// </summary> /// <param name="var"></param> /// <param name="assignment"></param> /// <param name="csp"></param> /// <returns></returns> protected override IEnumerable <object> OrderDomainValues(Variable var, Assignment assignment, CSProblem csp) { if (!this.IsLcvHeuristicEnabled) { return(csp.GetDomain(var)); } else { return(this.ApplyLeastConstrainingValueHeuristic(var, csp)); } }
/// <summary> /// Implements the least constraining value heuristic. /// </summary> /// <param name="var"></param> /// <param name="csp"></param> /// <returns></returns> private IList <object> ApplyLeastConstrainingValueHeuristic(Variable var, CSProblem csp) { var pairs = (from value in csp.GetDomain(var) let num = this.CountLostValues(var, value, csp) select new Pair <object, int>(value, num)).ToList(); pairs.Sort((o1, o2) => o1.GetSecond() < o2.GetSecond() ? -1 : o1.GetSecond() > o2.GetSecond() ? 1 : 0); IList <object> result = new List <object>(); foreach (Pair <object, int> pair in pairs) { result.Add(pair.GetFirst()); } return(result); }
private int CountLostValues(Variable var, object value, CSProblem csp) { int result = 0; Assignment assignment = new Assignment(); assignment.SetAssignment(var, value); foreach (IConstraint 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 DomainRestoreInfo ReduceDomains(Variable var, object value, CSProblem 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.Push(var); result.StoreDomainFor(var, domain); csp.SetDomain(var, new Domain(new object[] { value })); this.ReduceDomains(queue, csp, result); } } else { result.SetEmptyDomainFound(true); } return(result.Compactify()); }
private object GetMinConflictValueFor(Variable var, Assignment assignment, CSProblem csp) { IList <IConstraint> constraints = csp.GetConstraints(var); Assignment duplicate = assignment.Copy(); int minConflict = int.MaxValue; IList <object> resultCandidates = new List <object>(); foreach (object value in csp.GetDomain(var)) { duplicate.SetAssignment(var, value); int currConflict = this.CountConflicts(duplicate, constraints); if (currConflict <= minConflict) { if (currConflict < minConflict) { resultCandidates.Clear(); minConflict = currConflict; } resultCandidates.Add(value); } } return(resultCandidates.Count != 0 ? Util.Util.SelectRandomlyFromList(resultCandidates) : null); }
// ////////////////////////////////////////////////////////////// // heuristics for selecting the next unassigned variable and domain ordering /// <summary> /// Implements the minimum-remaining-values heuristic. /// </summary> /// <param name="csp"></param> /// <param name="assignment"></param> /// <returns></returns> private IList <Variable> ApplyMrvHeuristic(CSProblem csp, Assignment assignment) { IList <Variable> result = new List <Variable>(); int mrv = int.MaxValue; foreach (Variable var in csp.Variables) { if (!assignment.HasAssignmentFor(var)) { int num = csp.GetDomain(var).Size(); if (num <= mrv) { if (num < mrv) { result.Clear(); mrv = num; } result.Add(var); } } } return(result); }
protected void ReduceDomains(FIFOQueue <Variable> queue, CSProblem csp, DomainRestoreInfo info) { while (!(queue.Count == 0)) { Variable var = queue.Pop(); foreach (IConstraint constraint in csp.GetConstraints(var)) { if (constraint.GetScope().Count == 2) { Variable neighbor = csp.GetNeighbor(var, constraint); if (this.Revise(neighbor, var, constraint, csp, info)) { if (csp.GetDomain(neighbor).IsEmpty()) { info.SetEmptyDomainFound(true); return; } queue.Push(neighbor); } } } } }
/// <summary> /// Primitive operation, ordering the domain values of the specified /// variable. This default implementation just takes the default order /// provided by the CSProblem. /// </summary> /// <param name="var"></param> /// <param name="assignment"></param> /// <param name="csp"></param> /// <returns></returns> protected virtual IEnumerable <object> OrderDomainValues(Variable var, Assignment assignment, CSProblem csp) { return(csp.GetDomain(var)); }