Skip to content
Kudos AI

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.

FoundationsModule 125 min · 100 XP
A map of Australia turning into a graph of seven nodes and nine edges, then a search tree that counts every assignment order collapsing into one that counts each assignment once.

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 X1,…,XnX_1, \dots, X_n.
  • A domain DiD_i 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 {red,green,blue}\{\text{red}, \text{green}, \text{blue}\}, and one constraint per shared border:

WA≠NT,WA≠SA,NT≠SA,NT≠Q,SA≠Q,\mathrm{WA} \neq \mathrm{NT},\quad \mathrm{WA} \neq \mathrm{SA},\quad \mathrm{NT} \neq \mathrm{SA},\quad \mathrm{NT} \neq \mathrm{Q},\quad \mathrm{SA} \neq \mathrm{Q}, SA≠NSW,SA≠V,Q≠NSW,NSW≠V.\mathrm{SA} \neq \mathrm{NSW},\quad \mathrm{SA} \neq \mathrm{V},\quad \mathrm{Q} \neq \mathrm{NSW},\quad \mathrm{NSW} \neq \mathrm{V} .

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:

SA5NT, Q, NSW3WA, V2T0\begin{array}{ll} \mathrm{SA} & 5 \\ \mathrm{NT},\ \mathrm{Q},\ \mathrm{NSW} & 3 \\ \mathrm{WA},\ \mathrm{V} & 2 \\ \mathrm{T} & 0 \end{array}

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.

Python

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.

WAdegree 2NTdegree 3Qdegree 3NSWdegree 3Vdegree 2SAdegree 5Tdegree 0
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: n!n! orderings times dnd^n value combinations,

7!×37=5,040×2,187=11,022,480.7! \times 3^7 = 5{,}040 \times 2{,}187 = 11{,}022{,}480 .

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

dn=37=2,187,d^n = 3^7 = 2{,}187 ,

a factor of 5,0405{,}040 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 T1T_1 of duration d1d_1 finishes before T2T_2 starts is T1+d1≤T2T_1 + d_1 \le T_2. A deadline is a restriction on every domain. A shared tool becomes a disjunctive constraint: either A+10≤BA + 10 \le B or B+10≤AB + 10 \le A.
  • Sudoku. Eighty-one variables, one per square, with domain {1,…,9}\{1, \dots, 9\} 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 T1+d1≤T2T_1 + d_1 \le T_2 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.