240 likes | 425 Vues
The n queens problem. Many solutions to a classic problem: On an n x n chess board, place n queens so no queen threatens another. Minimal example – 4 queens. Queen threatens next piece in any row, column or diagonal. 4 x 4 Board. Problem: Place 4 queens so no queen is threatened.
E N D
The n queens problem Many solutions to a classic problem: On an n x n chess board, place n queens so no queen threatens another
Minimal example – 4 queens Queen threatens next piece in any row, column or diagonal 4 x 4 Board Problem: Place 4 queens so no queen is threatened
What kind of problem? Design state space for search nodes – how to represent state edges – what transformations to neighbouring state e.g., state: positions of four queens edge: move any queen to new position
Possible state spaces Every state has four queens: Neighbour state has one queen in different position
formulation Start state is undecided: • Random? • Guess based on knowledge? Form of graph – all states are potential solutions Edges for neighbour relationship are undirected
formulation How many states? • Any queen anywhere: 164 = 65536 (n2)n • Queens on different squares: 16x15x14x13 = 43680 n2!/(n2-n)! • Queens in separate columns: 44 = 256 nn • Queens in separate cols, rows: 4x3x2x1 = 24 n!
1. Complete state formulation What actions, branching factor • Move a queen anywhere: 4x16= 64 n x n2 • Move queen to open space: 4x12 = 48 n x (n2-n) • Move queen in column: 4x3 = 12 n x (n-1) • Exchange rows of two queens: 4x3/2 = 6 n(n-1)/2
Fitness function: no queen threatened Operationalizing Heuristics for evaluating states: how (relatively) bad is the threat situation? • Number of unthreatened queens • Total pairwise threats Depends on the representation; e.g., need to count column threats?
Finding solution: local search repeat create random state find local optimum based on fitness function heuristic (e.g.,max number of unthreatened queens) until fitness function satisfied
Partial-solution spaces Every state has 0,1,2,3 or 4 queens Edges lead to states with one more queen
Partial state formulation Start state is fixed: • Empty board Form of graph – hierarchical, directed, multi-partite Actions/changes are directed edges that add one more queen into destination state
Partial state formulation How many states? Any queen anywhere: 160+161+162+163+164 i=0,n (n2)i = 69905 (c/w 65536) Any queen on empty square: 47296 (43680) i=0,nn2!/(n2-n)! Queens in separate columns: 341 (256) i=0,n ni Queens in separate rows/cols: 40 (24) i=1,n n!/(n-i)!
Partial state formulation How many neighbours: branching factor • Place a queen anywhere: 4x4= 16 n2 • Place queen on open space: 12 < b ≤ 16 n(n-1)<b≤n2 • Place queen in column: 4 n • Place queen in col, row: 0 < b ≤ 4 0 < b ≤ n
Partial state formulation Heuristics for evaluating states: is there a threat? e.g., Total pairwise threats: 0 0 Number of unthreatened spaces 1 2
Partial formulation Search is globally controlled – spanning tree - Better for ‘optimizing’ - finding multiple solutions and choosing best
Example algorithm from Peter Alfeld <http://www.apl.jhu.edu/~hall/NQueens.html> Analyze this algorithm • Complete / incremental? • How many states? • Branching factor? • Heuristic evaluation of fitness?
Example algorithm from Peter Alfeld <http://www.apl.jhu.edu/~hall/NQueens.html> • Complete / incremental? • How many states? • Branching factor? • Heuristic evaluation? • Incremental • O(Σi=0,n ni) • n • No conflict
An O(n) algorithmic solution ACM SIGART Bulletin, 2(2), page 22-24, 1991 -finds one arrangement of queens solution by Marty Hall based on this algorithm: O(n) algorithm