UPI Study › Courses › Introduction to Artificial Intelligence › Free lessons › Lesson 4.8 Free with a free account
Working with Constraint Satisfaction Problems
Introduction to Artificial Intelligence · Chapter 4: Search, Heuristics, and Game StrategySection 4.8 · lesson 30 of 6913 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Introduction to Artificial Intelligence.
Lesson complete
Earn the 3 college credits for Introduction to Artificial Intelligence
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Sequential Decision Making with Markov Processes →← Pruning Game Trees with Alpha-Beta TechniquesNext lesson: Sequential Decision Making with Markov Processes →
In this lesson
Solve scheduling, map coloring, and Sudoku with backtracking and constraint propagation.
- Define a CSP by its variables, domains, and constraints and draw its constraint graph
- Apply backtracking search with MRV, degree, and least-constraining value heuristics
- Use forward checking and the AC-3 algorithm to shrink domains
- Model Sudoku and exam scheduling as constraint satisfaction problems
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (1,792 words). Read along, or search it.
Opening Examples
Exams, Maps, and Sudoku Share a Structure
Schedule five exams, so no student has two at the same time. Color a map with three colors, so no two neighbors match. Fill in a Sudoku grid. These puzzles look unrelated, yet they share one structure: choose a value for each of several things, while obeying rules about which combinations are allowed. General search treats a problem as a path through states, and never looks inside them. A constraint satisfaction problem, or CSP, exposes the structure instead. Watch how much work that saves.
Definition
Variables, Domains, and Constraints
A CSP has three ingredients. The variables are the things to decide. Each variable has a domain: the set of values it may take. And the constraints are rules that limit which combinations of values are allowed. For map coloring, the variables are the regions, the domain is red, green and blue, and neighbors must differ. For Sudoku, the 81 cells, the digits 1 to 9, and no repeats in a row, column or box. For exams, the exams themselves, the time slots, and exams sharing students at different times. A solution gives every variable a value, and breaks no constraint. A partial assignment that breaks nothing so far is called consistent.
Constraint Types
Unary, Binary, and Higher-Order Constraints
Constraints come in kinds. A unary constraint involves one variable: South must be green. A binary constraint involves two: North and East must differ. A higher-order one involves several: all nine digits in a row are different.
Running Example
Coloring a Five-Region Island
Here's our running example. An island has five regions: Center, North, East, South and West. Center touches all four others, and each outer region touches its two neighbors around the ring. Each region must be painted red, green or blue, neighbors must differ, and South is already green. The constraint graph makes the structure visible: each variable is a node, and each binary constraint is an edge. At a glance, Center has four edges: it's the most constrained. And a region touching no others would be an isolated node, solvable by itself.
Backtracking Search
Assign, Check, and Back Up
Backtracking search builds a solution one variable at a time. Pick an unassigned variable, try a value, and check it against the variables already assigned. If all is well, move on. If a variable has no legal value left, back up to the most recent earlier choice, and try something different. That backing up is the backtracking. Two facts make CSPs special. First, a partial assignment that already breaks a rule is dropped at once, with nothing beyond it explored. Second, the order of assignment doesn't change which solutions exist, only how much effort they take. So the search is free to choose its order wisely.
Worked Example
A Poor Order Hits a Dead End
Here's backtracking on the island with a poor order: North, South, East, West, then Center, trying red, then green, then blue. North is red. South is green, its only legal value. East neighbors red and green, so it's blue. West, by the same reasoning, is also blue. Now Center. Its neighbors already use red, green and blue. Dead end. West, East and South each had only one legal value, so the search backs all the way up and retries North, as green. Then South is green, East is red, West is red, and Center is blue. Solved, but steps one to four were wasted: the dead end only showed up at Center.
Ordering Heuristics
MRV, Degree, and Least-Constraining Value
Three heuristics help. Two choose which variable comes next, and one chooses which value to try first. Minimum remaining values, or MRV, picks the variable with the fewest legal values left. One value means it's forced. None means failure, right away. That's fail first: find problems early, while they're cheap to fix. The degree heuristic breaks ties among MRV choices. It picks the variable in the most constraints with unassigned variables, because it affects the most neighbors. And least-constraining value picks the value that rules out the fewest options for the neighbors. That's succeed first: leave as much freedom as possible.
Worked Example
Least-Constraining Value Picks Green
Here's least-constraining value on the island. South is green, so Center, East and West each have red or blue left. North is still free. Giving North green removes nothing from its neighbors, because green is already gone from them. Red or blue would cut an option from three neighbors. So it tries green.
Quick Check
MRV With a Degree Tie-Breaker
Quick check. Three unassigned variables. X has 2 legal values left, and constraints with 3 unassigned variables. Y has 2 values left, and 1 unassigned neighbor. W has 3 values left, and 4 unassigned neighbors. Using MRV, with the degree heuristic to break ties, which variable comes next? Pause, and decide. X. MRV looks only at values left, so X and Y tie at two, and W, with three, is out. The degree heuristic breaks the tie: X constrains three unassigned variables, Y only one. W is tempting for its four neighbors, but degree only breaks MRV's ties.
Forward Checking
Crossing Off Values From Neighbors
Heuristics steer the search. A second idea, constraint propagation, shrinks the domains, so dead ends are noticed sooner. The simplest form is forward checking. Each time a variable is assigned, remove its value from the domains of its unassigned neighbors. If any neighbor's domain becomes empty, backtrack at once. In Sudoku, it's exactly what you do after writing a 5: cross 5 off the candidates of all 20 cells in the same row, column and box.
Arc Consistency
The AC-3 Procedure
Forward checking only notices an empty domain, so it can miss trouble that's already certain. Arc consistency looks deeper. Two variables joined by a constraint form an arc. The arc from X to Y is consistent, if every value in X's domain has a partner in Y's domain, one that satisfies the constraint. A value with no partner can never be part of a solution, so it's deleted. The AC-3 procedure keeps a queue of arcs. It takes one, and deletes the unsupported values. If that domain changed, it adds back the arcs that point to that variable. It stops when the queue is empty, or a domain is.
Worked Example
AC-3 Solves a Three-Variable Ordering
A small example. P, Q and R each have the domain 1, 2, 3, with P less than Q, and Q less than R. Check P to Q: P equals 3 has no larger Q, so delete it. Check Q to P: Q equals 1 has no smaller P, so delete it. Q to R: Q equals 3 has no larger R. Delete. R to Q: R equals 1 and 2 have no smaller Q left. Delete both. Q's domain changed, so P to Q is checked again. P equals 2 has lost its partner. What's left: P is 1, Q is 2, R is 3. Propagation alone solved this one, with no guessing. That's a bonus, not a guarantee.
Limitation
Arc Consistency Without a Solution
Here's why it's not a guarantee. Three regions all border each other, and only red and blue are available. Every value in every domain still has a partner in each neighbor, so AC-3 removes nothing. Yet no solution exists. Propagation shrinks the search, but a search is still needed to settle the rest.
Comparison
When Each Check Finds the Dead End
The checks catch dead ends at different times. Back to the island, with North red, then South green. Plain backtracking notices only when it reaches Center, after four assignments. Forward checking notices right after East is set blue, when Center's domain empties: three assignments. Arc consistency notices right after South is green. Center, East and West are each down to blue, and Center and East are neighbors: they can't both be blue. Two assignments. The stronger check costs more per step, and often saves much more in search. Running AC-3 after every assignment is a common way to combine them.
Quick Check
Arc Consistency for a Gap of Two
Quick check. X and Y each have the domain 1, 2, 3. The constraint: X is at least Y plus 2. After arc consistency, what's left in each domain? Pause, and predict. X is 3, and Y is 1. X equals 1 or 2 would need Y at most 0, or below, so both go. And Y equals 2 or more would need X to be 4 or more. Gone too. Option B keeps Y equals 2, which has no partner. Option C keeps X equals 2, which has none either. Arc consistency checks both directions.
Combined Approach
Five Assignments With No Backtracking
Now solve the island with MRV, the degree heuristic and forward checking together. South goes first: its domain has just one value, green. Center, East and West lose green. Center comes next. MRV ties at two values, and the degree heuristic favors Center, with three unassigned neighbors. Center is red, so East and West are down to blue. East has one value left, blue, and North loses blue. West is blue. And North has only green left. Five assignments, with no backtracking, against the eight-step trace from before.
Application
Sudoku as a CSP
Sudoku fits exactly. The variables are the 81 cells, and every domain is the digits 1 to 9. A given digit just shrinks a cell's domain to one value. As binary constraints, every cell must differ from its 20 peers: 8 in its row, 8 in its column, and 4 more in its box. Written as one all-different rule for nine cells, it's a higher-order constraint. MRV is how most people solve Sudoku without naming it: find the cell with the fewest possibilities. Forward checking is the crossing off. Subtler cases, like two cells in a box both limited to the same pair of digits, need the box's all-different rule. Checking cells in pairs, as arc consistency does, misses them.
Application
Scheduling Five Exams in Three Slots
Scheduling problems are CSPs in disguise. Five exams, Math, Physics, Chemistry, Art and Music, go into three slots: Monday morning, Monday afternoon, and Tuesday morning. Some students take Math, Physics and Chemistry together, so those three need different slots. Chemistry and Art share students, and so do Art and Music. The Physics lab is closed on Monday morning: a unary constraint. And Art must come before Music: a binary constraint that's an ordering, not a difference. The triangle uses all three slots. One timetable that works: Math Monday morning, Physics Monday afternoon, Chemistry Tuesday morning. Then Art only has to avoid Chemistry's slot, and come before Music: Art Monday morning, Music Monday afternoon.
Key Takeaways
CSP Search and Propagation Summed Up
To sum up. A CSP is variables, domains and constraints, and its constraint graph shows which variables are tied together. Backtracking assigns one variable at a time, and backs up on dead ends. The order changes the effort, not the solutions. MRV and degree pick the most constrained variable first. Least-constraining value tries the value that leaves neighbors the most freedom. Forward checking and AC-3 cross out impossible values, so dead ends show up sooner. Propagation shrinks the search, but doesn't always replace it.
Reflection
Four Volunteers, Three Shifts
Here's something to think about. Four volunteers fill three shifts. Two friends must never share a shift, and one volunteer can't work shift one. What are the variables, the domains and the constraints? And which constraint is unary?
About Introduction to Artificial Intelligence at UPI Study
Introduction to Artificial Intelligence (Computer Science 373) is a self-paced online course of 69 video lessons in 9 chapters, from intelligent agents and search to game strategy, neural networks, logic and reasoning, machine learning, language, robotics and AI ethics. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: AI vs Current Trends in CS and IT
Why take it here
Why students take Introduction to Artificial Intelligence with UPI Study
Where the credit goes
Universities that work with NCCRS
Introduction to Artificial Intelligence carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Working with Constraint Satisfaction Problems
What is a constraint satisfaction problem in AI?
What is the minimum remaining values heuristic?
What is the difference between forward checking and arc consistency?
Does arc consistency always solve a CSP?
About the Introduction to Artificial Intelligence course
How many college credits is Introduction to Artificial Intelligence?
How much does Introduction to Artificial Intelligence cost and what is included?
Which universities accept UPI Study credits?
How long does Introduction to Artificial Intelligence take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Introduction to Artificial Intelligence.
69 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →