private void TaskOnCellValueChanged(int value, int row, int column) { // the variables are inserted in the list in order, so this should // work as long as the way to insert them doesn't change Variable <int> variable = csp.GetVariables().ElementAt(9 * row + column); var result = (value != 0) ? Solver.UpdateVariable(csp, initial_assignment, variable, value) : Solver.RemoveVariable(csp, initial_assignment, variable, new Domain <int>(1, 2, 3, 4, 5, 6, 7, 8, 9)); }
private async Task <Assignment <Tval> > Backtrack(ConstraintSatisfactionProblem <Tval> csp, Assignment <Tval> assignment) { if (assignment.IsComplete(csp.GetVariables())) { return(assignment); } Variable <Tval> variable = variableSelectionStrategy.SelectUnassignedVariable(csp, assignment); foreach (Tval value in domainValueSelectionStragety.getOrderedDomainValues(variable, assignment, csp)) { assignment.Assign(variable, value); OnVariableAssigned(variable, value); InferenceResults <Tval> inference = new InferenceResults <Tval>(); inference.StoreDomainForVariable(variable, variable.GetDomain()); variable.UpdateDomain(new Domain <Tval>(value)); // it should already be consistent if it reached this point // since we make a pre-solving inference //bool isConsistent = assignment.IsConsistent(csp.GetAllDiffConstraints()); //if (isConsistent) if (true) { inference = await inferenceStrategy.Infer(csp, variable, value, inference); if (inference.IsAssignmentConsistent()) { Assignment <Tval> result = await Backtrack(csp, assignment); if (result != null) { return(result); } } } assignment.RemoveAssignment(variable); OnAssignmentRemoved(variable, value); inference.RestoreOldDomains(); } return(null); }
public override Variable <Tval> SelectUnassignedVariable(ConstraintSatisfactionProblem <Tval> csp, Assignment <Tval> assignment) { HashSet <Variable <Tval> > unassigned_variables = new HashSet <Variable <Tval> >(csp.GetVariables()); unassigned_variables.ExceptWith(assignment.GetAssignedVariables()); Variable <Tval> variable = unassigned_variables.First(); foreach (Variable <Tval> v in unassigned_variables) { variable = (variable.GetDomain().Size() < v.GetDomain().Size()) ? variable : v; } return(variable); }