Artificial Intelligence and Machine Learning · Lecture 05 of 16

Constraint satisfaction · Part 1

Modeling, backtracking and propagation

Translate a problem into legal assignments and remove impossible choices before doing more search.

01Variables, domains and constraints
02Backtracking and variable ordering
03Support, propagation and local consistency
Constraint satisfaction · Part 1 · Start with these ideas

From choices to a solution

1 · ModelName the choices and the rules.
2 · SearchBuild an assignment; undo a choice when it fails.
3 · PropagateRemove values that can no longer satisfy a rule.

Running example: three variables, three values each, and three simple rules.

01 · Model the problem

A CSP turns choices into rules

Maths: Mon 9AI: Mon 11Shared studentsRule: their time slots must differ

University timetable

Variables: one time-slot choice per course.

Domains: available time slots.

Constraint: courses sharing students cannot overlap.

A hard constraint must hold. A preference, such as fewer gaps, needs a score or an objective.

01 · Model the problem

A domain lists a variable’s choices

One variable, one chosen value

X ∈ dom(X)X: a variable · dom(X): its allowed values

Example: dom(X) = {0, 1, 2}.

How many complete assignments?

dⁿn: number of variables · d: choices per variable

With 3 variables and 3 choices each: 3³ = 27.

X1: 0, 1, 2X2: 0, 1, 2X3: 0, 1, 23 × 3 × 3 = 27 complete assignments

When domains have different sizes, multiply their sizes instead.

Mathematical definition · Constraint satisfaction

Counting total assignments

Multiply the domain sizes

∏i=1n|dom(Xi)|∏ means multiply · |S| is the size of set S

n is the number of variables; i selects one variable Xᵢ.

Different domain sizes

dom(X₁)={0,1,2}; dom(X₂)={“a”,“b”}.

3 × 2 = 6
X₁Possible X₂ values
0“a” or “b”
1“a” or “b”
2“a” or “b”

A total assignment chooses one value for every variable. If every domain has size d, the product becomes dⁿ.

01 · Model the problem

An assignment fills in the choices

Partial assignment example

X11
X2?
X3?

Context A = {X₁ = 1}.
The question marks mean “unassigned”.

Total: every variable has a value

X11
X22
X30

Assignment a = {X₁ = 1, X₂ = 2, X₃ = 0}.

An atomic assignment sets one variable. “Partial” allows any subset, including all variables. A total assignment is a solution only when every rule holds.

Mathematical definition · Constraint satisfaction

Restricting an assignment: a[S]

Start with a total assignment

a = {X₁=1, X₂=2, X₃=0}

The letter a names the whole assignment. The lookup a[X₂] gives the value 2.

Keep only the variables in S

S = {X₁, X₃}
a[S] = {X₁=1, X₃=0}

S is a set of selected variables. a[S] keeps their variable–value pairs.

Restriction drops pairs; it does not change values. For a partial assignment, choose S only from variables that already have a value.

01 · Model the problem

Check a rule when its scope is filled

RuleScope: variables it usesWith X₁=1, X₂=2, X₃=?
X₁ < 2{X₁} · unaryTrue: 1 < 2
X₃ < X₁{X₃, X₁} · binaryPending: X₃ is missing
X₁ + X₂ = X₃{X₁, X₂, X₃} · ternaryPending: X₃ is missing

The scope of c is its variable set; its arity is |scope(c)|. Here the sum rule is only a ternary-scope illustration, not a course-CSP rule.

Mathematical definition · Constraint satisfaction

The evaluation function ε(a, c)

A rule returns 1 or 0

ε(a, c) = 1   if c holds
ε(a, c) = 0   if c fails

ε names evaluation; a supplies the scoped values; c is the constraint.

Example: c is X₃ < X₁

Scoped assignmentEvaluation
{X₁=1, X₃=0}ε(a,c)=1
{X₁=0, X₃=1}ε(a,c)=0
{X₁=1}Pending: X₃ is missing

The input must supply all variables in scope(c). For a larger assignment a, evaluate ε(a[scope(c)], c). Pending is not a third value of ε.

Course §2.2 · Constraints · Logical connectives

Combining conditions: ∧ means AND, ∨ means OR

A connective combines true/false statements. Here each X₁, X₂, X₃ has domain {0,1,2}.

∧ · AND: all conditions must hold

(X₁ < 2) ∧ (X₂ > 0)
∧ (X₃ < X₁)

This is the course CSP: satisfy all three rules together.

(1,0,0): false overall.
The middle rule fails; the others hold.

∨ · OR: at least one must hold

(X₁ = 1) ∨ (X₂ = 2)

An illustrative OR constraint.
Either part may hold; both may hold too.

(1,0,0): true overall.
The left part holds, so this OR holds.

A CSP requires an AND across its constraints. An individual constraint may contain OR. The OR example is a separate illustration, not an extra course rule.

Mathematical definition · Constraint satisfaction

Formal definition of a CSP

P = (𝓧, dom, 𝓒)

𝓧 = {X₁, …, Xₙ}

𝓧 is the variable set; n counts its variables.

𝓒 = {c₁, …, cₘ}

𝓒 is the constraint set; m counts its rules.

dom is a function

X ↦ dom(X)

For each variable X, dom returns its allowed-value set.

Each c ∈ 𝓒 has a scope contained in 𝓧. All values chosen for X must belong to dom(X).

P names the problem. Solving it means finding one or all total assignments that use allowed values and satisfy every constraint.

01 · Model the problem

The running CSP has exactly two solutions

Problem P = (𝓧, dom, 𝓒)

𝓧: variables {X₁, X₂, X₃}.

dom: allowed values; each starts at {0,1,2}.

𝓒: three rules, all required:
(X₁ < 2) ∧ (X₂ > 0) ∧ (X₃ < X₁).

Solutions: every rule holds

X₁X₂X₃
110
120

Of the 27 complete assignments, only these two satisfy all three rules.

All solutions: (X₁=1) ∧ ((X₂=1) ∨ (X₂=2)) ∧ (X₃=0).
∧ means AND; ∨ means OR. The grouped OR leaves X₂ a choice.

Mathematical definition · Constraint satisfaction

The mathematical test for a solution

∀ c ∈ 𝓒,   ε(a[scope(c)], c) = 1∀ = for every · ∈ = belongs to

A domain-valid total assignment a is a solution exactly when every scoped evaluation is 1.

Course candidateε for X₁<2ε for X₂>0ε for X₃<X₁
(1,1,0)111
(2,1,0)011

∀ requires an AND (∧) across all constraints. a[scope(c)] supplies each rule’s values; one failed rule rejects the candidate.

Interactive lab · Constraint satisfaction

Generate-and-test: the naive solution

Set X₁ = 1 and leave X₂ and X₃ unassigned. Predict the rule results; then set X₃ = 0 and X₂ = 2.

02 · Systematic solving

Generate a candidate, then test every rule

1 · GenerateChoose a value for every variable.
2 · TestCheck all constraints.
3 · Keep or rejectKeep a solution; try the next candidate.

Small running example

3³ = 27

Three variables, three values each.

The search space grows quickly

4¹⁵ = 1,073,741,824

Fifteen variables, four values each.

Can we reject a bad choice before completing all the other choices? Backtracking does exactly that.

02 · Systematic solving

Backtracking cuts a branch as soon as it fails

{}Empty contextX₁ = 0X₁ = 1X₁ = 2ContinueContinueReject 9 completions
ChooseAdd one value to the partial assignment.
CheckTest rules whose scopes are now complete.
Undo on failureTry the next value; skip this branch’s completions.

Here X₁ = 2 already violates X₁ < 2. Plain backtracking does not yet reject X₁ = 0: X₃ is still unassigned.

Mathematical definition · Constraint satisfaction

Which constraints can a context evaluate?

c ≡ A ⇔ scope(c) ⊆ vars(A)

A is an updatable partial assignment called context; vars(A) is the set of variables already assigned. Here ≡ means “can be evaluated in”, not “is true”. c ≡ A if all variables in c are present in A.

Enough input does not mean true

A={X₁=1, X₃=0}. For c: X₁=X₃,
c ≡ A holds, but ε(A[scope(c)],c)=0.

The set DFS checks now

𝓒e = {c ∈ 𝓒 | c ≡ A}

𝓒ₑ contains the rules whose scopes are complete. The bar | means “such that”.

⊆ means “is a subset of”. If a variable in scope(c) is missing, c ≢ A: the rule is pending, so plain DFS cannot evaluate it yet.

02 · Systematic solving

DFS solver: Backtracking

DFS_solver(𝓧, 𝓒, A):
    𝓒ₑ ← {c ∈ 𝓒 | c ≡ A}
    if some c ∈ 𝓒ₑ has ε(A[scope(c)],c)=0:
        return ∅
    if 𝓧 = ∅: return {A}
    choose X ∈ 𝓧; s ← ∅
    for x ∈ dom(X):
        s ← s ∪ DFS_solver(𝓧∖{X}, 𝓒∖𝓒ₑ,
                           A ∪ {X=x})
    return s

What changes in recursion?

𝓧: variables still to assign.

𝓒: rules still to check; 𝓒ₑ: rules checkable now.

A: partial context; s: set of solutions.

∖: remove from a set.
∪: combine sets.

Start with all variables and rules, and A=∅. Returning ∅ means no solutions from this branch; {A} contains one solution.

Interactive lab · Constraint satisfaction

Follow one branch, then watch DFS undo it

Step from the empty context. Find the X₁ = 2 branch and its failed rule; then continue until both solutions are recorded. Focus a node to inspect its values.

02 · Systematic solving

Choose the next decision to reduce wasted work

Variable order: fewest choices first

|dom(A)| = 2 < |dom(B)| = 5|dom(A)| means the number of remaining values for A.

MRV: minimum remaining values. Choose A; it has fewer alternatives.

Value order: leave neighbors options

Rule A ≠ B; dom(A)={1,2}, dom(B)={1,3}.

TryB values left
A = 1{3}
A = 2{1,3}

Least-constraining value: try 2 first.

Degree can break an MRV tie: prefer a variable linked to more constraints on unassigned neighbors. Ordering changes effort, not the solutions.

Course §2.3.3 · From search to domain filtering

Consistency algorithms: filter before branching

DFS can be inefficient: it may try every value of B even when the chosen A has no possible partner.

DFS: discover failure four times

Rule A < B; both domains are {1,2,3,4}.

Choose A = 4, then try…Rule check
B = 14 < 1 ✗
B = 24 < 2 ✗
B = 34 < 3 ✗
B = 44 < 4 ✗

Consistency: remove the dead end first

dom(A): {1,2,3,4}

No allowed B is greater than 4.
Remove 4 before branching on A.

One domain check rules out every completion with A=4, while keeping all solutions.

Consistency algorithms remove values that cannot satisfy a constraint. Repeating these deductions as domains change is constraint propagation.

02 · Reduce the domains

Propagation removes choices that cannot work

Rule A < B; both domains initially contain {1,2,3,4}. For a unary rule, filtering is often called domain consistency.

A < BA:B:123412344 has no larger B1 has no smaller A

Remove A = 4 and B = 1. The remaining domains are dom(A)={1,2,3} and dom(B)={2,3,4}; no branching was needed.

Course §2.3.3.1 · What GAC does

Generalized arc consistency (GAC)

GAC requires every remaining value to have support for each rule involving its variable.

The idea: keep values that can work

A value has support if some allowed combination of the other values makes the checked rule true.

The GAC algorithm removes values without support and propagates the effects.

Why “generalized”?

The same test works for different scope sizes:

1 variableX < 2
2 variablesX < Y
3 variablesX + Y = Z

X, Y and Z name variables.

First identify the variable–rule connections. Then define support precisely and use a worklist to schedule the checks.

02 · Reduce the domains

Constraint network: show which rules use which variables

X₁X₂X₃X₁ < 2X₂ > 0X₃ < X₁VariablesConstraints

Bipartite graph: two kinds of node

Circle: a variable X.

Rectangle: a constraint c.

Link: X belongs to the scope of c.

A rule using k variables has k links; k is its scope size.

To make domain filtering systematic, record which rules involve each variable. Each network link becomes a support check for GAC.

Mathematical definition · Constraint satisfaction

Constraint network: G = (V, U, E₀)

Two vertex sets

V = 𝓧 = {A,B,C}

V contains the variables.

U = 𝓒 = {c₁,c₂}

c₁ is A<B; c₂ is B<C. U contains the constraints.

Edges join a variable to a rule

E₀ ⊆ V × U
(X,c) ∈ E₀ ⇔ X ∈ scope(c)

V×U means all variable–constraint pairs. E₀ keeps only pairs connected by a scope.

For this chain, E₀={(A,c₁),(B,c₁),(B,c₂),(C,c₂)}. Bipartite means every edge crosses between the two kinds of vertex.

02 · Reduce the domains

Support means at least one partner works

Rule A < B. A value of A stays only if some remaining B is larger.

Try A = 2. Remove B = 3 and B = 4: its support disappears. One working partner is enough; every partner need not work.

Mathematical definition · Constraint satisfaction

Arc consistency: every value needs a witness

Given a constraint network G and a constraint c with scope(c)={X,Y₁,…,Yₖ}, the edge (X,c) is arc consistent if:

∀ x ∈ dom(X),   ∃ y₁ ∈ dom(Y₁), …, yₖ ∈ dom(Yₖ):
ε({X=x, Y₁=y₁, …, Yₖ=yₖ}, c) = 1x and yᵢ are candidate values; i=1,…,k indexes the k other variables.

Read it in three steps

1 · ∀ x ∈ dom(X)
Take every allowed value of X, one at a time.

2 · ∃ y₁,…,yₖ in their domains
Find one combination of values for the other variables.

3 · ε(assignment, c) = 1
Those values must make rule c true. They are a support, or witness.

Example: c is A < B

Check X=A; the only partner is Y₁=B (k=1). Both domains are {1,2,3,4}.

Value of AOne supporting B
12, since 1 < 2
23, since 2 < 3
34, since 3 < 4
4None → remove A=4

A witness may depend on x. Checking (A,c) and checking (B,c) are separate edge tests.

Mathematical definition · Constraint satisfaction

GAC notation: pending checks and retained values

Use the course’s worklist name E. E₀ is the initial set of all network edges.

E: checks still to perform

E ← E₀

A pair (X,c) means “check variable X against rule c”.

{(A,c), (B,c)} → {(B,c)}

Taking (A,c) removes a pending check, not the rule or network edge.

NX: values kept in this check

NX = {x ∈ dom(X) |
x has support for c}

X is a variable; x is one value. The subscript X identifies the checked variable.

dom(X) ← NX

Replace the domain only if it has shrunk.

In the pseudocode, c′ names another rule and Z another variable. The next slide uses these names to schedule checks affected by a deletion.

02 · Reduce the domains

GAC: pseudocode and its flow

dom is the current domain mapping; E contains pending checks. The variable set 𝓧 and constraint set 𝓒 stay fixed.

01 GAC(dom, E):
02   while E ≠ ∅:
03     take and remove (X,c) from E
04     NX ← {x ∈ dom(X) | x has support for c}
05     if NX ≠ dom(X):
06       dom(X) ← NX
07       if dom(X)=∅: return dom
08       E ← E ∪ {(Z,c′) | c′≠c ∧ Z≠X
09                        ∧ {X,Z}⊆scope(c′)}
10   return dom

Follow the control flow

02–04 · Select and filter

Take one check; compute the values with support.

05–06 · Did anything change?

No: take the next check. Yes: replace the domain.

07–09 · Fail or propagate

Empty domain: return immediately. Otherwise add affected checks to E.

10 · No checks remain

Return the pruned domains.

Start with E=E₀. ∅ means empty; ∪ adds checks. Lines 08–09 revisit another variable Z in another rule c′ that also uses X.

Interactive lab · Constraint satisfaction

Watch domains shrink and checks return

Compare dom(X) before with NX kept. When values are removed, highlighted checks show the update to E.

02 · Reduce the domains

Deleting a partner can break an earlier check

Chain A < B, B < C; each domain starts at {1,2,3,4}.

A = 3B = 4C ≤ 4A < BB < CB = 4 has no support → remove itNow A = 3 has no larger B → recheck and remove it

A = 3 initially has B = 4 as a witness. After B = 4 disappears, put (A, A < B) back on the worklist.

Mathematical definition · Constraint satisfaction

GAC: which checks must be revisited?

After dom(X) shrinks in a check against c, another variable can lose support in another rule.

E ← E ∪ {(Z,c′) | c′≠c ∧ Z≠X ∧ {X,Z} ⊆ scope(c′)}

Read the three filters

c′≠c: consider another rule in 𝓒.

Z≠X: check another variable.

{X,Z}⊆scope(c′): both occur in that rule.

Connect to the chain example

X=B; c is B<CThe domain just revised.
Z=A; c′ is A<BThe check that may be outdated.
Add (A, A<B) to E

Add a check, not a deleted value. ∧ means all three filters must hold; ∪ keeps existing pending checks and adds these pairs.

02 · Reduce the domains

Read the domains before drawing a conclusion

1 · A domain is empty

dom(X) = ∅∅ is the empty set; X has no possible value.

No solution in this branch.

2 · All domains are singletons

{1}, {2}, {0}Domains of X₁, X₂, X₃, in that order.

After all GAC checks, the one remaining assignment is a solution.

3 · Choices remain

{1}, {1,2}, {0}Domains of X₁, X₂, X₃, in that order.

A solution may or may not exist. More reasoning is needed.

A fixed point means no further pruning. If choices remain, domain splitting creates branches and runs GAC again in each one (Part 2).

02 · Reduce the domains

A partner for each rule is not one shared solution

X₁X₂X₃==≠Every variable has domain {1,2,3,4}

Separate witnesses can disagree

X₁ = X₂ has the witness (1,1).

X₂ = X₃ has the witness (1,1).

X₁ ≠ X₃ has the witness (1,2).

But: the two equalities force X₁ = X₃, contradicting the inequality.

(X₁=X₂) ∧ (X₂=X₃) ∧ (X₁≠X₃): all three must hold for the same assignment. GAC finds separate witnesses, not one shared solution.

Lecture 05 · Wrap-up

Check that you can explain the three ideas

ModelWhat are the variables, their domains and the rules?
BacktrackWhich complete-scope rule lets us reject this partial assignment?
PropagateWhich value has lost every supporting partner?

A total assignment, pruned domains and a solution are different things. Be able to show the difference with the course example.

Sources & further reading

Continue exploring

Course definitions and original examples

AIML · Constraint Satisfaction Problems ↗

Course CSP, DFS, support, GAC, splitting and local search.

Read the fuller explanation

This pack’s handout ↗

Definitions, worked deductions and the assumptions behind each result.

Further reading: Poole & Mackworth, Artificial Intelligence: Foundations of Computational Agents, Chapter 4.

Continue learning
AIML · Lecture 05 · Constraint satisfaction · Part 1
Course home