470 likes | 483 Views
Explore various search methodologies like DFS, BFS, A*, and more for problem-solving, with examples, implementation details, and comparisons. Learn about state space, operators, and evaluation functions in problem-solving.
E N D
Problem Solving • Missionaries and Cannibals • Hanoi Towers • Maze • Puzzles (8-Puzzle, 15-Puzzle) • Brix (消除方塊) • Freecell (新接龍) • Magic Cube (Rubik's Cube) • Web Spidering • Xmas Gift, etc. Problem Space: (1) Condition (restricts) (2) Actions (operators) - and costs (3) Stats - initial/start state - goal state(s) - dead state(s) 最古老談論 State Search 是春秋時代的 ( )
Example (Missionaries and Cannibals) • Start (M, C, Canoe) =(0,0,0) • operators • Move one cannibal across the river. • Move two cannibals across the river. • Move one missionary across the river. • Move two missionaries across the river. • Move one missionary and one cannibal.
(A,B,C)()() (B,C)(A)() (B,C)()(A) (C)(A)(B) (C)(B)(A) (A,C)()(B) (C)()(A,B) (A,C)(B)() (C)(A,B)() (C)()(A,B) ()(C)(A,B) (C)(A,B)() ()(A,B)(C) Example (Hanoi Tower)
Example (Maze, dead states) Infinite Cycle Pitfall
Example (Brix) (Dead) https://www.youtube.com/watch?v=kPi8p42pzKI#t=1m43s
Example (Freecell) Possibly Dead
Contents • Brute force search • Depth-first search • Breadth-first search • Depth first iterative deepening • Properties of search methods • Implementations • Heuristics • Hill climbing • Best first search • Beam search • Optimal paths • A* algorithms • Uniform cost search • Greedy search
Outline Problem Solving Data-Driven (forward-chaining) Goal-Driven (back-chaining) e.g. Brute-Force Search (exhaustive Search) Beam Search Maze, Proof Blind Search (Generate & Test) Heuristic Search (Informed Method) DFS BFS Iterative Deepening Search Hill Climbing Best-first S. A* family A* Alg. British Museum Procedure (identifying the optimal path) Uniform Cost Search (Branch and Bound, Dijkstra) Not always complete (infinite loop) Evaluation function Greedy Search
Brute Force Search • Search methods that examine every node in the search tree – also called exhaustive. • Generate and test is the simplest brute force search method: • Generate possible solutions to the problem. • Test each one in turn to see if it is a valid solution. • Stop when a valid solution is found. • The method used to generate possible solutions must be carefully chosen.
Depth-First Search • An exhaustive search method. • Follows each path to its deepest node, before backtracking to try the next path.
Breadth-First Search • An exhaustive search method. • Follows each path to a given depth before moving on to the next depth.
Comparison of Depth-First and Breadth-First Search • In some situations, DFS will never find a solution, even though one exists. (即使重複的node已先排除, e.g. 永遠選最右邊的叉路) • DFS – resource (memory) saving BFS – near optimal solution e.g. 解的數量及分佈 DFID (next page)
Depth-First Iterative Deepening • Based on both depth-first and breadth-first search. • Carries out DFS to depth of 1, 2, 3 … • Not as inefficient in time as it might appear, particularly for very large trees, in which it only needs to examine the largest row (the last one) once. • As good as DFS 使用 Revocation 只記action,節省記憶體 • As good as BFS 避免 infinite loop,而且找到的近似最佳解 DFID, IDS (Iterative Deepening Search): Case 1. Resource 有限 (memory/旅遊時間) Case 2. Game Tree, Min-Max 變化大,限時
Properties of Search Methods • Complexity (say, #(states) Problem/Representation) • How much time and memory the method uses. • Completeness (infinite loop? Algorithm) • A complete method is one that is guaranteed to find a goal if one exists. • Optimality & Admissibility ( Algorithm) • A method is optimal (or admissible) if it is guaranteed to find the best path to the goal. • Irrevocability ( Implementation) • Example: DFS to solve 15-puzzle • Counter example: DFS to solve Brix Representation: e.g.砝碼秤重問題 N個砝碼有一個瘕疪品 O(N), O(log2N), O(log3N)? 有些實作的方法 可以只記錄 action (而非 state) 再使用 revocation 搜尋
Outline Problem Solving Data-Driven (forward-chaining) Goal-Driven (back-chaining) e.g. Brute-Force Search (exhaustive Search) Beam Search Maze, Proof Blind Search (Generate & Test) Heuristic Search (Informed Method) DFS BFS Iterative Deepening Search Hill Climbing Best-first S. A* family A* Alg. British Museum Procedure (identifying the optimal path) Uniform Cost Search (Branch and Bound, Dijkstra) Not always complete Evaluation function Greedy Search
Implementations • Depth-first search can be implemented using a queue to store states • But a stack makes more sense • And enables us to create a recursive depth-first search function • Breadth-first search implementations are almost identical to depth-first • Except that they place states on the back of the queue instead of on the front.
Heuristics • Heuristic: a rule or other piece of information that is used to make methods such as search more efficient or effective. • In search, often use a heuristic evaluation function, f(n): • f(n) tells you the approximate distance of a node, n, from a goal node. • f(n) may not be 100% accurate, but it should give better results than pure guesswork.
Heuristics – how informed? h(n) h*(n) 與「保證最佳解」有關,稍後A* 完整討論 • The more informed a heuristic is, the better it will perform. • Heuristic h is more informed than j, if: • j(n) h(n)h*(n) for all nodes n. • A search method using h will search more efficiently than one using j. • A heuristic should reduce the number of nodes that need to be examined. 8-puzzles: h1(),h2(),h3(); Maze: straight-line distance 如何找 h(n)? Relaxing Problems
Hill Climbing DFS Hill Climbing Best First A* • An informed, irrevocable search method. • Easiest to understand when considered as a method for finding the highest point in a three dimensional search space: • Check the height one foot away from your current location in each direction; North, South, East and West. • As soon as you find a position whose height is higher than your current position, move to that location, and restart the algorithm. 展開過程中遇到更好的點,就直接走過去,走一步算一步; 樹形沒記錄,向前無法回頭,不是實用的解題法。另有一說, Hill Climbing是展開的點先排好,再Stack到OPEN list去,便可回溯。
Best-First Search DFS Hill Climbing Best First A* • Works rather like hill-climbing: • Picks the most likely path (based on heuristic value) from the partially expanded tree at each stage. • Tends to find a shorter path than depth-first or breadth-first search, but does notguarantee to find the best path. 讓最有希望的解,在 Queue 上插隊到前面去 不保證最佳解的原因是? 題示: f = g + h
Beam Search • Breadth-first method. • Only expands the best few paths at each level. • Thus has the memory advantages of depth-first search. • Not exhaustive, and so may not find the best solution. • May not find a solution at all.
Outline Problem Solving Data-Driven (forward-chaining) Goal-Driven (back-chaining) e.g. Brute-Force Search (exhaustive Search) Beam Search Maze, Proof Blind Search (Generate & Test) Heuristic Search (Informed Method) DFS BFS Iterative Deepening Search Hill Climbing Best-first S. A* family A* Alg. British Museum Procedure (identifying the optimal path) Uniform Cost Search (Branch and Bound, Dijkstra) Not always complete Evaluation function Greedy Search
Optimal Paths • An optimal path through a tree is one that is the shortest possible path from root to goal. • In other words, an optimal path has the lowest cost (not necessarily at the shallowest depth, if edges have costs associated with them). • The British Museum procedure finds an optimal path by examining every possible path, and selecting the one with the least cost. • There are more efficient ways. BM: 所有State 都要能回溯其 path/actions, 找出所有解再選最佳解
A* Algorithms OPEN List & CLOSED List • Uses the cost function: • f(node) = g(node) + h(node). • g(node) is the cost of the path so far leading to the node. • h(node) is an underestimate of how far node is from a goal state. • f is a path-based evaluation function. • An A* algorithm expands paths from nodes that have the lowest f value.
即使goal, i.e., Z,已放在OPEN 而且經評估h=0, 唯尚未確定其為lowest之前都不算找到最佳解 A ~ ~ ~ ~ X Y ~ ~ Check if it is a goal when we popup it. Z replacement or insertion For any X in the opt. path, 如果 A…X...Z 才是 Opt. Solution 則 ZY將被 永久放在 OPEN List f(X)≤f*(X)<f*(ZY)=f(ZY) Add START to OPEN list while OPEN not empty get node n from OPEN that has the lowest f(n) if n is GOAL then return path move n to CLOSED for each n’ = CanMove(n , direction) g(n’) = g(n) + cost(n,n’) calculate f(n’)=g(n’)+h(n’) if n’ in OPEN list and new n’ is not better , continue if n’ in CLOSED list and new n’ is not better , continue remove any n’ from OPEN and CLOSED add n as n’s parent add n’ to OPEN …從OPEN開始 end for end while if we get here , then there is No Solution
A* Alg (admissible heuristic) • A* algorithms are optimal: • They are guaranteed to find the shortest path to a goal node. • Provided h(node) is always an underestimate. (h is an admissible heuristic). • A* methods are also optimally efficient – they expand the fewest possible paths to find the right one. • If h is not admissible, the method is called A, rather than A*.
A 8 2 4 4 B 5 C 9 5 3 1 2 3 5 E6 D 3 F 4 3 4 1 2 2 8 K 2 1 G 2 H 3 4 2 7 5 L 1 1 M 0 A* Alg (example) Note: (1) Why OPEN list (rather than a tree…?) 不必用 DFS 或 BFS 把每條路徑都找出來比較 (2) Why “underestimate?” 試想如果 E and C 高估, Step 6 可能已得到非最佳解 (M/12) 還有「取代既有 CLOSED」未在此例 若有此情形,則復活者重新加入OPEN
Uniform Cost Search • Also known as branch and bound. • Like A*, but uses: • f(node) = g(node); h( )=0 • g(node) is the cost of the path leading up to node. • Note: All Cost >0 or Dijkstra Recall: 此即以前看過的 Dijkstra Algorithm shortest path searching 從未要求一開始就開圖, 遇到的時候可以查知即可
Greedy Search Greedy Search: f(n)=h(n) 不變, 無可能替換親源節點,所以效果 和 Best-first Search相同,差別在Gr-有CLOSED list,剔除重複節點 Be-重複的節點可能再次展開 • Like A*, but uses: • f(node) = h(node); g( )=0 • Hence, always expands the node that appears to be closest to a goal, regardless of what has gone before. • Not optimal, and not guaranteed to find a solution at all. • Can easily be fooled into taking poor paths.
Extended Research • Real time A* • Iterative deepening A* • Parallel search • Bidirectional search • Nondeterministic search • Nonchronological backtracking
Recall: Outline Problem Solving Data-Driven (forward-chaining) Goal-Driven (back-chaining) e.g. Brute-Force Search (exhaustive Search) Beam Search Maze, Proof Blind Search (Generate & Test) Heuristic Search (Informed Method) DFS BFS Iterative Deepening Search Hill Climbing Best-first S. A* family A* Alg. British Museum Procedure (identifying the optimal path) Uniform Cost Search (Branch and Bound, Dijkstra) Not always complete Evaluation function Greedy Search
Outline Problem Solving Data-Driven (forward-chaining) Goal-Driven (back-chaining) e.g. Brute-Force Search (exhaustive Search) Beam Search Maze, Proof Blind Search (Generate & Test) Heuristic Search (Informed Method) DFS BFS Iterative Deepening Search Hill Climbing Best-first S. A* family • Real time A*, Iterative deepening A* • Parallel search • Bidirectional search • Island-Driven Search • Nondeterministic search • Nonchronological backtracking A* Alg. Uniform Cost Search (Branch and Bound, Dijkstra) Greedy Search
Iterative Deepening A* • A* is applied iteratively, with incrementally increasing limits on f(n). • Works well if there are only a few possible values for f(n). • The method is complete, and has a low memory requirement, like depth-first search. 結合A* 與 Beam Search Methods 補看更多 branches 看能否得解或得更好的解。 See also: Real-time A* -- 不再謀定而後動,設定時間參數,時限到,就走到目前OPENlist的首位節點,它代表目前最佳的中途點,改為起點,再重來。
Parallel Search • Some search methods can be easily split into tasks which can be solved in parallel. • Important concepts to consider are: • Task distribution • Load balancing • Tree ordering 同時pop N個 nodes 出來展開 宣告 OpenTmp, 並暫記thisNode 比對的 Openlist N+1 thisNode被取代 刪除這個暫記區 沒被取代的OpenTmp抄到Open 存取主要Open/Close 要lock
Bidirectional Search • Also known as wave search. • Useful when the start and goal are both known. • Starts two parallel searches – one from the root node and the other from the goal node. • Paths are expanded in a breadth-first fashion from both points. • Where the paths first meet, a complete and optimal path has been formed. (if all costs are equal) 從頭(起點)從尾(終點)兩端互找, e.g. 1 – 智慧拼盤 OPEN(CLOSED) 共同點 e.g. 2 – 迷宮中留下如何到達此點的記錄 Note: 目前的作法不能保證為最佳解 See also: Island-Driven Search (c.f. And-or tree, i.e., goal tree)
Nondeterministic Search • Useful when very little is known about the search space. • Combines the depth first and breadth first approaches randomly. • Avoids the problems of both, but does not necessarily have the advantages of either. • New paths are added to the queue in random positions, meaning the method will follow a random route through the tree until a solution is found. 亂跳式搜尋 (DFS x BFS ) 在OPEN list 隨機放前放後, 也可直接以樹形搜尋,在葉 節點上隨機展開。 早期的變化方法(無hueristics) 可避免DFS與BFS各自的風險
Nonchronological backtracking • Depth first search uses chronological backtracking. • Does not use any additional information to make the backtracking more efficient. • Nonchronological backtracking involves going back to forks in the tree that are more likely to offer a successful solution, rather than simply going back to the next unexplored path. 回溯到“直系”親代任一點,該點提供最好的“旁系”路徑分支,此法可以不搭配Open list 板凳,直接以樹形回溯 早期的變化方法(有hueristics) Say,本來預估的成本怎麼變差了?
Extended Study • Can you write a Navigation Program? • How do you ride to Taipei 101? • Heuristic function and Real-time A* (開圖的錯覺) • In case you need to insert a midway point? (注意附加功能) • What can Innovation Play in Problem Solving? • Creative idea upon states/actions/heuristics … • Edge computing (e.g.state=checklist, problem=TSP…) • e.g. leaving the kid’s scissors home when eating outdoors…
Preview Advanced Search Constructive Methods (search tree for goals) • General Metaheuristics • Exchanging heuristics • Iterated local search • (Ant Colony Optimization) • Taboo Search • Simulated annealing • Genetic algorithms 由這兩個例子 來看這些 methods Search for Goals (8-Queens/conflict=0) Search for Optima (TSP/ lowest cost) Constraint Satisfaction forward checking (Dead Ends?) DFS (64 x 63 x…) DFS (n x (n-1) x…) A* Algorithm Heuristic (which branch first?) Most-Constrained Variables Local Search (metaheuristics) Heuristic Repair k-opt exchanging search space search tree search plane = untraceable branches Iterative Methods (search plane for optima)