Beruflich Dokumente
Kultur Dokumente
A state is an assignment of values to some or all variables. Consistent assignment: assignment does not violate the constraints.
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), }
4
Example: Map-Coloring
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).
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 in polynomial time by linear programming
Varieties of constraints
Unary constraints involve a single variable,
e.g., SA green
Preference (soft constraints) e.g. red is better than green can be represented by a cost for each variable assignment => Constrained optimization problems.
8
Example: Cryptarithmetic
10
Backtracking search
Variable assignments are commutative,
Eg [ WA = red then NT = green ] equivalent to [ NT = green then WA = red ]
Depth-first search for CSPs with single-variable assignments is called backtracking search
Backtracking search
function BACKTRACKING-SEARCH(csp) % returns a solution or failure return RECURSIVE-BACKTRACKING({} , csp) function RECURSIVE-BACKTRACKING(assignment, csp) % returns a solution or failure if assignment is complete then return assignment var SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp) for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) do if value is consistent with assignment according to CONSTRAINTS[csp] then add {var=value} to assignment result RECURSIVE-BACKTRACKING(assignment, csp) if result failure then return result remove {var=value} from assignment return failure
12
Backtracking example
13
Backtracking example
14
Backtracking example
15
Backtracking example
16
17
18
19
Forward Checking
Idea:
Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values
21
Forward checking
Idea:
Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values
22
Forward checking
Idea:
Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values
23
Forward checking
Idea:
Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values
1 1
X1 {1,2,3,4}
X2 {1,2,3,4}
2
3 4
X3 {1,2,3,4}
X4 {1,2,3,4}
1 1
X1 {1,2,3,4}
X2 {1,2,3,4}
2
3 4
X3 {1,2,3,4}
X4 {1,2,3,4}
26
1 1
X1 {1,2,3,4}
X2 { , ,3,4}
2
3 4
X3 { ,2, ,4}
X4 { ,2,3, }
27
1 1
X1 {1,2,3,4}
X2 { , ,3,4}
2
3 4
X3 { ,2, ,4}
X4 { ,2,3, }
28
1 1
X1 {1,2,3,4}
X2 { , ,3,4}
2
3 4
X3 { , , , }
X4 { ,2, , }
29
1 1
X1 {1,2,3,4}
X2 { , , ,4}
2
3 4
X3 { ,2, ,4}
X4 { ,2,3, }
30
1 1
X1 {1,2,3,4}
X2 { , , ,4}
2
3 4
X3 { ,2, , }
X4 { , ,3, }
31
1 1
X1 {1,2,3,4}
X2 { , , ,4}
2
3 4
X3 { ,2, , }
X4 { , ,3, }
32
1 1
X1 {1,2,3,4}
X2 { , , ,4}
2
3 4
X3 { ,2, , }
X4 { , , , }
33
1 1
X1 { ,2,3,4}
X2 {1,2,3,4}
2
3 4
X3 {1,2,3,4}
X4 {1,2,3,4}
34
1 1
X1 { ,2,3,4}
X2 { , , ,4}
2
3 4
X3 {1, ,3, }
X4 {1, ,3,4}
35
1 1
X1 { ,2,3,4}
X2 { , , ,4}
2
3 4
X3 {1, ,3, }
X4 {1, ,3,4}
36
1 1
X1 { ,2,3,4}
X2 { , , ,4}
2
3 4
X3 {1, , , }
X4 {1, ,3, }
37
1 1
X1 { ,2,3,4}
X2 { , , ,4}
2
3 4
X3 {1, , , }
X4 {1, ,3, }
38
1 1
X1 { ,2,3,4}
X2 { , , ,4}
2
3 4
X3 {1, , , }
X4 { , ,3, }
39
1 1
X1 { ,2,3,4}
X2 { , , ,4}
2
3 4
X3 {1, , , }
X4 { , ,3, }
40
Constraint Propagation
Simplest form of propagation makes each arc consistent Arc X Y (link in constraint graph) is consistent iff
for every value x of X there is some allowed y
41
Arc consistency
Simplest form of propagation makes each arc consistent X Y is consistent iff
for every value x of X there is some allowed y
42
Arc consistency
Simplest form of propagation makes each arc consistent X Y is consistent iff
for every value x of X there is some allowed y
Arc consistency
Simplest form of propagation makes each arc consistent X Y is consistent iff
for every value x of X there is some allowed y
If X loses a value, neighbors of X need to be rechecked Arc consistency detects failure earlier than forward checking Can be run as a preprocessor or after each assignment
44
Example: n-queens
States: 4 queens in 4 columns (44 = 256 states) Actions: move queen in column Goal test: no attacks Evaluation: h(n) = number of attacks
Given random initial state, we can solve n-queens for large n with high probability
47
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
48
Summary
CSPs are a special kind of problem:
states defined by values of a fixed set of variables goal test defined by constraints on variable values