• Aucun résultat trouvé

ECLiPSeELearning HelmutSimonis Chapter6:SearchStrategies(N-Queens)

N/A
N/A
Protected

Academic year: 2021

Partager "ECLiPSeELearning HelmutSimonis Chapter6:SearchStrategies(N-Queens)"

Copied!
458
0
0

Texte intégral

(1)

Problem Program Naive Search Improvements

Chapter 6: Search Strategies (N-Queens)

Helmut Simonis

Cork Constraint Computation Centre Computer Science Department

University College Cork Ireland

ECLiPSe ELearning

Overview

Helmut Simonis Search Strategies 1

(2)

Problem Program Naive Search Improvements

Licence

This work is licensed under the Creative Commons

Attribution-Noncommercial-Share Alike 3.0 Unported License.

To view a copy of this license, visit http:

//creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

Helmut Simonis Search Strategies 2

(3)

Problem Program Naive Search Improvements

Outline

1 Problem

2 Program

3 Naive Search

4 Improvements

Helmut Simonis Search Strategies 3

(4)

Problem Program Naive Search Improvements

What we want to introduce

Importance of search strategy, constraints alone are not enough

Dynamic variable ordering exploits information from propagation

Variable and value choice

Hard to find strategy which works all the time search builtin, flexible search abstraction

Different way of improving stability of search routine

Helmut Simonis Search Strategies 4

(5)

Problem Program Naive Search Improvements

Example Problem

N-Queens puzzle

Rather weak constraint propagation

Many solutions, limited number of symmetries Easy to scale problem size

Helmut Simonis Search Strategies 5

(6)

Problem Program Naive Search Improvements

Outline

1 Problem

2 Program

3 Naive Search

4 Improvements

Helmut Simonis Search Strategies 6

(7)

Problem Program Naive Search Improvements

Problem Definition

8-Queens

Place 8 queens on an 8 × 8 chessboard so that no queen attacks another. A queen attacks all cells in horizontal, vertical and diagonal direction. Generalizes to boards of size N × N.

Helmut Simonis Search Strategies 7

(8)

Problem Program Naive Search Improvements

Problem Definition

8-Queens

Place 8 queens on an 8 × 8 chessboard so that no queen attacks another. A queen attacks all cells in horizontal, vertical and diagonal direction. Generalizes to boards of size N × N.

Solution for board size 8 × 8

Helmut Simonis Search Strategies 8

(9)

Problem Program Naive Search Improvements

A Bit of History

This is a rather old puzzle

Dudeney (1917) cites Nauck (1850) as source

Certain solutions for all sizes can be constructed, this is not a hard problem

Long history in AI and CP papers

Important: Haralick and Elliot (1980) describing the first-fail principle

Helmut Simonis Search Strategies 9

(10)

Problem Program Naive Search Improvements

Model

Program (Array version) Program (List Version)

Outline

1 Problem

2 Program Model

Program (Array version) Program (List Version)

3 Naive Search

4 Improvements

Helmut Simonis Search Strategies 10

(11)

Problem Program Naive Search Improvements

Model

Program (Array version) Program (List Version)

Basic Model

Cell based Model

A 0/1 variable for each cell to say if it is occupied or not Constraints on rows, columns and diagonals to enforce no-attack

N

2

variables, 6N − 2 constraints Column (Row) based Model

A 1..N variable for each column, stating position of queen in the column

Based on observation that each column must contain exactly one queen

N variables, N

2

/2 binary constraints

Helmut Simonis Search Strategies 11

(12)

Problem Program Naive Search Improvements

Model

Program (Array version) Program (List Version)

Model

assign [X 1 , X 2 , ...X N ] s.t.

∀1 ≤ i ≤ N : X i ∈ 1..N

∀1 ≤ i < j ≤ N : X i 6= X j

∀1 ≤ i < j ≤ N : X i 6= X j + i − j

∀1 ≤ i < j ≤ N : X i 6= X j + j − i

Helmut Simonis Search Strategies 12

(13)

Problem Program Naive Search Improvements

Model

Program (Array version) Program (List Version)

Main Program (Array Version)

:-module(array).

:-export(top/0).

:-lib(ic).

top:-

nqueen(8,Array), writeln(Array).

nqueen(N,Array):- dim(Array,[N]), Array[1..N] :: 1..N,

alldifferent(Array[1..N]), noattack(Array,N),

labeling(Array[1..N]).

Helmut Simonis Search Strategies 13

(14)

Problem Program Naive Search Improvements

Model

Program (Array version) Program (List Version)

Generating binary constraints

noattack(Array,N):- (for(I,1,N-1),

param(Array,N) do (for(J,I+1,N),

param(Array,I) do

subscript(Array,[I],Xi), subscript(Array,[J],Xj), D is I-J,

Xi #\= Xj+D, Xj #\= Xi+D )

).

Helmut Simonis Search Strategies 14

(15)

Problem Program Naive Search Improvements

Model

Program (Array version) Program (List Version)

Main Program (List Version)

:-module(nqueen).

:-export(top/0).

:-lib(ic).

top:-

nqueen(8,L), writeln(L).

nqueen(N,L):- length(L,N), L :: 1..N,

alldifferent(L), noattack(L), labeling(L).

Helmut Simonis Search Strategies 15

(16)

Problem Program Naive Search Improvements

Model

Program (Array version) Program (List Version)

Generating binary constraints

noattack([]).

noattack([H|T]):- noattack1(H,T,1), noattack(T).

noattack1(_,[],_).

noattack1(X,[Y|R],N):- X #\= Y+N,

Y #\= X+N, N1 is N+1,

noattack1(X,R,N1).

Helmut Simonis Search Strategies 16

(17)

Problem Program Naive Search Improvements

Outline

1 Problem

2 Program

3 Naive Search

4 Improvements

Helmut Simonis Search Strategies 17

(18)

Problem Program Naive Search Improvements

Default Strategy

1

Skip Animation

Helmut Simonis Search Strategies 18

(19)

Problem Program Naive Search Improvements

Default Strategy

1 2

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 19

(20)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 20

(21)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 21

(22)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

6 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 22

(23)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 6 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 23

(24)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 24

(25)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4

7 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 25

(26)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

2 7 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 26

(27)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

4 2 7 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 27

(28)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

4 8 2 7 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 28

(29)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

4 8 2 7

4

8 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 29

(30)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

4 8 2 7

4

2 8 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 30

(31)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

4 8 2 7

4

2 6 8 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 31

(32)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

4 8 2 7

4

2 6 8 3

3

4 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 32

(33)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

4 8 2 7

4

2 6 8 3

3 4

2 4 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 33

(34)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

4 8 2 7

4

2 6 8 3

3 4

5 2 4 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 34

(35)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5

4

2 8 6

4 5

4 8 2 7

4

2 6 8 3

3 4

5 7 2 4 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 35

(36)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 36

(37)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 6 4 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 37

(38)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4

3 6 4 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 38

(39)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8 6 4 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 39

(40)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8 6

4 7 4

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 40

(41)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8 6

4 5 3 7 4

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 41

(42)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8 6

4 5 6 3 7 4

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 42

(43)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8 6

4 5 6 8

3 7 4

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 43

(44)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7 4

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 44

(45)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 8 4

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 45

(46)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4

5 7 8 2

4 3 8

6 4 5 6 8

3 5 7

4 3 8 4

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 46

(47)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4

5 7 8 2

4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 47

(48)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4

5 7 8 2

4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 5 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 48

(49)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4

5 7 8 2

4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 2 5 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 49

(50)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 2 5 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 50

(51)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 8 2 5 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 51

(52)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5

3 8 2 5 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 52

(53)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6

5 3 6 8 2 5 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 53

(54)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6

5 3 6 8 2 4 7 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 54

(55)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6

5 3 6 8

2 4 5 2 7 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 55

(56)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 2 7 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 56

(57)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 6

2 7 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 57

(58)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3 3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 6

2 7

4 8 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 58

(59)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3 3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 6

2 7

4 5 2 8 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 59

(60)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3 3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6

5 3 6 8

2 4 5 4 6

2 7

4 5 4 2 8 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 60

(61)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3 3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 6

2 7

4 5 4 7

2 8 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 61

(62)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3 3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 6

2 7

4 5 4 7

2 5 6 8 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 62

(63)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3 3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 6

2 7

4 5 4 7

2 5 6 3 6 8 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 63

(64)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3 3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 6

2 7

4 5 4 7

2 5 6 7 7 3 6 8 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 64

(65)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3 3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 6

2 7

4 5 4 7

2 5 6 7 8 2 7 3 6 8 5

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 65

(66)

Problem Program Naive Search Improvements

Default Strategy

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3 3 4 5 7 8

2 4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6 5 3 6 8

2 4 5 4 6

2 7

4 5 4 7

2 5 6 7 8 4 2 7 3 6 8 5

1

Back to Start

Helmut Simonis Search Strategies 66

(67)

Problem Program Naive Search Improvements

First Solution

1 2 3

5 4 2 8

6 4 5 4 8

2 7

4 2 6 8

3

3 4

5 7 8 2

4 3 8

6 4 5 6 8

3 5 7

4 3 5 8 4

3 4 6

5 3 6 8

2 4 5 4 6

2 7

4 5 4 7

2 5 6 7 8 4 2 7 3 6 8 5

1

Helmut Simonis Search Strategies 67

(68)

Problem Program Naive Search Improvements

Observations

Even for small problem size, tree can become large Not interested in all details

Ignore all automatically fixed variables

For more compact representation abstract failed sub-trees

Helmut Simonis Search Strategies 68

(69)

Problem Program Naive Search Improvements

Compact Representation

Number inside triangle: Number of choices Number under triangle: Number of failures

1 2

4 7

3

6 10

4

3

2 3

2

1 2

7

4

1 2

2 6 8 5 1

Helmut Simonis Search Strategies 69

(70)

Problem Program Naive Search Improvements

Exploring other board sizes

How stable is the model?

Try all sizes from 4 to 100 Timeout of 100 seconds

Helmut Simonis Search Strategies 70

(71)

Problem Program Naive Search Improvements

Naive Stategy, Problem Sizes 4-100

0 5 10 15 20 25

0 5 10 15 20 25 30

Time[s]

Problem Size

"naive/all.txt"

Helmut Simonis Search Strategies 71

(72)

Problem Program Naive Search Improvements

Observations

Time very reasonable up to size 20 Sizes 20-30 times very variable Not just linked to problem size

No size greater than 30 solved within timeout

Helmut Simonis Search Strategies 72

(73)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Outline

1 Problem

2 Program

3 Naive Search

4 Improvements

Dynamic Variable Choice Improved Heuristics

Making Search More Stable

Helmut Simonis Search Strategies 73

(74)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Possible Improvements

Better constraint reasoning

Remodelling problem with 3 alldifferent constraints Global reasoning as described before

Not explored here Better control of search

Static vs. dynamic variable ordering Better value choice

Not using complete depth-first chronological backtracking

Helmut Simonis Search Strategies 74

(75)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Static vs. Dynamic Variable Ordering

Heuristic Static Ordering

Sort variables before search based on heuristic Most important decisions

Smallest initial domain Dynamic variable ordering

Use information from constraint propagation Different orders in different parts of search tree Use all information available

Helmut Simonis Search Strategies 75

(76)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

First Fail strategy

Dynamic variable ordering

At each step, select variable with smallest domain Idea: If there is a solution, better chance of finding it Idea: If there is no solution, smaller number of alternatives Needs tie-breaking method

Helmut Simonis Search Strategies 76

(77)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Caveat

First fail in many constraint systems have slightly different tie breakers

Hard to compare result across platforms

Best to compare search trees, i.e. variable choices in all branches of tree

Helmut Simonis Search Strategies 77

(78)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Modification of Program

:-module(nqueen).

:-export(top/0).

:-lib(ic).

top:-

nqueen(8,L), writeln(L).

nqueen(N,L):- length(L,N), L :: 1..N,

alldifferent(L), noattack(L),

labeling(L). é replace with

Helmut Simonis Search Strategies 78

(79)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Modification of Program

:-module(nqueen).

:-export(top/0).

:-lib(ic).

top:-

nqueen(8,L), writeln(L).

nqueen(N,L):- length(L,N), L :: 1..N,

alldifferent(L), noattack(L),

search(L,0,first_fail,indomain,complete,[]).

Helmut Simonis Search Strategies 79

(80)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

The search Predicate

Packaged search library in ic constraint solver Provides many different alternative search methods Just select a combination of keywords

Extensible by user

Helmut Simonis Search Strategies 80

(81)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

search Parameters

search(L,0,first_fail,indomain,complete,[])

1

List of variables (or terms, covered later)

2

0 for list of variables

3

Variable choice, e.g. first_fail, input_order

4

Value choice, e.g. indomain

5

Tree search method, e.g. complete

6

Optional argument (or empty) list

Helmut Simonis Search Strategies 81

(82)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Variable Choice

Determines the order in which variables are assigned input_order assign variables in static order given first_fail select variable with smallest domain first most_constrained like first_fail, tie break based on number of constraints in which variable occurs

Others, including programmed selection

Helmut Simonis Search Strategies 82

(83)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Value Choice

Determines the order in which values are tested for selected variables

indomain Start with smallest value, on backtracking try next larger value

indomain_max Start with largest value

indomain_middle Start with value closest to middle of domain

indomain_random Choose values in random order

Helmut Simonis Search Strategies 83

(84)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Comparison

Board size 16x16

Naive (Input Order) Strategy First Fail variable selection

Helmut Simonis Search Strategies 84

(85)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Naive (Input Order) Strategy (Size 16)

1 2 3 4 5

145 209

4

188 322

8

197 358

9

173 292

10

200 326 11

184 282 12

6 7

4 8

4

8 13

6

9 15

12 8

2 3

4 9 4

1 2

8 10 4

11 8

12 7 16 6 15 12 14 9 13 2 5 3 1

Helmut Simonis Search Strategies 85

(86)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1

Skip Animation

Helmut Simonis Search Strategies 86

(87)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2

1

Back to Start Skip Animation

Helmut Simonis Search Strategies 87

(88)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3

3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 88

(89)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6

5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 89

(90)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8

4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 90

(91)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13

7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 91

(92)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11

6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 92

(93)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10

2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 93

(94)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9

13 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 94

(95)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7

15 13 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 95

(96)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 15 13 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 96

(97)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 13 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 97

(98)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 16 13 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 98

(99)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7

11 16 13 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 99

(100)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 11 15 16 13 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 100

(101)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 11 15 16 13

9 14 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 101

(102)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 11 15 16 13

9 12 14 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 102

(103)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 11 15 16

13 9 12 14 16 14 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 103

(104)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 11 15 16

13 9 12 14

7 8 16 14 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 104

(105)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 11 15 16

13 9 12

14 7

10 8 16 14 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 105

(106)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 11 15 16

13 9 12

14 7 10 13

8 16 14 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 106

(107)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 11 15 16

13 9 12

14 7 10 13

8 12 9 16 14 2 6 7 4 5 3 1

Back to Start Skip Animation

Helmut Simonis Search Strategies 107

(108)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail Strategy (Size 16)

1 2 3 6 8 13 11 10 9 7 11 14

15 7 11 15 16

13 9 12

14 7 10 13

8 12

8 9 16 14 2 6 7 4 5 3 1

Back to Start

Helmut Simonis Search Strategies 108

(109)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Comparing Solutions

Naive First Fail

Helmut Simonis Search Strategies 109

(110)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Comparing Solutions

Naive First Fail

Solutions are different!

Helmut Simonis Search Strategies 110

(111)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

FirstFail, Problem Sizes 4-100

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

0 10 20 30 40 50 60 70 80 90

Time[s]

Problem Size

"first_fail/all.txt"

Helmut Simonis Search Strategies 111

(112)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Observations

This is much better

But some sizes are much harder Timeout for sizes 88, 91, 93, 97, 98, 99

Helmut Simonis Search Strategies 112

(113)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Can we do better?

Improved initial ordering

Queens on edges of board are easier to assign Do hard assignment first, keep simple choices for later Begin assignment in middle of board

Matching value choice

Values in the middle of board have higher impact Assign these early at top of search tree

Use indomain_middle for this

Helmut Simonis Search Strategies 113

(114)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Modified Program

:-module(nqueen).

:-export(top/0).

:-lib(ic).

top:-

nqueen(16,L),writeln(L).

nqueen(N,L):- length(L,N), L :: 1..N,

alldifferent(L), noattack(L), reorder(L,R),

search(R,0,first_fail,indomain_middle,complete,[]).

Helmut Simonis Search Strategies 114

(115)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Reordering Variable List

reorder(L,L1):-

halve(L,L,[],Front,Tail), combine(Front,Tail,L1).

halve([],Tail,Front,Front,Tail).

halve([_],Tail,Front,Front,Tail).

halve([_,_|R],[F|T],Front,Fend,Tail):- halve(R,T,[F|Front],Fend,Tail).

combine(C,[],C):-!.

combine([],C,C).

combine([A|A1],[B|B1],[B,A|C1]):- combine(A1,B1,C1).

Helmut Simonis Search Strategies 115

(116)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Start from Middle (Size 16)

9

Skip Animation

Helmut Simonis Search Strategies 116

(117)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Start from Middle (Size 16)

9 8

8

Back to Start Skip Animation

Helmut Simonis Search Strategies 117

(118)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Start from Middle (Size 16)

9 8 12

10 8

Back to Start Skip Animation

Helmut Simonis Search Strategies 118

(119)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Start from Middle (Size 16)

9 8 12

5

9 10 8

Back to Start Skip Animation

Helmut Simonis Search Strategies 119

(120)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Start from Middle (Size 16)

9 8 12

5 14

6 9 10 8

Back to Start Skip Animation

Helmut Simonis Search Strategies 120

(121)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Start from Middle (Size 16)

9 8 12

5 14

6

5 6 9 10 8

Back to Start Skip Animation

Helmut Simonis Search Strategies 121

(122)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Start from Middle (Size 16)

9 8 12

5 14

6 4

4 5 6 9 10 8

Back to Start Skip Animation

Helmut Simonis Search Strategies 122

(123)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Start from Middle (Size 16)

9 8 12

5 14

6 4

12 4 5 6 9 10 8

Back to Start Skip Animation

Helmut Simonis Search Strategies 123

(124)

Problem Program Naive Search Improvements

Dynamic Variable Choice Improved Heuristics Making Search More Stable

Start from Middle (Size 16)

9 8 12

5 14

6 4

12

3

11 4 5 6 9 10 8

Back to Start Skip Animation

Helmut Simonis Search Strategies 124

Références

Documents relatifs

analysed in [7, 8] mobile search behaviour in the context of a distributed event and compared search characteristics and performance of a naturalistic user study to those of mobile

Starting the query they were given a document list, opened the documents and decided individually whether document was relevant for the topic.. If they were relevant, they were

In this paper, a new complete technique to compute Maximal Satisfiable Subsets (MSSes) and Minimally Unsatisfiable Subformulas (MUSes) of sets of Boolean clauses is intro- duced..

Use normal state as starting point Consider routing in each failure case Aggregate flows in rerouted network Calculate bounds on traffic in failure

The attendees of the demo will learn how FAIRnets Search can be used to gain insights into the usage of existing neural networks and datasets in machine learning.. In the online

To achieve this goal, the following main tasks need to be solved: analyze the existing semantic search technologies used by search engines and identify the main tasks

As an initial experiment with healthcare data, we implemented Carrot2 as a service, and the cluster labels from PubMed search result snippets for the input query

It is possible that participants are trig- gered to check more books by the distinct functionality of the different panels of the multistage interface, especially since the open