Intelligence Artificial Intelligence Ian Gent [email protected] Constraint Programming 1.

17
Artificial Intelligence Intelligence Ian Gent [email protected] Constraint Programming 1

Transcript of Intelligence Artificial Intelligence Ian Gent [email protected] Constraint Programming 1.

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

Artificial IntelligenceIntelligence

Ian [email protected]

Constraint Programming 1

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

Artificial IntelligenceIntelligence

Part I : Constraint Satisfaction Problems

Constraint Programming 1

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

3

Constraint Satisfaction Problems

CSP = Constraint Satisfaction Problems AI exams, CSP =/= Communicating Sequential Processes

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

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

4

Who Cares?

Many problems can be represented as CSP’s e.g. scheduling, timetabling, graph coloring, puzzles,

supply chain management, parcel routing, party arranging …

Many Constraint Programming toolkits available CHIP ILOG Solver [expensive commercially] Eclipse [free academically] Mozart [available as rpm for Linux]

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

5

Colouring as CSP

Can we colour all 4 nodes with 3 colours so that no two connected nodes the same colour?

Variable for each node All Di = { red, green, blue}

Constraint for each edge all constraints of the form

xi xj

Solution gives a colouring It’s a binary CSP

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

6

SAT as a CSP

Variable in CSP for each variable/letter in SATEach domain Di = {true, false}

Constraint corresponds to each clause disallows unique tuple which falsifies clause e.g. (not A) or (B) or (not C)

not < A = true, B = false, C = true >

Not binary CSP unless all clauses 2-clauses

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

7

N-Queens as a CSP

Chessboard puzzlee.g. when n = 8…

place 8 queens on a 8x8 chessboard so that no two attack each other

Variable xi for each row i of the board

Domain = {1, 2, 3 … , n} for position in rowConstraints are:

xi xj queens not in same column

xi - xj i-j queens not in same SE diagonal

xj - xi i - j queens not in SW diagonal

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

8

Constraint Satisfaction Problems

CSP = Constraint Satisfaction Problems AI exams, CSP =/= Communicating Sequential Processes

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

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

9

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

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

10

More Complex Constraints

Constraints don’t need to be simple D O N A L D+ G E R A L D= R O B E R T

Cryptarithmetic: all letters different and sum correctVariables are D, O, N, A, L, G, E, R, B, TDomains:

{0,1,2,3, … , 9} for O, N, A L,E,R,B,T {1,2,3, … , 9} for D, G

How do we express it?

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

11

Donald + Gerald = Robert

We can write one long constraint for the sum: 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

But what about the difference between variables? Could write D =/= O, D=/=N, … B =/= T Or express it as a single constraint on all variables AllDifferent(D,O,N,A,L,G,E,R,B,T)

These two constraints express the problem precisely both involve all the 10 variables in the problem

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

12

Search in Constraints

The basics of search are (guess what) the same as usual

Depth first search is the most commonly usedWhat toolkits (Solver, Mozart … ) add is

propagation during search done automatically

In particular they offer efficient implementations of propagation algorithms like Forward checking Maintaining Arc consistency

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

13

Forward Checking

The simplest good propagation is Forward CheckingThe idea is very simple

If you set a variable and it is inconsistent with some other variable, backtrack!

To do this we need to keep up to date the current state of each variable Add a data structure to do this, the current domain CD Initially, CDi = Di

When we set variable xj = v

remove xi = u from CDi if some constraint is not consistent with both xj = v, xi= u

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

14

Forward Checking

For implementation, we have to cope with undoing the effects of forward checking after backtracking One way is to store CDi on the stack at each depth in search,

so it can be restoredexpensive on spacevery easy in languages which make copies automatically,

e.g. Lisp, Prolog Another is to store only the changes to CDi

then undo destructive changes to data structures on backtracking

usually faster but can be more fiddly to implement

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

15

Forward Checking, example

Variables x, yDx = Dy = {1,2,3,4,5}Constraint x < y - 1 Initially CDx = CDy = {1,2,3,4,5} If we set x = 2, then:

the only possible values are y = 4, y = 5so set CDy = {4,5}

If we set x = 4, then no possible values of y remain, I.e. CDy = { } retract choice of x = 4 and backtrack

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

16

Heuristics

As usual we need efficient variable ordering heuristics

One is very important, like unit propagation in SAT: If any CD is of size 1

we must set that variable to the remaining value

More generally, a common heuristic is “minimum remaining value” I.e. choose variable with smallest size CD motivated by most constrained first, or also some

theoretical considerations.

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

17

Next time

Arc Consistency Special kinds of constraints, like all differentFormulation of constraint problemsHow to organise a party