private static List <OverFloorType> MAC3HeadTailChecker(Variable headVar, Variable tailVar, CSP problem) { var ret = new List <OverFloorType>(); //Loop remaining domain values var remainingDomains = tailVar.remDomain.ToList(); foreach (var remDomTail in remainingDomains) { int validTailValue = 0; problem.AssignValue(tailVar, remDomTail, false); var remDomainHead = headVar.remDomain.ToList(); foreach (var remDomHead in remDomainHead) { var alreadyAssigned = problem.IsVariableAssigned(headVar); if (!alreadyAssigned) { problem.AssignValue(headVar, remDomHead, false); } bool valid = problem.ValidateConstraints(); if (!valid) { validTailValue++; break; } if (!alreadyAssigned) { problem.RemoveValue(headVar); } } if (validTailValue == remDomainHead.Count()) { ret.Add(remDomTail); } problem.RemoveValue(tailVar); } return(ret); }
/// <summary> /// Loop all variables and values searching for a solution for the CSP /// </summary> /// <param name="problem"></param> /// <returns></returns> public static List <Assignment> RecursiveBacktrackingSearch(CSP problem) { if (problem.Goal()) { Debug.Log("Goal completed!"); return(problem.GetAssignments()); } //Loop unassigned var orderedVars = problem.OrderVariablesByMinimumRemainingValues(); foreach (var variable in orderedVars) { Debug.Log("Variable evaluated:" + variable.pos.ToString()); var orderedAssignments = problem.OrderAssignmentsByLeastRestrictingValues(variable); foreach (var assign in orderedAssignments) { Debug.Log("Value evaluated:" + assign.value.ToString()); //Assign value bool validResult = problem.AssignValue(assign.variable, assign.value); if (!validResult) { throw new Exception("Error getting available values"); } List <Assignment> validAssignments = null; validAssignments = RecursiveBacktrackingSearch(new CSP(problem)); if (validAssignments != null) { return(validAssignments); } else { Debug.Log("Backtracking value:" + assign.value.ToString()); problem.RemoveValue(assign.variable); } } } Debug.Log("Backtracking"); return(null); }
private static List<OverFloorType> MAC3HeadTailChecker(Variable headVar, Variable tailVar, CSP problem) { var ret = new List<OverFloorType>(); //Loop remaining domain values var remainingDomains = tailVar.remDomain.ToList(); foreach (var remDomTail in remainingDomains) { int validTailValue = 0; problem.AssignValue(tailVar, remDomTail, false); var remDomainHead = headVar.remDomain.ToList(); foreach (var remDomHead in remDomainHead) { var alreadyAssigned = problem.IsVariableAssigned(headVar); if(!alreadyAssigned)problem.AssignValue(headVar, remDomHead, false); bool valid = problem.ValidateConstraints(); if (!valid) { validTailValue++; break; } if (!alreadyAssigned) problem.RemoveValue(headVar); } if (validTailValue == remDomainHead.Count()) ret.Add(remDomTail); problem.RemoveValue(tailVar); } return ret; }
/// <summary> /// Loop all variables and values searching for a solution for the CSP /// </summary> /// <param name="problem"></param> /// <returns></returns> public static List<Assignment> RecursiveBacktrackingSearch(CSP problem) { if (problem.Goal()) { Debug.Log("Goal completed!"); return problem.GetAssignments(); } //Loop unassigned var orderedVars = problem.OrderVariablesByMinimumRemainingValues(); foreach (var variable in orderedVars) { Debug.Log("Variable evaluated:" + variable.pos.ToString()); var orderedAssignments = problem.OrderAssignmentsByLeastRestrictingValues(variable); foreach (var assign in orderedAssignments) { Debug.Log("Value evaluated:" + assign.value.ToString()); //Assign value bool validResult = problem.AssignValue(assign.variable, assign.value); if (!validResult) throw new Exception("Error getting available values"); List<Assignment> validAssignments = null; validAssignments = RecursiveBacktrackingSearch(new CSP(problem)); if (validAssignments != null) { return validAssignments; } else { Debug.Log("Backtracking value:" + assign.value.ToString()); problem.RemoveValue(assign.variable); } } } Debug.Log("Backtracking"); return null; }