Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Solving by Search

(N-Queens Problem)

Credit: Pedro Meseguer



Overview

Systematic Search
•  MiniZinc model for 4-queens
•  Search Tree
•  Backtracking (BT)
•  Conflict-Directed Backjumping (CBJ)
•  Dynamic Backtracking (DB)
•  Heuristics


CSP: Search
4-queens in MiniZinc
1 2 3 4
Formulation:
•  Variables: one queen per row x1
•  Domains: available columns x2
•  Constraints: x3
different columns and different diagonals
x4
xi ≠ xj | xi - xj | ≠ | i - j |
4-queens
array[1..4] of var 1..4: BOARD; % variables and domains

constraint forall(row1 in 1..4)(



forall(row2 in 1..4 where row1<row2)(

BOARD[row1]!=BOARD[row2] /\ 

abs(BOARD[row1]-BOARD[row2])!=abs(row1-row2))); 

solve satisfy;
CSP: Search
Search Tree
State space: explored as a tree
•  root: empty x
•  one variable per level
•  sucessors of a node: a b c
•  one sucessor per value of the next level variable
•  meaning: variable ← value

Tree:
•  each branch defines an assignment
•  depth n (number of variables)
•  branching factor d (domain size)
CSP: Search
Search tree for 4-queens

1 2 3 4
x1

x2

x3

x4
(1,1,1,1) (2,1,1,1) (3,1,1,1) (4,1,1,1) (4,4,4,4)

CSP: Search
Backtracking Algorithm
Depth-first tree traversal (DFS)

At each node:
•  check every completely assigned constraint
•  if consistent, continue DFS
•  otherwise, prune current branch
•  continue DFS

Complexity: O(dn)

CSP: Search
Backtracking on 4-queens

x1 1 2

1 2 3 4

x2 1 2 3 4 1 2 3 4
x1 Q Q
x2 Q Q Q Q
x3 1 2 3 4 1 2 3 4 1 x3 Q Q Q
Q Q
Q
x4 Q Q Q
Q Q
x4 1 2 3 4 1 2 3

25 nodes solution
CSP: Search
Problems of Backtracking
Thrashing: the first choice is
•  the same failure can incompatible with
be rediscovered an any last choice
exponential number 

of times

Improvements:
•  check not completely assigned constraints: propagation
•  jumping to the source of failure: non-chronological 

backtracking

CSP: Search
Non-chronological Backtracking

X1 Backtrack to the

X2 last culprit variable: X4
X3
X4 X1
X5 1
X6 dead-end X2
3
X7
X8 5 X3

2 X4
Changing X5 does NOT 

4
remove the dead-end X5

CSP: Search
X6
Conflict Set
CS(xk): assig. variables in conflict with some value of xk

X1 {} •  Backtrack: jumps to the 



X2 1 1 {1} last variable in CS(xk)

X3 1 2 1 2 {1,2}
X4 1 {1} •  CS(xk) is backed-up:
X5 1 4 2
{1,2,4}
xk → xi
X6 1 3 2 4 3 1 2 3
{1,2,3,4} CS(xi)=CS(xi)∪CS(xk) - {xi}

X7
X8 •  conflicts of xk with var.
before xi are passed to xi
CSP: Search
Conflict-Directed Backjumping
Non-chronological backtracking:
•  jumps to the last variable in the conflict-set
•  the conflict set is backed-up
•  intermediate decisions are removed

{x1 ← 1} 1 2 3 4

x1 Q1
{x3 ← 2} {x1}
x2 Q3
x3 Q2 {x1}
{x2 ← 4}
x4 x1 x3 x3 x1 {x1, x3}

x4 CSP: Search
Nogoods
Nogood: subset of incompatible assignments
•  original constraints
•  during search, new nogoods are discovered


Example: map colouring, x1, x2, x3 adjacent, D = {a,b}


¬(x1 = a ∧ x3 = a) or equivalently
x1 = a → x3 ≠ a
lhs rhs
last variable in lhs
Nogood resolution: new nogood
x1 = a → x3 ≠ a
x1 = a → x2 ≠ b
x2 = b → x3 ≠ b
CSP: Search
every value for x3
Dynamic Backtracking
Non-chronological backtracking:
•  one nogood per each incompatible value
•  empty domain: new ng by nogood resolution
•  backtrack to the variable in rhs(ng)
{x1 ← 1}
1 2 3 4
Q1 x1=1 → x4 ≠ 1 

{x3 ← 2} x1 x3=2 → x4 ≠ 2 

x2 Q3 x3=2 → x4 ≠ 3 

x1=1 → x4 ≠ 4
{x2 ← 4} x3 Q2
x4 x1=1x3=2x3=2 x1=1
ng: x1=1 → x3 ≠ 2
x4 CSP: Search
Dynamic Backtracking (II)
Non-chronological backtracking:
•  jumps to the last decision responsible for the dead-end
•  intermediate decisions are NOT removed

{x1 ← 1} {x1 ← 1}
1 2 3 4
x1=1→x3≠2
{x3 ← 2} x1 Q1 {x2 ← 4}
Q3 new tree
x2
{x2 ← 4} x3 xQ
1=1
2
x4 x1=1x3=2x3=2 x1=1
x4
CSP: Search
Variable Ordering
Static variable ordering: variable associated with level

X1


X2


X3


 ... ... ... ...
X4
... ... ... ...
Not needed for an exhaustive tree

Dynamic variable ordering:


•  each branch considers every var but not in the same order

X1
X2 X3 X4 X4
X3 ... X4 ... X3 ... X2 ...
X4 ... X2 ... X2 ... X3 ...
CSP: Search
Variable Selection
Node q, what is the next variable to assign?

1. There is a solution in succ(q): any var is fine


2. There is no solution in succ(q): assign var that 

sooner detects that there is no solution


What is more often?


•  Except trivial problems, case 2
•  Biggest effort: escaping from subproblems 

without solution

Fail-first: first the variable that sooner detects failure



CSP: Search
Heuristics
Min-domain:
•  First the variable with less compatible values
with the current partial solution
•  Minimize search tree size

Max-degree:
•  First the variable involved in more constraints
•  Maximize constraint propagation

Combination:
•  First the variable with min domain/degree
CSP: Search

You might also like