0% found this document useful (0 votes)
27 views40 pages

CHP - 5 - CSPs - Hamid - AI

Uploaded by

Danial Khawaja
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)
27 views40 pages

CHP - 5 - CSPs - Hamid - AI

Uploaded by

Danial Khawaja
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/ 40

Artificial Intelligence

Constraint Satisfaction Problems

Hamid Raza Malik


What is Search For?
▪ Assumptions about the world: a single agent, deterministic actions, fully observed
state, discrete state space

▪ Search for Planning: sequences of actions


▪ The path to the goal is the important thing
▪ Paths have various costs, depths
▪ Heuristics give problem-specific guidance

▪ Search for assignments


▪ Assign values to variable while respecting certain constraint
Constraint Satisfaction Problems
Constraint Satisfaction Problems
▪ CSP: A Problem that requires its solution with some limitation/conditions also
known as constraints.
▪ It consists of:
▪ Finite set of variables V1, V2, …, Vn which stores the solution.
▪ Nonempty domain of possible values for each variable D1, D2, … Dn
▪ Finite set of constraints C1, C2, …, Cm
▪ Each constraint Ci limits the values that variables can take, e.g., V1 ≠ V2
▪ Ci = (scope, relation) Where scope is set of variables that participates in constraints
▪ Relation defines the value that variable can take.
▪ A state is an assignment of specific values to some or all of the variables.
▪ An assignment is consistent if it violates no constrains.
▪ An assignment is complete if it gives a value to every variable.
▪ A solution is a consistent and complete assignment.
Converting Problems to CSP

Step 1: Create a variable set

Step 2: Create a domain set.


Step 3: Create a constraint set with variables and domains (if possible)
after considering the constrains.

Step 4: Find optimal solution.


Example: Sudoku

▪ Variables:
▪ Each (open) square
▪ Domains:
▪ {1,2,…,9}
▪ Constraints:
9-way alldiff for each column
9-way alldiff for each row
9-way alldiff for each region
(or can have a bunch of pairwise
inequality constraints)
Sudoku

•Variables: 81 slots

•Domains =
{1,2,3,4,5,6,7,8,9}

•Constraints:
•27 not-equal
Constraint
propagation
23
426

Each row, column and major block must be alldifferent


“Well posed” if it has unique solution: 27 constraints
Sudoku

Each row, column and major block must be alldifferent


“Well posed” if it has unique solution
Real-world CSPs
▪ Assignment problems
▪ e.g., who teaches what class
▪ Timetabling problems
▪ e.g., which class is offered when and where?
▪ Transportation scheduling
▪ Factory scheduling
▪ Notice that many real-world problems involve real-valued
variables
Constraint satisfaction problems
▪ An assignment is complete when every variable has a value.
▪ A solution to a CSP is a complete assignment that satisfies all
constraints.
▪ Some CSPs require a solution that maximizes an objective function.
▪ Applications:
▪ Scheduling the Hubble Space Telescope,
▪ Floor planning for VLSI (Very large-scale integration)
▪ Map coloring,
▪ Cryptography
Example: Map-Coloring
▪ Variables:
WA, NT, Q, NSW, V, SA, T
▪ Domains:
Di = {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: Map-Coloring

Solutions are complete and consistent assignments,


▪ e.g., WA = red, NT = green,Q = red,NSW = green,
V = red,SA = blue,T = green
Example: 4-Queens

▪ Variables: 0 1 2 3
Q1, Q2, Q3, Q4 0
▪ Domains: 1
4 * 4 chess Board
2
▪ Constraints: 3

1. One Row cannot have more than 1-Q


2. One column cannot have more than 1-Q
3. Diagonal cannot have more than 1-Q
Example: Map-Coloring
CSP Assignment
• Each state in CSP is defined by an assignment of value to some of
the variables, {V1 = Xi , Vj = Xj }

OR
• A state is an assignment of values to some of all variables.
• Types of assignments
• Local Assignment
• Partial Assignment
• Complete Assignment
CSP Assignment

• Legal/ Consistent Assignment


• An Assignment that does not violate constraints is called a legal
assignment
• Example: Map coloring CSP

• Complete Assignment
• An Assignment is complete when every variable has a value.

• Partial Assignment
• An Assignment is partial when variable has no value.
Varieties of CSPs
▪ Discrete variables
▪ finite domains:
▪ n variables, domain size d → O(dn) 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
▪ linear constraints solvable
19
in polynomial time by linear programming
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., crypt arithmetic column constraints
Example: Crypt arithmetic
▪ Variables:
F T U W R O X 1 X2 X3
▪ Domain: {0,1,2,3,4,5,6,7,8,9}
▪ Constraints: Alldiff (F,T,U,W,R,O)
▪ O + O = R + 10 · X1
▪ X1 + W + W = U + 10 · X2
▪ X2 + T + T = O + 10 · X3
▪ X3 = F, T ≠ 0, F ≠ 0
Crypt arithmetic
1) Digit ranges from 0 to 9 only.
2) Each Variable should have unique and distinct value.
3) Each Letter, Symbol represents only one digit throughout the
problem.
4) You have to find the value of each letter in the Cryptarithmetic.
5) There must be only one solution to the problem.
6) The Numerical base, unless specifically stated, is 10.
7) Numbers must not begin with zero i.e. 0123 (wrong) , 123 (correct).
8) After replacing letters by their digits, the resulting arithmetic
operations must be correct.
CSP as a standard search problem
▪ A CSP can easily be expressed as a standard search problem.
▪ Initial State: the empty assignment {}.
▪ Operators: Assign value to unassigned variable provided that there is
no conflict.
▪ Goal test: assignment consistent and complete.

▪ Path cost: constant cost for every step.

▪ Solution is found at depth n, for n variables


▪ Hence depth first search can be used
Backtracking search
▪ Variable assignments are commutative,
▪ Eg [ WA = red then NT = green ]
equivalent to [ NT = green then WA = red ]

▪ Only need to consider assignments to a single variable at each node


→b = d and there are dn leaves
▪ Depth-first search for CSPs with single-variable assignments is called
backtracking search

▪ Backtracking search basic uninformed algorithm for CSPs


Example
Example
Example
Example
Example

x
Red? x
Green? x
Blue?
Constraint graph
▪ Binary CSP: each constraint relates two variables

▪ Constraint graph:
▪ nodes are variables
▪ arcs are constraints

▪ CSP benefits
▪ Standard representation pattern
▪ Generic goal and successor functions
▪ Generic heuristics (no domain specific expertise).

▪ Graph can be used to simplify search.


▪ e.g. Tasmania is an independent subproblem.
NT
Q
WA
SA NSW

V
NT
Q T
WA NT
Q
NS
SA WA
W NS
SA
W
V
NT V
Q T
WA T
NT
NS Q NT
SA WA
Q
W WA
NS
SA NS
W SA
V W
V
T V
NT T
Q T
WA
NS NT
SA Q
W
WA
NS
V SA
W
T V
NT T
Q
WA NT
Q
NS WA
SA
W NS
SA
W
V
V
T
T
Improving backtracking efficiency
▪ General-purpose methods can give huge speed gains:
▪ Which variable should be assigned next?
▪ In what order should its values be tried?
▪ Can we detect inevitable failure early?
• Heuristics:
1. Most constrained variable
2. Most constraining variable
3. Most constraining variable
4. Forward checking
Most constrained variable
▪ Most constrained variable:
choose the variable with the fewest legal values

▪ a.k.a. minimum remaining values (MRV) heuristic


Most constraining variable
▪ Tie-breaker among most constrained variables
▪ Most constraining variable:
▪ choose the variable with the most constraints on remaining variables
Least constraining value
▪ Given a variable, choose the least constraining value:
▪ the one that rules out the fewest values in the remaining variables

▪ Combining these heuristics makes 1000 queens feasible


Forward Checking
▪ Idea:
▪ Keep track of remaining legal values for unassigned variables
▪ Terminate search when any variable has no legal values
Forward checking

▪ Idea:
▪ Keep track of remaining legal values for unassigned variables
▪ Terminate search when any variable has no legal values
Forward checking
▪ Idea:
▪ Keep track of remaining legal values for unassigned variables
▪ Terminate search when any variable has no legal values
Forward checking
▪ Idea:
▪ Keep track of remaining legal values for unassigned variables
▪ Terminate search when any variable has no legal values

▪ No more value for SA: backtrack


Summary
▪ CSPs
▪ special kind of problem: states defined by values of a fixed set of variables, goal test defined by
constraints on variable values

▪ Backtracking=depth-first search with one variable assigned per node


▪ Heuristics
▪ Variable ordering and value selection heuristics help significantly

▪ Constraint propagation does additional work to constrain values and detect


inconsistencies
▪ Works effectively when combined with heuristics

▪ Iterative min-conflicts is often effective in practice.


▪ Graph structure of CSPs determines problem complexity
▪ e.g., tree structured CSPs can be solved in linear time.

You might also like