0% found this document useful (0 votes)
11 views20 pages

05 CSP

The document provides an overview of Constraint Satisfaction Problems (CSPs), defining them as tuples consisting of variables, domains, and constraints. It discusses various concepts such as consistency, global constraints, and techniques for solving CSPs, including backtracking and local search strategies. Examples like the 3-coloring problem and Sudoku illustrate the application of CSPs in artificial intelligence.

Uploaded by

leimu.864
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views20 pages

05 CSP

The document provides an overview of Constraint Satisfaction Problems (CSPs), defining them as tuples consisting of variables, domains, and constraints. It discusses various concepts such as consistency, global constraints, and techniques for solving CSPs, including backtracking and local search strategies. Examples like the 3-coloring problem and Sudoku illustrate the application of CSPs in artificial intelligence.

Uploaded by

leimu.864
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Constraint Satisfaction

Introduction to Artificial Intelligence


© G. Lakemeyer

G. Lakemeyer

Winter Term 2024/25


3-coloring Problem

Northern
Territory
Queensland
Western
© G. Lakemeyer

Australia
South
Australia New
South
Wales
Victoria

Tasmania

AI/WS-2024/25 2 / 20
Sudoku

1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

A 9 A 9 7 2 8 5 3 6 1 4

B 9 1 2 7 3 B 6 5 8 4 9 1 2 7 3

C 1 C 1 3 4 7 6 2 9 5 8
© G. Lakemeyer

D 4 5 7 6 D 4 8 5 2 7 6 3 9 1

E 7 8 E 3 9 7 5 1 8 4 2 6

F 6 3 5 F 2 1 6 9 3 4 5 8 7

G 5 7 G 8 6 9 1 4 5 7 3 2

H 3 6 H 5 2 3 6 8 7 1 4 9

I 7 4 1 2 9 I 7 4 1 3 2 9 8 6 5

AI/WS-2024/25 3 / 20
What is a Constraint Satisfaction Problem (CSP)?
A CSP is defined as a tuple ⟨X , D , C ⟩, where
X = {X1 , . . . , Xn } is a set of variables;
D = {D1 , . . . , Dn } is a set of domains, one for each variable;
C is a set of constraints that specify allowable combinations of values.
A constraint has the form ⟨scope, rel⟩, where scope is a tuple of variables and
rel a relation over those variables. Rel can be given explicitly or as a function
© G. Lakemeyer

that computes membership in the relation.

Example Constraint
Let X1 and X2 have domain {1, 2, 3} The constraint that X1 must be bigger
than X2 can be given as

⟨(X1 , X2 ), {(3, 1), (3, 2), (2, 1)}⟩ or simply ⟨(X1 , X2 ), X1 > X2 ⟩

Domains can be finite, infinite or even continuous. Map-coloring and Sudoku


have finite domains. Scheduling experiments on the Webb telescope is a
continuous CSP.
AI/WS-2024/25 4 / 20
Some Terminology

Assignment: assigns (some) variables values from their domains;


assignments are called consistent or legal if they do not violate any
constraints.
© G. Lakemeyer

Complete assignment: assigns every variable a value


Solution: complete and consistent assignment
Partial assignment: leaves some variable unassigned
Partial solution: a partial, consistent assignment

AI/WS-2024/25 5 / 20
3-coloring of Australia
Northern
Territory
Queensland
Western
3CA = ⟨X , D , C ⟩ with Australia
South
Australia New

X = {WA, NT , Q , NSW , V , SA, T } South


Wales
Victoria
Di = {red , green, blue} for all i ∈ X .
Tasmania

C = {SA ̸= WA, SA ̸= NT , SA ̸= Q ,
SA ̸= NSW , SA ̸= V , WA ̸= NT ,
NT ̸= Q , Q ̸= NSW , NSW ̸= V }
© G. Lakemeyer

Here: X ̸= Y is short for ⟨(X , Y ), Y ̸= Y ⟩.

Often a CSP like 3CA is represented as a constraint graph:


NT
Q
WA

SA NSW

AI/WS-2024/25 T 6 / 20
More on Constraint Graphs

The 3CA constraint graph has only binary constraints.


For n-ary constraints one can use hypergraphs. In addition to nodes that
represent variables of the CSP, hypergraphs contain nodes that represent
constraints with edges connecting to all variables involved in the constraint.

Cryptarithmetic example with non-binary constraints


© G. Lakemeyer

Theorem: Every finite-domain CSP can be converted into an equivalent CSP


with only binary constraints (needs additional variables)
Here equivalent means same solutions for the variable of the original CSP.

AI/WS-2024/25 7 / 20
Inference: Constraint Propagation

Idea:
Before assigning values to variables, use constraints among variables to
reduce the number of consistent assignments for each variable.
Distinction between local and global consistency.
© G. Lakemeyer

Let us begin with local consistency among a fixed number of variables.

Node consistency:
Unary constraint on variables.

Consider variable X with DX = {1, 2, 3} and unary constraing X < 3.


=⇒ Remove 3 from DX .

AI/WS-2024/25 8 / 20
Arc Consistency

Xi is arc-consistent wrt. Xj if for every value of Di there is a value of Dj that


satisfies the constraint on the arc (edge) (Xi , Xj ).
A graph is arc-consistent if every variable is arc-consistent with every other
variable.
© G. Lakemeyer

Consider X and Y with DX = DY = {0, 1, . . . , 9} and constraint Y = X 2 or


((X , Y ), {(0, 0), (1, 1), (2, 4), (3, 9)}).
To make X arc-consistent with Y , reduce DX to {0, 1, 2, 3}.
To make Y arc-consistent with X , reduce DY to {0, 1, 4, 9}.

AI/WS-2024/25 9 / 20
Algorithm to compute arc-consistency
© G. Lakemeyer

Computational complexity is O (cd 2 ), where c is the number of binary


constraints (arcs) and d is the maximal domain size of variables.

AI/WS-2024/25 10 / 20
In General: k -Consistency

A CSP is k -consistent if, for any k − 1 variables and for any consistent
assignment to those variables, a consistent value can always be assigned to
any k the variable.
A CSP is strongly k -consistent, if it is i-consistent for all 1 ≤ i ≤ k .
© G. Lakemeyer

Note 1: Node consistency corresponds to 1-consistency and arc consistency


to 2-consistency.
Note 2: If a CSP with n variables is strongly n-consistent, then a consistent
assignment to all variables must exist. However, computing n-consistency is
exponential in n in both time and space. (Constraint satisfaction is an
NP-complete problem in general.)

AI/WS-2024/25 11 / 20
Global Constraints
Global constraints are constraints that involve an arbitrary number (not
necessily all) of variable.

An example is the Alldiff constraint. Alldiff (X1 , . . . , Xk ) requires that all k


variables have distinct values.
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

A 9 A 9 7 2 8 5 3 6 1 4

B 9 1 2 7 3 B 6 5 8 4 9 1 2 7 3
© G. Lakemeyer

C 1 C 1 3 4 7 6 2 9 5 8

D 4 5 7 6 D 4 8 5 2 7 6 3 9 1

E 7 8 E 3 9 7 5 1 8 4 2 6

F 6 3 5 F 2 1 6 9 3 4 5 8 7

G 5 7 G 8 6 9 1 4 5 7 3 2

H 3 6 H 5 2 3 6 8 7 1 4 9

I 7 4 1 2 9 I 7 4 1 3 2 9 8 6 5

Alldiff (A1, A2, A3, A4, A5, A6, A7, A8, A9)
Alldiff (A1, A2, A3, B1, B2, B3, C1, C2, C3)
etc.
Other examples are resource constraints in scheduling problems.
AI/WS-2024/25 12 / 20
Backtracking

Roughly: Systematically assign values to variables, on failure retract a choice


and try another one.
© G. Lakemeyer

AI/WS-2024/25 13 / 20
Example 4-Queens
© G. Lakemeyer

AI/WS-2024/25 14 / 20
Variable and Value Ordering
Minimum-remaining-value Heuristic
Choose a variable for assignment with the least number of values that can be
assigned consistently.
Idea behind: Such a variable has a high chance of failure, avoiding useless
tries of other variables.

Degree Heuristic
© G. Lakemeyer

NT
Q
Choose a variable with the highest WA

SA NSW
number of constraints on other unas-
V
signed variables.
T

Least-constraining Value
Choose a value that rules out fewest choices for neighboring variables.
Suppose WA = red, NT = green, and the next choice is for Q. Blue is a bad
choice as it leaves no value for SA.
AI/WS-2024/25 15 / 20
Inference and Backjumping for Intelligent Backtracking

Maintaining Arc Consistency (MAC)


After Xi is assigned a value, Inference calls AC − 3, but only start with arcs
(Xj , Xi ) in the queue for unassigned variables Xj which are neighbors of Xi . If
the domain of any variable becomes empty, backtrack immediately!

Backjumping
© G. Lakemeyer

Keep a list of assignments in conflict with the next variable assignment.


Backtrack to the most recent assignment in this conflict set. (MAC can
produce the conflict set.)

Partial assignment: NT
Q
WA
{Q = red , NSW = green, V = blue, T = red } SA NSW

Conflict set: V

{Q = red , NSW = green, V = blue}. T

Jump over T to a new value for V .

AI/WS-2024/25 16 / 20
Local Search

Another approach is to choose an assignment to all variables and then modify


assignments of conflicted variables. A good strategy is the min-conflict
heuristic: choose a value that minimizes the number of conflicts for that
variable.

8-Queens Problem
© G. Lakemeyer

AI/WS-2024/25 17 / 20
Tree-structured Constraint Graphs

Constraint Tree
A constraint graph is a tree if any two variable are connected by only one path.

Topologically Sorted Tree


Choose any variable as the root of the tree
© G. Lakemeyer

Choose an ordering such that each variable appears after its parent in
the tree

AI/WS-2024/25 18 / 20
Solving Tree-structured CSPs

Directional Arc Consistency (DAC)


A CSP with an ordering of variables X1 , . . . , Xn is directional arc-consistent if
Xi is arc-consistent with each Xj for j > i.
© G. Lakemeyer

AI/WS-2024/25 19 / 20
Turning Constraint Graphs into Trees

1 Choose a subset S of variables such that the constraint graph becomes


a tree after removal of S (cycle set).
2 For each assignment to all vars in S that satisfies all constraints on S,
1 remove from the domains of remaining variables any values that are
inconsistent with the assignment for S;
2 if the remaining CSP has a solution, return it together with the assignment
for S.
© G. Lakemeyer

AI/WS-2024/25 20 / 20

You might also like