CSCE475/875 Multiagent Systems

Handout 1.  Backtracking Search for CSPs

August 25, 2011

(Based on Russell, S. and P. Norvig (2010) (3rd. Edition) Artificial Intelligence: A Modern Approach, Upper Saddle River, NJ: Pearson Education.)

 

 

1.         The Basic Backtracking Search for CSP

 

The term backtracking search (A*!) is used for a DFS that chooses values for one variable at a time and backtracks when a variable has no legal values left to assign.

 

Function BACKTRACKING-SEARCH(csp) returns a solution, or failure

   Return RECURSIVE-BACKTRACKING({},csp)

End function

 

Function RECURSIVE-BACKTRACKING(assignment,csp) returns a solution, or failure

If assignment is complete then return assignment

var ß SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp)

for each value in ORDER-DOMAIN-VALUES(var,assignment,csp) do

    add {var = value} to assignment

    result ß RECURSIVE-BACKTRACKING(assignment,csp)

    if result <> failure then return result

    remove {var = value} from assignment

end for loop

return failure

End Function

2.         But, Here Are the Questions …

 

We need to find general-purpose methods that address the following questions:

1.      Which variable should be assigned next, and in what order should its values be tried? (ORDER-DOMAIN-VALUES and SELECT-UNASSIGNED-VARIABLES)

2.      What are the implications of the current variable assignments for the other unassigned variables?

3.      When a path fails—that is, a state is reached in which a variable has no legal values—can the search avoid repeating this failure in subsequent paths?

 

2.1.      Variable and Value Ordering

 

By default, SELECT-UNASSIGNED-VARIABLE simply selects the next unassigned variable in the order given by the list VARIABLES[csp].  However, this static variable ordering seldom results in the most efficient search.

 

The intuitive idea—choosing the variable with the fewest “legal” values—is called the minimum remaining values (MRV) heuristic, aka “most constrained variable” or “fail-first” heuristic.  If there is a variable X with zero legal values remaining, the MRV heuristic will select X and failure will be detected immediately—avoiding pointless searches through other variables which always will fail when X is finally selected.

 

The MRV heuristic doesn’t help if every variable has the same number of values.  In this case the degree heuristic comes in handy.  It attempts to reduce the branching factor on future choices by selecting the variable that is involved in the largest number of constraints on other unassigned variables.  MRV is powerful, degree as a tie-breaker.

 

Once a variable has been selected, choose the value—least-constraining-value heuristic.  It prefers the value that rules out the fewest choices for the neighboring variables in the constraint graph.

 

2.2.      Propagating Information through Constraints

 

So far, our search algorithm considers the constraints on a variable only at the time that the variable is chosen by SELECT-UNASSIGNED-VARIABLE.  But by looking at some of the constraints earlier in the search, or even before the search has started, we can drastically reduce the search space.

 

·         Forward Checking.  Whenever a variable X is assigned, the forward checking process looks at each unassigned variable Y that is connected to X by a constraint and deletes from Y’s domain any value that is inconsistent with the value chosen for X.  The MRV is a natural partner for forward checking.  Forward checking can detect partial assignments that are inconsistent with the constraints of the problem, and the algorithm will therefore backtrack immediately.

 

·         Constraint Propagation.  Although forward checking detects many inconsistencies, it does not detect all of them because it does not look far enough. One option is to utilize arc-consistency.  An arc is a directed arc in the constraint graph.  The arc between X and Y is consistent if, for every value x of X, there is some value y of Y that is consistent with x.

 

Function AC-3(csp) returns the CSP, possibly with reduced domains

    Inputs: csp, a binary CSP with variables

    Local variables: queue, a queue of arcs, initially all the arcs in csp

    while queue is not empty do

         ßREMOVE-FIRST(queue)

          If REMOVE-INCONSISTENT-VALUES() then

               For each  in NEIGHBORS[]- do

                    Add () to queue

               End for

      End while

  End function

 

Function REMOVE-INCONSISTENT-VALUES() returns true iff remove a value

                   removed ß false

                   for each x in DOMAIN[] do

                        if no value y in DOMAIN[] allows (x,y) to satisfy the constraint btw. &

                        Then delete x from DOMAIN[]; removed ß true

                   End for

                   Return removed

             End function

 

After applying AC-3, either every arc is arc-consistent, or some variable has an empty domain, indicating that the CSP cannot be made arc-consistent (and thus the CSP cannot be solved).

 

3.         Local Search for Constraint Satisfaction Problems

The min conflicts algorithm is a search algorithm to solve constraint satisfaction problems (CSP problems).

It assigns random values to all the variables of a CSP. Then it selects randomly a variable, whose value conflicts with any constraint of the CSP. Then it assigns to this variable the value with the minimum conflicts. If there are more than one minimum, it chooses one among them randomly. After that, a new iteration starts again until a solution is found or a pre-selected maximum number of iterations is reached.

Because a CSP can be interpreted as a local search problem when all the variables have assigned a value (complete states), the min conflicts algorithm can be seen as a heuristic that chooses the state with the minimum number of conflicts.

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 assignment for csp

    for i = 1 to max_steps do

        if current is a solution of csp then return current

        var ß a randomly chosen, conflicted variable from VARIABLES[csp]

        value ß the value v for var that minimizes CONFLICTS(var,v,current,csp)

        set var = value in current

 return failure