300 likes | 427 Vues
This document explores the fundamental concept of search in artificial intelligence as a problem-solving mechanism. It illustrates how various AI issues can be framed as search problems, exemplified by games like chess, puzzles, and maze navigation. It delves into the representation of these problems as graphs, with nodes as states and edges as actions. The document compares depth-first and breadth-first search algorithms, detailing their mechanisms, structures, and examples. These foundational techniques are vital for finding optimal paths and solutions in complex AI scenarios.
E N D
Searching as an AI problem • Many AI problem framed as search • General idea • You know the individual steps towards solving a problem • You need to find out which (combination of) steps lead to the solution • So you need to search for the right combination
Searching as an AI problem • Example: • Playing chess: search for the right moves to move to a winner location • Puzzle: the order to move pieces so that the puzzle is solved • Walking in a maze: making the right moves to get out of the maze
Searching as an AI problem • States: the various situations • Certain position of chess pieces; current location in the maze • Actions: Allowable action at each state • Which chess piece can move where; where can I move in the maze • Initial/Start state: the starting location • Starting position of chess; the entrance of a maze • Final/Goal state: the goal • Position that checkmate; the exit of a maze
Searching as an AI problem • Searching can be generalized as a graph • Graph • Vertices/Nodes: labels • Edges: connecting two vertices • Directed vs. Not directed • Cycles • Acyclic graph: graph without a cycle • Trees
Searching as an AI problem • Searching as a graph problem • States : vertices • Directed Edges : if a action move from one state to another • Start state, goal state: the corresponding vertices • Search problem: find a path from the start vertex to the end vertex
Finding a path in a graph • General idea of a graph search • Start at the initial vertex • Visit the nodes that can be reached by the vertices already visited • Continue until the goal is find or until there is no more vertices to visit • Algorithms differ with • Order of visit • Data structure needed
Finding a path in a graph • Depth first search • Going “deeper” whenever possible • Always pick a vertex that is just visited to visit next • Need a stack • Breadth first search • Going “wider” whenever possible • Pick a vertex based on “first come first visited” principle • Need a queue
Depth first search -- example B C Stack: {I} I D F A Not yet visited E Current G Visited
Depth first search -- example B C Stack: {I, A} I D F A Not yet visited E Current G Visited
Depth first search -- example B C Stack: {I, A, C} I D F A Not yet visited E Current G Visited
Depth first search -- example B C Stack: {I, A, C, D} I D F A Not yet visited E Current G Visited
Depth first search -- example B C Stack: {I, A, C, D} I D F A Not yet visited E Current G Visited A is not put on the stack because it has been visited
Depth first search -- example B C Stack: {I, A, C} I D F A Not yet visited E Current G Visited D has no other vertex to go to, so is popped
Depth first search -- example B C Stack: {I, A, C, F} I D F A Not yet visited E Current G Visited
Depth first search -- example B C Stack: {I, A, C, F, G} I D F A Not yet visited E Current G Visited Goal is reached
Depth first search • Start with an initial vertex • Insert the vertex on the stack • Make the initial vertex as visited • At each step, check if the vertex at the top of the stack has vertex not yet visited • If exist, then pick one of the vertices to put on top of stack • If not, pop the current vertex out of the stack • Repeat until the goal is reached or the stack is empty
Breadth first search -- example B C Queue: {I} I D F A Not yet visited E Current G Visited
Breadth first search -- example B C Queue: {A, B} I D F A Not yet visited E Current G Visited
Breadth first search -- example B C Queue: {B, C} I D F A Not yet visited E Current G Visited A is not put on the queue because it is visited
Breadth first search -- example B C Queue: {C, E} I D F A Not yet visited E Current G Visited
Breadth first search -- example B C Queue: {E, D, F} I D F A Not yet visited E Current G Visited
Breadth first search -- example B C Queue: {D, F, G} I D F A Not yet visited E Current G Visited A is not put on the queue because it is already there
Breadth first search -- example B C Queue: {F, G} I D F A Not yet visited E Current G Visited G is not put on the queue because it is already there
Breadth first search -- example B C Queue: {F, G} I D F A Not yet visited E Current G Visited Goal is found
Breadth first search • Use a (first-in-first-out) queue instead of a stack • The initial vertex is put on the queue • At each step • Look at the top of the queue • Look at all the vertices that the top of the queue points to • Add them to the end of the queue if they have not been visited and are not on the queue already • Remove the top of the queue • Repeat until goal is reached or queue empty
Search – locating the path • For depth first search • Path can be listed by popping from the stack • For breadth first search • We need to keep track of each vertex’s “parent” (which vertex leads to the vertex adding onto the queue)
Depth first/Breadth first search: strength and limitations • Both • Easy to implement (try it yourself!) • Exhaustive – guarantee to find the solution • Depth first search • Very dependent on which vertex to choose • Breadth first search • More likely to find a good path • The queue can grow very big
Depth first/Breadth first search: strength and limitations • In an Artificial Intelligence context • Graphs are too big -- Can’t generate all the vertices • Exhaustive search takes too long • Need faster solutions (e.g. robot can’t wait a day before it moves)