Definition: A Constraint Satisfaction Problem (CSP) is a mathematical
problem defined by:
1. Variables: These are the unknowns that need to be solved. Each
variable can take a value from a specific domain.
2. Domains: Each variable has a set of possible values (the domain)
from which it can choose.
3. Constraints: These are the conditions that the values assigned to the
variables must satisfy. Constraints define the relationships between the
variables.
CSP Structure:
Variables (X): X1, X2, X3, ..., Xn
Domains (D): D(X1), D(X2), ..., D(Xn)
Constraints (C): A set of rules that restrict the possible values for the
variables.
Example:
Problem: Assigning colors to a map such that no two adjacent regions
have the same color.
Variables: Regions of the map (e.g., Region1, Region2, ...)
Domains: Possible colors (e.g., Red, Green, Blue)
Constraints: Adjacent regions must not share the same color.
Solving CSPs - Backtracking Algorithm
Backtracking is a common method used to solve CSPs. It works by trying to
assign values to the variables step-by-step and then backtracking (undoing
assignments) when a constraint is violated.
Backtracking Algorithm Steps:
1. Select an unassigned variable.
2. Choose a value from the domain of the variable.
3. Check if the current assignment satisfies all constraints:
o If yes, move to the next unassigned variable.
o If no, backtrack by undoing the last assignment and trying a
different value.
4. Repeat until all variables are assigned valid values or no solution
exists.
Backtracking Example:
Variables: X1, X2, X3
Domains: {1, 2, 3}
Constraints: X1 ≠ X2, X2 ≠ X3.
Solution:
1. Try X1 = 1, then X2 = 2.
2. Check if X2 ≠ X3, which is true, so we can assign X3 = 3.
3. Done!
The example solved in the class:
Variables: A,B,C
Domains: {1, 2, 3}
Constraints: A>B, B ≠ C, A≠ C.
Practical Use Cases for CSPs
CSPs are widely used in real-world applications, including:
Sudoku: Each square on the grid is a variable, and the domain is the
numbers 1 through 9. The constraints are that each row, column, and
3x3 block must contain unique numbers.
Graph Coloring: Assigning colors to the regions of a map such that
adjacent regions have different colors.
Scheduling Problems: Assigning time slots to tasks such that no two
tasks overlap, and other constraints are satisfied (e.g., resource
allocation).
Constraint Propagation & Inference
Constraint Propagation refers to reducing the domains of variables by
enforcing constraints. It tries to infer the values that variables can take
based on other variables' assignments.
Types of Inference:
1. Forward Checking:
o After assigning a value to a variable, we check all unassigned
variables to see if the assignment violates any constraints.
o If a variable has no valid value remaining after an assignment, it
triggers backtracking.
Example:
If X1 is assigned the value 1, check whether the domain of X2 or X3 is still
valid considering the constraint X1 ≠ X2. If the domain of X2 becomes
empty, backtrack.
2. Arc-Consistency (AC-3):
o Ensures that for every variable, every value in its domain has a
consistent value in the adjacent variable's domain.
o AC-3 reduces the domains of the variables and prunes
inconsistent values, simplifying the CSP.
o Although it doesn’t solve the CSP, it reduces the search space,
making further solving steps (e.g., backtracking) more efficient.
AC-3 Algorithm:
The AC-3 algorithm checks each variable's domain to ensure that all its values have a valid
corresponding value in its neighboring variable’s domain. If a value has no valid counterpart,
it is removed.
1. Turn each binary constraint into two arcs e.g., A≠B becomes A≠B and B≠A
2. Add all the arcs to an agenda
3. Repeat until the agenda is empty
a. Take an arc (Xi,Xj) off the agenda and check it
b. For every value of Xi there must be some value of Xj
c. Remove any inconsistent values from Xi
d. If Xi has changed, add all arcs of the form (X?,Xi)to the agenda.
Example solved in the class:
Variables: A, B, C
Domains:
A: {1,2,3}
B: {1,2,3}
C: {1,2,3}
Constraints:
A<B
B=C