Problem Solving by Searching CSC361 AI CSC361: Problem Solving & Search
Problem Solving by Searching What is Problem Solving? How do humans solve problems? Problem solving agents. Problem formulations. Examples. Search algorithms. AI CSC361: Problem Solving & Search
Human Problem Solving: What is a problem? You have come across the word ‘problem’ as: “problems in physics, mathematics, etc.” But what is a problem? How to define it? And when do we, as humans, say we are faced with a problem? How do we solve a problem? AI CSC361: Problem Solving & Search
Human Problem Solving: What is a state? We talk about state of ‘something’. What does it mean? How to describe/represent state of something? How do you represent a book in a program? Set of variables (or attributes) describing something + a unique value for each of the variables = a possible state of that thing. AI CSC361: Problem Solving & Search
Human Problem Solving: When are we faced with a problem? When we are not satisfied with the current state of the environment we are in. We want to be in some satisfactory state but there is no obvious way to reach that satisfactory state. Example: 2343 + 3636 = ???? Example: I am in my home, have to attend lecture in 15mins. Example: I have been asked to find a shop in Olaya. AI CSC361: Problem Solving & Search
Human Problem Solving: How do we solve a problem? To solve a problem we must change the current unsatisfactory state and go into a satisfactory state. We can change state of the environment by taking action. But before taking action we must know where to go i.e. the satisfactory state or the the goal state. Goal formulation. AI CSC361: Problem Solving & Search
Human Problem Solving: How do we solve a problem? Must also determine which actions to take to reach the goal state. This is called finding a solution to the problem. Solution is a sequence of actions that take an agent from an unsatisfactory, initial state to a satisfactory goal state. Solution is an algorithm … a sequence of steps. Finding the solution means solving the problem. Humans use a general method to solve problems: search. AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Problem Solving Agent AI CSC361: Problem Solving & Search
Example: Problem Solving On holiday in Romania; currently in Arad. Flight leaves tomorrow from Bucharest. Formulate goal: be in Bucharest Formulate problem: states: being in various cities actions: driving actions between cities Find solution: sequences of driving actions from city to city – Arad, Sibiu, Fagaras, Bucharest. How? AI CSC361: Problem Solving & Search
Example: Map of Romania AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Problem Formulation? Assumption: Environment is static, deterministic and fully observable agent knows which state it will be in and solution is a sequence. Single-state problem. How to formulate the problem? Problem formulated by specifying four things: initial state, actions, goal test & path cost. Solution: sequence of actions leading from initial state to goal state. AI CSC361: Problem Solving & Search
Problem Formulation: Example 1 Finding route between Arad and Bucharest. initial state e.g., "at Arad" actions or successor function S(x) = set of action–state pairs e.g., S(Arad) = {<Arad Zerind, Zerind>, … } goal test e.g., x = "at Bucharest“ or Bucharest?(x) path cost e.g., sum of distances, number of actions executed, etc. c(x,a,y) is the step cost, assumed to be ≥ 0 AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search State Space When states and actions are specified a state space is defined. States + Actions State Space State space consists of a set of states and a set of actions connecting states with each other. State space can be viewed as state-space graph. An agent searches in the state space to find a path from the initial state to the goal state. AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search State Space Representing the state and actions can be difficult. Why? Real world is very complex impossible to represent all the details of its state within a computer. State information must be abstracted out from the real world state. Actions must be simplified. So… (Abstract) state = set of real states (Abstract) action = complex combination of real actions e.g., "Arad Zerind" represents a complex set of possible routes, detours, rest stops, etc. AI CSC361: Problem Solving & Search
Problem Formulation: Example 2 Vacuum Cleaner agent. State: (location, status) Actions: (Left, Right, Suck) AI CSC361: Problem Solving & Search
Problem Formulation: Example 2 initial state (A, Dirty). actions (Right, Left, Suck, No-op). goal test Square A and B are clean. path cost Cost 1 per action. AI CSC361: Problem Solving & Search
Problem Formulation: Example 3 The 8-Puzzle: States … can be represented as 2 dimensional array. Actions … move blank up, down, left, right. AI CSC361: Problem Solving & Search
Problem Formulation: Example 3 The 8-Puzzle: initial state [7 2 4, 5 b 6, 8 3 1]. actions (Right, Left, Up, Down). goal test Is the state [b 1 2, 3 4 5, 6 7 8]. path cost Cost 1 per action. AI CSC361: Problem Solving & Search
Problem Formulation: Example 4 Robot Assembly states?: real-valued coordinates of robot joint angles parts of the object to be assembled actions?: continuous motions of robot joints goal test?: complete assembly path cost?: time to execute AI CSC361: Problem Solving & Search
Problem Formulation: Example 4 You are given two jugs, a 4 gallon jug and a 3 gallon jug. Neither has any measuring marks on it. There is a pump that can be used to fill the jugs with water. How can you get exactly 2 gallons of water into the 4 gallon jug? AI CSC361: Problem Solving & Search
General Search Algorithm Basic Idea: Search for the goal state in the state space graph, keeping track of path from the state state. But you do not have the state space graph .. It is often too large to store. Instead the state space graph is generated as the search is carried out, generating successors of states visited. AI CSC361: Problem Solving & Search
General Search Algorithm AI CSC361: Problem Solving & Search
General Search Algorithm Basically three actions are performed during the search algorithm: SELECT GOAL CHECK EXPAND AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Expanding Nodes AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Expanding Nodes AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Expanding Nodes AI CSC361: Problem Solving & Search
Implementation: General Search AI CSC361: Problem Solving & Search
Implementation: Nodes A state is a (representation of) environment. A node is a data structure constituting part of a search tree includes state, parent node, action, path cost g(x), depth The Expand function creates new nodes, filling in the various fields. AI CSC361: Problem Solving & Search
Implementation: Fringe Fringe contains nodes that have been generated during search but not goal-checked yet. Fringe is implemented as a queue, stack or a priority queue, depending on the strategy. Another name for fringe – the Open List. Another list – the Closed List, is used to keep a list of node that have been goal checked and found to be not the goal state. Search graph (or state space graph) search tree. AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Search Strategy A search strategy concerned with the order nodes are expanded during search. How to judge a Search Strategies? completeness: does it always find a solution if one exists? time complexity: number of nodes generated space complexity: maximum number of nodes in memory optimality: does it always find a least-cost solution? Time and space complexity are measured in terms of b: maximum branching factor of the search tree d: depth of the least-cost solution m: maximum depth of the state space (may be ∞) AI CSC361: Problem Solving & Search
Search Strategies: Classification Search Strategies can be Uninformed Search Strategy Informed Search Strategy Uninformed search strategies use only the information available in the problem definition. Informed search strategies use other domain specific information. AI CSC361: Problem Solving & Search
Search Strategies: Classification Uninformed Search Strategies Breadth-first search Uniform-cost search Depth-first search Depth-limited search Iterative deepening search Informed Search Strategies Best First Search Uniform Search Greedy Search A* AO* AI CSC361: Problem Solving & Search
Which is better strategy? Strategies are judged on the basis of the following: completeness: does it always find a solution if one exists? time complexity: number of nodes generated space complexity: maximum number of nodes in memory optimality: does it always find a least-cost solution? Time and space complexity measured in terms of: b: maximum branching factor of the search tree d: depth of the least-cost solution m: maximum depth of the state space (may be ∞) AI CSC361: Problem Solving & Search
Uninformed Search: Breadth First Strategy– Expand the root node first, then nodes at depth d in the search tree are expanded first before nodes at depth d+1. Strategy implemented by implementing OPEN LIST as a FIFO queue. Pseudo-code: function BREADTH-FIRST-SEARCH (problem) returns a solution or failure; return TREE-SEARCH (problem, ENQUEUE-AT-END); AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search OPEN [A] CLOSED [] OPEN [B C] CLOSED [A] 1 2 OPEN [D E F G] CLOSED [A B C] OPEN [C D E] CLOSED [A B] 3 4 AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Breadth First Note: b is the branch factor; d is the depth of solution Complete? Yes (if b is finite) Time? 1+b+b2+b3+… +bd + b(bd-1) = O(bd+1) Space? O(bd+1) (keeps every node in memory) Optimal? Yes (if cost = 1 per step) Nodes can be generated at the rate of 100MB/sec, so in 24 hrs 8640 GB. AI CSC361: Problem Solving & Search
Uninformed Search: Uniform Cost Search Strategy: Expand the least cost un-expanded node from the OPEN LIST. Strategy implemented by implementing OPEN LIST as a priority queue. Pseudo-code: function UNIFORM-COST-SEARCH (problem) returns a solution or failure; return TREE-SEARCH (problem, ENQUEUE-BASED-ON-COST); AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Uniform Cost Search BFS finds shallowest goal state, but this may not always be the least cost solution. UCS expands the lowest cost node. Cost measured by path cost function g(n), where n is a node BFS UCS with g(n) = Depth(n). UCS finds cheapest solution first. Condition: path cost must never decrease along a path i.e. g(sucessor(n)) >= g(n). AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search 10 5 15 S A B C G S A B C 1 5 15 OPEN [A1 B5 C15] CLOSED [S] OPEN [S0] CLOSED [] S STATE SPACE S A B C 11 15 G 10 OPEN [B5 G11 C 15] CLOSED [S A] OPEN [G10 G11 C15] CLOSED [S A B] S A B C 11 5 15 G GOAL FOUND HERE AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Uniform Cost Search Complete? Yes, if step cost ≥ ε Time? # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε)) where C* is the cost of the optimal solution Space? # of nodes with g ≤ cost of optimal solution, O(bceiling(C*/ ε)) Optimal? Yes – nodes expanded in increasing order of g(n). AI CSC361: Problem Solving & Search
Uninformed Search: Depth-First Search Strategy: Expand a node from the OPEN LIST which is at the deepest level. Strategy implemented by implementing OPEN LIST as a stack. Pseudo-code: function DEPTH-FIRST-SEARCH (problem) returns a solution or failure; return TREE-SEARCH (problem, ENQUEUE-AT-FRONT); AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search OPEN [A] CLOSED [] OPEN [B C] CLOSED [A] 2 1 OPEN [D E C] CLOSE [A B] OPEN [H I E C] CLOSE [A B D] 3 4 AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search OPEN [I E C] CLOSED [ABDH] OPEN [E C] CLOSED [ABDHI} 5 6 OPEN [J K C] CLOSED [ABDHIE] OPEN [K C] CLOSED [ABDHIEJ] 7 8 AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search 9 10 11 12 AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Depth-First Search Complete? No: fails in infinite-depth spaces, spaces with loops Modify to avoid repeated states along path complete in finite spaces Time? O(bm): terrible if m is much larger than d but if solutions are dense, may be much faster than breadth-first Space? O(bm), i.e., linear space! Big advantage Optimal? No AI CSC361: Problem Solving & Search
Uninformed Search: Depth-Limited Search DFS not complete because it may go down infinite paths and never back up and return a solution. Depth-limited search tries to overcome this problem by cutting-off search at a depth-limit. Example: Depth-limit = 19 for Romania route finding problem. Why? Problem: How to choose the depth-limit? DLS: complete (if right limit chosen), not optimal, time complexity O(bl) and space complexity O(b.l). AI CSC361: Problem Solving & Search
Uninformed Search: Iterative Deepening Search (IDS) In DLS best depth-limit is hard to find. Romania route-finding problem is 19 the best limit? No! It is 9. Why? IDS avoids choosing the depth limit. It searches with all depth limits: l = 0, 1, 2, … IDS combines benefits of DFS & BFS: it is optimal and complete like BFS and has modest memory requirements like DFS. AI CSC361: Problem Solving & Search
Iterative Deepening Search (IDS) Pseudo-code: function ITERATIVE-DEEPENING-SEARCH (problem) returns a solution or failure; for depth 0 to infinity do if DEPTH-LIMITED-SEARCH (problem, depth) succeeds then return its results; end return failure AI CSC361: Problem Solving & Search
Iterative Deepening Search (IDS) AI CSC361: Problem Solving & Search
Iterative Deepening Search (IDS) AI CSC361: Problem Solving & Search
Iterative Deepening Search (IDS) AI CSC361: Problem Solving & Search
Iterative Deepening Search (IDS) Number of nodes generated in a depth-limited search NDLS = b0 + b1 + b2 + … + bd-2 + bd-1 + bd Number of nodes generated in IDS NIDS = (d+1)b0 + d b1 + (d-1)b2 + … + 3bd-2 +2bd-1 + 1bd How? dl=0 b0 dl=1 b0 + b1 dl=2 b0 + b1 + b2 ….. dl=d b0 + b1 + b2 + … + bd-2 + bd-1 + bd Total: (d+1)b0 + d b1 + (d-1)b2 + … + 3bd-2 +2bd-1 + 1bd AI CSC361: Problem Solving & Search
Iterative Deepening Search (IDS) For b = 10, d = 5, NDLS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111 NIDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 = 123,456 Overhead = (123,456 - 111,111)/111,111 = 11% IDS is not as bad as it appears. AI CSC361: Problem Solving & Search
Iterative Deepening Search (IDS) Complete? Yes Time? (d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd) an advantage Optimal? Yes, if step cost = 1 AI CSC361: Problem Solving & Search
Bi-directional Search Basic Idea: start two simultaneous searches from the start state and the goal state. Motivation: AI CSC361: Problem Solving & Search
Bi-directional Search Forward and backward searches can follow any strategy. Solution is found when the searches meet halfway in between. If BFS used both ways, nodes generated: O(2 bd/2) = O (bd/2 ) Example: If b = 10 and d = 6, BFS generates 1,111,111 nodes and bi-directional search generates 2,222 nodes. AI CSC361: Problem Solving & Search
Bi-directional Search Many issues must be dealt with: How to efficiently check whether the node belongs to the other fringe before expansion? Can predecessor of a node be generated? What if there are many goal states? Space complexity is a problem. Complete and optimal if both searches are BF. AI CSC361: Problem Solving & Search
AI CSC361: Problem Solving & Search Summary What is a problem? What is problem solving? What is problem formulation? How are problems solved? What are different categories of search strategies? What are various un-informed search strategies? AI CSC361: Problem Solving & Search