Lecture 2 - Search (1) PDF
Document Details
Uploaded by GainfulMedusa2157
University of Malta
Josef Bajada
Tags
Summary
This lecture covers search techniques in artificial intelligence. It discusses various search algorithms including uninformed, informed, and local search, with examples and applications. The document also provides an overview of algorithmic complexity and optimization.
Full Transcript
Dr Josef Bajada Department of Artificial Intelligence University of Malta...
Dr Josef Bajada Department of Artificial Intelligence University of Malta [email protected] Search Techniques I C S 1 0 1 7 / I C S 1 0 2 0 – F O U N D AT I O N S O F A R T I F I C I A L I N T E L L I G E N C E Agenda 1. Introduction 2. Examples of problems solved using search techniques. 3. Uninformed Search 4. Informed Search and Heuristics 5. Local Search and Meta-Heuristics 6. Adversarial Search © JOSEF BAJADA 2 Introduction Empty Truck, §Search is one of the most powerful Parcel not at Destination techniques to solve AI problems. refuel §Problem is formulated as a Directed Graph. § Set of nodes stop load drive § Set of directional edges connecting nodes. Empty Truck, §The Intelligent Agent searches through the Parcel not at Destination, Out of Fuel stop graph to find the solution. Parcel not at Destination, stop Out of Fuel unload What is a graph? https://www.youtube.com/watch?v=ZQY4IfEcGvM Parcel at Destination © JOSEF BAJADA 3 Solving Problems using Search STATE SPACE SEARCH SOLUTION SPACE SEARCH § Nodes: possible states. § Nodes: candidate solutions. § Edges: state transitions. § Edges: “moves” (modifications) to a § Solution: Path from the start node candidate solution to obtain to the desired end node. another solution. § Solution: Desired end node. © JOSEF BAJADA 4 Example: Route Planning What is the fastest route to the emergency location? Shortest Distance? Path of least cost (traffic)? © JOSEF BAJADA 5 Example: Solving Puzzles 1 2 3 1 2 8 4 3 4 5 7 6 5 6 7 8 Initial State Goal State © JOSEF BAJADA 6 Example: Automated Planning Given: §Initial State §Goal Condition §Possible Actions System must find the right action sequence to achieve the goal condition. § Logical, Numeric or Temporal Constraints Ambulance must reach patient within timeframe, and have enough fuel to return. § Partial observability and Uncertainty Likelihood that traffic conditions change on the way? © JOSEF BAJADA 7 Example: Constraint Solving Search for an “arrangement” that satisfies all the constraints. §Map Colouring Problem Use the minimum amount of colours Two touching regions cannot have the same colour. §Sudoku Each row, column, and 3x3 sub-grid must have all numbers from 1 to 9. §N-Queens Each queen must be on its own row and column. © JOSEF BAJADA 8 Example: Optimization Solution must: § Achieve the goal § Satisfy all constraints § Minimize (maximise) some cost (utility) function Maximise production Minimise cost of production Maximise use of renewables Ambulance routing can consider fuel costs in its solution (distance, traffic etc.) How does one prove optimality? © JOSEF BAJADA 9 Modelling Challenges §Fully vs Partially Observable §Deterministic vs Stochastic §Discrete vs Continuous §Static vs Dynamic §Single Agent vs Multi Agent § Competitive vs Cooperative © JOSEF BAJADA 10 Algorithmic Complexity Algorithmic Complexity is the measure of how much computational resources the algorithm needs in proportion to the size of its input. §Complexity is typically represented using the big 𝑶 notation. § An approximation of an algorithm’s growth rate in terms of its input size. § E.g. Searching linearly through a list of 𝒏 items has complexity 𝑶 𝒏 §Time Complexity § Will the algorithm take a long time to complete as n grows? §Space Complexity § Will the algorithm need more memory to run as n grows? The challenge with AI problems is a high algorithmic complexity if solved “naively”. © JOSEF BAJADA 11 Uninformed (Blind) Search §Expand each state to find all its successors. §Keep expanding until the desired state is found. §The order with which states are expanded can impact greatly the time it takes to find a solution. § It might never be found at all. §Different strategies determine in which order the states are expanded. § Breadth first search § Depth first search § Variants of the above © JOSEF BAJADA 12 Breadth First Search §Expand all the nodes at the same level before proceeding to expanding their successors. §Shallowest unexpanded node is selected for expansion. § New discovered nodes are added to the back of the queue. §Guaranteed to find the shallowest path. § Not necessarily optimal path! Shallowest path is optimal path only if the cost is a non-decreasing function of the depth of the node. © JOSEF BAJADA 13 Complexity of Breadth First Search §Breadth First Search is attractive to use because most commonly the depth corresponds to cost (even if not necessarily optimal). §However, its performance is heavily dependent on the branching factor of the problem (how many successors each node generates). §Time Complexity If each node generates b successors Solution is at depth d Complexity is 𝑂(𝑏! ) §Space Complexity is also 𝑂(𝑏! ) Unexpanded successors in queue 𝑂(𝑏! ) Explored set (if doing graph search) 𝑂(𝑏!"# ) Exponential Complexity! 𝑏 = 4, 𝑑 = 10, 𝑏 ! = 1,048,576 𝑏 = 5, 𝑑 = 10, 𝑏 ! = 9,765,625 © JOSEF BAJADA 14 Depth First Search §Expand the first successor, and its first successor recursively, until a node with no successors is found. §Then back track one level upwards, and repeat. §No optimality guarantees. § A solution with a shorter path could be left unexplored. §Time Complexity is 𝑂 𝑏 % § Where 𝑚 is the maximum depth. §Space Complexity however can be lower § If doing tree search it is 𝑂 𝑏𝑚 § Keep only the unexplored branches 𝑏 along one path of depth 𝑚 § If doing graph search it is 𝑂 𝑏" § We need to keep the explored set to avoid cycles. Can keep expanding one branch infinitely! © JOSEF BAJADA 15 Other Variants §Uniform-Cost Search – A variant of Breadth First Search that takes into account different costs for each edge. § Uses a priority queue, least cumulative cost first rather than shallowest first. §Depth-Limited Search – A variant of Depth First Search, but sets a limit, 𝑙, to the depth (to avoid expanding infinitely). § Search is incomplete. It can miss the solution if it is at a depth 𝑑 > 𝑙. §Iterative-Deepening Depth First Search – Start with 𝑙 = 0, and gradually increment the depth limit. § Combines the advantages of Breadth First Search (shallowest solution is found first), and those of Depth First Search (lower space complexity). § With each iteration, the same tree is generated again, incurring time. §Hybrid solutions are also possible § For example starting with Breadth First Search until starting to approach memory limits, and switching to Iterative-Deepening Depth First Search afterwards. © JOSEF BAJADA 16 Informed (Best-First) Search §Try to determine the attractiveness of a node, 𝑛, when compared to the others that are still to be expanded. §Use some evaluation function, 𝒇(𝒏), which produces a cost estimate from the state information and the goal it is trying to reach. §Unexplored nodes are maintained in a priority queue, ordered by 𝒇 𝒏 , (lowest first). © JOSEF BAJADA 17 Evaluation Function §This evaluation function, 𝑓(𝑛), can involve multiple components, typically: § The actual cost, 𝒈 𝒏 , to reach the node, 𝑛, from the initial state. § The estimated cost, 𝒉(𝒏), to reach a goal node from node 𝑛. 𝒈(𝒏) Current 𝒉(𝒏) Initial Goal Node State Node 𝑛 The cost estimation function, 𝒉(𝒏), is known as the heuristic function. © JOSEF BAJADA 18 Heuristics A heuristic is a criteria to help us decide which course of action to take if we have incomplete information. Humans use heuristics all the time: §Choosing which study units to take. § Is the subject interesting? Do I like the lecturer’s style? Is it difficult? §Choosing their career path. § Does it have good job prospects? Do I like that field? Does it pay well? §Choosing their partner. § Do I like how they look? Do I have fun when they are around? Just like in search, we want to minimise the chances of going down the wrong path and having to back track our life choices. © JOSEF BAJADA 19 Heuristic Functions §A heuristic function, 𝒉(𝒏), estimates the cost of the cheapest path from the state at node, 𝑛, to a goal state. §Heuristics that incorporate the domain knowledge of the problem being solved can be very powerful to guide a search algorithm. 1 2 3 8 4 7 6 5 Straight-Line (Euclidean) Distance Manhattan Distance Initial State © JOSEF BAJADA 20 Greedy Best-first Search §Expands the node that is estimated to be closest to the goal. Evaluation function 𝑓 𝑛 = ℎ 𝑛 Greedy because at each step it tries to get as close to the goal as it can. § Ignores the cost to reach the node from the initial node. §Greedy Best-first Search is complete in graph search (maintaining a list of explored states to avoid cycles), as long as the search space is finite. § Otherwise it suffers from the same problem of depth first search. §The worst case time and space complexity is still 𝑂(𝑏 ! ), but if it is using an informative heuristic function, this is rarely the case. §Greedy Best-First Search does not guarantee optimality. © JOSEF BAJADA 21 A* Search §A* Search is a very popular best-first search strategy. §It incorporates both the cost to reach the node, 𝑛, from the initial node and also the estimate to reach the goal from node, 𝑛. § Evaluation function 𝑓 𝑛 = 𝑔 𝑛 + ℎ(𝑛) § Node with the lowest estimated total cost to reach the goal is explored first. §𝑔(𝑛) is adjusted if a cheaper path to 𝑛 is discovered. 2 4 A (f = 17) after 2 (f = 15) h=10 D Solution: S A D F G h=6 B F G S 5 h=13 h=3 3 h=0 5 E 6 7 (f = 18) h=4 (f = 22) after 3 (f = 18) after 6 1 C (f = 14) 3 (f = 18) after 4 h=7 (f = 17) after 1 (f = 16) after 5 © JOSEF BAJADA 22 A* Search - Complexity §A* Search is a very good search strategy if coupled with a good heuristic function. § Complete (guaranteed to find the solution if it exists). § Optimal if the heuristic is admissible and consistent (in case of graph search). § Still useful to find a solution if the heuristic is not admissible (as long as it is informative). §High search space complexity - 𝑶(𝒃𝒅) § The whole fringe of unexplored nodes needs to be maintained in memory (similar to Breadth First Search) § The explored set (in case of graph search) § Not suitable for problems with very large search spaces. © JOSEF BAJADA 23 Variants of A* Search §Iterative Deepening A* (IDA*) – Perform a depth-first search until the total cost 𝑓 𝑛 = 𝑔 𝑛 + ℎ 𝑛 exceeds a given threshold. § Less space complexity, with repeated evaluation trade-off. §Weighted A* - Giving a weight to the heuristic component of the evaluation function: 𝒇 𝒏 = 𝒈 𝒏 + 𝜺𝒉 𝒏 § If 𝜀 > 1, bias is towards states that are estimated closer to the goal. § Trades off optimality for speed § Empirically shown to be significantly faster than A* in a number of domains. © JOSEF BAJADA 24 Watch how a variant of the A* algorithm planned the path for an autonomous vehicle in the DARPA Grand Challenge. https://www.youtube.com/watch?v=qXZt-B7iUyw 4 mins © JOSEF BAJADA 25 Constraint Satisfaction and Optimization §In these kinds of problems, the path to reach the goal is irrelevant. § The goal node is a feasible solution in itself. §For these problems Solution Space Search tends to be used. § The start node is a candidate solution (not necessarily feasible) § Each successor node is also a candidate solution. §Search through variations of the initial solution to find a feasible one, or a higher quality one. § Quality is measured in terms of some objective function (minimise cost / maximise utility) © JOSEF BAJADA 26 Local (Neighbourhood) Search §Also known as meta-heuristics. §Explore the neighbourhood of the node (solution) and choose the next node (solution) to explore. §They typically use minimal amount of memory. §Often more effective in very large (possibly infinite / continuous) search spaces. §Useful for optimization problems § Modify the current solution a tiny bit, to see if its objective function can be improved. §Susceptible to getting stuck in local maxima/minima. § Introduce random jumps / perturbations. § Explore the space from multiple starting points © JOSEF BAJADA 27 Hill Climbing (Greedy Local Search) §Simplest local search algorithm. §Choose the successor with the best value, and repeat until no other successors have a better value. §Will get stuck in a local maximum. §Incomplete § If “best” solution is not actually feasible, the algorithm does not know how to look for alternative solutions in the search space. Source: Artificial Intelligence: A Modern Approach Stuart J. Russell and Peter Norvig © JOSEF BAJADA 28 Simulated Annealing Combines Hill Climbing with a random walk to get a chance to escape local minima / maxima. 1. Randomly choose a successor (instead of the best successor). 2. If successor is better accept it. 3. If successor is not better, accept it with some probability less than 1, determined according to: § how worse the successor is, and § how long the algorithm has been searching for. © JOSEF BAJADA 29 Local Beam Search Local Beam Search parallelises the search by looking at a population instead of just one solution. 1. Start with a population of 𝑘 randomly generated nodes, (𝑘 > 1). 2. Generate all successors of all 𝑘 nodes. 3. Select population of 𝑘 best successors. 4. Repeat from step 1 until the solution is found / cannot be improved. Stochastic Beam Search is a variant which selects the 𝑘 successors with a probability based on their “goodness”. © JOSEF BAJADA 30 Genetic Algorithms A Genetic Algorithm (GA) also starts from a population of 𝑘 random “individuals” (nodes). 1. Each individual is evaluated using a fitness function. 2. Probability of an individual to reproduce depends on its fitness. 3. A crossover operator applies to pairs of individuals selected for reproduction to obtain a new individual. 4. A random mutation with a small independent probability is applied. In GAs, individuals are represented as strings, to facilitate crossover. § The chosen encoding of the solution as a string can greatly affect the success or failure of this technique. GAs are particularly effective if the solution can take advantage of independently evolved blocks from different parent individuals. © JOSEF BAJADA 31 Other Local Search Variants §Tabu Search – A variant of simulated annealing that keeps a “tabu list” (solutions to avoid) to help escape plateaus. Typically the tabu list contains solutions that were already seen over the last 𝑛 iterations, (minimising the chances of revisiting the same solution space). §Particle Swarm Optimization (PSO) – Starts with a population “swarm” of candidate solutions (particles), which are updated in every iteration. Particles “move” according to a “velocity” calculated from the difference between their fitness and the best fitness value. §Evolutionary Algorithms Several algorithms inspired by nature have been proposed such as Ant Colony Optimization (ACO), Artificial Bee Colony (ABC), Bees Algorithm, Hunting Search, Firefly Algorithm, Cuckoo Search etc. © JOSEF BAJADA 32 Summary §Search is an effective technique for solving various problems. §Uninformed search is not scalable for real-world problems. §Informed (Best-First) search algorithms use heuristic functions to determine which states to explore next. §Under certain conditions (the heuristic function is admissible and consistent), the A* search algorithm guarantees optimality. §For certain problems, like constraint satisfaction, a solution-space search is more effective. §Local (Neighbourhood) search algorithms can search through large solution spaces, but are susceptible to getting stuck in local optima. © JOSEF BAJADA 33 Further Reading Stuart J. Russell and Peter Norvig, “Artificial Intelligence: A Modern Approach”, Pearson, 2016 Chapters 3 and 4 David L. Poole and Alan K. Mackworth, “Artificial Intelligence: Foundations of Computational Agents”, 2nd Edition, Cambridge University Press, 2017 Chapter 3 Chapter 4 © JOSEF BAJADA 34