You are on page 1of 30

Constraint Satisfaction Problems II

 Efficient Solution of CSPs

 Local Search
Reminder: CSPs

 CSPs:
 Variables
 Domains
 Constraints
 Implicit (provide code to compute)
 Explicit (provide a list of the legal tuples)
 Unary / Binary / N-ary

 Goals:
 Here: find any solution
 Also: find all, find best, etc.
Backtracking Search
Improving Backtracking

 General-purpose ideas give huge gains in speed


 … but it’s all still NP-hard

 Filtering: Can we detect inevitable failure early?

 Ordering:
 Which variable should be assigned next? (MRV)
 In what order should its values be tried? (LCV)

 Structure: Can we exploit the problem structure?


Arc Consistency and Beyond
Arc Consistency of an Entire CSP
 A simple form of propagation makes sure all arcs are simultaneously consistent:

NT Q
WA SA
NSW
V

 Important: If X (head) loses a value, neighbors of X need to be rechecked!


 Arc consistency detects failure earlier than forward checking (SA->NT)
 Must rerun after each assignment!
Limitations of Arc Consistency

 After enforcing arc


consistency:
 Can have one
solution left
 Can have multiple
solutions left
 Can have no solutions left (and
not know it)
 Arc consistency still runs What went
wrong here?
inside a backtracking search!
K-Consistency
K-Consistency
 Increasing degrees of consistency
 1-Consistency (Node Consistency): Each single node’s domain has a
value which meets that node’s unary constraints

 2-Consistency (Arc Consistency): For each pair of nodes, any


consistent assignment to one can be extended to the other
 K-Consistency: For each k nodes, any consistent assignment
to k-1
can be extended to the kth node.

 Higher k more expensive to compute

 (You need to know the k=2 case: arc consistency)


Strong K-Consistency
 Strong k-consistency: also k-1, k-2, … 1 consistent
 Claim: strong n-consistency means we can solve without backtracking!
 Why?
 Choose any assignment to any variable
 Choose a new variable
 By 2-consistency, there is a choice consistent with the first
 Choose a new variable
 By 3-consistency, there is a choice consistent with the first 2
 …

 Lots of middle ground between arc consistency and n-consistency! (e.g. k=3,
called path consistency)
Structure
Problem Structure
 Extreme case: bài toán phụ độc lập
 VD: Tasmania và vùng đất liền không kề nhau

 Các bài toán phụ độc lập có thể xem như


những thành phần liên thông trong đồ thị
ràng buộc
 Giả sử đồ thị n biến được chia thành các thành
phần liên thông với c biến
 Trường hợp xấu nhất là O((n/c)(dc)), gần như tuyến tính
 VD: n = 80, d = 2, c =20
 Trong CSP thường, 280 = 4 tỉ năm với tốc độ 10 triệu
nodes/s
 Trong cấu trúc này, (4)(220) = 0.4 giây với 10 triệu
nodes/s
Tree-Structured CSPs

 Định lý: nếu đồ thị ràng buộc không có vòng, bài toán CSP có thể được giải trong thời
gian O(n*d2)
 Còn với những bài CSP bình thường thì trường hợp xấu nhất là O(dn)
Tree-Structured CSPs
 Thuật toán cho CSPs dạng cây:
 Sắp xếp: Chọn biến làm gốc, sắp xếp các biến sao cho cha trước con

 Remove backward: For i = n : 2, apply RemoveInconsistent(Parent(Xi),Xi)


 Assign forward: For i = 1 : n, assign Xi consistently with Parent(Xi)

 Runtime: O(n d2) (why?)


Tree-Structured CSPs
 KL1: Sau khi duyệt ngược, tất cả các cung từ gốc đến lá đều “phù hợp”
 CM: Với cung X->Y sau khi được làm cho “phù hợp”, miền giá trị của Y không thể bị
giảm sau đó nữa (vì con của Y đã được xử lý trước khi đến Y)

 KL2: Nếu có điều 1, việc gán xui sẽ không cần phải quay lui
 CM: Quy nạp

 Tại sao thuật toán không đúng trong trường hợp có chu trình?
Improving Structure
Nearly Tree-Structured CSPs

 Điều kiện: Khởi đầu chọn gán 1 node, bỏ giá trị đó ra khỏi miền các node kề nó
 Điều kiện chọn tập hợp cắt (cutset): Gán (xét mọi trường hợp) tập hợp các
node sao cho đồ thị còn lại tạo thành 1 cây
 Tập hợp cắt kích thước c cho O( (dc) (n-c) d2 ), rất nhanh nếu c nhỏ
Cutset Conditioning

Chọn tập hợp cắt


SA

Khởi gán
(mọi trường hợp)
SA SA SA

Tính toán CSP còn lại


sau khi gán

Giải CSP còn lại


(dạng cây)
Cutset Quiz
 Find the smallest cutset for the graph below.
Iterative Improvement
Iterative Algorithms for CSPs
 Phương pháp tìm kiếm cục bộ thường được sử dụng trong trạng thái hoàn thành, tức
là tất cả các biến đã được gán

 Để áp dụng vào CSPs:


 Chọn biến chưa thỏa ràng buộc
 Gán lại giá trị
 No fringe! Live on the edge.

 Algorithm: While not solved, (vẫn còn ràng buộc bị vi phạm)


 Chọn biến: chọn ngẫu nhiên biến xung đột
 Chọn giá trị: min-conflicts heuristic:
 Chọn giá trị vi phạm ít ràng buộc nhất
 VD, hill climb với h(n) = tổng ràng buộc bị vi phạm
Example: 4-Queens

 Trạng thái: 4 queens in 4 columns (44 = 256 states)


 Operators: move queen in column
 Goal test: no attacks
 Evaluation: c(n) = number of attacks
Performance of Min-Conflicts
 Given random initial state, can solve n-queens in almost constant time for arbitrary
n with high probability(e.g., n = 10,000,000)!

 The same appears to be true for any randomly-generated CSP except in a narrow
range of the ratio
Summary: CSPs
 CSPs are a special kind of search problem:
 States are partial assignments
 Goal test defined by constraints
 Basic solution: backtracking search

 Speed-ups:
 Ordering
 Filtering
 Structure

 Iterative min-conflicts is often effective in practice


Local Search
Hill Climbing
 Simple, general idea:
 Start wherever
 Repeat: move to the best neighboring state
 If no neighbors better than current, quit

 What’s bad about this approach?


 Complete?
 Optimal?

 What’s good about it?


Hill Climbing Diagram
Genetic Algorithms

 Giải thuật di truyền sử dụng phép “chọn lọc tự nhiên”


 Giữ lại N trường hợp tốt nhất ở mỗi bước chọn (selection) dựa trên hàm fitness
 Gom cặp và lai ghép chéo (crossover), với những đột biến (mutation) để tạo sự đa dạng

 Phương pháp này hay bị hiểu nhầm, áp dụng sai (hoặc nói không tốt)
Example: N-Queens

 Why does crossover make sense here?


 When wouldn’t it make sense?
 What would mutation be?
 What would a good fitness function be?

You might also like