0% found this document useful (0 votes)
8 views

Lecture8 Notes

This document discusses constraint satisfaction problems (CSPs) and techniques for solving CSPs, including: - CSPs are defined by a set of variables with domains of possible values and a set of constraints specifying allowed value combinations. A solution satisfies all constraints. - Backtracking search is the basic algorithm, assigning values to variables one at a time and backtracking on failures. - Techniques like variable ordering, value ordering, and constraint propagation can improve efficiency by detecting failures earlier and pruning the search space. Variable ordering heuristics include minimum remaining values and most constraining variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture8 Notes

This document discusses constraint satisfaction problems (CSPs) and techniques for solving CSPs, including: - CSPs are defined by a set of variables with domains of possible values and a set of constraints specifying allowed value combinations. A solution satisfies all constraints. - Backtracking search is the basic algorithm, assigning values to variables one at a time and backtracking on failures. - Techniques like variable ordering, value ordering, and constraint propagation can improve efficiency by detecting failures earlier and pruning the search space. Variable ordering heuristics include minimum remaining values and most constraining variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Subject:

Topic:

Instructor: Amir EL-Ghamry


Sec 6.2-6.3
 Assumptions about the world: a single agent, deterministic actions, fully observed
state, discrete state space.

 Planning: sequences of actions


 The path to the goal is the important thing.
 Paths have various costs, depths.
 Heuristics give problem-specific guidance.

 Identification: assignments to variables


 The goal itself is important, not the path.
 All paths at the same depth (for some formulations)
 CSPs are a specialized class of identification problems
Faculty of computer and information sciences
Mansoura University
Information System Department

Constraint satisfaction problem (CSP)


 Constraint Satisfaction Problems (CSP):
— CSP defined by
 set of variables, 𝑋1 , 𝑋2 , … … … , 𝑋𝑛 and
 set of constraints, 𝐶1 , 𝐶2 , … … … , 𝐶𝑚
— Each variable 𝑋𝑖 has a nonempty domain 𝐷𝑖 of possible values.
— Each constraint 𝐶𝑖 involves some subset of the variables and specifies the
allowable combinations of values for that subset.
— A state of the problem is defined by an assignment of values to some or all
of the variables, 𝑋𝑖 = 𝑣𝑖 ; 𝑋𝑗 = 𝑣𝑗 .
— An assignment that does not violate any constraints is called a consistent
or legal assignment.
— A solution to a CSP is a complete assignment that satisfies all the
constraints.
— Some CSPs also require a solution that maximizes an objective function.
 Variables:

 Domains:

 Constraints: adjacent regions must have different colors


Implicit:

Explicit:

 Solutions are assignments satisfying all constraints, e.g.:


 Formulation 1:
 Variables:
 Domains:
 Constraints
 Formulation 2:
 Variables:

 Domains:

 Constraints:

Implicit:

Explicit:
9
Faculty of computer and information sciences
Mansoura University
Information System Department

Example 1: Map-Coloring (cont’d)


 It is helpful to visualize a CSP as a constraint graph, as shown in
following Figure.
— The nodes of the graph correspond to variables of the problem and
— the arcs correspond to constraints.
Faculty of computer and information sciences
Mansoura University
Information System Department

Example 1: Map-Coloring
 General-purpose CSP algorithms use the graph structure to speed
up search. E.g., Tasmania is an independent sub-problem!

 Treating a problem as a CSP confers several important benefits:


— The successor function and goal test can be written in a generic
way that applies to all CSPs.
— Develop effective, generic heuristics that require no additional
domain-specific expertise.
— The structure of the constraint graph can be used to simplify
the solution process.
Faculty of computer and information sciences
Mansoura University
Information System Department

Example: Cryptarithmetic

 Variables: F; T; U; W; R; O; 𝑋1 ; 𝑋2 ; 𝑋3 .
 Domains: {0; 1; 2; 3; 4; 5; 6; 7; 8; 9}.
 Constraints: Alldiff (F,T,U,W,R,O)
— O + O = R + 10. 𝑋1
— 𝑋1 + W + W = U + 10. 𝑋2
Solution: T = 9, W = 3, O = 8,
— 𝑋2 + T + T = O + 10. 𝑋3 F = 1, U = 7, R = 6
— 𝑋3 = F, 𝑇 ≠ 0, 𝐹 ≠ 0
 Discrete Variables
 Finite domains
 Size d means O(d n) complete assignments
 e.g., Boolean CSPs, including Boolean satisfiability (NP-
complete)
 Infinite domains (integers, strings, etc.)
 e.g., job scheduling, variables are fixed start/end times
for each job

 Continuous variables
 e.g., start/end times for Hubble Telescope
observations.
 Varieties of Constraints
 Unary constraints involve a single variable (equivalent to
reducing domains), e.g.:

 Binary constraints involve pairs of variables, e.g.:

 Higher-order constraints involve 3 or more variables:


e.g., cryptarithmetic column constraints

 Preferences (soft constraints):


 e.g., red is better than green
 Often representable by a cost for each variable
assignment
 Gives constrained optimization problems
 (We’ll ignore these until we get to Bayes’ nets)
 Scheduling problems: e.g., when can we all meet?
 Timetabling problems: e.g., which class is offered when and where?
 Assignment problems: e.g., who teaches what class?
 Hardware configuration
 Transportation scheduling
 Factory scheduling
 Circuit layout
 Fault diagnosis
 … lots more!

 Many real-world problems involve real-valued variables…


16
 Standard search formulation of CSPs

 States defined by the values


assigned so far (partial assignments)
 Initial state: the empty assignment, {}
 Successor function: assign a value to
an unassigned variable
 Goal test: the current assignment is
complete and satisfies all constraints

 We’ll start with the straightforward,


naïve approach, then improve it.
17
Faculty of computer and information sciences
Mansoura University
Information System Department

Backtracking Search
 Given a formulation of CSPs as search problems, any of the search
algorithms from Chapters 3 and 4 can solve CSPs.

 The term backtracking search is used for a depth-first search that


chooses values for one variable at a time and backtracks when a
variable has no legal values left to assign.
 Backtracking search is the basic uninformed algorithm for CSPs
Backtracking Example

CS 420: Artificial Intelligence 18


Backtracking Example

CS 420: Artificial Intelligence 19


Backtracking Example

CS 420: Artificial Intelligence 20


Backtracking Example

CS 420: Artificial Intelligence 21


Faculty of computer and information sciences
Mansoura University
Information System Department

Backtracking Example (cont’d)


Faculty of computer and information sciences
Mansoura University
Information System Department

Improving Backtracking Efficiency


 General-purpose methods can give huge gains in speed:
1. Which variable should be assigned next?
2. In what order should its values be tried?
3. Can we detect inevitable failure early?
21
 Variable Ordering: Minimum remaining values (MRV):
 Choose the variable with the fewest legal left values in its
domain

22
Faculty of computer and information sciences
Mansoura University
Information System Department

Variable and value ordering


 Minimum Remaining Values (MRV)
— Choose the variable with the fewest legal values
a) All variables have 3 possible values  Choose any, Say
WA: WA→ Red
b) NT and SA are MRV  choose any, Say NT: NT→ Green
c) SA is MRV : SA→Blue.
Faculty of computer and information sciences
Mansoura University
Information System Department

Variable and value ordering (cont’d)


 Most Constraining Variables (MCV)
— Choose variable with the most constraints on remaining
variables.
— It selects the variable that is involved in the largest number of
constraints on other unassigned variables.
a) From the constraints graph, MCV is SA (5 Constraints):
SA→ Blue.
b) NT, Q, NSW are MCV  choose Any, Say NT: NT→ Green
c) Q, NSW are MCV  Choose any, Say Q: Q → Red.
Faculty of computer and information sciences
Mansoura University
Information System Department

Variable and value ordering (cont’d)


 Least Constraining Values (LCV)
— Given a variable, choose the least constraining value: the one that
rules out the fewest values in the remaining variables.
— Order the values by descending number of choices for the
remaining variables.
Faculty of computer and information sciences
Mansoura University
Information System Department

Propagating information through constraints


 Constraint information can be used to drastically reduce the
search space (inference during search).
 At every variable assignment, we can propagate constraints
onto domains of other variables
— Forward checking
— Constraint Propagation
— Arc consistency
8
Forward checking

 Can we detect inevitable failure early?


 And avoid it later?
 Forward checking idea: keep track of remaining legal values for
unassigned variables.
 Terminate search when any variable has no legal values.

October 13, 2015 TLo (IRIDIA) 24


Forward checking

 Assign {WA=red}
 Effects on other variables connected by constraints with WA
 NT can no longer be red
 SA can no longer be red

October 13, 2015 TLo (IRIDIA) 25


Forward checking

 Assign {Q=green}
 Effects on other variables connected by constraints with WA
 NT can no longer be green
 NSW can no longer be green
 SA can no longer be green
 MRV heuristic will automatically select NT and SA next, why?

October 13, 2015 TLo (IRIDIA) 26


Forward checking

 If V is assigned blue
 Effects on other variables connected by constraints with WA
 SA is empty
 NSW can no longer be blue
 FC has detected that partial assignment is inconsistent with the constraints
and backtracking can occur.

October 13, 2015 TLo (IRIDIA) 27


Example: 4-Queens Problem

X1 X2
1 2 3 4 {1,2,3,4} {1,2,3,4}
1
2
3
4
X3 X4
{1,2,3,4} {1,2,3,4}

[4-Queens slides copied from B.J. Dorr CMSC 421 course on AI]
October 13, 2015 TLo (IRIDIA) 28
Example: 4-Queens Problem

X1 X2
1 2 3 4 {1,2,3,4} {1,2,3,4}
1
2
3
4
X3 X4
{1,2,3,4} {1,2,3,4}

October 13, 2015 TLo (IRIDIA) 29


Example: 4-Queens Problem

X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
{ ,2, ,4} { ,2,3, }

October 13, 2015 TLo (IRIDIA) 30


Example: 4-Queens Problem

X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
{ ,2, ,4} { ,2,3, }

October 13, 2015 TLo (IRIDIA) 31


Example: 4-Queens Problem

X1 X2
1 2 3 4 {1,2,3,4} { , ,3,4}
1
2
3
4
X3 X4
{ , , , } { ,2,3, }

October 13, 2015 TLo (IRIDIA) 32


Example: 4-Queens Problem

X1 X2
1 2 3 4 { ,2,3,4} {1,2,3,4}
1
2
3
4
X3 X4
{1,2,3,4} {1,2,3,4}

October 13, 2015 TLo (IRIDIA) 33


Example: 4-Queens Problem

X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, ,3, } {1, ,3,4}

October 13, 2015 TLo (IRIDIA) 34


Example: 4-Queens Problem

X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, ,3, } {1, ,3,4}

October 13, 2015 TLo (IRIDIA) 35


Example: 4-Queens Problem

X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } {1, ,3, }

October 13, 2015 TLo (IRIDIA) 36


Example: 4-Queens Problem

X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } {1, ,3, }

October 13, 2015 TLo (IRIDIA) 37


Example: 4-Queens Problem

X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } { , ,3, }

October 13, 2015 TLo (IRIDIA) 38


Example: 4-Queens Problem

X1 X2
1 2 3 4 { ,2,3,4} { , , ,4}
1
2
3
4
X3 X4
{1, , , } { , ,3, }

October 13, 2015 TLo (IRIDIA) 39


Faculty of computer and information sciences
Mansoura University
Information System Department

Can we detect inevitable failure early?


 Forward checking (cont’d)
— Forward Checking: when variable X is assigned, prune
domains of unassigned variables
— Idea: Keep track of remaining legal values for unassigned
variables.
— Terminate search when any variable has no legal values.

 In 3rd row, after WA & Q


are assigned, NT & SA are
reduced to single value
 In 4th row, after V is
assigned, the domain of SA
is empty  inconsistency
of assignment is detected

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

NT Q
WA
SA
NSW
V

 NT and SA cannot both be blue!


 Why didn’t we detect this yet?
 Constraint propagation: reason from constraint to constraint
12
Faculty of computer and information sciences
Mansoura University
Information System Department

Can we detect inevitable failure early?


 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.

Consistent
Faculty of computer and information sciences
Mansoura University
Information System Department

Can we detect inevitable failure early?


 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.

Inconsistent, the domain blue is detected to make this


arc consistent
Faculty of computer and information sciences
Mansoura University
Information System Department

Can we detect inevitable failure early?


 Arc Consistency: (cont’d)
— 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 must be rechecked


Faculty of computer and information sciences
Mansoura University
Information System Department

Can we detect inevitable failure early?


 Arc Consistency: (cont’d)
— Arc consistency detects failure earlier than forward checking.
— Can be run as a preprocessor or after each assignment.

Inconsistent, delete the domain blue, SA is empty


Early detection of failures, terminate!

You might also like