package aima.core.search.csp;
import java.util.ArrayList;
import java.util.List;
import aima.core.util.Util;
/**
* Artificial Intelligence A Modern Approach (3rd Ed.): Figure 6.8, Page 221.
*
*
*
*
* function MIN-CONFLICTS(csp, max-steps) returns a solution or failure
* inputs: csp, a constraint satisfaction problem
* max-steps, the number of steps allowed before giving up
* current = an initial complete assignment for csp
* for i = 1 to max steps do
* if current is a solution for csp then return current
* var = a randomly chosen conflicted variable from csp.VARIABLES
* value = the value v for var that minimizes CONFLICTS(var, v, current, csp)
* set var = value in current
* return failure
*
*
*
* Figure 6.8 The MIN-CONFLICTS algorithm for solving CSPs by local search. The
* initial state may be chosen randomly or by a greedy assignment process that
* chooses a minimal-conflict value for each variable in turn. The CONFLICTS
* function counts the number of constraints violated by a particular value,
* given the rest of the current assignment.
*
* @author Ruediger Lunde
* @author Mike Stampone
*/
public class MinConflictsStrategy extends SolutionStrategy {
private int maxSteps;
/**
* Constructs a min-conflicts strategy with a given number of steps allowed
* before giving up.
*
* @param maxSteps
* the number of steps allowed before giving up
*/
public MinConflictsStrategy(int maxSteps) {
this.maxSteps = maxSteps;
}
public Assignment solve(CSP csp) {
Assignment assignment = generateRandomAssignment(csp);
fireStateChanged(assignment, csp);
for (int i = 0; i < maxSteps; i++) {
if (assignment.isSolution(csp)) {
return assignment;
} else {
List