CMU-Q Lecture 7: Searching in solution space Constraint Satisfaction Problems (CSPs) Teacher: Gianni A. Di Caro

Similar documents
Lecture 6: Constraint Satisfaction Problems (CSPs)

DIT411/TIN175, Artificial Intelligence. Peter Ljunglöf. 30 January, 2018

What is Search For? CS 188: Artificial Intelligence. Constraint Satisfaction Problems

CS 343: Artificial Intelligence

Constraint Satisfaction

Constraint Satisfaction Problems

CS 188: Artificial Intelligence Fall 2011

Constraint Satisfaction. AI Slides (5e) c Lin

Constraint Satisfaction Problems. A Quick Overview (based on AIMA book slides)

Artificial Intelligence Constraint Satisfaction Problems

Announcements. Homework 4. Project 3. Due tonight at 11:59pm. Due 3/8 at 4:00pm

Constraint Satisfaction Problems

Constraint Satisfaction Problems. Chapter 6

Foundations of Artificial Intelligence

Australia Western Australia Western Territory Northern Territory Northern Australia South Australia South Tasmania Queensland Tasmania Victoria

Announcements. CS 188: Artificial Intelligence Fall 2010

Constraint Satisfaction Problems

Constraint Satisfaction Problems

Constraint Satisfaction Problems

Announcements. Homework 1: Search. Project 1: Search. Midterm date and time has been set:

CS 188: Artificial Intelligence. What is Search For? Constraint Satisfaction Problems. Constraint Satisfaction Problems

Constraint Satisfaction Problems. Chapter 6

Constraint Satisfaction Problems Part 2

CS 4100 // artificial intelligence

Constraint Satisfaction Problems

CSE 473: Artificial Intelligence

10/11/2017. Constraint Satisfaction Problems II. Review: CSP Representations. Heuristic 1: Most constrained variable

Constraint Satisfaction Problems

Lecture 18. Questions? Monday, February 20 CS 430 Artificial Intelligence - Lecture 18 1

Chapter 6 Constraint Satisfaction Problems

Constraint Satisfaction Problems

Example: Map-Coloring. Constraint Satisfaction Problems Western Australia. Example: Map-Coloring contd. Outline. Constraint graph

Solving Problems by Searching: Constraint Satisfaction Problems

CS 771 Artificial Intelligence. Constraint Satisfaction Problem

What is Search For? CSE 473: Artificial Intelligence. Example: N-Queens. Example: N-Queens. Example: Map-Coloring 4/7/17

Constraint Satisfaction Problems

Constraint satisfaction problems. CS171, Winter 2018 Introduction to Artificial Intelligence Prof. Richard Lathrop

CS W4701 Artificial Intelligence

Constraint Satisfaction Problems. slides from: Padhraic Smyth, Bryan Low, S. Russell and P. Norvig, Jean-Claude Latombe

CONSTRAINT SATISFACTION

Artificial Intelligence

Reading: Chapter 6 (3 rd ed.); Chapter 5 (2 nd ed.) For next week: Thursday: Chapter 8

Constraint Satisfaction Problems

Outline. Best-first search

CS 4100/5100: Foundations of AI

ARTIFICIAL INTELLIGENCE (CS 370D)

Spezielle Themen der Künstlichen Intelligenz

Constraint Satisfaction Problems (CSPs)

CS 188: Artificial Intelligence. Recap: Search

CS 188: Artificial Intelligence Fall 2008

Announcements. CS 188: Artificial Intelligence Fall Large Scale: Problems with A* What is Search For? Example: N-Queens

CS 188: Artificial Intelligence Fall 2011

Space of Search Strategies. CSE 573: Artificial Intelligence. Constraint Satisfaction. Recap: Search Problem. Example: Map-Coloring 11/30/2012

Outline. Best-first search

Constraints. CSC 411: AI Fall NC State University 1 / 53. Constraints

General Methods and Search Algorithms

What is Search For? CS 188: Artificial Intelligence. Example: Map Coloring. Example: N-Queens. Example: N-Queens. Constraint Satisfaction Problems

Week 8: Constraint Satisfaction Problems

Announcements. CS 188: Artificial Intelligence Spring Today. Example: Map-Coloring. Example: Cryptarithmetic.

Constraint Satisfaction. CS 486/686: Introduction to Artificial Intelligence

Constraint Satisfaction Problems (CSPs) Introduction and Backtracking Search

Constraint Satisfaction Problems. Some material from: D Lin, J You, JC Latombe

CMU 15-7/381 CSPs. Teachers: Ariel Procaccia Emma Brunskill (THIS TIME) With thanks to Ariel Procaccia and other prior instructions for slides

Constraint Satisfaction Problems

Lars Schmidt-Thieme, Information Systems and Machine Learning Lab (ISMLL), University of Hildesheim, Germany, Course on Artificial Intelligence,

Constraint Satisfaction Problems

Constraint (Logic) Programming

CONSTRAINT SATISFACTION

Today. Introduction to Artificial Intelligence COMP 3501 / COMP Lecture 5. Constraint Satisfaction Problems (CSP) CSP Definition

Announcements. Reminder: CSPs. Today. Example: N-Queens. Example: Map-Coloring. Introduction to Artificial Intelligence

DIT411/TIN175, Artificial Intelligence. Peter Ljunglöf. 6 February, 2018

Announcements. CS 188: Artificial Intelligence Fall Reminder: CSPs. Today. Example: 3-SAT. Example: Boolean Satisfiability.

CS 188: Artificial Intelligence Fall 2008

Constraint satisfaction search. Combinatorial optimization search.

Games and Adversarial Search II Alpha-Beta Pruning (AIMA 5.3)

What is Search For? CS 188: Ar)ficial Intelligence. Constraint Sa)sfac)on Problems Sep 14, 2015

Constraint Satisfaction Problems

Artificial Intelligence

Artificial Intelligence

CS 188: Artificial Intelligence Spring Announcements

Comments about assign 1. Quick search recap. Constraint Satisfaction Problems (CSPs) Search uninformed BFS, DFS, IDS. Adversarial search

Mathematical Programming Formulations, Constraint Programming

Lecture 9 Arc Consistency

Example: Map coloring

Announcements. CS 188: Artificial Intelligence Spring Today. A* Review. Consistency. A* Graph Search Gone Wrong

Set 5: Constraint Satisfaction Problems Chapter 6 R&N

Recap: Search Problem. CSE 473: Artificial Intelligence. Space of Search Strategies. Constraint Satisfaction. Example: N-Queens 4/9/2012

6.034 Quiz 1, Spring 2004 Solutions

1 Tree Search (12 points)

Set 5: Constraint Satisfaction Problems

Moving to a different formalism... SEND + MORE MONEY

Constraint Satisfaction Problems (CSPs)

Constraint Satisfaction Problems (Backtracking Search)

CS 188: Artificial Intelligence

Material. Thought Question. Outline For Today. Example: Map-Coloring EE562 ARTIFICIAL INTELLIGENCE FOR ENGINEERS

CS 188: Artificial Intelligence

4 Search Problem formulation (23 points)

CS 188: Artificial Intelligence Spring Announcements

Today. CS 188: Artificial Intelligence Fall Example: Boolean Satisfiability. Reminder: CSPs. Example: 3-SAT. CSPs: Queries.

Discussion Section Week 1

Transcription:

CMU-Q 15-381 Lecture 7: Searching in solution space Constraint Satisfaction Problems (CSPs) Teacher: Gianni A. Di Caro

AI PLANNING APPROACHES SO FAR Goal: Find the (best) sequence of actions that take from a start state to a goal state (under full observability and deterministic assumptions) Atomic state representations: Uninformed/Informed Search algorithms, minimum cost plans, state is indivisible (~black-box) State 1 State 2 State i State n 1 2 i n Doha Tehran Ankara Athens Rome 3564b1287 35641b287 356412b87 12345678b 2 24 245 245163 2

AI PLANNING APPROACHES SO FAR Goal: Find the (best) sequence of actions that take from a start state to a goal state (under full observability and deterministic assumptions) Factored state representations: Propositional logic, STRIPS language, binary variables, feasibility, Planning Graph, GRAPHPLAN, domain-independent heuristics for informed search algorithms Proposition 1 Proposition 2 vv Proposition i Proposition 1 Proposition 1 Proposition 2 Proposition 2 t=1 vv vv t=2 Proposition i Proposition n t=3 Proposition j Proposition j At(Plane, JFK) In(John, Taxi) Handling(Pen) Having(Cake) Having(Money) 3

SO FAR, SOLUTION = PATH TO A GOAL STATE Plan ~ Sequence of actions ~ Path in the state space http://kociemba.org/twophase.htm 4

PATH SEARCH VS. SOLUTION STATE SEARCH Factored states (to exploit state structure) State: a set of n variables What if path doesn t matter? (i.e., t disappears ) Goal is not given as an explicit configuration Plan: o Before: Ordered set of actions to reach a givel goal Resulting in a sequence of states, where each state can have a different number of components (e.g., different # of true propositions) o Now: Action set ~ Set of assignment of values to n state variables Simultaneous, in parallel, in any sequence n is fixed, the cardinality of the state set doesn t change State variables ~ Decision variables that need to be set to feasible values o Result of actions/decisions not in conflict with each other Every different assignment of values result into a different state 5

PATH SEARCH VS. SOLUTION STATE SEARCH State set = Set of all state variable configurations in terms of values assigned from their domains of definition Each configuration is a potential candidate solution to the problem, but only the feasible ones are the actual solutions to the problem Search in the solution set Feasible state set = Subset of all configurations / assignments that do not violate any constraint A assignment of values to state variable is said consistent if it is feasible Solution / Plan: ~ Consistent assignment to the n state/decision variables 6

CHALLENGES IN SOLUTION STATES PROBLEMS Some (most) subsets of assignments can be unfeasible based on a set of given constraints o Challenge: Find even one feasible solution assignment, a goal configuration Feasible solutions are relatively easy to generate, but their number is overwhelming (typically exponential in problem description) and they have different values (cost or objective/reward) o Challenge: Find the best assignment (min cost or max reward) In order to describe the factored problem states, variables of any type can be part of the state description o Challenge: Deal with variables ranging over different, nasty, domains Constraint Satisfaction Problems: Find configurations satisfying (all or the highest number of) constraints Optimization problems: Find optimal configuration (objective/cost) 7

CONSTRAINT SATISFACTION PROBLEMS (CSP) Set of decision Variables: V = {V 1,..,V N } Domains: Sets of D i possible values for each variable V i Set of Constraints: C = {C 1,..,C K } restricting the values the variables can simultaneously take (e.g., Not [V 1 =1 V 5 = 3 V 7 = B] ) A constraint consists of: Variable tuple (e.g., (V 1, V 5, V 7 ) ) and: List of (not)feasible values for tuple (e.g., [ (V 2,V 3 ), { (R,B), (R,G) } ]) Or functional relations (e.g., V 2 V 3, V 1 > V 4 + 5) Goal: find an assignment of values to all variables such that no constraint is violated (additional, cost-related goals can be included) Exploiting factored states, a CSP formulation allows useful generalpurpose algorithms with more power than standard search algorithms 8

EXAMPLE: N-QUEENS Put n queens on an n n board with no two queens attack each other: not on same row, column, or diagonal State: Position of the n queens, one per column (or row) Variables: Q i position of queen in column i Domains: {1,, 8} Constraints: No queen attack each other Q i = k Q j k, j = 1,, 8, j i Similar constraints for diagonals Q 1 Q 2 Q 3 Q 4 Q 5 Q 6 Q 7 Q 8 n = 8 4,426,165,368 state configurations, only 92 feasible 9

CSP EXAMPLE: MAP COLORING Given n different colors, color a map so that adjacent areas are different colors 10

MAP COLORING Variables WA, NT, Q, NSW, V, T, SA Domain Constraints All region borders have different colors Solutions 11

EXAMPLE: SUDOKU Variables: X ij, each open square Domain: {1:9} Constraints: 9-way all diff col 9-way all diff row 9-way all diff box 12

JOB SCHEDULING (SPECIFIC CASE) A set of J jobs, J 1,, J n A set of R resources, R 1, R 2,, R m to do the jobs Each job j is a sequence of operations O j 1,..., O j Lj to be scheduled according to process plans: O j 1 O j 2 O j 3. Each operation has a fixed processing time and requires the use of resources R i, a resource can have capacity constraints Each job has a ready time and a due time A resource can only be used by a single operation at a time. All jobs must be completed by a due time. Problem: assign a start time to each job such that all jobs are completed by their due times respecting all constraints 13

JOB SCHEDULING 14

CLASS SCHEDULING 4 more required classes to graduate A: Algorithms B: Bayesian Learning C: Computer Programming D: Distributed Computing A few restrictions Algorithms must be taken same semester as Distributed computing Computer programming is a prereq for Distributed computing and Bayesian learning, so it must be taken in an earlier semester Advanced algorithms and Bayesian Learning are always offered at the same time, so they cannot be taken the same semester 3 semesters (semester 1,2,3) when can take classes 15

DEFINE CSP 4 more required classes to graduate: A, B, C, D A must be taken same semester as D C is a prereq for D and B so must take C earlier than D & B A & B are always offered at the same time, so they cannot be taken the same semester 3 semesters (semester 1,2,3) when can take classes 16

DEFINE CSP 4 more required classes to graduate: A, B, C, D A must be taken same semester as D C is a prereq for D and B so must take C earlier than D & B A & B are always offered at the same time, so they cannot be taken the same semester 3 semesters (semester 1,2,3) when can take classes Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, A=D, C < B, C < D 17

TYPES OF CSPS Discrete-domain variables (Constraint programming) Finite domains (Map coloring, Sudoku, N-queens, SAT) Our focus! Infinite domains (Integers or strings, deadline-free JSS) Constraint language is needed to understand relations J 1 +d 1 J 2 without enumerating all tuples Integer programming methods deal effectively with (integer, binary) problems with linear constraints Continuous variables (planning, blending, positioning, ) Linear/convex programming for linear/convex constraints 18

TYPES OF CONSTRAINTS Unary: involve a single variable Binary: involve two variables n-ary: involve n variables Soft constraints: violation incurs a cost, the problem becomes a constraint optimization one 19

CONSTRAINT GRAPH Variables Vertices Constraints Edges A < 2 Unary: Self-edges Binary: regular edges A B n-ary: hyperedges (hypergraphs) nodes with hyperedges are indicated by A > C C B C Q WA NT NS W F T U W R O SA V T C 3 C 2 C 1 20

CRYPTARITHMETIC PUZZLES Base-10 arithmetic + TWO TWO = F T U W R O F OU R V = {O,W,T,R,U,F} D = {0,, 9} C 3 C 2 C 1 10 0 (O+O) + 10 1 (W+W)+10 2 (T+T) = 10 0 R+10 1 U+10 2 O+10 3 F {O+O = R+10C 1, C 1 +W+W=U+10C 2, C 2 +T+T=O+10C 3, C 3 = F} V = {O,W,T,R,U,F, C 1, C 2, C 3 } Auxiliary vars 21

BINARY CONSTRAINT GRAPHS It s always possible to reduce a hypergraph to a binary constraint graph! http://ktiml.mff.cuni.cz/~bartak/constraints/binary.html But this is not always the best thing to do. If you want to know more On the Conversion between Non-Binary and Binary Constraint Satisfaction Problems. Bacchus, F. and van Beek, P. In Proceedings of the 15th AAAI Conference on Artificial Intelligence (AAAI-1998), pages 310-318, 1998. 22

SOLVING CSPS: BASIC SEARCH WITH SEARCH ALGORITHMS? States: Partial assignments to the n variables Initial state: Empty state Action: Select an unassigned variable i and assign a feasible value from its domain D i to it (not wrt other vars) Goal test: Assignment consistent (no violations) and complete (all variables assigned) Step cost: Constant Solution is found at depth n, using depth-limited DFS Size of the search tree? 23

WHY NOT JUST DO BASIC SEARCH? n = 3 variables each taking d = 3 values??? N 1 = 9 = nd 1?? 2?? 3???1??2??3???1??2??3 11? 12? 13? 1?1 1?2 1?3 21? 22? 23? 2?1 2?2 2?3 31? 32? 33? 3?1 3?2 3?3 11? 21? 31??11?12?13 12? 22? 32??21?22?23 13? 23? 33??31?32?33 1?1 2?1 3?1?11?21?31 1?2 2?2 3?2?12?22?32 1?3 2?3 3?3?13?23?33 N 2 = N 1 n 1 d = nd n 1 d = n n 1 d 2 = 9 2 3 = 54 N 3 = N 2 n 2 d = n(n 1)(n 2)d 3 = 162 24

WHY NOT JUST DO BASIC SEARCH? n = 4 variables each taking d = 4 values Branching factor level 1: b = nd b=(n-1)d nd nodes in level 1 Each of the nd nodes has n 1 d children in level 2 Each of the n 1 d nodes has n 2 d children in level 3 n(n 1)(n 2) (1)d n nodes Generate a search tree of n! d n but only d n assignments are possible! The same states get generated multiple times It doesn t take advantage of the factored state representation! 25

COMMUTATIVITY! The order of assigning the variables has no effect on the final outcome CSPs are commutative: Regardless of the assignment order, the same partial solution is reached for a defined set of assignment values Don t care about path! Only a single variable at each node in the search tree needs to be considered!! (can fix the order) d n number of leaves in the search tree! 26

COMMUTATIVITY n = 3 variables each taking d = 3 values??? Var 1 N 1 = 3 = d 1?? 2?? 3?? Var 2 11? 12? 13? 21? 22? 23? 31? 32? 33? N 2 = n n = d 2 = 9 Var n N 3 = d 2 = 27 27

BACKTRACKING: DFS WITH SINGLE VARIABLE ASSIGNMENTS Only consider a single variable at each point Don t care about path Order of variable assignment doesn t matter, so fix ordering Only consider values which do not conflict with assignments made so far Depth-first search (max depth = n) for CSPs with these two improvements is called backtracking search 28

BACKTRACKING Function Backtracking(csp) returns soln or fail o Return Backtrack({}, csp) Function Backtrack(assignment, csp) returns soln or fail o If assignment is complete, return assignment o V i select_unassigned_var(csp) o For each val in order-domain-values(var, csp, assign) If value is consistent with assignment Add [V i = val] to assignment Result Backtrack(assignment, csp) If Result fail, return result Remove [V i = val] from assignments o Return fail 29

BACKTRACKING Function Backtracking(csp) returns soln or fail o Return Backtrack({}, csp) Function Backtrack(assignment, csp) returns soln or fail o If assignment is complete, return assignment o V i select_unassigned_var(csp) o For each val in order-domain-values(var, csp, assign) If value is consistent with assignment Add [V i = val] to assignment Result Backtrack(assignment, csp) If Result fail, return result Remove [V i = val] from assignments o Return fail 30

BACKTRACKING Does the variable/value order used affect how long backtracking takes to find a solution? Does the variable/value order used affect the solution found by backtracking? 31

(A=3) EXAMPLE VARIABLES: A,B,C,D DOMAIN: {1,2,3} CONSTRAINTS: A B, A=D, C < B, C < D VARIABLE ORDER: ALPHABETICAL VALUE ORDER: DESCENDING 32

(A=3) EXAMPLE VARIABLES: A,B,C,D DOMAIN: {1,2,3} CONSTRAINTS: A B, A=D, C < B, C < D VARIABLE ORDER: ALPHABETICAL VALUE ORDER: DESCENDING (A=3, B=3) inconsistent with A B (A=3, B=2) (A=3, B=2, C=3) inconsistent with C < B (A=3, B=2, C=2) inconsistent with C < B (A=3, B=2, C=1) (A=3, B=2, C=1,D=3) VALID 33

(A=1) EXAMPLE VARIABLES: A,B,C,D DOMAIN: {1,2,3} CONSTRAINTS: A B, A=D, C < B, C < D VARIABLE ORDER: ALPHABETICAL VALUE ORDER: ASCENDING 34

(A=1) EXAMPLE VARIABLES: A,B,C,D DOMAIN: {1,2,3} CONSTRAINTS: A B, A=D, C < B, C < D VARIABLE ORDER: ALPHABETICAL VALUE ORDER: ASCENDING (A=1,B=1) inconsistent with A B (A=1,B=2) (A=1,B=2,C=1) (A=1,B=2,C=1,D=1) inconsistent with C < D (A=1,B=2,C=1,D=2) inconsistent with A=D (A=1,B=2,C=1,D=3) inconsistent with A=D 35

EXAMPLE VARIABLES: A,B,C,D DOMAIN: {1,2,3} CONSTRAINTS: A B, A=D, C < B, C < D VARIABLE ORDER: ALPHABETICAL VALUE ORDER: ASCENDING (A=1) (A=1,B=1) inconsistent with A B (A=1,B=2) (A=1,B=2,C=1) (A=1,B=2,C=1,D=1) inconsistent with C < D (A=1,B=2,C=1,D=2) inconsistent with A=D (A=1,B=2,C=1,D=3) inconsistent with A=D No valid assignment for D, return result = fail Backtrack to (A=1,B=2,C=) Try (A=1,B=2,C=2) but inconsistent with C < B Try (A=1,B=2,C=3) but inconsistent with C < B No other assignments for C, return result= fail Backtrack to (A=1,B=) (A=1,B=3) (A=1,B=3,C=1) (A=1,B=3,C=1,D=1) inconsistent with C < D (A=1,B=3,C=1,D=2) inconsistent with A = D (A=1,B=3,C=1,D=3) inconsistent with A = D Return result = fail Backtrack to (A=1,B=3,C=) (A=1,B=3,C=2) inconsistent with C < B (A=1,B=3,C=3) inconsistent with C < B No remaining assignments for C, return fail Backtrack to (A=1,B=) No remaining assignments for B, return fail Backtrack to A (A=2) (A=2,B=1) (A=2,B=1,C=1) inconsistent with C < B (A=2,B=1,C=2) inconsistent with C < B (A=2,B=1,C=3) inconsistent with C < B No remaining assignments for C, return fail Backtrack to (A=2,B=?) (A=2,B=2) inconsistent with A B (A=2,B=3) (A=2,B=3,C=1) (A=2,B=3,C=1,D=1) inconsistent with C < D (A=2,B=3,C=1,D=2) ALL VALID 36

BACKTRACKING ON MAP COLORING 3-color problem 9 variables, connected as a grid including diagonals Constraint graph has 1-1 correspondence to problem graph 37

BACKTRACKING ON MAP COLORING 3-color problem 27 variables, connected as an irregular graph 38

ORDERING MATTERS! Function Backtracking(csp) returns soln or fail Return Backtrack({},csp) Function Backtrack(assignment,csp) returns soln or fail If assignment is complete, return assignment V i select_unassigned_var(csp) For each val in order-domain-values(var, csp, assign) If value is consistent with assignment Add [V i = val] to assignment Result Backtrack(assignment,csp) If Result fail, return result Remove [V i = val] from assignments Return fail Any ideas about what might be good orders? 39

Next variable? o Random or static ORDERING HEURISTICS o Variable with the fewest legal values: Minimum remaining values (MRV) heuristic (aka the most constrained variable, the fail-first var) o Variable with the largest number of constraints on other unassigned variables, reduces b on future choices (Degree heuristic) Variable s value? o Value that leaves most choices for the neighboring variables in the constraint graph, max flexibility (least-constrainingvalue heuristic), fail-last 40

LIMITATIONS OF BACKTRACKING Can an inevitable failure be detected earlier? Can problem structure can be exploited? Can the search space be reduced to speed up computation? CSPs are difficult because what seem like a locally good decision (value assignment to variable) may turn up leading to global inconsistency much later on Pre-process the CSP problem such that local choices are more likely to be globally consistent 41

PROPAGATE INFORMATION If we choose a value for one variable, that affects its neighbors And then potentially those neighbors choices propagate We can use this inference to prune the search space. 42

Definition: ARC CONSISTENCY An arc (connection between two variables X Y in constraint graph) is consistent if: For every value could assign to X (tail of the arc) There exists some value of Y (head of the arc) that could be assigned without violating the constraint If a variable is not arc consistent with another one, it can be made so by removing some values from its domain. Arc consistency ~ Minimal set of values that can be used for assignments without violating the constraint This can be done recursively Form of constraint propagation that enforces arc consistency, maintains the problem solutions, and prunes the tree! 43

ARC CONSISTENCY IN PRACTICE (To-Do-Arcs in the queue) Different arcs, different tails: A B vs. B A 44

AC-3 (COMPUTATIONAL COMPLEXITY?) Input: CSP Output: CSP, possible with reduced domains for variables, or inconsistent Local variables: queue, initially queue of all arcs (binary constraints in csp) While queue is not empty (X i, X j ) = Remove-First(queue) [domainx i, anychangetodomainx i ] = Revise(csp, X i, X j ) if anychangetodomainx i == true if size(domainx i ) = 0 else Return csp return inconsistent for each X k in Neighbors(X i ) except X j add (X k, X i ) to queue Function Revise(csp, X i, X j ) returns DomainX i and anychangetodomainx i anychangetodomainx i = false for each x in Domain(X i ) if no value y in Domain(X j ) allows (x, y) to satisfy constraint between (X i, X j ) delete x from Domain(X i ) anychangetodomainx i = true Add arcs (X i, X j ) and (X j, X i ) for i, j constraint D domain values C binary constraints Complexity of revise function? D 2 Number of times can put a constraint in queue? D Total: CD 3 45

SUFFICIENT? After we run AC-3 have we always found a solution? (aka only 1 value left for each variable) Yes No 46

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D (subset of constraints from before) 47

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D (subset of constraints from before) Constraints both ways: A B, B A, C < B, B > C, C < D, D > C 48

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D (subset of constraints from before) Constraints both ways: A B, B A, C < B, B > C, C < D, D > C Queue: AB, BA, BC, CB, CD, DC 49

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D (subset of constraints from before) Constraints both ways: A B, B A, C < B, B > C, C < D, D > C Queue: AB, BA, BC, CB, CD, DC Pop AB: for each x in Domain(A) if no value y in Domain(B) that allows (x,y) to satisfy constraint between (A,B) delete x from Domain(A) No change to domain of A 50

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D (subset of constraints from before) Constraints both ways: A B, B A, C < B, B > C, C < D, D > C Queue: AB, BA, BC, CB, CD, DC Pop AB Queue: BA, BC, CB, CD, DC 51

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D (subset of constraints from before) Constraints both ways: A B, B A, C < B, B > C, C < D, D > C Queue: AB, BA, BC, CB, CD, DC Pop AB Queue: BA, BC, CB, CD, DC Pop BA for each x in Domain(B) if no value y in Domain(A) that allows (x,y) to satisfy constraint between (B,A) delete x from Domain(B) No change to domain of B 52

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D (subset of constraints from before) Constraints both ways: A B, B A, C < B, B > C, C < D, D > C Queue: AB, BA, BC, CB, CD, DC Queue: BA, BC, CB, CD, DC Queue: BC, CB, CD, DC Pop BC for each x in Domain(B) if no value y in Domain(C) that allows (x,y) to satisfy constraint between (B,C) delete x from Domain(B) If B is 1, constraint B >C cannot be satisfied. So delete 1 from B s domain, B={2,3} Also have to add neighbors of B (except C) back to queue: AB Queue: AB, CB, CD, DC 53

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D Queue: AB, BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BC, CB, CD, DC A-D = {1,2,3} Queue: AB, CB, CD, DC B={2,3}, A/C/D = {1,2,3} Pop AB For every value of A is there a value of B such that A B? Yes, so no change 54

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D Queue: AB, BA, BC, CB, CD, DC, A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BC, CB, CD, DC A-D = {1,2,3} Queue: AB, CB, CD, DC B={2,3}, A/C/D = {1,2,3} Queue: CB, CD, DC B={2,3}, A/C/D = {1,2,3} Pop CB o For every value of C is there a value of B such that C < B o If C = 3, no value of B that fits o So delete 3 from C s domain, C = {1,2} o Also have to add neighbors of C (except B) back to queue: no change because already in 55

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D Queue: AB, BA, BC, CB, CD, DC, A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BC, CB, CD, DC A-D = {1,2,3} Queue: AB, CB, CD, DC B={2,3}, A/C/D = {1,2,3} Queue: CB, CD, DC B={2,3}, A/C/D = {1,2,3} Queue: CD, DC B={2,3}, C = {1,2} A,D = {1,2,3} Pop CD o For every value of C, is there a value of D such that C < D? o Yes, so no change 56

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D Queue: AB, BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BC, CB, CD, DC A-D = {1,2,3} Queue: AB, CB, CD, DC B={2,3}, A/C/D = {1,2,3} Queue: CB, CD, DC B={2,3}, A/C/D = {1,2,3} Queue: CD, DC B={2,3}, C = {1,2} A,D = {1,2,3} Queue: DC B={2,3}, C = {1,2} A,D = {1,2,3} For every value of D is there a value of C such that D > C? o Not if D = 1 o So D = {2,3} 57

AC-3 EXAMPLE Variables: A,B,C,D Domain: {1,2,3} Constraints: A B, C < B, C < D Queue: AB, BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BA, BC, CB, CD, DC A-D = {1,2,3} Queue: BC, CB, CD, DC A-D = {1,2,3} Queue: AB, CB, CD, DC B={2,3}, A/C/D = {1,2,3} Queue: CB, CD, DC B={2,3}, A/C/D = {1,2,3} Queue: CD, DC B={2,3}, C = {1,2} A,D = {1,2,3} Queue: DC B={2,3}, C = {1,2} A,D = {1,2,3} A = {1,2,3} B={2,3}, C = {1,2} D = {2,3} 58

REMARKS ON AC Enforcing consistency involves (is equivalent to) adding new constraints: contracting a variable domain by removing one of its values is the same as saying that the variable can t take that value: D(v) = {1,2,3} After AC D(v) = {1,3} is equivalent to add the constraint v 2 Enforcing consistency involves explicitly adding unary constraints that are implicit given the existing binary constraints Unearthing implicit constraints can be also interpreted as inferring new constraints that hold (are entailed ) given existing ones Consistency is a form of inference / entailment computation process Arc consistency as message passing: AC is a propagation algorithm, it s like sending messages to neighbors on the graph. Every time a domain changes, this schedule sending new messages, and repeat until convergence (no changes in domains) 59

FORWARD CHECKING: RUNT-TIME INFERENCE AC-3 can ran before the search begins to prune search tree; it operates on the entire search tree (expensive!) It s a form of inference (inferring reductions) It can also be seen as the application of a filter that recursively removes inconsistent values What if, instead, we make (local) inference at run-time? Forward checking: When assign a variable, make all of its neighbors arcconsistent (purely local) Information locally propagates from assigned to unassigned variables Question: Can we do FC on non binary arcs? Yes, but it needs some adjustments Christian Bessière, Pedro Meseguer, Eugene Freuder, Javier Larrosa, On Forward Checking for Non-binary Constraint Satisfaction, In CP 99: 5th Int. Conference on Principles and Practice of Constraint Programming, 60

BACKTRACKING + FORWARD CHECKING Function Backtrack(assignment, csp) returns soln or fail o If assignment is complete, return assignment o V i select_unassigned_var(csp) o For each val in order-domain-values(var, csp, assign) If value is consistent with assignment Add [V i = val] to assignment Make domains of all neighbors of V i arc-consistent with [V i = val] Result Backtrack(assignment, csp) If Result fail, return result Remove [V i = val] from assignments o Return fail Note: When backtracking, domains must be restored! 61

BACKTRACKING + FORWARD CHECKING ON COLORING 3-color problem 27 variables, connected as an irregular graph 62

BACKTRACKING + FORWARD CHECKING ON COLORING 3-color problem 49 variables, connected as an irregular graph but with only local connections 63

MAINTAINING ARC CONSISTENCY (MAC) Forward checking doesn t ensure all arcs are consistent, only the local ones, no look-ahead AC-3 can detect failure faster than forward checking The MAC algorithm includes AC-3 in the search, executing it from the arcs of the locally unassigned variables What s the downside? Computation! 64

MAINTAINING ARC CONSISTENCY (MAC) Function Backtrack(assignment, csp) returns soln or fail o If assignment is complete, return assignment o V i select_unassigned_var(csp) o For each val in order-domain-values(var, csp, assign) If value is consistent with assignment Add [V i = val] to assignment Run AC-3 to make all variables arc-consistent with [V i = val]. Initial queue is arcs (X j, X i ) of neighbors of X i that are unassigned, but add other arcs if these vars change domains. Result Backtrack(assignment, csp) If Result fail, return result Remove [V i = val] from assignments o Return fail Note: When backtracking, domains must be restored! 65

MAC ON COLORING 3-color problem 27 variables, connected as an irregular graph 66

MAC ON COLORING 3-color problem 49 variables, connected as an irregular graph but with only local connections 67

SUFFICIENT TO AVOID BACKTRACKING? If we do maintain arc consistency (MAC), we will never have to backtrack while solving a CSP True False 68

AC LIMITATIONS After running AC-3 o Can have one solution left o Can have multiple solutions left o Can have no solutions left (and not know it) Arc-consistent but no feasible assignment What went wrong here? 69

COMPLEXITY CSPs in general are NP-complete Valued, optimization version of CSPs are usually NP-hard Some structured domains, like those with a constraint tree, are easier and can be solved in polynomial time Constraint tree o Any 2 variables in constraint graph connected by <= 1 path Can be solved in time linear in # of variables 70

SUMMARY (SO FAR) Be able to define real world CSPs Understand basic algorithm (backtracking) Complexity relative to basic search algorithms Enumerative approach Doesn t require problem specific heuristics Ideas shaping search (ordering heuristics) Pruning space through propagating information Arc consistency Constraint inference: global vs. local Tradeoffs: + reduces search space - computation costs Computational complexity Relevant reading: R&N Chapter 6 71