Application focus 1: Compiling Crosswords using CP part I PowerPoint PPT Presentation

presentation player overlay
1 / 93
About This Presentation
Transcript and Presenter's Notes

Title: Application focus 1: Compiling Crosswords using CP part I


1
Application focus 1 Compiling Crosswords using
CP (part I)
  • Andy King and Colin Pigden
  • With thanks to Daniel Le Berre of Université
    dArtois

2
The crossword compiling problem (fill-in
crossword)
  • Given a dictionary of words
  • and a crossword grid
  • Insert a subset of the dictionary (possibly using
    a word twice) into the vertical and horizontal
    word positions

3
Crossword compiling in 2 steps
  • It is sufficient to find all combinations of
    characters that can arise at the intersection
    points
  • Words can then be fleshed out by searching the
    dictionary like so (may generate more solutions)

4
Arc-consistency algorithm(initialisation and
pass 1)
s1 s7 a, b, , z w 1a, 3a, 5a, 1d,
2d, 7u consider 1a ? w w w - 1a d w
? d w 6 ? w1 ? s1 ? w4 ? s2
torque, quirky, encore, turkey,
clique, droopy, anyhow, wreath, napkin s1
w1 w ? d t, n, c, q, a, e, d, w s2
w4 w ? d k, q, r, h, o, a w w
? 1d w w ? 2d
5
Arc-consistency algorithm(pass 2)
consider 3a ? w w w - 3a d w ? d
w 4 ? w1?s3 ? w4?s4 mini,
quay, yogi, loci, ugly s3 u, y, q, l, m s4
y, i w w ? 1d w w ? 2d
6
Arc-consistency algorithm(pass 3)
consider 5a ? w w w - 5a d torque,
quirky, encore, turkey, clique, droopy,
anyhow, wreath, napkin s5 t, n, c, q, a, e,
d, w s6 k, q, r, h, o, a s7 y, n, e, w,
h w w ? 1d w w ? 2d w w ? 7u
7
Arc-consistency algorithm(pass 4 2 more needed)
consider 1d ? w w w - 1d d colon,
tempt, pique, crypt, would s1 t, c, w s3
m, y, u, l s5 t, d, n w w ? 1a w
w ? 3a w w ? 5a
8
Weak pruning/many iterationsfor 75,706 words?
9
Pass 1
10
Pass 2
11
Pass 3
12
Pass 4
13
Pass 5
14
Pass 6
15
Pass 7
16
Pass 8
17
Pass 9
18
Pass 10
19
Pass 11
20
Pass 12
21
Pass 13
22
Pass 14
23
Pass 15
24
Pass 16
25
Pass 17
26
Pass 18
27
Pass 19
28
Pass 20
29
Pass 21 (final)
30
Search and Arc-consistency
31
Non-example of backtracking
  • Grid
  • 40 words
  • 64 intersections
  • Dictionary
  • 75,706 English words
  • Timing
  • 23 seconds
  • 3.2GHz
  • 1 GByte RAM

32
Example of backtracking (level i where si was
last halved)
  • Dictionary
  • 75,706 words
  • Timing
  • 83 seconds

33
Backtracking under the micro-scope (now inverted)
s9 ewr
s9 e
s9 wr
  • intersections
  • 29 strata
  • inconsistency
  • 9-13 stratum

s9 w
s9 r
s12 rie
s12 r
s12 ie
s13 aeuoi
s13 ae
s13 uoi
s13 a
s13 e
s13 u
s13 oi
s13 o
s13 i
34
Prioritising search most versus least frequent
  • Consider dividing s1 t,c,w
  • s1 occurs in 1a (note arbitrariness)
  • If s1 t then 1a torque or turkey
  • If s1 c then 1a clique
  • If s1 w then 1a wreath
  • Frequency rank to obtain ordered sets
  • c, w, t to try less frequent characters first
  • t, c, w to try more frequent characters first

35
Prioritising search most versus least frequent
first
36
Proportion of least frequent
37
By product is some interesting words
38
Arc-consistency in language J (part II)
  • Andy King
  • With thanks to Colin Pigden for the use of his
    dictionaries

39
Industrial applications of CLPand resources
(part III)
  • Andy King
  • With thanks to Andrew Verden

40
CLP applications
  • Fleet assignment the assignment of a set of
    aircraft of different types to a predefined
    schedule of flights
  • Job-shop scheduling a set of jobs and set of
    machines are given, each job consisting on a
    partially ordered set of tasks
  • Nurse scheduling specify which days on and
    which days off given constraints on personnel
    policy, nurses qualifications and individual
    requests
  • Interpreting sloppy stick figures recognizing
    drawings with missing model parts and noisy data
  • See at PACT, CP, Constraints and the applications
    page http//www-icparc.doc.ic.ac.uk/eclipse/appl/
  • Do not be deceived constraint solving is usually
    hidden beneath interface coded in Java.

41
Growth of constraint programming
  • Results 64 submissions to ICLP in 2001 66
    submissions to the ICFP in 2001 compared with 135
    submissions to CP in 2001
  • Systems AKL, Amulet and Garnet, B-Prolog,
    Bertrand, CHIP, CPLEX, Cassowary, Cooldraw,
    Thinglab, ECLiPSe, GNU-Prolog, IF/Prolog, ILOG
    Solver, Interval Solver for Microsoft Excel,
    Jsolver, Numerica, Oz, Prolog IV, RISC-CLP(Real),
    SICStus
  • Laboratories and startups CCC, IC-Parc, NICTA,
    IF/Computer, ILOG, PrologIA, Vine Solutions, etc

42
Paper and on-line resources
  • For ECLiPSe see the ECLiPSe Constraint Library
    Manual in the on-line documentation
  • Krzysztof Apt, Principles of Constraint
    Programming, Cambridge, 2003
  • Kim Marriott and Stuckey, Programming with
    Constraints, MIT Press, 1998
  • Roman Barták, On-line Guide to Constraint
    Programming, see http//kti.ms.mff.cuni.cz/barta
    k/constraints/

43
Review articles
  • Algorithms for Constraints Satisfaction problems
    A Survey, Kumar, The AI Magazine, 13(1), 32-44,
    1992
  • Google Vipin Kumar
  • Constraint Satisfaction Problems Algorithms and
    Applications, Brailsford, Potts and Smith,
    European Journal of Operational Research, 119(3),
    1999
  • Templeman e-journal

44
Commerce versus science(sickofantic hyperbole)
  • Were you to ask me which programming paradigm is
    likely to gain most in commercial significance
    over the next 5 years Id have to pick
    Constrained Logic Programming (CLP), even though
    its perhaps currently one of the least known and
    understood
  • Dick Pountain
  • BYTE, February 1995

Constraint programming represents one of the
closest approaches computer science has yet made
to the Holy Grail of programming the user states
the problem, the computer solves it Eugene C.
Freuder CONSTRAINTS, April 1997
45
CLP in a nutshell (part IV)
  • Andy King
  • With thanks to no one!

46
What is the n-queens problem?
  • Place n queens on an n by n chessboard so that
    none of them can take each other
  • Solutions and non-solutions for, say, n 6 are
  • There are n2!/(n!(n2-n)!) possible ways of
    placing n queens on an n by n board, that is,
    1947792 for n 6

Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
47
What is the generate and test paradigm?
  • generate place all the queens on the chessboard
    in some configuration
  • test check whether the configuration is safe,
    that is, whether any one of the queens can take
    one another
  • repeat generate and test, searching until either
    a solution is found or all configurations are
    exhausted.

possible solutions
solutions
all configurations
48
A representation fora 6-queen configuration
  • A map (or equivalently a function) from each row
    number to a unique column number
  • The map 1?2, 2?4, 3?6, 4?1, 5?3, 6?5 represents
    the safe configuration
  • This representation assumes that exactly
    one queen occurs in each row
  • if two queens occurred in the same row the
    the configuration is a non-solution (take
    horizontally)
  • if zero queens occurred in one row, then at least
    two queens must occur in another row (take
    horizontally)
  • Each map is a permutation (scrambling) the above
    map can be represented by 2,4,6,1,3,5 which is
    just a scrambling of 1,2,3,4,5,6

Y
Y
Y
Y
Y
Y
49
How can generate-and-test solve 6-queens?
Y
6
Y
6
Y
6
Y
5
Y
Y
5
5
Y
4
Y
4
Y
4
Y
Y
Y
3
3
3
Y
2
Y
2
Y
2
Y
Y
Y
1
1
1
1
2
3
4
5
6
1
2
3
4
6
1
2
3
4
5
6
5
Y
6
Y
6
Y
6
Y
5
Y
5
Y
5
Y
Y
Y
4
4
4
Y
Y
Y
3
3
3
Y
2
Y
2
Y
2
Y
Y
Y
1
1
1
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
First solution found on the 188th configuration
50
How is generate-and-test realised in Prolog?
  • nqueens(N, Soln)-
  • length(Soln, N),
  • range(1, N, Range),
  • perm(Soln, Range), generator
  • safe(Soln). tester

51
How is generate-and-test realised in Prolog?
(cont)
  • Or for pearl (google page 6 of Computing Convex
    Hulls with a Linear Solver) layout
  • range(N, N, R) - !,
  • R N.
  • range(M, N, M Rest_R) -
  • M lt N,
  • Next_M is M 1,
  • range(Next_M, N, Rest_R).

range(M, N, M Rest_R) - Next_M is M
1, (M N -gt Rest_R range(Next_M,
N, Rest_R) ).
52
How can (the generator) be coded in Prolog?
  • Require
  • ?- perm(L, a,b,c).
  • L a,b,c ?
  • L a,c,b ?
  • L b,a,c ?
  • L b,c,a ?
  • L c,a,b ?
  • L c,b,a ?
  • no
  • Thus construct
  • ?- select(X,a,b,c, Rs).
  • X a, Rs b,c ?
  • X b, Rs a,c ?
  • X c, Rs a,b ?
  • no

53
How perm/2 be coded in Prolog?
  • perm(, ).
  • perm(XXs, Ls) -
  • my_select(X, Ls, Rs), eclipse import
  • perm(Xs, Rs).
  • my_select(X, XXs, Xs).
  • my_select(X, CNCNs, CNRs) -
  • my_select(X, CNs, Rs).

54
Taking diagonally for the configuration CN1, ,
CN6
  • Suppose that CN1 2
  • Consider those CN2 which are unsafe relative to
    CN1
  • In either case
  • 1abs(CN1CN2), thus
  • require 1?abs(CN1 CN2)
  • Also 2?abs(CN1 CN3) etc
  • Suppose that CN5 3
  • Consider those CN3 which are unsafe relative to
    CN5
  • In either case
  • 2abs(CN5CN3), thus
  • require 2?abs(CN5CN3)
  • Also 1?abs(CN5CN6) etc

Y
Y
Y
Y
Y
Y
55
How can safe/1 (the tester) be coded in Prolog?
?- safe(1,2,3,4,5,6). no ?-
safe(2,4,6,1,3,5). yes
  • safe().
  • safe(CN CNs)-
  • no_attack(CNs, CN, 1),
  • safe(CNs).
  • no_attack(, _, _).
  • no_attack(CNCNs, First_CN, Diff) -
  • Diff \ abs(First_CN CN),
  • Next_Diff is Diff 1,
  • no_attack(CNs, First_CN, Next_Diff).

56
Does generate-and-test scale smoothly with n?
  • perm(Soln, Range) instantiates Soln to 1,2Xs
    and then calls itself recursively to instantiate
    Xs.
  • Recursive call is a waste of effort because the
    configuration is bound to cause safe(Soln) to
    fail.
  • If failure detected earlier, then not waste
    effort and also related, unsafe configurations
    would be failed together.

57
What is the test-and-generate paradigm?
  • generate place one new queen on the chessboard
    to construct a configuration incrementally
  • test check whether the new queen is safe as
    soon as it is placed on the board discard
    partial configurations that are definitely
    unsafe.
  • repeat incremental generation and testing,
    searching until either a solution is found or all
    configurations are exhausted.

58
How can the test-and-generate solve 6-queens?
6
6
6
5
5
5
4
4
4
3
3
3
Y
2
2
Y
2
Y
Y
Y
1
1
1
1
2
3
4
6
5
1
2
3
4
5
6
1
2
3
4
5
6
6
6
6
5
5
5
4
4
4
Y
Y
Y
3
3
3
Y
Y
Y
2
2
2
Y
Y
Y
1
1
1
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
First solution found on the 31st partial
configuration (choice)
59
How can test-and-generate be coded in ECLiPSe?
  • ECLiPSe is not a Prolog but it supports a
    Prolog-like (freeze) meta-call
  • suspend(Goal, Priority, Triggers) where
  • Goal is a goal
  • Priority is a level 1-12 (low-high)
  • A term or list of terms of the form
    Trigger-gtVariable
  • suspend(X lt Y, 2, X-gtinst, Y-gtinst) will invoke
    X lt Y when either X or Y are instantiated
  • The suspend library also provides some suspending
    tests such as lt and

60
How can test-and-generate be coded in ECLiPSe?
(cont)
  • nqueens(N, Soln)-
  • length(Soln, N), range(1, N, Range),
  • safe(Soln), tester
  • perm(Soln, Range). generator
  • no_attack(, _, _).
  • no_attack(CNCNs, First_CN, Diff) -
  • suspend(my_test(Diff, First_CN, CN), 2,
    First_CN-gtinst, CN-gtinst),
  • Next_Diff is Diff 1,
  • no_attack(CNs, First_CN, Next_Diff).
  • my_test(Diff, First_CN, CN) -
  • (nonvar(First_CN) -gt
  • suspend(Diff \ abs(First_CN - CN), 2,
    CN-gtinst)
  • suspend(Diff \ abs(First_CN - CN),
    2, First_CN-gtinst)
  • ).

61
A devious optimisation
  • Observe that First_CN is nearer the beginning of
    Soln, hence it is instantiated before CN
  • No point suspending on both First_CN and CN

no_attack(, _, _). no_attack(CNCNs,
First_CN, Diff) - suspend(Diff \ abs(First_CN
- CN), 2, CN-gtinst), Next_Diff is Diff 1,
no_attack(CNs, First_CN, Next_Diff).
62
How can test-and-generate be coded in SICStus?
  • no_attack(, _, _).
  • no_attack(CNCNs, First_CN, Diff) -
  • my_test(Diff, First_CN, CN),
  • Next_Diff is Diff 1,
  • no_attack(CNs, First_CN, Next_Diff).
  • - block my_test(?, -, ?), my_test(?, ?, -).
  • my_test(Diff, First_CN, CN) -
  • Diff \ abs(First_CN CN).

63
How does test-and-generate scale with n?
  • Better scaling achieved just by altering the
    control the logic is unchanged
  • Blocks (delays) are now are part of any 2nd
    generation Prolog.
  • The goals safe(Soln) and perm(Soln, Range) are
    said to coroutine.

64
Constraint satisfaction problems (CSPs)
  • A CSP consists of
  • a finite set of variables X x1, , xn
  • a domain D that is a mapping x1? S1,, xn? Sn
    where each Si is a finite set
  • a constraint C(x1, , xn) that is a finite set of
    primitive constraints C c1, , cm where
    var(ci) ? X
  • The CSP is interpreted as the problem of deciding
  • whether C(v1, , vn) ? v1 ? D(x1) ? ? vn ?
    D(xn) is
  • satisfiable whether it possesses a solution

65
What is an example CSP?
Colour the regions of a map with a limited number
of colours, subject to the condition that no two
adjacent regions share the same colour. For
instance, consider the CSP which encodes the
problem of colouring Australia so that Western
Australia is red
  • The set of variables is X WA, NT, Q, NS, SA,
    V, T
  • The domain is D WA ? red, NT ? S, , T ? S
    where S red, yellow, blue
  • The set of constraints is C WA ? NT, WA ? SA,
    NT ? SA, NT ? Q, SA ? Q, SA ? NS, SA ? V, Q ? NS,
    NS ? V

66
Coding the CSP in ECLiPSe (constrain-and-generate)
eclipse 2 colour(R). R 2, 1, 2, 3, 1,
1 More (0.00s cpu) ? R 2, 1, 2, 3, 1,
2 More (0.00s cpu) ? R 2, 1, 2, 3, 1,
3 More (0.00s cpu) ? R 3, 1, 3, 2, 1,
1 More (0.00s cpu) ? R 3, 1, 3, 2, 1,
2 More (0.00s cpu) ? R 3, 1, 3, 2, 1,
3 Yes (0.00s cpu)
- use_module(library(fd)). colour(Regions) -
Regions NT, Q, NS, SA, V, _T, Regions
1..3, WA 1..1, WA \
NT, WA \ SA, NT \ SA, NT \ Q,
SA \ Q, SA \ NS, SA \ V, Q \ NS,
NS \ V, labeling(Regions).
67
What is another example of a CSP?
Crypto-arithmetic problems puzzles in which
digits are replaced with distinct letters of the
alphabet or other symbols. Consider the classic
SEND, MORE, MONEY puzzle that is due to Dudeney
Strand Magazine, July, 1924
SEND MORE MONEY
C2C2C1
  • The set of variables is X S, E, N, D, M, O, R,
    Y, C1, C2, C3
  • The domain is D S ? 0,,9, , R ? 0,,9,
    C1 ? 0,1, C2 ? 0,1, C3 ? 0,1
  • The set of constraints is C CP ? CA where
  • CP S ? E, S ? N, S ? D, S ? M, S ? O, S ? R, E
    ? N, E ? D, E ? M, E ? O, E ? R, N ? D, N ? M, N
    ? O, N ? R, D ? M, D ? O, D ? R, M ? O, M ? R, O
    ? R
  • CA D E Y 10C1, N R C1 E 10C2,
  • E O C2 N 10C3, S M C3 O 10M

68
Coding the crypto-arithmetic CSP in ECliPSe
- use_module(library(fd)). crypto(Digits) -
Digits S,E,N,D,M,O,R,Y, Digits
0..9, Carrys C1, C2, C3,
Carrys 0..1,
alldifferent(Digits), D E Y 10
C1, N R C1 E 10 C2,
E O C2 N 10 C3, S M C3
O 10 M, labeling(Digits).
eclipse 2 crypto(D). D 2, 8, 1, 7, 0, 3,
6, 5 More (0.00s cpu) ? D 2, 8, 1, 9, 0,
3, 6, 7 More (0.00s cpu) ? D 3, 7, 1, 2,
0, 4, 6, 9 More (0.00s cpu) ? D 3, 7, 1,
9, 0, 4, 5, 6 More (0.00s cpu) ?
2817 0368 03185
69
Recall a representation for 6-queens
  • The set of variables is X X1, , X6 where Xi
    is the column number for the queen in row i
  • The assignment X1 2, X2 4 , X6 5
    represents the safe configuration
  • This representation assumes that exactly
    one queen occurs in each row
  • if two queens occurred in the same row then the
    configuration is a non-solution (take
    horizontally)
  • if zero queens occurred in one row, then at least
    two queens must occur in another row (take
    horizontally)

Y
Y
Y
Y
Y
Y
70
6-queens as a CSP (cont)
  • The domain is D X1 ? 1,,6, , X6 ? 1,,6
  • The set of constraints is C CP ? CD where
  • CP X1 ? X2, X1 ? X3, X1 ? X4, X1 ? X5, X1 ?
    X6, X2 ? X3, X2 ? X4, X2 ? X5, X2 ? X6, , X4 ?
    X5, X5 ? X6
  • This ensures that queens cannot take vertically
  • CD 1 ? abs(X1 X2), 2 ? abs(X1 X3), 3 ?
    abs(X1 X4), 4 ? abs(X1 X5), 5 ? abs(X1 X6),
    1 ? abs(X2 X3), 2 ? abs(X2 X4), 3 ? abs(X2
    X5), 4 ? abs(X2 X6), , 1 ? abs(X5 X6)
  • This ensures that queens cannot take diagonally

71
Coding the n-queens CSP in ECliPSe
- use_module(library(fd)). nqueens(N, Soln)-
length(Soln, N), Soln 1..N, safe(Soln),
alldifferent(Soln), labeling(Soln). safe(). sa
fe(CN CNs) - no_attack(CNs, CN,
1), safe(CNs). no_attack(, _,
_). no_attack(CNCNs, First_CN, Diff) -
Diff \ abs(First_CN - CN), Diff \ First_CN
- CN, Diff \ CN - First_CN, Next_Diff is
Diff 1, no_attack(CNs, First_CN, Next_Diff).
72
Running the n-queens program
eclipse 26 nqueens(6, S). S 2, 4, 6, 1, 3,
5 More (0.00s cpu) ? S 3, 6, 2, 5, 1,
4 More (0.00s cpu) ? S 4, 1, 5, 2, 6,
3 More (0.00s cpu) ? S 5, 3, 1, 6, 4,
2 More (0.00s cpu) ? No (0.00s cpu)
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
73
How does constrain-and-generate solve 6-queens?
x
x
x
x
6
x
x
x
6
x
x
6
x
x
x
x
5
x
x
5
x
x
x
x
5
x
x
x
x
x
x
4
x
x
x
x
x
4
x
4
x
x
x
Y
x
x
x
x
x
x
x
x
3
3
3
Y
x
x
x
x
x
Y
x
x
x
x
x
2
x
2
x
2
x
x
x
x
Y
x
x
x
x
Y
x
x
x
x
Y
x
1
x
x
1
1
1
2
3
4
6
5
1
2
3
4
5
6
1
2
3
4
5
6
x
x
6
x
x
x
x
x
6
x
x
x
x
x
x
6
x
x
x
x
x
5
x
x
x
x
x
x
5
x
x
x
x
x
5
x
x
x
x
x
x
Y
x
x
x
x
Y
x
x
x
x
4
x
4
x
4
Y
x
x
x
x
x
Y
x
x
x
x
x
x
x
x
Y
x
x
3
3
3
Y
x
x
x
x
Y
x
x
x
x
Y
x
x
x
x
x
2
x
2
x
2
x
x
x
Y
x
x
x
Y
x
x
x
x
x
x
x
x
Y
x
1
1
1
1
2
3
4
5
6
1
2
3
4
5
6
1
2
3
4
5
6
First solution found on the 21st partial
configuration (choice)
74
How CLP is realised (part V)
  • Andy King

75
With and without labelling
- use_module(library(fd)). bounds(X, Y, Z)-
X 1..5, Y 1..2, Z 3..5, X Y
Z. labeling(X, Y, Z). eclipse 8
bounds(X, Y, Z). X X4, 5 Y Y1, 2 Z
Z3, 4 Yes (0.00s cpu)
- use_module(library(fd)). bounds(X, Y, Z)-
X 1..5, Y 1..2, Z 3..5, X Y
Z, labeling(X, Y, Z). eclipse 8
bounds(X, Y, Z). X 4, Y 1, Z 3 Yes (0.00s
cpu) ? X 5, Y 1, Z 4 Yes (0.00s cpu) ?
X 5, Y 2, Z 3 Yes (0.00s cpu)
76
Unravelling (understanding) bounds propagation
  • Initially 1?X?5, 1?Y?2 and 3?Z?5
  • Consider X Y Z
  • Thus 4min(Y)min(Z)?X?max(Y)max(Z)7
  • Tighten by 4?X, hence 4?X?5, 1?Y?2 and 3?Z?5
  • Consider Y X - Z
  • Thus -1min(X)-max(Z)?Y?max(X)-min(Z)2
  • Cannot tighten Y
  • Consider Z X - Y
  • Thus 2min(X)-max(Y)?Z?max(X)-min(Y)4
  • Tighten by Z?4, hence 4?X?5, 1?Y?2 and 3?Z?4

77
Unravelling (unwinding) labelling
bounds(X, Y, Z)- X 1..5, Y
1..2, Z 3..5, X Y Z,
labeling(X, Y, Z).
eclipse 8 bounds(X, Y, Z). X 1, Y 1, Z
3 Yes (0.00s cpu) ? X 1, Y 1, Z 4 Yes
(0.00s cpu) ? X 1, Y 1, Z 5 Yes (0.00s
cpu) ? X 1, Y 2, Z 3 Yes (0.00s cpu) ?
X 1, Y 2, Z 4 Yes (0.00s cpu) ? X
1, Y 2, Z 5 Yes (0.00s cpu) ?
78
Search without bounds propagation (is inefficient)
  • Without bounds propagation, maximum of 523 30
    cases need to be checked against X Y Z
  • With bounds propagation, maximum of 222 8
    cases need to be checked
  • When intelligence fails, resort to brute force to
    systematically enumerate all the combinations and
    check them against the constraints

bounds(X, Y, Z)- X 1..5, Y
1..2, Z 3..5, X Y Z, labeling(X, Y,
Z).
bounds(X, Y, Z)- X 4..5, Y
1..2, Z 3..4, X Y Z, labeling(X, Y,
Z).
79
Bounds propagation can avoid search
  • Initially -4?X?4 and -4?Y?4
  • Consider Y 2(X-1), so X (Y/2)1
  • Thus -1 min(Y)/21?X?max(Y)/213
  • Tighten -1?X?3, hence -1?X?3 and -4?Y?4
  • Consider Y X
  • Thus -1min(X)?Y?max(X)3
  • Tighten -1?Y?3, hence -1?X?3 and -1?Y?3
  • Consider Y 2(X-1), so X (Y/2)1
  • Thus 1/2 min(Y)/21?X?max(Y)/215/2
  • Tighten 1?X?2, hence 1?X?2 and -1?Y?3
  • Consider Y X
  • Thus 1min(X)?Y?max(X)2
  • Tighten -1?Y?3, hence 1?X?2 and 1?Y?2

cross(X, Y) - X, Y -4..4, Y 2(X -
1), Y X. eclipse 8 cross(X, Y). X
2 Y 2 Yes (0.00s cpu)
80
Bounds propagation can avoid search (cont)
cross(X, Y) - X, Y -4..4, Y 2(X -
1), Y X. eclipse 8 cross(X, Y). X 2 Y
2 Yes (0.00s cpu)
  • The story so far 1?X?2 and 1?Y?2
  • Consider Y 2(X-1), so X (Y/2)1
  • Thus 3/2 min(Y)/21?X?max(Y)/212
  • Tighten 2?X?2, hence 2?X?2 and 1?Y?2
  • Consider Y X
  • Thus 2min(X)?Y?max(X)2
  • Tighten 2?Y, hence 2?X?2 and 2?Y?2

81
Extreme propagation theory node- and
arc-consistency
  • Intuition
  • propagation makes a set of constraints consistent
  • stop propagation as soon as consistency achieved
  • A primitive constraint c is node-consistent with
    respect to D whenever the following holds
  • if var(c) x then
  • for all dx ? D(x) it follows that the constraint
    cx ? dx is satisfiable (true)
  • A primitive constraint c is arc-consistent with
    respect to D whenever the following holds
  • if var(c) x,y and x ? y then
  • for all dx ? D(x) there exists dy ? D(y) such
    that the constraint cx ? dx, y ? dy is
    satisfiable (true)

82
Extreme propagation node- and arc-consistency
(cont)
  • A CSP with constraints C c1,, cn is
  • node-consistent with respect to D whenever each
    ci is node-consistent with respect to D
  • arc-consistent with respect to D whenever each
    ci is arc-consistent with respect to D

83
Example and non-example of node-consistency
  • Consider a CSP with
  • X u, v
  • D u ? 1,2, v ? 1,2
  • C c1, c2 where c1 (u lt v) and c2 (v 2)
  • For node-consistency
  • var(c1) u,v so c1 is vacuously
    node-consistent
  • var(c2) v but c2 is not node-consistent since
    there exists dv1?D(v) such that the constraint
    c2v ? dv (12) is unsatisfiable
  • Set D(v) 2 (perform propagation) to remedy
    this inconsistency

84
Example and non-example of arc-consistency
  • Consider the revised CSP with
  • D u?1,2, v?2
  • C c1, c2 where c1 (u lt v) and c2 (v 2)
  • For arc-consistency
  • var(c2) v so c2 is vacuously arc-consistent
  • var(c1) u,v but c1 is not arc-consistent
    since
  • not for all dx ? D(x) there exists dy ? D(y) such
    that the constraint c1x ? dx, y ? dy is
    satisfiable
  • there exists dx ? D(x) not there exist dy ? D(y)
    such that the constraint c1x ? dx, y ? dy is
    satisfiable
  • there exists dx ? D(x) for all dy ? D(y) the
    constraint
  • c1x ? dx, y ? dy is unsatisfiable
  • there exists du2? D(u) for all dv? D(v)2 the
    constraint
  • c1u ? du, v ? dv(2 lt2) is unsatisfiable
  • Set D(u) 1 (propagate) to fix this
    inconsistency

85
Original, intermediate and final CSP
  • Original CSP (not node-consistent)
  • X u, v
  • D u ? 1,2, v ? 1,2
  • C c1, c2 where c1 (u lt v) and c2 (v 2)
  • Intermediate CSP (node- but not arc-consistent)
  • X u, v
  • D u ? 1,2, v ? 2
  • C c1, c2 where c1 (u lt v) and c2 (v 2)
  • Final CSP (both node- and arc-consistent)
  • X u, v
  • D u ? 1, v ? 2
  • C c1, c2 where c1 (u lt v) and c2 (v 2)

86
Propagation in practice bounds-consistency
  • A primitive constraint c is bounds-consistent
    with respect to D whenever the following holds
  • if x ? var(c) and d ? min(D(x)), max(D(x))
    then
  • for each yi ? y1, , ym var(c) - x there
    exists min(D(yi)) ? di ? max(D(yi)) such that the
    constraint cx ? d, y1 ? d1, , ym ? dm is
    satisfiable (true).
  • A CSP with constraints C c1,, cn is
  • bounds-consistent with respect to D whenever
    each ci is bounds-consistent with respect to D
  • Bounds consistency is more efficient to enforce
    than node- and arc-consistency because it only
    reasons about maxima and minima values of D (not
    values in between)
  • Never remove elements from middle of domain only
    the upper and lower ends of the domain

87
Propagation though enforcing bounds-consistency
  • Consider a CSP where
  • X x,y,z
  • D x ? 1,2,3,4,5, y ? 1,2, z ? 3,4,5
  • C c where c (x y z)
  • c is not bounds-consistent with respect to D
    because
  • cx?1, y? dy, z ? dz is unsatisfiable for all
    1?dy?2 and 3?dz?5.
  • The problem is that D(x) contains values that are
    too low
  • min(D(x)) lt min(D(y)) min(D(z)) 13 4
  • Thus set D x ? 4,5, y ? 1,2, z ? 3,4,5

88
Propagation though enforcing bounds-consistency
(part ii)
  • Now consider the revised CSP where
  • X x,y,z
  • D x ? 4,5, y ? 1,2, z ? 3,4,5
  • C c where c (x y z) ? (z x - y)
  • c is not bounds-consistent with respect to D
    because
  • cz?5, x ? dx, y? dy is unsatisfiable for all
    4?dx?5 and 1?dy?2.
  • The problem is that D(z) contains values that are
    too high
  • max(D(z)) gt max(D(x)) - min(D(y)) 5-1 4
  • Thus set D x ? 4,5, y ? 1,2, z ? 3,4
  • c is now bounds-consistent with respect to D

89
What choices (degrees of freedom) exist in search?
  • Consider the CSP where
  • X x, y,
  • D x?0, 1,y?0, 1
  • C x ? y
  • Observe the choice of variable. For instance,
    label x first (rather than y first) or vice
    versa.
  • Observe the choice of value. For example, label x
    or y with 0 first (rather than 1 first) or vice
    versa
  • eclipse 7 X, Y0..1,
  • X lt Y.
  • X X0, 1, Y Y0, 1
  • Yes (0.00s cpu)

90
What choices exist in search? (cont)
  • Consider the CSP where
  • X x, y,
  • D x ? 0, 1, y ? 0, 1
  • C x ? y
  • Table gives the number of labelling steps to find
    a first solution (stop searching when all
    variables are assigned)
  • The number of labelling steps is a measure of the
    overall running time

91
Choice of variable is it really that important?
  • Consider a binary CSP where X A, , H ,
  • D A ? S, , H ? S and S 0,1,2
  • Now label E (see the next slide).
  • E fixed so no propagation occurs from G to E.
    Moreover, after initial propagation, no more
    occur from E to G so it is as if it as if E-G is
    not there.
  • Thus partitioned CSP into one sub-CSP over
    A,B,C,D and another sub-CSP over F,G,H,J.
  • To final a solution to CSP
  • search 34 configurations of A,B,C,D sub-CSP
  • search 34 configurations of F,G,H,J sub-CSP
  • total is 3(34 34) 3(81 81) 486 1968339
  • Thus labelling can decompose a CSP into
    independent sub-CSPs that are cheaper to solve
    than the whole

B
A
C
D
E
F
G
H
J
92
Choice of variable is it that important? (cont)
B
A
C
B
A
B
C
A
C
D
D
0
D
1
2
F
F
F
G
G
G
H
H
H
J
J
J
93
Application focus 2 Compiling Crosswords using
sat4j (part VII)
  • Andy King and Colin Pigden
  • With thanks to Daniel Le Berre of Université
    dArtois
Write a Comment
User Comments (0)
About PowerShow.com