from the second lecture on Constraints on AI in

advertisement
Artificial Intelligence
Constraint
Programming 2
Ian Gent
ipg@cs.st-and.ac.uk
Artificial Intelligence
Part
Part
Part
Part
I:
II:
III:
IV:
Constraint
Programming 2
Arc Consistency
MAC
Special kinds of constraints
Formulation
Constraint Satisfaction Problems
 CSP = Constraint Satisfaction Problems
 A CSP consists of:
 a set of variables, X
 for each variable xi in X, a domain Di
 Di is a finite set of possible values
 a set of constraints restricting tuples of values
 if only pairs of values, it’s a binary CSP
 A solution is an assignment of a value in Di to each
variable xi such that every constraint satisfied
3
Formal Definition of Constraints
 A constraint Cijk… involving variables xi, xj, xk …
 is any subset of combinations of values from Di, Dj, Dk …
 I.e. Cijk...  Di x Dj x Dk …
 indicating the allowed set of values
 Most constraint programming languages/toolkits
allow a number of ways to write constraints:
 e.g. if D1 = D2 = {1,2,3} …
 { (1,2), (1,3), (2,1), (2,3), (3,1), (3,2) }
 x1  x2
 CtNeq(x1,x2)
 I’ll use whatever notation seems right at the time
4
Arc Consistency
 Last time, we saw forward checking
 extremely simple form of reasoning
 if variable x has value v
 & constraint involves variables x-y
 & constraint disallows x=v, y = u,
 then remove value u from domain of y
 This time, look at a more complex form of reasoning
 arc consistency
 In graph theory, arc = edge
 In constraints, arc will be a (directed) constraint
5
Arc Consistency
 Arc consistency is based on a very simple concept
 if we can look at just one constraint and see that x=v is
impossible …
 obviously we can remove the value x=v from consideration
 How do we know a value is impossible?
 If the constraint provides no support for the value
 e.g. if Dx = {1,4,5} and Dy = {1, 2, 3}
 then the constraint x > y provides no support for x=1
 we can remove x=1 from Dx
6
Arc Consistency Propagation
 When we remove a value from Dx, we may get new
removals because of it
 E.g. Dx = {1,4,5}, Dy = {1, 2, 3}, Dz= {2, 3, 4, 5}





x > y, z > x
As before we can remove 1 from Dx, so Dx = {4,5}
But now there is no support for Dz = 2,3,4
So we can remove those values, Dz = {5}, so z=5
Before AC applied to y-x, we could not change Dz
 This can cause a chain reaction
7
Establishing Arc Consistency
 If we consider every constraint and full propagation,
there are only two possibilities:
 the final problem will be arc-consistent
 every value in every domain is supported by some
other value in every constraint
 we have established arc consistency
 at some point some variable will have an empty domain
 no value can be given to that variable
 the problem is insoluble due to arc consistency
 we have seen a domain wipe out
 A number of algorithms can be used to establish AC 8
AC3
 AC3 is one of many algorithms (1 - 7 and lots more!)
 Only consider binary constraints with 2 variables
 For this algorithm, we revise directed arcs
 I.e. when considering constraint x-y we remove values
from y
 when considering y-x we remove values from x
 of course both constraints are really the same one
 AC3 can be done in polynomial time, em3
 (e = number of constraints in problem, m = domain size)
 emphasises that AC does not solve CSP’s
9
AC3
 Set Q = List of all arcs (directed constraints)
 While (Q not empty)
 remove the first arc y-x from Q
 revise the arc y-x
 I.e. if all values in Dx supported by element of Dy do nothing
 else remove any values from Dx not supported by y

for all constraints involving x (including x-y)
– add the arc x-y to Q if not already a member
 If Dx empty, fail (domain wipe out)
 If no Domain empty, succeed (established AC)
10
Exercises
 Describe AC3 as a search algorithm
 in terms of the generic search algorithm
 so what’s the search problem?
 Consider the constraints x < y, y < z, z < x
 obviously inconsistent, and AC on its own can prove this
 Show what AC3 does when Dx= Dy= Dz={1,2,3,4,5,6,7,8,9}
 Describe a constraint satisfaction problem which …
 is not arc-consistent but we can establish AC
 but the problem has no solution
11
Maintaining AC (MAC)
 Like any other propagation, we can use AC in
search
 I.e. search proceeds as follows:
 establish AC at the root
 when AC3 terminates, choose a new variable/value
 re-establish AC given the new variable choice (I.e.
maintain AC)
 repeat;
 backtrack if AC gives domain wipe out
 The hard part of implementation is undoing effects of
AC
12
Special kinds of Consistency
 Some kinds of constraint lend themselves to special
kinds of arc-consistency
 Consider the all-different constraint
 the named variables must all take different values
 not a binary constraint
 can be expressed as n(n-1)/2 not-equals constraints
 We can apply (e.g.) AC3 as usual
 But there is a much better option
13
All Different
 Suppose Dx = {2,3} = Dy, Dz = {1,2,3}
 All the constraints xy, yz, zx are all arc consistent
 e.g. x=2 supports the value z = 3
 the single ternary constraint AllDifferent(x,y,z) is not!
 We must set z = 1
 Generalised Arc Consistency applies to n-ary
constraints (but complicated and expensive)
 A special purpose algorithm exists for All-Different to
establish GAC in efficient time
 Special purpose propagation algorithms are vital
14
Formulation of CSP’s
 All-different is an example of the importance of
formulation
 all-different(x,y,z) much better than xy, yz, zx
 even though logically equivalent
 In general, it’s hard to find the best formulation
 Remember DONALD + GERALD = ROBERT
 The formulation I gave had just 2 constraints
 all-different and a complicated arithmetic constraint
 All-different fine, but neither FC nor MAC can do
much with the arithmetic constraint
15
Cryptarithmetic Revisited
 FC cannot propagate until only one variable left in
constraint
 AC cannot propagate until only two variables left
 When coded in ILOG Solver, search backtracks
8018 times
 How can we formulate the problem better?
 Hint: we’d like to consider the sum in each column
separately
16
DONALD + GERALD = ROBERT
 One solution is to add more variables to the problem
 Variables C1, C2, C3, C4, C5









Ci represents carry from previous column i
DCi = {0,1}
Now we can express more constraints
D + D = 10*C1 + T
C1 + L + L = 10*C2 + R
C2 + A + A = 10*C3 + E
C3 + N + R = 10*C4 + B
C4 + O + E = 10*C5 + O
C5 + D + G = R
17
This shouldn’t work ?!?
 We’ve made the problem bigger, so how can it
help?
 Before, there were 93 107 possibilities
 now there are 25 = 32 times as many!
 The constraints now involve fewer variables
 constraint propagation can happen sooner
 variables can be set sooner (reduced to one value)
 domain wipe out & backtracking occurs earlier
 In ILOG Solver, this encoding needs only 212
 down from 8,018
 if that doesn’t impress you, call it minutes (or hours)
18
Download