Artificial Intelligence Constraint Programming 1 Ian Gent ipgcs

  • Slides: 17
Download presentation
Artificial Intelligence Constraint Programming 1 Ian Gent ipg@cs. st-and. ac. uk

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

Artificial Intelligence Part I : Constraint Programming 1 Constraint Satisfaction Problems

Artificial Intelligence Part I : Constraint Programming 1 Constraint Satisfaction Problems

Constraint Satisfaction Problems z CSP = Constraint Satisfaction Problems y AI exams, CSP =/=

Constraint Satisfaction Problems z CSP = Constraint Satisfaction Problems y AI exams, CSP =/= Communicating Sequential Processes z A CSP consists of: y a set of variables, X y for each variable xi in X, a domain Di x Di is a finite set of possible values y a set of constraints restricting tuples of values x if only pairs of values, it’s a binary CSP z A solution is an assignment of a value in Di to each variable xi such that every constraint satisfied 3

Who Cares? z Many problems can be represented as CSP’s y e. g. scheduling,

Who Cares? z Many problems can be represented as CSP’s y e. g. scheduling, timetabling, graph coloring, puzzles, supply chain management, parcel routing, party arranging … z Many Constraint Programming toolkits available y y CHIP ILOG Solver [expensive commercially] Eclipse [free academically] Mozart [available as rpm for Linux] 4

Colouring as CSP z Can we colour all 4 nodes with 3 colours so

Colouring as CSP z Can we colour all 4 nodes with 3 colours so that no two connected nodes the same colour? z Variable for each node y All Di = { red, green, blue} z Constraint for each edge y all constraints of the form x xi xj z Solution gives a colouring z It’s a binary CSP 5

SAT as a CSP z Variable in CSP for each variable/letter in SAT z

SAT as a CSP z Variable in CSP for each variable/letter in SAT z Each domain Di = {true, false} z Constraint corresponds to each clause y disallows unique tuple which falsifies clause y e. g. (not A) or (B) or (not C) x not < A = true, B = false, C = true > z Not binary CSP unless all clauses 2 -clauses 6

N-Queens as a CSP z Chessboard puzzle z e. g. when n = 8…

N-Queens as a CSP z Chessboard puzzle z e. g. when n = 8… y place 8 queens on a 8 x 8 chessboard so that no two attack each other z Variable xi for each row i of the board z Domain = {1, 2, 3 … , n} for position in row z Constraints are: y xi xj y xi - xj i-j y xj - xi i - j queens not in same column queens not in same SE diagonal queens not in SW diagonal 7

Constraint Satisfaction Problems z CSP = Constraint Satisfaction Problems y AI exams, CSP =/=

Constraint Satisfaction Problems z CSP = Constraint Satisfaction Problems y AI exams, CSP =/= Communicating Sequential Processes z A CSP consists of: y a set of variables, X y for each variable xi in X, a domain Di x Di is a finite set of possible values y a set of constraints restricting tuples of values x if only pairs of values, it’s a binary CSP z A solution is an assignment of a value in Di to each variable xi such that every constraint satisfied 8

Formal Definition of Constraints z A constraint Cijk… involving variables xi, xj, xk …

Formal Definition of Constraints z A constraint Cijk… involving variables xi, xj, xk … y is any subset of combinations of values from Di, Dj, Dk … y I. e. Cijk. . . Di x Dj x Dk … y indicating the allowed set of values z Most constraint programming languages/toolkits allow a number of ways to write constraints: y e. g. if D 1 = D 2 = {1, 2, 3} … x { (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2) } x x 1 x 2 x Ct. Neq(x 1, x 2) z I’ll use whatever notation seems right at the time 9

More Complex Constraints z Constraints don’t need to be simple x x + x

More Complex Constraints z Constraints don’t need to be simple x x + x = D G R O E O N R B A A E L L R D D T z Cryptarithmetic: all letters different and sum correct z Variables are D, O, N, A, L, G, E, R, B, T z Domains: y {0, 1, 2, 3, … , 9} for O, N, A L, E, R, B, T y {1, 2, 3, … , 9} for D, G z How do we express it? 10

Donald + Gerald = Robert z We can write one long constraint for the

Donald + Gerald = Robert z We can write one long constraint for the sum: y 100000*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 z But what about the difference between variables? y Could write D =/= O, D=/=N, … B =/= T y Or express it as a single constraint on all variables y All. Different(D, O, N, A, L, G, E, R, B, T) z These two constraints y express the problem precisely y both involve all the 10 variables in the problem 11

Search in Constraints z The basics of search are (guess what) the same as

Search in Constraints z The basics of search are (guess what) the same as usual z Depth first search is the most commonly used z What toolkits (Solver, Mozart … ) add is y propagation during search done automatically z In particular they offer efficient implementations of propagation algorithms like y Forward checking y Maintaining Arc consistency 12

Forward Checking z The simplest good propagation is Forward Checking z The idea is

Forward Checking z The simplest good propagation is Forward Checking z The idea is very simple y If you set a variable and it is inconsistent with some other variable, backtrack! z To do this we need to keep up to date the current state of each variable y Add a data structure to do this, the current domain CD y Initially, CDi = Di y When we set variable xj = v x remove xi = u from CDi if some constraint is not consistent with both xj = v, xi= u 13

Forward Checking z For implementation, we have to cope with undoing the effects of

Forward Checking z For implementation, we have to cope with undoing the effects of forward checking after backtracking y One way is to store CDi on the stack at each depth in search, so it can be restored x expensive on space x very easy in languages which make copies automatically, e. g. Lisp, Prolog y Another is to store only the changes to CDi x then undo destructive changes to data structures on backtracking x usually faster but can be more fiddly to implement 14

Forward Checking, example z z z Variables x, y Dx = Dy = {1,

Forward Checking, example z z z Variables x, y Dx = Dy = {1, 2, 3, 4, 5} Constraint x < y - 1 Initially CDx = CDy = {1, 2, 3, 4, 5} If we set x = 2, then: y the only possible values are y = 4, y = 5 x so set CDy = {4, 5} z If we set x = 4, then y no possible values of y remain, I. e. CDy = { } y retract choice of x = 4 and backtrack 15

Heuristics z As usual we need efficient variable ordering heuristics z One is very

Heuristics z As usual we need efficient variable ordering heuristics z One is very important, like unit propagation in SAT: y If any CD is of size 1 x we must set that variable to the remaining value z More generally, a common heuristic is “minimum remaining value” y I. e. choose variable with smallest size CD y motivated by most constrained first, or also some theoretical considerations. 16

Next time z z Arc Consistency Special kinds of constraints, like all different Formulation

Next time z z Arc Consistency Special kinds of constraints, like all different Formulation of constraint problems How to organise a party 17