710 likes | 982 Views
Course: Engineering Artificial Intelligence. Dr. Radu Marinescu. Lecture 4. Today’s class. Constraint satisfaction problems (CSP). Sudoku. Variables: 81 slots Domains = {1,2,3,4,5,6,7,8,9} Constraints: 27 not-equal. Constraint propagation. 2 3 4 6. 2.
E N D
Course:Engineering Artificial Intelligence Dr. Radu Marinescu Lecture 4
Today’s class • Constraint satisfaction problems (CSP)
Sudoku • Variables: 81 slots • Domains = {1,2,3,4,5,6,7,8,9} • Constraints: • 27 not-equal Constraint propagation 2 34 6 2 Each row, column and major block must be alldifferent “Well posed” if it has unique solution: 27 constraints
Sudoku Each row, column and major block must be alldifferent “Well posed” if it has unique solution: 27 constraints
Constraint satisfaction problems • CSP • state – is defined by variable Xi with values from domain Di • goaltest – is a set of constraints specifying allowable combinations of values for subsets of variables • Applications for CSPs • Scheduling tasks (transportation, maintenance) • Robot planning tasks • Puzzles • Tail-Gate assignment (airports) • Radio-link frequency assignment (telecom) • …
Example: map coloring • variables: WA, NT, Q, SA, NSW, V, T • domains: Di = {red, green, blue} • constraints: adjacent regions must have different colors • e.g., WA ≠ NT or (WA, NT) in {(red, green), (red, blue), (green, red), (green, blue), (blue, red), (blue, green)}
Example: map coloring • Solutions are complete and consistent assignments • E.g., WA = red, NT = green, SA = blue, Q = red, NSW = green, V = red, T = green
Constraint graph General class of problems: Binary CSP Unary constraint arc variable Vi with values in domain Di Binary constraint arc (Unary constraint just cut down domains) This diagram is called a constraint graph
Constraint graph General class of problems: Binary CSP Unary constraint arc variable Vi with values in domain Di Binary constraint arc (Unary constraint just cut down domains) This diagram is called a constraint graph Basic problem: Find dj in Di for each Vi s.t. all constraints satisfied (finding consistent labeling for variables)
N-Queens as CSP Classic “benchmark” problem Place N queens on an NxN chessboard so that none can attack the other Variables are board positions in NxN chessboard Domains Queen or blank Constraints Two positions on a line (vertical, horizontal, diagonal) cannot both be Q
Scheduling as CSP activity Choose time for activities e.g., observations on Hubble telescope, or terms to take required classes time Variables are activities Domains sets of start times (or “chunks” of time) Constraints 1. Activities that use the same resource cannot overlap in time 2. Preconditions satisfied
3-SAT as CSP The original NP-complete problem Find values for Boolean variables A, B, C, … that satisfy the formula Variables clauses Domains Boolean variable assignments that make clauses true Constraints Clauses with shared Boolean variables must agree on value of variables
Varieties of CSPs • Discrete variables • Finite domains • n variables, domain size d → O(dn) complete assignments • E.g., map coloring, n-queens, … • Infinite domains • Integers, strings • E.g., job scheduling (variables are start/end days of each job) • Continuous variables • Usually, the domain of the variables are real numbers
Varieties of constraints • Unaryconstraints involve one variable • E.g., SA ≠ green • Binaryconstraints involve pairs of variables • E.g., SA ≠ WA • Higher-orderconstraints involve 3 or more variables • E.g., crypt-arithmetic column constraints (see next)
Example: crypt-arithmetic puzzle Variables F T U O R W X1 X2 X3 Domains {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Constraints AllDiff(F, T, U, O, R, W) O + O = R + 10∙X1 X1 + W + W = U + 10∙X2 X2 + T + T = O + 10∙X3 X3 = F F ≠ 0 T ≠ 0
Good news / Bad news • Good news – very general & interesting class problems • Bad news – includes NP-hard (intractable) problems So, good behavior is a function of domain not the formulation as a CSP
CSP example Given 40 courses (8.01, 8.02, …, 6.840) and 10 terms (Fall 1, Spring 1, ,,, Spring 5). Find a legal schedule Constraints Pre-requisites Courses offered on limited terms Limited number of courses per term Avoid time conflicts Note: CSPs are not for expressing (soft) preferences, e.g. minimize difficulty, balance subject areas, etc
Choice of variables & values VARIABLESDOMAINS Legal combinations of for example 4 courses (but this is huge set of values) A. Terms? B. Terms Slots? Courses offered during that term Subdivide terms into slots e.g. 4 of them (Fall 1,1) (Fall 1,2) (Fall 1,3) (Fall 1,3) C. Courses? Terms or term slots (Term slots allow expressing constraints on limited number of courses/term)
Constraints Use courses as variables and term slots as values Term before For pairs of courses that must be ordered 6.001 6.034 Prerequisite > Term after Courses offered only in some terms > Filter domain Term not equal For pairs offered at same or overlapping times Avoid time conflicts> 6.001 6.034
Solving CSPs • Solving CSPs involve some combination of: • Constraint propagation, to eliminate values that could not be part of any solution • Search, to explore valid assignments
Constraint propagation (aka Arc Consistency) • Arc consistency eliminates values from domain of variable that can never be part of a consistent solution • Directed arc (Vi, Vj) is arc consistent if
Constraint propagation (aka Arc Consistency) • Arc consistency eliminates values from domain of variable that can never be part of a consistent solution • Directed arc (Vi, Vj) is arc consistent if • We can achieve consistency on arc by deleting values from Di (domain of variable at tail of constraint arc) that fail condition
Constraint propagation (aka Arc Consistency) • Arc consistency eliminates values from domain of variable that can never be part of a consistent solution • Directed arc (Vi, Vj) is arc consistent if • We can achieve consistency on arc by deleting values from Di (domain of variable at tail of constraint arc) that fail condition • Assume domain size at most d and there are e binary constraints • A simple algorithm for arc consistency is O(ed3) – note that just verifying arc consistency takes O(ed2) for each arc
Constraint propagation example V1 Graph coloring initial domains are indicated R, G, B different-color constraint V2 V3 R, G G
Constraint propagation example V1 Graph coloring initial domains are indicated R, G, B different-color constraint V2 V3 R, G G V1 R, G, B V2 V3 R, G G Each undirected arc is really two directed constraint arcs, the effects shown above are from examining BOTH arcs.
Constraint propagation example V1 Graph coloring initial domains are indicated R, G, B different-color constraint V2 V3 R, G G V1 R, G, B V2 V3 R, G G Each undirected arc is really two directed constraint arcs, the effects shown above are from examining BOTH arcs.
Constraint propagation example V1 Graph coloring initial domains are indicated R, G, B different-color constraint V2 V3 R, G G V1 R, B V2 V3 R, G G Each undirected arc is really two directed constraint arcs, the effects shown above are from examining BOTH arcs.
Constraint propagation example V1 Graph coloring initial domains are indicated R, G, B different-color constraint V2 V3 R, G G V1 R, B V2 V3 R G Each undirected arc is really two directed constraint arcs, the effects shown above are from examining BOTH arcs.
Constraint propagation example V1 Graph coloring initial domains are indicated R, G, B different-color constraint V2 V3 R, G G V1 B V2 V3 R G Each undirected arc is really two directed constraint arcs, the effects shown above are from examining BOTH arcs.
Constraint propagation example V1 Graph coloring initial domains are indicated R, G, B different-color constraint V2 V3 R, G G V1 B V2 V3 R G Each undirected arc is really two directed constraint arcs, the effects shown above are from examining BOTH arcs.
Constraint propagation example V1 Graph coloring initial domains are indicated R, G, B different-color constraint V2 V3 R, G G V1 B V2 V3 R G CSP is arc-consistent
But, arc consistency is not enough in general Graph coloring R, G arc consistent but no solutions R, G R, G
But, arc consistency is not enough in general Graph coloring R, G arc consistent but no solutions R, G R, G arc consistent but 2 solutions: B,R,G; B,G,R B, G R, G R, G
But, arc consistency is not enough in general Graph coloring R, G arc consistent but no solutions R, G R, G arc consistent but 2 solutions: B,R,G; B,G,R B, G R, G R, G arc consistent but 1 solutions: B,R,G; B, G Assume B, R not allowed R, G R, G
But, arc consistency is not enough in general Graph coloring R, G arc consistent but no solutions R, G R, G arc consistent but 2 solutions: B,R,G; B,G,R B, G R, G R, G Need to search to find solutions (if any) arc consistent but 1 solutions: B,R,G; B, G Assume B, R not allowed R, G R, G
Searching for solutions – backtracking (BT) • Standard search formulation • Initial state: the empty assignment {} • Successor function: assign a value to an unassigned variable that does not conflict with the current assignment • fail is no legal assignments • Goal test: the current assignment is complete • This is the same for all CSPs • Every solution appears at depth n (for n variables) • Path is irrelevant • Depth-first search for CSPs is called backtracking • Variable assignments are commutative (e.g., [WA=red then NT=blue] is the same as [NT=blue then WA=red]
Searching for solutions – backtracking (BT) When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search) R B V1assignments G R G V2assignments R G R G V3assignments R G R G R G R G V1 R, G,B V3 V2 R, G R, G
Searching for solutions – backtracking (BT) When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search) R B V1assignments G R G V2assignments R G R G V3assignments R G R G R G R G Inconsistent with V1 = R V1 R, G,B Backup at inconsistent assignment V3 V2 R, G R, G
Searching for solutions – backtracking (BT) When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search) R B V1assignments G R G V2assignments R G R G V3assignments R G R G R G R G Inconsistent with V1 = R V1 R, G,B Backup at inconsistent assignment V3 V2 R, G R, G
Searching for solutions – backtracking (BT) When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search) R B V1assignments G R G V2assignments R G R G V3assignments R G R G R G R G Inconsistent with V1 = R Inconsistent with V2 = G V1 R, G,B Backup at inconsistent assignment V3 V2 R, G R, G
Searching for solutions – backtracking (BT) When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search) R B V1assignments G R G V2assignments R G R G V3assignments R G R G R G R G consistent Inconsistent with V1 = R Inconsistent with V2 = G V1 R, G,B Backup at inconsistent assignment V3 V2 R, G R, G
Combine backtracking and constraint propagation • A node in BT tree is partial assignment in which the domain of each variable has been set (tentatively) to singleton set • Use constraint propagation (arc-consistency) to propagate the effect of this tentative assignment i.e., eliminate values inconsistent with current values
Combine backtracking and constraint propagation • A node in BT tree is partial assignment in which the domain of each variable has been set (tentatively) to singleton set • Use constraint propagation (arc-consistency) to propagate the effect of this tentative assignment i.e., eliminate values inconsistent with current values • Question: how much propagation to do?
Combine backtracking and constraint propagation • A node in BT tree is partial assignment in which the domain of each variable has been set (tentatively) to singleton set • Use constraint propagation (arc-consistency) to propagate the effect of this tentative assignment i.e., eliminate values inconsistent with current values • Question: how much propagation to do? • Answer: not much, just local propagations from domains with unique assignments, which is called forward checking (FC). This conclusion is not necessarily obvious, but it generally holds in practice
Backtracking with forward checking (BT-FC) When examining assignment Vi = dk, remove any values inconsistent with that assignment From neighboring domains in the constraint graph R V1assignments V2assignments V3assignments V1 R, G,B V3 V2 R, G R, G
Backtracking with forward checking (BT-FC) When examining assignment Vi = dk, remove any values inconsistent with that assignment From neighboring domains in the constraint graph R V1assignments V2assignments G V3assignments V1 R V3 V2 G G
Backtracking with forward checking (BT-FC) When examining assignment Vi = dk, remove any values inconsistent with that assignment From neighboring domains in the constraint graph R V1assignments V2assignments G V3assignments V1 R We have a conflict whenever a domain becomes empty V3 V2 G
Backtracking with forward checking (BT-FC) When examining assignment Vi = dk, remove any values inconsistent with that assignment From neighboring domains in the constraint graph V1assignments G V2assignments V3assignments When backing up, need to restore domain values, since deletions were done to reach consistency with tentative assignments considered during search V1 R, G,B V3 V2 R, G R, G
Backtracking with forward checking (BT-FC) When examining assignment Vi = dk, remove any values inconsistent with that assignment From neighboring domains in the constraint graph V1assignments G V2assignments V3assignments V1 G V3 V2 R R
Backtracking with forward checking (BT-FC) When examining assignment Vi = dk, remove any values inconsistent with that assignment From neighboring domains in the constraint graph V1assignments G R V2assignments V3assignments V1 G V3 V2 R R