36
Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Embed Size (px)

Citation preview

Page 1: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Artificial IntelligenceConstraint satisfaction problems

Fall 2008

professor: Luigi Ceccaroni

Page 2: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Problem characterization

• A constraint satisfaction problem (or CSP) is a special kind of problem that satisfies some additional structural properties beyond the basic requirements for problems in general.

• In a CSP, the states are defined by the values of a set of variables and the goal test specifies a set of constraints that the values have to obey.

Page 3: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Problem characterization

• State components:– Variables– Domains (possible values for the variables)– (Binary) constraints between variables

• Goal: to find a state (a complete assignment of values to variables), which satisfies the constraints

• Examples:– map coloring– crossword puzzles– n-queens– resource assignment/distribution/location

Page 4: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Representation

• State = constraint graph– variables (n) = node tags– domains = node content– constraints = directed and tagged arcs between nodes

• Example: map coloring

C1

C2 C4

C3

C1

C2

C3 C4

blue, red, green blue, green

blue

blue, red

initial state

Page 5: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Representation

• In the search tree, a variable is assigned at each level.

• Solutions have to be complete assignment, therefore they appear at depth n, the number of variable and maximum depth of the tree.

• Depth-first search algorithms are popular in CSPs.

• The simplest class of CSP (map coloring, n-queens) are characterized by:– discrete variables– finite domains

Page 6: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Finite domains

• If the maximum size of the domain of any variable is d, then the number of possible complete assignments is O(dn), exponential in the number of variables.

• CSPs with finite domain include Boolean CSPs, whose variables can only be true or false.

• In most practical applications, CSP algorithms can solve problems with domains orders of magnitude larger than the ones solvable by uninformed search algorithms.

Page 7: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Infinite domains

• With infinite domains (e.g., integers and strings), to describe constraints enumerating all legal combinations of values is not possible.

• A constraint language has to be used, for example:

– job-start1 + 5 ≤ job-start3

• There exist algorithms for linear constraints over integer variables.

• No algorithm exists for general non-linear constraints over integer variables.

• In cases, infinite-domain problems can be reduced to a finite domain, just restricting the values of all variables (e.g., setting limits to the dates in which jobs can start).

Page 8: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Search-based algorithms

• Incremental formulation: – Initial state: empty assignment { }, with all unassigned variables– Successor function: assignment of a value to any variable not

yet assigned, provided it does not conflict with assigned variables

– Goal test: check if the current assignment is complete– Path cost: a constant cost (e.g., 1) for every step

• Complete formulation: – Each state is a complete assignment, which satisfies or not the

constraints.– Local-search methods work well in this case.

• Constraint propagation:– Before the search– During the search

Page 9: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Constraints

• The simplest type is the unary constraint, which constraints the values of just one variable.

• A binary constraint relates two variables.• Higher-order constraints involve three or more

variables. Cryptarithmetic puzzles are an example:

Page 10: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Cryptarithmetic puzzles

• Variables: F, T, U, W, R, O, X1, X2, X3

• Domains: {0,1,2,3,4,5,6,7,8,9}

• Constraints:– Alldiff (F,T,U,W,R,O)

– O + O = R + 10 · X1

– X1 + W + W = U + 10 · X2

– X2 + T + T = O + 10 · X3

– X3 = F, T ≠ 0, F ≠ 0

Page 11: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Depth-first search with backtracking

• Standard depth-first search on a CSP wastes time searching when constraints have already been violated.

• Because of the way that the operators have been defined, an operator can never redeem a constraint that has already been violated.

• A first improvement is:– To test constraints after each variable assignment– If all possible values violate some constraint, then the

algorithm backtracks to the last valid assignment• Variables are classified as: past, current, future.

Page 12: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Backtracking search algorithm

Page 13: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Backtracking search algorithm1. Set each variable as undefined. Empty stack. All variables are future

variables.2. Select a future variable as current variable.

If it exists, delete it from FUTURE and stack it (top = current variable),if not, the assignment is a solution.

3. Select an unused value for the current variable.If it exists, mark the value as used,if not, set current variable as undefined,

mark all its values as unused,unstack the variable and add it to FUTURE,if stack is empty, there is no solution,if not, go to 3.

4. Test constraints between past variables and the current one.If they are satisfied, go to 2,if not, go to 3.

(It is possible to use heuristics to select variables (2.) and values (3.).

Page 14: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Backtracking search algorithm

Page 15: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Example: 4-queens

• Place 4 queens, one per row, so that they do not attack each others– Variables: R1 … R4 (queens)– Domains: [1 … 4] for each Ri (columns)– Constraints: Ri does not attack Rj

not attackingRi Rj[1 .. 4] [1 .. 4]

Page 16: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

R1=1

R2=1 NOR2=2 NOR2=3

R3=1 NOR3=2 NOR3=3 NOR3=4 NO

R2=4

R3=1 NOR3=2

R4=1 NOR4=2 NOR4=3 NOR4=4 NO

R1=2

R2=1 NOR2=2 NOR2=3 NOR2=4

R3=1

R4=1 NOR4=2 NOR4=3

Backtracking R2

Backtracking R3, R2, R1

Page 17: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Propagating information through constraints

• So far the algorithm considers the constraints on a variable only at the time that the variable is chosen (e.g., by Select-Unassigned-Variable).

• By looking at some of the constraints earlier in the search, or even before the search has started, the search space can be drastically reduced.

Page 18: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Forward checking

• A way to make better use of constraints during search.

• Whenever a variable X is assigned:– the forward checking process looks at each

unassigned variable Y that is connected to X by a constraint and

– deletes from Y’s domain any value that is inconsistent with the value chosen for X.

Page 19: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Forward checking algorithm

Page 20: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Forward checking: example

Page 21: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Forward checking: example

Page 22: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Forward checking: example

Page 23: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Forward checking: example

• Idea: – Keep track of remaining legal values for unassigned variables– Terminate search when any variable has no legal values

Page 24: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Forward checking: example

• Idea: – Keep track of remaining legal values for unassigned variables– Terminate search when any variable has no legal values

Page 25: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Forward checking: example

• Idea: – Keep track of remaining legal values for unassigned variables– Terminate search when any variable has no legal values

Page 26: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Forward checking: example

• Idea: – Keep track of remaining legal values for unassigned variables– Terminate search when any variable has no legal values

Page 27: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

R1=1? propagation: R2=3,4; R3=2,4; R4=2,3 R1=1

R2=3? propagation : R3=R2=4? propagation : R3=2; R4=3 R2=4

R3=2? propagation : R4= No other value for R3. Backtracking to R2

No other value for R2. Backtracking to R1

R1=2? propagation : R2=4; R3=1,3; R4=1,3,4 R1=2

R2=4? propagation : R3=1; R4=1,3 R2=4

R3=1? propagation : R4=3 R3=1

R4=3? No propagations R4=3

Forward checking: 4-queens example

Page 28: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Constraint propagation

• Forward checking propagates information from assigned to unassigned variables, but doesn't provide early detection for all failures:

• NT and SA cannot both be blue!

• Constraint propagation repeatedly enforces constraints locally

Page 29: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Constraint propagation

• Forward checking does not detect the “blue” inconsistency, because it does not look far enough ahead.

• Constraint propagation is the general term for propagating the implications of a constraint on one variable onto other variables.

• The idea of arc consistency provides a fast method of constraint propagation that is substantially stronger than forward checking.

Page 30: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Arc consistency

• Simplest form of propagation makes each arc consistent• X Y is consistent iff

for every value x of X there is some allowed y

Page 31: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Arc consistency

• Simplest form of propagation makes each arc consistent• X Y is consistent iff

for every value x of X there is some allowed y

Page 32: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Arc consistency

• Simplest form of propagation makes each arc consistent• X Y is consistent iff

for every value x of X there is some allowed y

• If X loses a value, neighbors of X need to be rechecked.

•–

Page 33: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Arc consistency

• Simplest form of propagation makes each arc consistent• X Y is consistent iff

for every value x of X there is some allowed y

• If X loses a value, neighbors of X need to be rechecked• Arc consistency detects failure earlier than forward checking• Can be run as a preprocess or after each assignment

•–

Page 34: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Algorithm for arc consistency

Page 35: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

Algorithm for arc consistency: AC-3

• It uses a queue to keep track of the arcs that need to be checked for inconsistency.

• Each arc (Xi, Xj) in turn is removed from the agenda and checked.

• If any values need to be deleted from the domain of Xi, then every arc (Xk, Xj) pointing to Xi must be reinserted on the queue for checking.

Page 36: Artificial Intelligence Constraint satisfaction problems Fall 2008 professor: Luigi Ceccaroni

(C1,C2): eliminate AZUL

(C2,C1): ok

(C2,C3): ok

(C3,C2): eliminate AZUL

(C2,C4): ok

(C4,C2): eliminate AZUL

(C3,C4): eliminate VERDE

add (C2,C3)

(C4,C3): ok

(C2,C3): ok

C1

C2

C3 C4

AZUL, ROJO, VERDE AZUL, VERDE

AZUL

AZUL, ROJO

Initial list:(C1,C2), (C2,C1), (C2,C3), (C3,C2), (C2,C4), (C4,C2), (C3,C4), (C4,C3)

Algorithm for arc consistency: AC-3