1 / 30

Lecture 8 Search Techniques

Lecture 8 Search Techniques. Faculty of Information Technology Multimedia University. Outline. State representation of real-world problems. Solving a problem by traveling from the initial state to the final state. Search strategies: Depth First Search Breadth First Search.

hoang
Download Presentation

Lecture 8 Search Techniques

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 8Search Techniques Faculty of Information Technology Multimedia University TCP1211-Logic Programming

  2. Outline • State representation of real-world problems. • Solving a problem by traveling from the initial state to the final state. • Search strategies: • Depth First Search • Breadth First Search TCP1211-Logic Programming

  3. What is the problem??? Problem is a part of a world in a “bad state” (or unwanted state) Solving a Problem is taking that glimpse of the world and “transit” it to a “better state” (or desirable state) Solving a problem is: • Represent (all possible) states of the world under study • Find a procedure (a way) to change the states TCP1211-Logic Programming

  4. Understanding the problem In order to solve the problem, first we need to represent the Problem  State representation. A world under study (the problem) consists of some relevant entities Each entity has attribute(s) The values of the attributes of all entities denote the current state of the world under study. TCP1211-Logic Programming

  5. State representation - Example The world under study weather humidity temperature wind rain entities The state of the weather is the states of the following entities: • Humidity (High/Medium/Low) • Temperature (High/Medium/Low) • Wind (Strong/Medium/Mild) • Rain (No/Drizzling/Heavy) TCP1211-Logic Programming

  6. Example (cont…) For some people, a desirable weather is the sunny one. Humidity = Low Temperature = Medium Wind = Mild Rain = No TCP1211-Logic Programming

  7. Another example TCP1211-Logic Programming

  8. FNorth, WNorth, GNorth, CNorth F, W F, C Ferry Wolf F, G Ferry Cabbage Ferry Goat FSouth, WSouth FSouth, GSouth, FSouth, CSouth, WNorth, CNorth GNorth, CNorth F WNorth,GNorth GSouth, FNorth, WNorth, CNorth F,W FSouth, WSouth,GSouth, ... CNorth ... TCP1211-Logic Programming

  9. State Transition A state transition of the world under study is best depicted by a directed graph Root (Initial State) Transition Change of State Node 4 (state 4) Node 1 (state 1) ... Node 2 (state 2) . . . Node 3 (state 3) Node X Final State (Desirable State) TCP1211-Logic Programming

  10. Where is the solution? Root (Initial State) Answer Path from Initial State to Desirable State Just a Path . . . . . . Node X Final State (Desirable State) Node Y TCP1211-Logic Programming

  11. Search Strategies Two Basic Search Strategies: • Depth-first search • Breadth-first search Depth-first search goes to the left as far as possible before branching to the next right branch Breadth-first search visits all the nodes of one level before moving to the next level TCP1211-Logic Programming

  12. Depth First Search Level 0 I Level 1 A B C Level 2 D E G F H Level 3 K F F Level 4 M F Nodes Visited : I A D B E K M F Answer Path : I B E K F TCP1211-Logic Programming

  13. Characteristics of DFS • simple to implement • answer path might be longer than necessary • may get stuck in an infinite branch while the answer is just • in the next branch TCP1211-Logic Programming

  14. Solving a problem means… Given a system that represents a real world, the purpose of the search is to find a sequence of state transitions that brings the system from the initial state to the desired (final) state Generally: • The initial state is well known and UNIQUE • The final (desired) state is unknown • (but recognizable once it occurs) • May have many final (desired) states TCP1211-Logic Programming

  15. Finding Answer Path using DFS 1. Start with a singleton initial path consisting of the initial state 2. Extend this path, step by step, until it reaches a state which can be recognized as a final state. TCP1211-Logic Programming

  16. If path has reached Final-state Answer-Path = path and return this path else if path can be extended to a New-state save Current-state continue to extend the path from the New-state else backtrack and try to extend the path from the Previous-state in a different direction Extend algorithm Extend a path to a New-state Find the Next-state of the Previous-state in the path that is not already in thepath the state found is the New-State TCP1211-Logic Programming

  17. Path Effect I I Initial state IA extend A B C IAD extend IA backtrack D E G F H I backtrack IB extend K F F IBE extend M F IBEK extend If path has reached Final-state Answer-Path = path and return this path else if path can be extended to a New-state save Current-state continue to extend the path from the New-state else backtrack and try to extend the path from the Previous-state in a different direction IBEKM extend IBEK backtrack IBEKF extend Find the Next-state of the Previous-state in the path that is not already in thepath the state found is the New-State TCP1211-Logic Programming

  18. Requirements for DFS To solve problems using the DFS strategy, we must first • Define the initial state • Define the final state (or how it is recognized) • Define the next state (transitions) TCP1211-Logic Programming

  19. depth_first_search(AnsPath) :- initial_state(Init), depth_first([Init],AnsPath). depth_first([S|Path],[S]) :- final_state(S),!. depth_first([S|Path],[S|AnsPath]) :- extend([S|Path],S1), depth_first([S1,S|Path],AnsPath). extend([S|Path],S1) :- next_state(S,S1), not(member_state(S1,[S|Path])). member_state(X,[X|_]). member_state(X,[_|T]) :- member_state(X,T). TCP1211-Logic Programming

  20. intial_state(i). final_state(f). I next_state(i,a). next_state(i,b). next_state(i,c). next_state(a,d). next_state(b,e). next_state(b,g). next_state(e,k). next_state(k,m). next_state(k,f). ... A B C D E G F H K F F M F TCP1211-Logic Programming

  21. [n,n,n,n] [s,s,n,n] [s,n,s,n] ... fail [n,n,s,n] fail [s,s,s,n] ... [n,s,n,n] [n,s,s,n] [s,s,n,s] fail [n,s,n,s] [s,s,s,s] TCP1211-Logic Programming

  22. Final State Farmer: south Wolf: south Goat: south Cabbage: south Initial State Farmer: north Wolf: north Goat: north Cabbage: north initial_state([n,n,n,n]) final_state([s,s,s,s]) (Transitions) next states next_state(S, S1) :- move(S, S1), safe(S1). safe([F,W,G,C]) :- F=G,!. %Farmer with Goat %OR safe([F,W,G,C]) :- F=W, F=C. %Farmer with Wolf and with Cabbage TCP1211-Logic Programming

  23. move([F,W,G,C],[F1,W,G,C]) :- cross(F, F1). move([F,W,G,C],[F1,F1,G,C]) :- cross(F, F1). move([F,W,G,C],[F1,W,F1,C]) :- cross(F, F1). move([F,W,G,C],[F1,W,G,F1]) :- cross(F, F1).. cross(n, s). cross(s,n). TCP1211-Logic Programming

  24. Breadth First Search Level 0 I Level 1 A B C Level 2 D E G F H Level 3 K F F Level 4 M F Node Visited: I A B C D E G F Answer Path: I C F TCP1211-Logic Programming

  25. However… Breadth-first search strategy is: • Complicated (difficult to implement) • Always give the shortest path TCP1211-Logic Programming

  26. Summary • You should know how to build the state representation of a search problem. • Two search strategies – DFS, BFS. • You should also know how to implement DFS in Prolog. TCP1211-Logic Programming

  27. Prolog Programming – Tip No. 2 • Arguments of a clause are not fixed for input or input. • Example: The predicate conc(L1,L2,L3). conc([],L2,L2). conc([X|L1Tail],L2,[X|L3Tail]):- conc(L1Tail,L2,L3Tail). Input/output TCP1211-Logic Programming

  28. Prolog Tip No. 2 (cont.) • All of the following queries will work: • ?-conc([a,b],[c,d],L). • ?-conc(L,[c,d],[a,b,c,d]). • ?-conc(L1,L2,[a,b,c,d]). TCP1211-Logic Programming

  29. Prolog Tip No. 2 (cont) • Consider this familiar example: factorial(0,1). factorial(M,N):- M1 is M – 1, factorial(M1,N1), N is M * N1. %clue • Will the following query work? ?-factorial(5,N). TCP1211-Logic Programming

  30. Prolog Tip No. 2 (cont) • Will the following query work? ?-factorial(M,120). • The clue is  N is M * N1. TCP1211-Logic Programming

More Related