# Intelligence Artificial Intelligence Ian Gent Constraint Programming 1.

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

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

Intelligence Artificial Intelligence Part I :Constraint Satisfaction Problems Constraint Programming 1

3 Constraint Satisfaction Problems zCSP = Constraint Satisfaction Problems yAI exams, CSP =/= Communicating Sequential Processes 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 Who Cares? zMany problems can be represented as CSP’s ye.g. scheduling, timetabling, graph coloring, puzzles, supply chain management, parcel routing, party arranging … zMany Constraint Programming toolkits available yCHIP yILOG Solver [expensive commercially] yEclipse [free academically] yMozart [available as rpm for Linux]

5 Colouring as CSP zCan we colour all 4 nodes with 3 colours so that no two connected nodes the same colour? zVariable for each node yAll D i = { red, green, blue} zConstraint for each edge yall constraints of the form xx i  x j zSolution gives a colouring zIt’s a binary CSP

6 SAT as a CSP zVariable in CSP for each variable/letter in SAT zEach domain D i = {true, false} zConstraint corresponds to each clause ydisallows unique tuple which falsifies clause ye.g. (not A) or (B) or (not C) x  not zNot binary CSP unless all clauses 2-clauses

7 N-Queens as a CSP zChessboard puzzle ze.g. when n = 8… yplace 8 queens on a 8x8 chessboard so that no two attack each other zVariable x i for each row i of the board zDomain = {1, 2, 3 …, n} for position in row zConstraints are: yx i  x j queens not in same column yx i - x j  i-jqueens not in same SE diagonal yx j - x i  i - jqueens not in SW diagonal

8 Constraint Satisfaction Problems zCSP = Constraint Satisfaction Problems yAI exams, CSP =/= Communicating Sequential Processes 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

9 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

10 More Complex Constraints zConstraints don’t need to be simple x DONALD x+GERALD x= ROBERT zCryptarithmetic: all letters different and sum correct zVariables are D, O, N, A, L, G, E, R, B, T zDomains: y{0,1,2,3, …, 9} for O, N, A L,E,R,B,T y{1,2,3, …, 9} for D, G zHow do we express it?

11 Donald + Gerald = Robert zWe can write one long constraint for the sum: y100000*D + 10000*O + 1000*N + 100*A+ 10*L + D + 100000*G + 10000*E + 1000*R + 100*A+ 10*L + D = 100000*R + 10000*O + 1000*B + 100*E+ 10*R + T zBut what about the difference between variables? yCould write D =/= O, D=/=N, … B =/= T yOr express it as a single constraint on all variables yAllDifferent(D,O,N,A,L,G,E,R,B,T) zThese two constraints yexpress the problem precisely yboth involve all the 10 variables in the problem

12 Search in Constraints zThe basics of search are (guess what) the same as usual zDepth first search is the most commonly used zWhat toolkits (Solver, Mozart … ) add is ypropagation during search done automatically zIn particular they offer efficient implementations of propagation algorithms like yForward checking yMaintaining Arc consistency

13 Forward Checking zThe simplest good propagation is Forward Checking zThe idea is very simple yIf you set a variable and it is inconsistent with some other variable, backtrack! zTo do this we need to keep up to date the current state of each variable yAdd a data structure to do this, the current domain CD yInitially, CD i = D i yWhen we set variable x j = v xremove x i = u from CD i if some constraint is not consistent with both x j = v, x i = u

14 Forward Checking zFor implementation, we have to cope with undoing the effects of forward checking after backtracking yOne way is to store CD i on the stack at each depth in search, so it can be restored xexpensive on space xvery easy in languages which make copies automatically, e.g. Lisp, Prolog yAnother is to store only the changes to CD i xthen undo destructive changes to data structures on backtracking xusually faster but can be more fiddly to implement

15 Forward Checking, example zVariables x, y zDx = Dy = {1,2,3,4,5} zConstraint x < y - 1 zInitially CDx = CDy = {1,2,3,4,5} zIf we set x = 2, then: ythe only possible values are y = 4, y = 5 xso set CDy = {4,5} zIf we set x = 4, then yno possible values of y remain, I.e. CDy = { } yretract choice of x = 4 and backtrack

16 Heuristics zAs usual we need efficient variable ordering heuristics zOne is very important, like unit propagation in SAT: yIf any CD is of size 1 xwe must set that variable to the remaining value zMore generally, a common heuristic is “minimum remaining value” yI.e. choose variable with smallest size CD ymotivated by most constrained first, or also some theoretical considerations.

17 Next time zArc Consistency zSpecial kinds of constraints, like all different zFormulation of constraint problems zHow to organise a party