360 likes | 374 Views
Artificial Intelligence Constraint satisfaction problems. Fall 2008 professor: Luigi Ceccaroni. Problem characterization.
E N D
Artificial IntelligenceConstraint 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.
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
Representation • State = constraint graph • variables (n) = node tags • domains = node content • constraints = directed and tagged arcs between nodes • Example: map coloring blue, red C1 C2 blue C3 C1 C3 C4 C2 C4 blue, red, green blue, green initial state
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
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.
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).
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
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:
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
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.
Backtracking search algorithm • Set each variable as undefined. Empty stack. All variables are future variables. • 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. • 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. • 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.).
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 attacking Ri Rj [1 .. 4] [1 .. 4]
R1=1 R1=2 R2=1 NO R2=2 NO R2=3 R2=4 R2=1 NO R2=2 NO R2=3 NO R2=4 R3=1 NO R3=2 NO R3=3 NO R3=4 NO R3=1 NO R3=2 R3=1 R4=1 NO R4=2 NO R4=3 NO R4=4 NO R4=1 NO R4=2 NO R4=3 Backtracking R2 Backtracking R3, R2, R1
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.
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.
Forward checking: example • Idea: • Keep track of remaining legal values for unassigned variables • Terminate search when any variable has no legal values
Forward checking: example • Idea: • Keep track of remaining legal values for unassigned variables • Terminate search when any variable has no legal values
Forward checking: example • Idea: • Keep track of remaining legal values for unassigned variables • Terminate search when any variable has no legal values
Forward checking: example • Idea: • Keep track of remaining legal values for unassigned variables • Terminate search when any variable has no legal values
Forward checking: 4-queens example 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
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
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.
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
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
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 • 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
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.
Algorithm for arc consistency: AC-3 (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 AZUL, ROJO C1 C2 AZUL C3 C4 AZUL, ROJO, VERDE AZUL, VERDE Initial list: (C1,C2), (C2,C1), (C2,C3), (C3,C2), (C2,C4), (C4,C2), (C3,C4), (C4,C3)