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

PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

CMU-Q 15-381Lecture 7:

Searching in solution space

Constraint Satisfaction

Problems (CSPs)

Teacher:

Gianni A. Di Caro

Page 2: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

AI PLANNING APPROACHES SO FAR

Atomic state representations: Uninformed/Informed Search

algorithms, minimum cost plans, state is indivisible (~black-box)

2

Goal: Find the (best) sequence of actions that take from a start state

to a goal state (under full observability and deterministic assumptions)

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

Page 3: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

AI PLANNING APPROACHES SO FAR

3

Goal: Find the (best) sequence of actions that take from a start state

to a goal state (under full observability and deterministic assumptions)

vv

Factored state representations: Propositional logic, STRIPS

language, binary variables, feasibility, Planning Graph, GRAPHPLAN,

domain-independent heuristics for informed search algorithms

vv

Proposition 1

Proposition 2

Proposition i

¬ Proposition 1

Proposition 2

Proposition i

Proposition j

vv

¬ Proposition 1

¬ Proposition 2

Proposition n

¬ Proposition j

At(Plane, JFK) ∧ In(John, Taxi) ∧ Handling(Pen) ∧Having(Cake)∧ ¬Having(Money)

t=1 t=2 t=3

Page 4: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

SO FAR, SOLUTION = PATH TO A GOAL STATE

4

Plan ~ Sequence of actions

~ Path in the state space

http://kociemba.org/twophase.htm

Page 5: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

PATH SEARCH VS. SOLUTION STATE SEARCH

5

What if path doesn’t matter? (i.e., 𝑡 “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 𝑛 state variables

• Simultaneous, in parallel, in any sequence

• 𝑛 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

Factored states (to exploit state structure)

State: a set of 𝑛 variables

Page 6: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

PATH SEARCH VS. SOLUTION STATE SEARCH

6

Solution / Plan:

~ Consistent assignment to the n state/decision variables

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

Page 7: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

CHALLENGES IN SOLUTION STATES PROBLEMS

7

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)

Page 8: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

CONSTRAINT SATISFACTION PROBLEMS (CSP)

Set of decision Variables: V = {V1,..,VN}

Domains: Sets of Di possible values for each variable Vi

Set of Constraints: C = {C1,..,CK} restricting the values

the variables can simultaneously take

(e.g., Not [V1 =1 ∧ V5 = 3 ∧ V7 = B] )

A constraint consists of:

Variable tuple (e.g., (V1, V5, V7) ) and:

List of (not)feasible values for tuple (e.g., [ (V2,V3), { (R,B), (R,G) } ])

Or functional relations (e.g., V2 ≠V3, V1 > V4 + 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 general-

purpose algorithms with more power than standard search algorithms

8

Page 9: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

EXAMPLE: N-QUEENS

9

Put n queens on an 𝑛 × 𝑛 board with

no two queens attack each other:

not on same row, column, or diagonal

State: Position of the 𝑛 queens,

one per column (or row)

Domains:

{1, …, 8}

Constraints:

No queen attack each other

𝑄𝑖 = 𝑘 → 𝑄𝑗 ≠ 𝑘, ∀ 𝑗 = 1,⋯ , 8, 𝑗 ≠ 𝑖

Similar constraints for diagonals

Variables:

𝑄𝑖 position of queen in column 𝑖

Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8

𝑛 = 8 4,426,165,368 state

configurations, only 92 feasible

Page 10: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

CSP EXAMPLE: MAP COLORING

Given 𝑛 different colors, color a map so that adjacent areas

are different colors

10

Page 11: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

MAP COLORING

Variables

Domain

Constraints

Solutions

11

WA, NT, Q, NSW, V, T, SA

All region borders have different colors

Page 12: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

EXAMPLE: SUDOKU

Variables:

𝑋𝑖𝑗 , each open square

Domain:

{1:9}

Constraints:

9-way all diff col

9-way all diff row

9-way all diff box

12

Page 13: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

JOB SCHEDULING (SPECIFIC CASE)

A set of 𝐽 jobs, J1,…, Jn

A set of 𝑅 resources, R1, R2, …, Rm to do the jobs

Each job 𝑗 is a sequence of operations Oj1,..., O

jLj to be scheduled

according to process plans: Oj1 ≺ Oj

2 ≺ Oj3 ….

Each operation has a fixed processing time and requires the use of

resources Ri, 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

Page 14: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

JOB SCHEDULING

14

Page 15: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 16: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 17: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 18: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

J1+d1 ≤ J2 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

Page 19: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

TYPES OF CONSTRAINTS

Unary: involve a single variable

Binary: involve two variables

𝑛-ary: involve 𝑛 variables

Soft constraints: violation incurs a cost, the problem

becomes a constraint optimization one

19

Page 20: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

CONSTRAINT GRAPH

Variables ➔ Vertices

Constraints ➔ Edges

Unary: Self-edges

Binary: regular edges

𝑛-ary: hyperedges (hypergraphs) nodes

with hyperedges are indicated by □

20

A < 2

A B

C

A > C B ≠ C

WA

NT

SA

Q

NS

W

V

T

F T U W R O

C3 C2 C1

Page 21: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

CRYPTARITHMETIC PUZZLES

21

TWO

TWO =

F OU R

+F T U W R O

C3 C2 C1

100(O+O) + 101(W+W)+102(T+T) = 100R+101U+102O+103F

V = {O,W,T,R,U,F} D = {0, …, 9}

{O+O = R+10C1, C1+W+W=U+10C2, C2+T+T=O+10C3, C3 = F}

V = {O,W,T,R,U,F, C1, C2, C3} Auxiliary vars

Base-10 arithmetic

Page 22: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

BINARY CONSTRAINT GRAPHS

22

It’s always possible to reduce a

hypergraph to a binary constraint graph!

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.

If you want to know more …

http://ktiml.mff.cuni.cz/~bartak/constraints/binary.html

But this is not always the best thing to do ….

Page 23: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

SOLVING CSPS: BASIC SEARCH WITH

SEARCH ALGORITHMS?

States: Partial assignments to the 𝑛 variables

Initial state: Empty state

Action: Select an unassigned variable 𝑖 and assign a

feasible value from its domain Di 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 𝑛, using depth-limited DFS

Size of the search tree?

23

Page 24: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

WHY NOT JUST DO BASIC SEARCH?

24

𝑛 = 3 variables each taking 𝑑 = 3 values

???

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

𝑁2 = 𝑁1 𝑛 − 1 𝑑 = 𝑛𝑑 𝑛 − 1 𝑑 = 𝑛 𝑛 − 1 𝑑2 = 9 × 2 × 3 = 54

1?? 2?? 3?? ?1? ?2? ?3? ??1 ??2 ??3

𝑁1 = 9 = 𝑛𝑑

𝑁3 = 𝑁2 𝑛 − 2 𝑑 = 𝑛(𝑛 − 1)(𝑛 − 2)𝑑3 = 162

………

Page 25: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

WHY NOT JUST DO BASIC SEARCH?

25

𝑛 = 4 variables each taking 𝑑 = 4 values

Generate a search tree of 𝑛! 𝑑𝑛 but only 𝑑𝑛 assignments are possible!

The same states get generated multiple times

It doesn‘t take advantage of the factored state representation!

b=(n-1)d

Branching factor level 1: 𝑏 = 𝑛𝑑

𝑛𝑑 nodes in level 1

Each of the 𝑛𝑑 nodes has 𝑛 − 1 𝑑 children in level 2

Each of the 𝑛 − 1 𝑑 nodes has 𝑛 − 2 𝑑 children in level 3

𝑛(𝑛 − 1)(𝑛 − 2)⋯(1)𝑑𝑛 nodes

Page 26: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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)

➔ 𝑑𝑛 number of leaves in the search tree!

26

Page 27: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

COMMUTATIVITY

27

𝑛 = 3 variables each taking 𝑑 = 3 values

???

1?? 2?? 3??

𝑁1 = 3 = 𝑑

Var 1

11? 12? 13? 21? 22? 23? 31? 32? 33?

𝑁2 = 𝑛 𝑛 = 𝑑2 = 9

Var 2

𝑁3 = 𝑑2 = 27

………Var 𝑛

Page 28: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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 = 𝑛) for CSPs with these two

improvements is called backtracking search

28

Page 29: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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𝑉𝑖 select_unassigned_var(csp)

o For each val in order-domain-values(var, csp, assign)

• If value is consistent with assignment

• Add [𝑉𝑖 = val] to assignment

• Result Backtrack(assignment, csp)

• If Result ≠ fail, return result

• Remove [𝑉𝑖 = val] from assignments

o Return fail29

Page 30: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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𝑉𝑖 select_unassigned_var(csp)

o For each val in order-domain-values(var, csp, assign)

• If value is consistent with assignment

• Add [𝑉𝑖 = val] to assignment

• Result Backtrack(assignment, csp)

• If Result ≠ fail, return result

• Remove [𝑉𝑖 = val] from assignments

o Return fail30

Page 31: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 32: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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)

32

Page 33: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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)

(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

Page 34: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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)

34

Page 35: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

35

Page 36: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

36

(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

Page 37: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

BACKTRACKING ON MAP COLORING

37

3-color problem

9 variables, connected as a grid including diagonals

Constraint graph has 1-1 correspondence to problem graph

Page 38: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

BACKTRACKING ON MAP COLORING

38

3-color problem

27 variables, connected as an irregular graph

Page 39: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

𝑉𝑖 select_unassigned_var(csp)

For each val in order-domain-values(var, csp, assign)

If value is consistent with assignment

Add [𝑉𝑖= val] to assignment

Result Backtrack(assignment,csp)

If Result ≠ fail, return result

Remove [𝑉𝑖 = val] from assignments

Return fail

Any ideas about what might be good orders?39

Page 40: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

ORDERING HEURISTICS

Next variable?

o Random or static

o Variable with the fewest legal values:

Minimum remaining values (MRV)

heuristic (aka the most constrained

variable, the fail-first var)

40

Variable’s value?

o Value that leaves most choices for the

neighboring variables in the constraint

graph, max flexibility (least-constraining-

value heuristic), fail-last

o Variable with the largest number of constraints

on other unassigned variables, reduces b on

future choices (Degree heuristic)

Page 41: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

LIMITATIONS OF BACKTRACKING

41

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

Page 42: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

PROPAGATE INFORMATION

42

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.

Page 43: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

ARC CONSISTENCY

Definition:

An “arc” (connection between two variables 𝑋 𝑌 in

constraint graph) is consistent if:

For every value could assign to 𝑋 (tail of the arc)

There exists some value of 𝑌 (head of the arc) that

could be assigned without violating the constraint

43

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!

Page 44: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

ARC CONSISTENCY IN PRACTICE

44

(To-Do-Arcs in the queue)

Different arcs, different tails:

AB vs. BA

Page 45: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

(𝑋𝑖, 𝑋𝑗) = Remove-First(queue)

[domain𝑋𝑖, anyChangeToDomain𝑋𝑖] = Revise(csp, 𝑋𝑖, 𝑋𝑗)

if anyChangeToDomain𝑋𝑖 == true

if size(domain𝑋𝑖) = 0

return inconsistent

else

for each 𝑋𝑘 in Neighbors(𝑋𝑖) except 𝑋𝑗

add (𝑋𝑘, 𝑋𝑖) to queue

Return csp

Function Revise(csp, 𝑿𝒊, 𝑿𝒋) returns Domain𝑋𝑖 and anyChangeToDomain𝑋𝑖

anyChangeToDomain𝑋𝑖 = false

for each 𝑥 in Domain(𝑋𝑖)

if no value 𝑦 in Domain(𝑋𝑗) allows (𝑥, 𝑦) to satisfy constraint between (𝑋𝑖, 𝑋𝑗)

delete 𝑥 from Domain(𝑋𝑖)

anyChangeToDomain𝑋𝑖 = true45

Add arcs (𝑿𝒊, 𝑿𝒋) and (𝑿𝒋, 𝑿𝒊)for 𝒊, 𝒋 constraint

D domain valuesC binary

constraints

Complexity of revise function?

D2

Number of times can put a

constraint in queue?

D

Total:CD3

Page 46: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

SUFFICIENT?

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

46

Yes No

Page 47: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 48: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 49: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 50: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 51: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 52: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 53: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 54: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

AC-3 EXAMPLE

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

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D

Page 55: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

AC-3 EXAMPLE

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

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D

Page 56: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

AC-3 EXAMPLE

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

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D

Page 57: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

AC-3 EXAMPLE

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

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D

Page 58: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

AC-3 EXAMPLE

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

Variables: A,B,C,D

Domain: {1,2,3}

Constraints: A ≠ B, C < B, C < D

Page 59: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

REMARKS ON AC

59

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)

Page 60: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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 arc-

consistent (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

Page 61: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

BACKTRACKING + FORWARD CHECKING

Function Backtrack(assignment, csp) returns soln or fail

o If assignment is complete, return assignment

o 𝑉𝑖 select_unassigned_var(csp)

o For each val in order-domain-values(var, csp, assign)

• If value is consistent with assignment

• Add [𝑉𝑖= val] to assignment

• Make domains of all neighbors of 𝑽𝒊arc-consistent with [𝑽𝒊 = val]

• Result Backtrack(assignment, csp)

• If Result ≠ fail, return result

• Remove [𝑉𝑖 = val] from assignments

o Return fail

Note: When backtracking, domains must be restored!

61

Page 62: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

BACKTRACKING + FORWARD CHECKING ON COLORING

62

3-color problem

27 variables, connected as an irregular graph

Page 63: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

BACKTRACKING + FORWARD CHECKING ON COLORING

63

3-color problem

49 variables, connected as an irregular graph but with only local connections

Page 64: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

Page 65: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

MAINTAINING ARC CONSISTENCY (MAC)

Function Backtrack(assignment, csp) returns soln or fail

o If assignment is complete, return assignment

o 𝑉𝑖 select_unassigned_var(csp)

o For each val in order-domain-values(var, csp, assign)

• If value is consistent with assignment

• Add [𝑉𝑖 = val] to assignment

• Run AC-3 to make all variables arc-consistent with [𝑉𝑖 = val].

Initial queue is arcs (𝑋𝑗, 𝑋𝑖) of neighbors of 𝑋𝑖 that are

unassigned, but add other arcs if these vars change domains.

• Result Backtrack(assignment, csp)

• If Result ≠ fail, return result

• Remove [𝑉𝑖 = val] from assignments

o Return fail

65Note: When backtracking, domains must be restored!

Page 66: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

MAC ON COLORING

66

3-color problem

27 variables, connected as an irregular graph

Page 67: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

MAC ON COLORING

67

3-color problem

49 variables, connected as an irregular graph but with only local connections

Page 68: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

SUFFICIENT TO AVOID BACKTRACKING?

If we do maintain arc consistency (MAC), we will never

have to backtrack while solving a CSP

68

True False

Page 69: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

AC LIMITATIONS

After running AC-3

oCan have one solution left

oCan have multiple solutions left

oCan have no solutions left (and not know it)

69

What went

wrong here?

Arc-consistent but

no feasible assignment

Page 70: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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

70

• Constraint tree

o Any 2 variables in constraint graph connected by <= 1 path

• Can be solved in time linear in # of variables

Page 71: PowerPoint Presentationgdicaro/15381/slides/381-F18-7-CSP.pdf · AI PLANNING APPROACHES SO FAR 3 Goal: Find the (best) sequence of actions that take from a start state to a goal state

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