0% found this document useful (0 votes)
25 views50 pages

Week-7 - CSPs

Uploaded by

grupsakli
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)
25 views50 pages

Week-7 - CSPs

Uploaded by

grupsakli
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/ 50

Dr.

Ahmet Esad TOP


[email protected]
8
X7 9
X7 10
4 initial states (K=4)
9 X8 Branching factor b=2

9 9 Higher value is better


8 9 10
9 10

8 10
7 9 9
X6 X5

6
X7 8
X3 9
X7 9
States need to be encoded

Initial Population

oGenetic algorithms use a natural selection metaphor


o Keep best K individuals at each step (selection) based on a fitness function
o Also have pairwise crossover operators, with optional mutation to give variety
oPossibly the most misunderstood, misapplied (and even maligned)
technique around
o Represent state by a string of 8 digits in {1..8}
o S1 = ‘32752411’
o Why does crossover make sense here?
o Left quadrant and right quadrant have the right number of queens
o What would mutation be?
o Change 1 digit (1 queen location)
o What would a good fitness function be?
o Fitness function = # of non-attacking pairs
o F(Ssolution) = 8*7/2 = 28
o F(S1) = 24
oMany configuration and optimization problems can be formulated as local
search
oGeneral families of algorithms:
o Hill-climbing, continuous optimization
o Simulated annealing (and other stochastic methods)
o Local beam search: multiple interaction searches
o Genetic algorithms: break and recombine states

Many machine learning algorithms are local searches


oAssumptions about the world: a single agent, deterministic actions, fully
observed state, discrete state space

oPlanning: sequences of actions


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

oIdentification: assignments to variables


o The goal itself is important, not the path
o All paths at the same depth (for some formulations)
o CSPs are a specialized class of identification problems
N variables
domain D
x2
constraints x1

states goal test successor


partial complete; satisfies function
assignment constraints assign an unassigned variable
o Standard search problems:
o State is a “black box”: arbitrary data structure
o Goal test can be any function over states
o Successor function can also be anything

o Constraint satisfaction problems (CSPs):


o A special subset of search problems
o State is defined by variables Xi with values from a domain D
(sometimes D depends on i)
o Goal test is a set of constraints specifying allowable
combinations of values for subsets of variables
o Simple example of a formal representation language
o You are writing down a model of the World in CSP speak
o Allows useful general-purpose algorithms with more
power than standard search algorithms
o Because we can peek inside the state
o Variables:

o Domains:

o Constraints: adjacent regions must have different colors


Implicit:

Explicit:

o Solutions are assignments satisfying all constraints, e.g.:



oBinary CSP: each constraint relates (at
most) two variables

oBinary constraint graph: nodes are


variables, arcs show constraints

oGeneral-purpose CSP algorithms use the


graph structure to speed up search. E.g.,
Tasmania is an independent subproblem!
oFormulation 1:
oVariables:
oDomains:

We want a peaceful kingdom


oConstraints:
Horizontal

What about using 0 queens?


oFormulation 2:
oVariables:
Which row is the queen on
oDomains:

oConstraints:
Implicit:
(1,2)?
Explicit:
• Variables:

• Domains:

• Constraints:
o Variables:
o Each (open) square
o Domains:
o {1,2,…,9}
o 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) (Unary
constraints)
oThe Waltz algorithm is for interpreting line
drawings of solid polyhedra as 3D objects
oAn early example of an AI computation
posed as a CSP

?
oApproach:
o Each intersection is a variable
o Adjacent intersections impose
constraints on each other
o Solutions are physically realizable 3D
interpretations
oDiscrete Variables
o Finite domains
o Size d means O(𝑑 𝑛 ) complete assignments
o E.g., Boolean CSPs, including Boolean satisfiability
o each of the variables has two things: True and False
o Infinite domains (integers, strings, etc.)
o E.g., job scheduling, variables are start/end times for each job
o Linear constraints solvable
o E.g., this job has to end before that job starts

oContinuous variables
o E.g., start/end times for Hubble Telescope observations
o Non-integer – real valued times
o Varieties of Constraints
o Unary constraints involve a single variable
(equivalent to reducing domains), e.g.:

o Binary constraints involve pairs of variables, e.g.:

o Higher-order constraints involve 3 or more variables:


e.g., cryptarithmetic column constraints

o Preferences (soft constraints):


o E.g., Color this map but I like red. Use red if you can.
 Red is better than green
o Often representable by a cost for each variable
assignment
o Scheduling problems: e.g., when can we all meet?
o Timetabling problems: e.g., which class is offered when and where?
o Assignment problems: e.g., who teaches what class
o Hardware configuration
o Transportation scheduling
o Factory scheduling
o Circuit layout
o Fault diagnosis
o … lots more!

o Many real-world problems involve real-valued variables…


oStates defined by the values assigned so far
(partial assignments)
o Initial state (root of the search tree): the empty
assignment, {} (no variables have any values)
o Successor function: assign a value to an unassigned
variable
o Goal test: the current assignment is complete and
satisfies all constraints

oWe’ll start with the straightforward, naïve


approach, then improve it
oWhat would BFS do?
{} o The solutions are at the bottom
{WA=R}
(nightmare for BFS)
{WA=R, SA=B}
{WA=R, SA=B, NT=G}

oWhat would DFS do?


oLet’s see
o Not good
o We should be able to apply the goal test incrementally
o before assigning everything and then checking it
o Even it is evident that 2 blues at the second step is not allowed
o Backtracking search is the basic uninformed algorithm for solving CSPs

o Idea 1: One variable at a time


o Variable assignments are commutative, it doesn’t matter what order you got
to them, so fixed ordering
o Remember, the path doesn’t matter for CSPs
o i.e., [WA = red then NT = green] same as [NT = green then WA = red]
o Only need to consider assignments to a single variable at each step

o Idea 2: Check constraints as you go


o i.e., consider only values which do not conflict with previous assignments
o As soon as there is a conflict (violation), we don’t have to keep going/checking
o Might have to do some computation to check the constraints
o “Incremental goal test” – which we couldn’t do that with search

o Depth-first search with these two improvements is called


backtracking search
o Can solve n-queens for n  25
Where is {Red, Red}?

{Red, Green, Green}


is crossed off
o Backtracking = DFS + variable-ordering + fail-on-violation
o What are the choice points?
oGeneral-purpose ideas give huge gains in speed
o Not like A* or heuristics (which were custom to your
search problem)

oOrdering:
o Which variable should be assigned next?
o In what order should its values be tried?

oFiltering:
o Can we detect inevitable failure early?
o as opposed to wait until hitting a dead-end
oFiltering: Keep track of domains for unassigned variables and cross off bad options
oForward checking: Cross off values that violate a constraint when added to the
existing assignment

NT Q
WA
SA NSW
V

They are crossed-off

SA’s domain is shrinked to 0


o Forward checking propagates information from assigned to unassigned variables, but
doesn't provide early detection for all failures:

NT Q
WA
SA
NSW
V

o NT and SA cannot both be blue!


o Why didn’t we detect this yet?
o Constraint propagation: reason from constraint to constraint
o An arc X  Y is consistent iff for every x in the tail there is some y in the head which could be
assigned without violating a constraint
o For everything on the tail, there should be at least one OK option in the head.

NT Q
WA
SA
NSW
V
Not
Consistent
consistent

o Tail = NT, head = WA


o If NT = blue: we could assign WA = red
o If NT = green: we could assign WA = red
o If NT = red: there is no remaining assignment to WA that we can use Delete from the tail!
o Deleting NT = red from the tail makes this arc consistent
o Forward checking: Enforcing consistency of arcs pointing to each new assignment
o A simple form of propagation makes sure all arcs are consistent:

NT Q
WA SA
NSW
V Empty domain
Consistent
Not Consistent
Consistent (detected failure)
ConsistentNotConsistent
NotConsistent Consistent
Consistent

o Important: If X loses a value, neighbors of X need to be rechecked! Remember: Delete


from the tail!
o Arc consistency detects failure earlier than forward checking
o Can be run as a preprocessor or after each assignment
o While still searching with backtracking, running after every assignment will reduce backtracking rates
o What’s the downside of enforcing arc consistency?
𝑛2 - It can go back
(d times), so → 𝑑𝑛2

𝑑2

Runtime: O(n2d3), can be reduced to O(n2d2)


oAfter enforcing arc consistency:
o Can have one solution left 2 solutions
o Can have multiple solutions left
Consistent
o Can have no solutions left (and not
know it)

oArc consistency still runs inside a no solutions


backtracking search!
Consistent

There are 3 nodes here, arc consistency traffics in


pairs, will not detect violations between 3 nodes
You need K-consistency
oVariable Ordering: Minimum remaining values (MRV):
o Choose the variable with the fewest legal left values in its domain

oWhy min rather than max?


o Because the action is on the shrinking nodes
oAlso called “most constrained variable”
oAlso aka “Fail-fast” ordering
oValue Ordering: Least Constraining Value
o Given a choice of variable, choose the least
constraining value (easiest one that affects others less)
o i.e., the one that rules out the fewest values in the
remaining variables
o Note that it may take some computation to determine
this! (E.g., rerunning filtering)

oWhy least rather than most?

oCombining these ordering ideas makes


1000 queens feasible
oCSPs are a special kind of search problem:
o States are partial assignments
o Goal test defined by constraints

oBasic solution: backtracking search

oSpeed-ups:
o Ordering
o Filtering
Thanks for your attention!

You might also like