Chapter 3: Solving Problems by Searching Prepared by: Dr. Ziad Kobti ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 1 Problem-Solving Agent • Reflex agent -> base its actions on a direct mapping from states to actions. – Cannot operate well in large environments – Would take too long to learn • Goal-based agent -> consider future actions and the desirability of their outcomes – Example: problem-solving agent ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 2 Problem-Solving Agent • Intelligent Agents are supposed to: MAXIMIZE their performance measure – Adopt a GOAL – Problem Formulation: is the process of deciding what actions and states to consider, given a goal. – If the agent has no additional information about its environment, (unknown environment), the it has no choice but to try one of the actions at random. – But the agent can examine ‘future actions’ that can eventually lead to states of known value. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 3 Well-defined problems and solutions • A PROBLEM can be defined formally by five components: 1. The initial state that the agent starts in. 2. A description of the possible actions available to the agent. (actions applicable in a given state) 3. Description of what each action does; a transition model. State Space = initial state + actions + transition model 4. A goal test which determines whether a given state is a goal state. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 4 Well-defined problems and solutions • State space forms a directed network or graph in which the nodes are states and the links between nodes are actions. • A path in the state space is a sequence of states connected by a sequence of actions. • A path cost function that assigns a numeric cost to each path. The problem solving agent chooses a cost function that reflects its own performance measure. • The step cost is the cost of taking action from one state to another. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 5 Example ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 6 Formulating Problems • A model is an abstract mathematical description, and not the real thing. • The process of removing detail from a representation is called abstraction. • Can you formulate an abstract model for the “Wall following problem” robot? – States, initial state, actions, transition model, goal test, path cost. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 7 Toy example: The 8-puzzle • States: A state description specifies the lcoation of each of the eight tiles and the blank in one of the nine squares. • Initial state: Any state can be designated as the initial state. • Actions: The simplest formulation defines the actions as movements of the blank space Left, Right, Up, or Down. Different subsets of these are possible depending on where the blank is. • Transitional model: • Goal test: match? • Path Cost: same. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 8 Real-world problems • Route-finding problems – e.g. driving directions, routing video streams in computer networks, military operations planning, airline travel-planning systems, etc… • Touring problems – E.g. visit every city at least once, starting and ending in the same node. – TSP (Travelling Salesman Problem): visit every city exactly once with the aim to have the shortest (or least cost) tour. (NP-hard) • e.g. Movement of automatic circuit-board drill ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 9 A bit of complexity! • A problem is NP-hard if an algorithm for solving it can be translated into one for solving any NP-problem (nondeterministic polynomial time) problem. • NP-hard therefore means "at least as hard as any NP-problem," although it might, in fact, be harder. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 10 Real-world problems • A VLSI layout problem requires positioning millions of components and connections on a chip to minimize area, minimize circuit delays… • Robot navigation is a generalization of the routefinding problem, but rather than following a discrete set of routes, a robot can move in a continuous space with an infinite set of possible actions and states. • Automatic Assembly Sequencing of complex objects by a robot. Aim is to find an order in which to assemble the parts of some object. E.g. protein design – find sequence of amino acids that will fold into a 3D protein with the right properties to cure some disease. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 11 Searching for Solutions • Search algorithms work by considering various possible action sequences. – Search tree (nodes=states, expand a state by applying legal action by generating new states). ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 12 General tree-search and graph search algorithms function TREE-SEARCH( problem) returns a solution, or failure initialize the frontier using the initial state of problem loop do if the frontier is empty then return failure choose a leaf node and remove it from the frontier if the node contains a goal state then return the corresponding solution expand the chosen node, adding the resulting nodes to the frontier --function GRAPH-SEARCH(problem) returns a solution. or failure initialize the frontier using the initial stale of problem initialize the explored set to be empty loop do if the frontier is empty then return failure choose a leaf node and remove it from the frontier if the node contains a goal state then return the corresponding solution add the node to the explored set expand the chosen node, adding the resulting nodes to the frontier only if not in the frontier or explored set ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 13 Infrastructure for search algorithms • Search algorithms require a data structure to keep track of the search tree that is being constructed. • For each node n of the tree, we have a structure that contains four components: – n.STATE: the state in the state space to which the node corresponds; – n.PARENT: the node in the search tree that generated this node; – n.ACTION: the action that was applied to the parent to generate the node; – n.PATH-COST: the cost, traditionally denoted by g(n), of the path from the initial state to the node; as indicated by parent pointers. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 14 Infrastructure for search algorithms function CHILD-NODE(problem, parent, action) returns a node return a node with STATE = problem.RESULT(parent.STATE, action), PARENT = parent, ACTION = action, PATH COST = parent.PATH-COST + problem.STEP-COST(parent.STATE, action) ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 15 Measuring problem-solving performance • Evaluate an algorithm’s performance in four ways: – Completeness: • Is the algorithm guaranteed to find a solution when there is one? – Optimality: • Does the strategy find the optimal solution? – Time complexity: • How long does it take to find a solution? – Space complexity: • How much memory is needed to perform the search? (branching, depth, search cost, total cost) ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 16 Uninformed search strategies • Uninformed search (blind search) – Strategies have no additional information about states beyond that provided in the problem definition. – All the can do is generate successors and distinguish a goal state from a non-goal state. – All search strategies are distinguished by the order in which nodes are expanded. • Informed search (or heuristic search): – Know whether one non-goal state is “more promising” than another state. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 17 Uninformed Search: Breadth-first search • Root node is expanded first, the all the successors of the root node are expanded next, then their successors, and so on. • BFS is an instance of the general graph-search • A queue is used to keep track of node order. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 18 Breadth-first search on a graph function BREADTH-FIRST-SEARCH (problem) returns a solution, or failure node <-- a node with STATE = proldem.INITIAL- STATE, PATH-COST =0 if problem.GOAL-TEST(node.STATE) then return SOLUTION(node) frontier <-- a FIFO queue with node as the only element explored <-- an empty set loop do if EMPTY?(frontier) then return failure node q<--POP( frontier) /* chooses the shallowest node in frontier */ add node. STATE to explored for each action in problem.ACTIONS(node.STATE) do child <-- CHILD-NODE(problem, node , action) if child.STATE is not in explored or frontier then if problem.GOAL-TEST(child.STATE) then return SOLUTION( child) frontier <-- INSERT( child, frontier) (note: frontier = set of all leaf nodes available for expansion at any given point) ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 19 Time and Space Complexity: not so good! • • • • • Imagine searching a uniform tree where every state has b successors. The root of the search tree generates b nodes at the first level, each of which generates b more nodes, for a total of b2 at the second level. Each of these generates b more nodes, yielding b3 nodes at the third level, and so on. Now suppose that the solution is at depth d. In the worst case, it is the last node generated at that level. Then the total number of nodes generated is b + b2 + b3 + … + bd = 0(bd) where d is the depth (If the algorithm were to apply the goal test to nodes when selected for expansion, rather than when generated, the whole layer of nodes at depth d would be expanded before the goal was detected and the time complexity would be 0(bd+1).) As for space complexity: for any kind of graph search, which stores every expanded node in the explored set, the space complexity is always within a factor of b of the time complexity. For breadth-first graph search in particular, every node generated remains in memory. There will be O(bd+1) nodes in the explored set and O(bd) nodes in the frontier, so the space complexity is O(bd) Exponential complexity! Memory requirements are a bigger problem for BFS than its execution time Exponential-complexity search problems cannot be solved by uninformed methods for any but the smallest instances! ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 20 Uniform-cost search • BFS expands the shallowest unexpanded node assuming all step costs are equal. • If the step cost are not equal: – Uniform-cost search expands the node n with the lowest path cost g(n). – This is done by storing the frontier as a priority queue ordered by g. • Uniform-cost search expands nodes in order of their optimal path cost. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 21 Example • Use uniform-cost search from “Sibiu” to “Bucharest”. P.84 • Is the search optimal? (ie find least cost path?) ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 22 Depth-first search • DFS always expands the deepest node in the current frontier of the search tree. • DFS is an instance of the graph-search algorithm. • BFS uses a FIFO queue, DFS uses a LIFO queue • Typically a recursive algorithm (careful!) ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 23 ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 24 DFS complexity? • Time complexity of DFS is bounded by the size of the state space (which may be infinite!) O(bm) where m is the max depth. • Space complexity of DFS: need to store only a single path from the root to a leaf node, along with all remaining unexpanded sibling nodes for each node on the path. O(bm) better than BFS • Variant: backtracking search uses O(m) space ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 25 Depth-limited search • DFS fails in infinite state spaces. • Simply limit the depth and hope the goal is within that depth limit. Here we rely on knowledge about the problem! • Iterative deepening DFS – is a general strategy often used in combination with DF tree search, that finds the best depth limit. It works by gradually increasing the limit until a goal is found. Combines DFS and BFS. – Iterative Deepening is the preferred uninformed search method when the search space is large and the depth of the solution is not known. ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 26 ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 27 Bidirectional search • Run two simultaneous searches, one forward from the initial state and the other backward from the goal hoping that the two searches meet in the middle! Motivation: bd/2 + bd/2 < bd ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 28 Comparing uninformed search strategies Criterion BreadthFirst Complete? Yes Time See P91 Space See P91 Optimal? Yes UniformCost DepthFirst DepthLimited Iterative Deepening Bidirection al (if applicable) Yes No No Yes Yes Yes No No Yes Yes ©2013 by Dr.Ziad Kobti - May not be reproduced without permission. 29
© Copyright 2026 Paperzz