Constraint Satisfaction Problem: Teknik Elektro Fakultas Informatika Dan Teknik Elektro Institut Teknologi Del
Constraint Satisfaction Problem: Teknik Elektro Fakultas Informatika Dan Teknik Elektro Institut Teknologi Del
PROBLEM
14S3205 - Artificial Intelligence
Guntur Petrus Boy Knight
Teknik Elektro
Fakultas Informatika dan Teknik Elektro
Institut Teknologi Del
OUTLINE
• Constraint satisfaction problems
• CSP as a search problem
• Backtracking
• Forward Checking
• Constraint propagation in CSPs
• Arc-consistency
INTRODUCTION
• Previous chapters explored the idea that problems can be solved by
searching in a space of states.
• These states can be evaluated by domain-specific heuristics and tested to
see whether they are goal states
• From the point of view of the search algorithm, however, each state is
atomic, or indivisible—a black box with no internal structure
• We can solve a wide variety of problems more efficiently, using factored
representation for each state: a set of variables, each of which has a value
• A problem is solved when each variable has a value that satisfies all the
constraints on the variable
• A problem described this way is called a constraint satisfaction problem, or
CSP
MAIN IDEA
• Represent the constraints that solutions must satisfy in a uniform declarative
language
• Find solutions by GENERAL PURPOSE search algorithms with no changes from
problem to problem
• If we specify the problem in a formal declarative language, a general-
purpose algorithm will do everything else
CONSTRAINT SATISFACTION
PROBLEMS
• A CSP consists of:
• 𝐹𝑖𝑛𝑖𝑡𝑒 𝑠𝑒𝑡 𝑜𝑓 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒𝑠 𝑋1 , 𝑋2 , … , 𝑋𝑛
• Nonempty domain of possible values for each variable
𝐷1 , 𝐷2 , … , 𝐷𝑛 𝑤ℎ𝑒𝑟𝑒 𝐷𝑖 = 𝑣1 , 𝑣2 , … , 𝑣𝑘
• 𝐹𝑖𝑛𝑖𝑡𝑒 𝑠𝑒𝑡 𝑜𝑓 𝑐𝑜𝑛𝑠𝑡𝑟𝑎𝑖𝑛𝑡𝑠 𝐶1 , 𝐶2 , … , 𝐶𝑚
• A consistent assignment does not violate the constraints.
• Example problem: Sudoku
CONSTRAINT IN SUDOKU
In 1 row every
cell must have
different
number
In 1 square (sub-
region) every cell
must have different
number
In 1 colomn
every cell must
have different
number
CONSTRAINT SATISFACTION
PROBLEMS
• An assignment is complete when every variable is assigned a value.
• An assignment that does not violate any constraints is called a consistent or
legal assignment
• A partial assignment is one that assigns values to only some of the variables.
• A solution to a CSP is a complete, consistent assignment.
• Solutions to CSPs can be found by a completely general-purpose algorithm,
given only the formal specification of the CSP.
APPLICATIONS
• Map coloring
• Scheduling problems
• Class scheduling in ITDel
• e.g., which class is offered when and where?
• Scheduling the Hubble Space Telescope
• Bill Gates’ project using computer
• Sudoku
• Circuit layout
• …
EXAMPLE OF CSP: MAP
COLORING
• Variables: WA, NT, Q, NSW, V, SA, T
• Domains: 𝐷𝑖 = {red, green, blue}
• Constraints: adjacent regions must have
different colors
• e.g., WA ≠ NT
• So (WA,NT) must be in {(red, green),(red, blue),
(green, red), …}
EXAMPLE OF CSP: MAP
COLORING
• Solutions : Complete and consistent
assignments
• E.g. WA = red, NT = green,Q = red, NSW = green,
V = red, SA = blue, T = green
EXAMPLE OF CSP: N-QUEENS
• Variables: 𝑄𝑘
• Domains: 𝐷𝑖 = {1, 2, …, N}
• Constraints: each queen must not at a
vulnerable position relative to other queens
• Implicit: ∀𝑖, 𝑗 𝑛𝑜𝑛 − 𝑡ℎ𝑟𝑒𝑎𝑡𝑒𝑛𝑖𝑔 (𝑄𝑖 , 𝑄𝑗 )
• Explicit: 𝑄𝑖 , 𝑄𝑗 ∈ { 1,3 , 1,4 , … }
…
EXAMPLE OF CSP: N-QUEENS
• Variables: 𝐸𝑎𝑐ℎ (𝑜𝑝𝑒𝑛) 𝑠𝑞𝑢𝑎𝑟𝑒
• Domains: 𝐷𝑖 = {1, 2, …, 9}
• Constraints:
• 9-way alldiff for each column
• 9-way alldiff for each row
• 9-way alldiff for each square
BENEFITS OF CSP
• Clean specification of many problems, generic goal, successor function &
heuristics
• CSP “knows” which variables violate a constraint and hence where to focus
the search
• CSPs: Automatically prune off all branches that violate constraints
• State space search could do this only by hand-building constraints into the
successor function
CSP REPRESENTATIONS
• Constraint graph:
• nodes are variables
• arcs are (binary) constraints
• Standard representation pattern:
• variables with values
• Constraint graph simplifies search
• e.g. Tasmania is an independent
subproblem.
• Map coloring problem is an example of
binary CSP
• each constraint relates two variables
VARIETIES OF CSPS
• Discrete variables
• finite domains:
• n variables, domain size 𝑑 → 𝑂(𝑑 𝑛 ) complete assignments
• e.g., Boolean CSPs, includes Boolean satisfiability (NP-complete)
• infinite domains:
• integers, strings, etc.
• e.g., job scheduling, variables are start/end days for each job
• need a constraint language,
• e.g., StartJob1 + 5 ≤ StartJob3
• Continuous variables
• e.g., start/end times for Hubble Space Telescope observations
VARIETIES OF CONSTRAINTS
• Unary constraints involve a single variable
• e.g., SA ≠ green
• Binary constraints involve pairs of variables
• e.g., SA ≠ WA
• Higher-order constraints involve 3 or more variables
• e.g., sudoku
• Preference (soft constraints) e.g. red is better than green can be represented
by a cost for each variable assignment
CSP AS A SEARCH PROBLEM
• A CSP can easily be expressed as a search problem
• Initial State: the empty assignment {}.
• Successor function: Assign value to any unassigned variable provided that there
is not a constraint conflict.
• Goal test: the current assignment is complete.
• Path cost: a constant cost for every step.
• Solution is always found at depth n, for n variables
• Hence Depth First Search can be used
SEARCH AND BRANCHING
FACTOR
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} {1,2,3,4}
2
3 Q3 Q4
{1,2,3,4} {1,2,3,4}
4
EXAMPLE: 4-QUEENS PROBLEM
• Assign value to unassigned variable
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} {1,2,3,4}
2
3 Q3 Q4
{1,2,3,4} {1 ,2,3, 4}
4
EXAMPLE: 4-QUEENS PROBLEM
• Forward check
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} { , ,3,4}
2
3 Q3 Q4
{ ,2, ,4} { ,2,3, }
4
EXAMPLE: 4-QUEENS PROBLEM
• Assign value to unassigned variable
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} { , ,3,4}
2
3 Q3 Q4
{ ,2, ,4} { ,2,3, }
4
EXAMPLE: 4-QUEENS PROBLEM
• Forward check
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} { , ,3,4}
2
3 Q3 Q4
{ , , , } { ,2, , }
4
EXAMPLE: 4-QUEENS PROBLEM
• Backtrack
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} { , ,3,4}
2
3 Q3 Q4
{ , , , } { ,2, , }
4
EXAMPLE: 4-QUEENS PROBLEM
• Picking up a little later after two steps of backtracking
• Assign value to unassigned variable
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} {1,2,3,4}
2
3 Q3 Q4
{1,2,3,4} {1,2,3,4}
4
EXAMPLE: 4-QUEENS PROBLEM
• Forward Check
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} { , , ,4}
2
3 Q3 Q4
{1, ,3, } {1, ,3,4}
4
EXAMPLE: 4-QUEENS PROBLEM
• Assign value to unassigned variable
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} { , , ,4}
2
3 Q3 Q4
{1, ,3, } {1, ,3,4}
4
EXAMPLE: 4-QUEENS PROBLEM
• Forward Check
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} { , , ,4}
2
3 Q3 Q4
{1, , , } {1, ,3, }
4
EXAMPLE: 4-QUEENS PROBLEM
• Assign value to unassigned variable
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} { , , ,4}
2
3 Q3 Q4
{1, , , } { , ,3, }
4
EXAMPLE: 4-QUEENS PROBLEM
• Forward Check
Q1 Q2 Q3 Q4
Q1 Q2
1
{1,2,3,4} { , , ,4}
2
3 Q3 Q4
{1, , , } { , ,3, }
4
TOWARDS CONSTRAINT
PROPAGATION
• Forward checking propagates information from assigned to unassigned
variables, but doesn't provide early detection for all failures: