# Intelligence Artificial Intelligence Ian Gent Constraint Programming 2.

## Presentation on theme: "Intelligence Artificial Intelligence Ian Gent Constraint Programming 2."— Presentation transcript:

Intelligence Artificial Intelligence Ian Gent ipg@cs.st-and.ac.uk Constraint Programming 2

Intelligence Artificial Intelligence Part I :Arc Consistency Part II: MAC Part III: Special kinds of constraints Part IV:Formulation Constraint Programming 2

3 Constraint Satisfaction Problems zCSP = Constraint Satisfaction Problems zA CSP consists of: ya set of variables, X yfor each variable x i in X, a domain D i xD i is a finite set of possible values ya set of constraints restricting tuples of values xif only pairs of values, it’s a binary CSP zA solution is an assignment of a value in D i to each variable x i such that every constraint satisfied

4 Formal Definition of Constraints zA constraint C ijk… involving variables x i, x j, x k … yis any subset of combinations of values from D i, D j, D k … yI.e. C ijk...  D i x D j x D k … yindicating the allowed set of values zMost constraint programming languages/toolkits allow a number of ways to write constraints: ye.g. if D 1 = D 2 = {1,2,3} … x{ (1,2), (1,3), (2,1), (2,3), (3,1), (3,2) } xx 1  x 2 xCtNeq(x1,x2) zI’ll use whatever notation seems right at the time

5 Arc Consistency zLast time, we saw forward checking yextremely simple form of reasoning xif variable x has value v x& constraint involves variables x-y x& constraint disallows x=v, y = u, xthen remove value u from domain of y zThis time, look at a more complex form of reasoning yarc consistency zIn graph theory, arc = edge zIn constraints, arc will be a (directed) constraint

6 Arc Consistency zArc consistency is based on a very simple concept yif we can look at just one constraint and see that x=v is impossible … yobviously we can remove the value x=v from consideration zHow do we know a value is impossible? zIf the constraint provides no support for the value ze.g. if D x = {1,4,5} and D y = {1, 2, 3} ythen the constraint x > y provides no support for x=1 ywe can remove x=1 from D x

7 Arc Consistency Propagation zWhen we remove a value from D x, we may get new removals because of it zE.g. D x = {1,4,5}, D y = {1, 2, 3}, D z = {2, 3, 4, 5} yx > y, z > x yAs before we can remove 1 from D x, so D x = {4,5} yBut now there is no support for D z = 2,3,4 ySo we can remove those values, D z = {5}, so z=5 yBefore AC applied to y-x, we could not change D z zThis can cause a chain reaction

8 Establishing Arc Consistency zIf we consider every constraint and full propagation, there are only two possibilities: ythe final problem will be arc-consistent xevery value in every domain is supported by some other value in every constraint xwe have established arc consistency yat some point some variable will have an empty domain xno value can be given to that variable xthe problem is insoluble due to arc consistency xwe have seen a domain wipe out zA number of algorithms can be used to establish AC

9 AC3 zAC3 is one of many algorithms (1 - 7 and lots more!) zOnly consider binary constraints with 2 variables zFor this algorithm, we revise directed arcs yI.e. when considering constraint x-y we remove values from y xwhen considering y-x we remove values from x yof course both constraints are really the same one zAC3 can be done in polynomial time, em 3 y(e = number of constraints in problem, m = domain size) yemphasises that AC does not solve CSP’s

10 AC3 zSet Q = List of all arcs (directed constraints) zWhile (Q not empty) yremove the first arc y-x from Q yrevise the arc y-x xI.e. if all values in D x supported by element of D y do nothing xelse remove any values from D x not supported by y x for all constraints involving x (including x-y) –add the arc x-y to Q if not already a member yIf D x empty, fail (domain wipe out) zIf no Domain empty, succeed (established AC)

11 Exercises zDescribe AC3 as a search algorithm yin terms of the generic search algorithm yso what’s the search problem? zConsider the constraints x < y, y < z, z < x yobviously inconsistent, and AC on its own can prove this yShow what AC3 does when D x = D y = D z ={1,2,3,4,5,6,7,8,9} zDescribe a constraint satisfaction problem which … yis not arc-consistent but we can establish AC ybut the problem has no solution

12 Maintaining AC (MAC) zLike any other propagation, we can use AC in search zI.e. search proceeds as follows: yestablish AC at the root ywhen AC3 terminates, choose a new variable/value yre-establish AC given the new variable choice (I.e. maintain AC) yrepeat; ybacktrack if AC gives domain wipe out zThe hard part of implementation is undoing effects of AC

13 Special kinds of Consistency zSome kinds of constraint lend themselves to special kinds of arc-consistency zConsider the all-different constraint ythe named variables must all take different values ynot a binary constraint ycan be expressed as n(n-1)/2 not-equals constraints zWe can apply (e.g.) AC3 as usual zBut there is a much better option

14 All Different zSuppose D x = {2,3} = D y, D z = {1,2,3} zAll the constraints x  y, y  z, z  x are all arc consistent ye.g. x=2 supports the value z = 3 zthe single ternary constraint AllDifferent(x,y,z) is not! yWe must set z = 1 zGeneralised Arc Consistency applies to n-ary constraints (but complicated and expensive) zA special purpose algorithm exists for All-Different to establish GAC in efficient time zSpecial purpose propagation algorithms are vital

15 Formulation of CSP’s zAll-different is an example of the importance of formulation yall-different(x,y,z) much better than x  y, y  z, z  x yeven though logically equivalent zIn general, it’s hard to find the best formulation zRemember DONALD + GERALD = ROBERT zThe formulation I gave had just 2 constraints yall-different and a complicated arithmetic constraint zAll-different fine, but neither FC nor MAC can do much with the arithmetic constraint

16 Cryptarithmetic Revisited zFC cannot propagate until only one variable left in constraint zAC cannot propagate until only two variables left zWhen coded in ILOG Solver, search backtracks 8018 times zHow can we formulate the problem better? zHint: we’d like to consider the sum in each column separately

17 DONALD + GERALD = ROBERT zOne solution is to add more variables to the problem zVariables C1, C2, C3, C4, C5 yCi represents carry from previous column i yD Ci = {0,1} yNow we can express more constraints y D + D = 10*C1 + T yC1 + L + L = 10*C2 + R yC2 + A + A = 10*C3 + E yC3 + N + R = 10*C4 + B yC4 + O + E = 10*C5 + O yC5 + D + G = R

18 This shouldn’t work ?!? zWe’ve made the problem bigger, so how can it help? zBefore, there were 9 3 10 7 possibilities ynow there are 2 5 = 32 times as many! zThe constraints now involve fewer variables yconstraint propagation can happen sooner yvariables can be set sooner (reduced to one value) ydomain wipe out & backtracking occurs earlier zIn ILOG Solver, this encoding needs only 212 ydown from 8,018 yif that doesn’t impress you, call it minutes (or hours)