Modelling a Problem as Constraints
Variables, domains and constraints as a standard form, the constraint graph that comes with it, and the commutativity that shrinks the search space before any search happens.
The search path treated a problem as a black box: an initial state, a set of actions, a goal test. That works, but the solver learns nothing about why a state is bad. A constraint satisfaction problem opens the box. State the problem in a standard form and a general solver can reason about its structure without knowing what it is about.
The standard form
A CSP is three things.
- A set of variables .
- A domain for each, the values it may take.
- A set of constraints, each restricting the values some subset of variables may take simultaneously.
An assignment gives values to some or all variables. It is consistent if it violates no constraint, complete if every variable has a value, and a solution if it is both.
The example: colouring a map
Colour each region of Australia so that no two adjacent regions share a colour. Seven variables - Western Australia, Northern Territory, Queensland, New South Wales, Victoria, South Australia and Tasmania - each with the domain , and one constraint per shared border:
Nine constraints. Tasmania is an island, so it appears in none of them.
The constraint graph
Draw one node per variable and one edge per binary constraint and you have the constraint graph. Its shape is the solver's only map of the problem, and the most useful thing to read off it is degree, the number of constraints a variable takes part in:
South Australia borders every mainland region. Choosing its colour immediately restricts five other variables, which is why the third lesson assigns high-degree variables early. Tasmania restricts nothing, so its colour is free: that alone tells you the solutions come in groups of three.
There are 18 proper colourings in total, six distinct mainland colourings times three choices for Tasmania.
Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.
Colour it yourself below, and watch the second read-out rather than the first. It counts the proper colourings that still extend what you have assigned, and it corrects two readings of the paragraph above.
Fixing any one region leaves exactly six completions. South Australia with five borders, Tasmania with none: six either way, because the three colours are interchangeable and so every region takes every colour in a third of the solutions. High degree buys less search, not fewer answers. That is a weaker claim than it sounds, and it is the one the heuristics lesson actually needs.
Then press the quiet dead end. Western Australia red with Queensland green breaks no constraint at all, and has zero completions: Northern Territory and South Australia are each left needing blue, and they border each other. A consistent partial assignment need not be extendable. Nothing in the standard form notices, which is exactly why the next lesson exists.
Interactive: the constraint graph, and what it hides
Click a region to cycle its colour.
- Constraints broken
- 0
- Colourings still possible
- 18
- Regions assigned
- 0 / 7
- Colourings in all
- 18
The count on the right is the one worth watching. With 0 regions assigned and 0 constraints broken, 18 of the eighteen proper colourings survive. Two things it shows that the graph does not. Fixing any ONE region leaves exactly six, whether it is South Australia with five borders or Tasmania with none, because the three colours are interchangeable: degree buys less search, not fewer answers, which is a weaker and more useful claim than it first looks. And press the quiet dead end: Western Australia red with Queensland green breaks nothing at all and has zero completions, because Northern Territory and South Australia are each left needing blue and they border each other. A consistent partial assignment need not be extendable, and that gap is the whole reason the next lesson exists.
Commutativity, and why it is worth 5,040
A naive search would treat "assign WA, then NT" and "assign NT, then WA" as different branches. Count the leaves of that tree: orderings times value combinations,
But CSPs are commutative: applying a set of assignments in any order reaches the same partial assignment. So the order carries no information, and a solver may fix one variable per level of the tree. The leaf count becomes
a factor of smaller, and nothing has been given up. This is not a heuristic or an approximation; it is a redundancy in the naive formulation that should never have been there. Every algorithm in this path assumes it.
The same form, other problems
The point of a standard form is that unrelated problems land in it.
- Scheduling. One variable per task, valued by its start time. A precedence constraint that task of duration finishes before starts is . A deadline is a restriction on every domain. A shared tool becomes a disjunctive constraint: either or .
- Sudoku. Eighty-one variables, one per square, with domain and singleton domains for the givens. Twenty-seven all-different constraints, one per row, column and box.
- Eight queens. One variable per column, valued by row, with constraints forbidding two queens on a row or diagonal.
None of these needs a bespoke solver. The same code applies to all three because the reasoning it does - shrinking domains, choosing which variable to try next - is driven by the constraints, not by the subject matter.
The form is standard; the difficulty is not. Constraint satisfaction is NP-complete in general, and writing a problem in this form does not make it easy. What it buys is that all the cleverness can live in one solver instead of being reinvented per problem.
Beyond finite domains
Domains need not be small or even finite. A discrete domain can be infinite, like the integers, in which case constraints can no longer be listed as allowed pairs and a constraint language is needed to express directly. Linear constraints on integers have specialised solvers; general nonlinear constraints on integers have none, and cannot, since no algorithm for them exists.
Before the quiz
Be able to write a problem as variables, domains and constraints, draw the constraint graph and read degree from it, explain what commutativity removes and what it is worth here, and recognise the same form in scheduling and Sudoku.
References & further reading
- Stuart Russell, Peter Norvig, Artificial Intelligence: A Modern Approach, Pearson (3rd edition), 2010· Kudos AI reference library
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.
Unlock the full path
This first lesson is free. Enrol to take the mastery quiz, earn XP, and unlock every module, with more interactive, runnable examples throughout.