# PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Artificial Intelligence 3. Solving Problems By Searching.

## Presentation on theme: "PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Artificial Intelligence 3. Solving Problems By Searching."— Presentation transcript:

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Artificial Intelligence 3. Solving Problems By Searching

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Definition n Goal Formulation  Given situations we should adopt the “goal”  1 st step in problem solving!  It is a set of world states, only those in which the goal is satisfied  Action causes transition between world states n Problem Formulation  Process of deciding what actions and states to consider, and follows goal formulation

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Formulation of simple problem-solving agent Function SIMPLE-PROB-SOLV-AGENT(p) returns an action inputs: p; //percept Static: seq; // action sequence, initially empty state; //description of the current world g; //goal, initially null problem; //problem formulation state <- Update-State (state, p); If seq is empty then g <- Formulate-Goal(state) problem <- Formulate-Problem(state, g); seq <- Search(problem); action <- First(seq, state); seq <- Rest(seq); Return action

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Examples (1) traveling On holiday in Taiif n Formulate Goal  Be after two days in Paris n Formulate Problem  States: various cities  Actions: drive/fly between cities n Find Solution  Sequence of cities: e.g., Taiif, Jeddah, Riyadh, Paris

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Examples (2) Vacuum World 8 possible world states 3 possible actions: Left/Right/ Suck Goal: clean up all the dirt= state(7) or state(8) world is accessible  agent’s sensors give enough information about which state it is in (so, it knows what each of its action does), then it calculate exactly which state it will be after any sequence of actions. Single-State problem world is inaccessible  agent has limited access to the world state, so it may have no sensors at all. It knows only that initial state is one of the set {1,2,3,4,5,6,7,8}. Multiple-States problem

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Problem Definition n Initial state n Operator: description of an action n State space: all states reachable from the initial state by any sequence action n Path: sequence of actions leading from one state to another n Goal test: which the agent can apply to a single state description to determine if it is a goal state n Path cost function: assign a cost to a path which the sum of the costs of the individual actions along the path.

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Vacuum World States: S 1, S 2, S 3, S 4, S 5, S 6, S 7, S 8 Operators: Go Left, Go Right, Suck Goal test : no dirt left in both squares Path Cost: each action costs 1. S1S1 S2S2 S3S3 S6S6 S5S5 S4S4 S7S7 S8S8

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Real-world problems n Routine finding l Routing in computer networks l Automated travel advisory system l Airline travel planning system l Goal: the best path between the origin and the destination n Travelling Salesperson problem (TSP) l Is a famous touring problem in which each city must be visited exactly once. l Goal: shortest tour

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Riyadh Mecca Jeddah Al-Qassim Medina Riyadh Taiif Riyadh (a) The initial state (b) After expanding

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Data Structure for Search Tree  DataType Node: data structure with 5 components  Components:  State  Parent-node  Operator  Depth  Path-cost

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Search Strategies The strategies are evaluated based on 4 criteria: 1. Completeness: always find solution when there is one 2. Time Complexity: how long does it take to find a solution 3. Space Complexity: how much memory does it need to perform the search 4. Optimality: does the strategy find the highest-quality solution

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example: vacuum world n Single-state, start in #5. Solution?

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example: vacuum world n Single-state, start in #5. Solution? [Right, Suck] n Sensorless, start in {1,2,3,4,5,6,7,8} e.g., Right goes to {2,4,6,8} Solution?

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example: vacuum world n Sensorless, start in {1,2,3,4,5,6,7,8} e.g., Right goes to {2,4,6,8} Solution? [Right,Suck,Left,Suck] n Contingency l Nondeterministic: Suck may dirty a clean carpet l Partially observable: location, dirt at current location. l Percept: [L, Clean], i.e., start in #5 or #7 Solution?

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example: vacuum world n Sensorless, start in {1,2,3,4,5,6,7,8} e.g., Right goes to {2,4,6,8} Solution? [Right,Suck,Left,Suck] n Contingency l Nondeterministic: Suck may dirty a clean carpet l Partially observable: location, dirt at current location. l Percept: [L, Clean], i.e., start in #5 or #7 Solution? [Right, if dirt then Suck]

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Vacuum world state space graph n states? n actions? n goal test? n path cost?

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Vacuum world state space graph n states? integer dirt and robot location n actions? Left, Right, Suck n goal test? no dirt at all locations n path cost? 1 per action

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example: The 8-puzzle n states? n actions? n goal test? n path cost?

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example: The 8-puzzle n states? locations of tiles n actions? move blank left, right, up, down n goal test? = goal state (given) n path cost? 1 per move [Note: optimal solution of n-Puzzle family is NP-hard]

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example: robotic assembly n states?: real-valued coordinates of robot joint angles parts of the object to be assembled n actions?: continuous motions of robot joints n goal test?: complete assembly n path cost?: time to execute

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Tree search algorithms n Basic idea: l offline, simulated exploration of state space by generating successors of already-explored states (a.k.a.~expanding states)

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example: Romania

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Single-state problem formulation A problem is defined by four items: 1. initial state e.g., "at Arad" 2. actions or successor function S(x) = set of action–state pairs l e.g., S(Arad) = {, … } 3. goal test, can be l explicit, e.g., x = "at Bucharest" l implicit, e.g., Checkmate(x) 4. path cost (additive) l e.g., sum of distances, number of actions executed, etc. l c(x,a,y) is the step cost, assumed to be ≥ 0 n A solution is a sequence of actions leading from the initial state to a goal state

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Selecting a state space n Real world is very complex  state space must be abstracted for problem solving n (Abstract) state = set of real states n (Abstract) action = complex combination of real actions l e.g., "Arad  Zerind" represents a complex set of possible routes, detours, rest stops, etc. n For guaranteed realizability, any real state "in Arad“ must get to some real state "in Zerind" n (Abstract) solution = l set of real paths that are solutions in the real world n Each abstract action should be "easier" than the original problem

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Tree search example

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Tree search example

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Tree search example

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Implementation: general tree search

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Implementation: states vs. nodes n A state is a (representation of) a physical configuration n 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 and using the SuccessorFn of the problem to create the corresponding states.

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Search strategies n A search strategy is defined by picking the order of node expansion n Strategies are evaluated along the following dimensions: l completeness: does it always find a solution if one exists? l time complexity: number of nodes generated l space complexity: maximum number of nodes in memory l optimality: does it always find a least-cost solution? n Time and space complexity are measured in terms of l b: maximum branching factor of the search tree l d: depth of the least-cost solution l m: maximum depth of the state space (may be ∞)

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Uninformed search strategies n Uninformed search strategies use only the information available in the problem definition Breadth-first search Uniform-cost search Depth-first search Depth-limited search Iterative deepening search

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Breadth-first search n Expand shallowest unexpanded node n Implementation: l fringe is a FIFO queue, i.e., new successors go at end

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Breadth-first search n Expand shallowest unexpanded node n Implementation: l fringe is a FIFO queue, i.e., new successors go at end

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Breadth-first search n Expand shallowest unexpanded node n Implementation: l fringe is a FIFO queue, i.e., new successors go at end

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Breadth-first search n Expand shallowest unexpanded node n Implementation: l fringe is a FIFO queue, i.e., new successors go at end

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Properties of breadth-first search n Complete? Yes (if b is finite) n Time? 1+b+b 2 +b 3 +… +b d + b(b d -1) = O(b d+1 ) n Space? O(b d+1 ) (keeps every node in memory) n Optimal? Yes (if cost = 1 per step) n Space is the bigger problem (more than time)

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Breadth-first search tree sample 0 expansion 1 expansion 2 expansions 3 expansions Branching factor: number of nodes generated by a node parent (we called here “b”)  Here after b=2

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Breadth First Complexity  The root  generates (b) new nodes  Each of which  generates (b) more nodes  So, the maximum number of nodes expended before finding a solution at level “d”, it is : 1+b+b 2 +b 3 +….+b d  Complexity is exponential = O(b d )

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Breadth First Algorithm Void breadth () { //initialize the empty queue queue=[]; //initialize the empty queue // initialize the start state state = root_node; // initialize the start state while (! Is_goal( state ) ) { if !visited(do if !visited(state) do add_to_back_of_queue(successors(state)); markVisited( markVisited(state); FAILURE if queue emptyreturn FAILURE; //state=first item in queue state = queue[0]; //state=first item in queue remove_first_item_from (queue); } SUCCESS return SUCCESS }

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Time and memory requirement in Breadth-first DepthNodesTimeMemory 011 millisec100 bytes 2111.1 sec11 Kb 411.11111 sec1 Mb 610 6 18 minutes111 Mb 810 8 31 hours11 Gb 1010 128 days1 Tb 1210 12 35 years111 Tb 1410 14 3500 years11.111 Tb Assume branching factor b=10; 1000 nodes explored/sec and 100 bytes/node

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example (Routing Problem) A B C SG 1 5 5 15 5 10

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Solution of the Routing problem using Breadth-first BA C 5151 S Sol= empty & Cost = infinity AB C G 11 5151 S Sol= {S,A,G} & Cost = 11 AB C G 11 5151 S G 10 New solution found better than the current Sol= {S,B,G} & Cost = 10 AB C G 11 5151 S G 10 G 20 New solution found but not better than what we have Sol= {S,B,G} & Cost = 10

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Solution of the Routing problem using Uniform Cost search AB C G 11 5151 S G 10 AB C G 11 5151 S AB C G 11 5151 S G 10 AB C 5151 S Sol= empty & Cost = infinity Sol= {S,A,G} & Cost = 11 New solution found better than the current Sol= {S,B,G} & Cost = 10 C will not be expanded as its cost is greater than the current solution 0

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Uniform-cost search n Expand least-cost unexpanded node n Implementation: l fringe = queue ordered by path cost n Equivalent to breadth-first if step costs all equal Complete? Yes, if step cost ≥ ε Time? # of nodes with g ≤ cost of optimal solution, O(b ceiling(C*/ ε) ) where C * is the cost of the optimal solution n Space? # of nodes with g ≤ cost of optimal solution, O(b ceiling(C*/ ε) ) Optimal? Yes – nodes expanded in increasing order of g(n)

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search n Expand deepest unexpanded node n Implementation: l fringe = LIFO queue, i.e., put successors at front

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Properties of depth-first search n Complete? No: fails in infinite-depth spaces, spaces with loops l Modify to avoid repeated states along path  complete in finite spaces n Time? O(b m ): terrible if m is much larger than d l but if solutions are dense, may be much faster than breadth- first n Space? O(bm), i.e., linear space! n Optimal? No

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth-first search tree sample 0 expansion 1 expansion 2 expansions 4 expansions Branching factor: number of nodes generated by a node parent (we called here “b”)  Here after b=2

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth First Complexity  Let b: is the branching factor  Let d: maximum depth to find solution  So, the maximum number of nodes expended before finding a solution at level “m”, it is : 1+b+b+b+….+b (m times) Memory need = b*d  Complexity in worst case = O(b d ) as “Breadth-First”  Complexity in best case = O(b*d) which is excellent!

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Depth First Algorithm Void depth () { //initialize the empty stack Stack=[]; //initialize the empty stack // initialize the start state state = root_node; // initialize the start state while (! Is_goal(state)) { if !visited(do if !visited(state) do add_to_ Stack(successors(state)); markVisited( markVisited(state); if Stack == [] FAILURE return FAILURE; //state=first item in Stack state = Stack[0]; //state=first item in Stack remove_first_item_from (Stack); } SUCCESS return SUCCESS }

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Time and memory requirement in Depth- first DepthNodes Time (best case) Memory 01 1 millisec 100 bytes 220 0.02 sec 2 Kb 440 0.04 sec 4 Kb 610 * 6 0.06 sec 6 Kb 810 * 8 0.08 sec 8 Kb 1010 *10 0.1 sec 10 Kb 1210 * 12 0.12 sec 12 Kb 1410 * 14 0.14 sec 14 Kb Assume branching factor b=10; 1000 nodes explored/sec and 100 bytes/node

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Example: Romania n On holiday in Romania; currently in Arad. n Flight leaves tomorrow from Bucharest n Formulate goal: l be in Bucharest n Formulate problem: l states: various cities l actions: drive between cities n Find solution: l sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Problem types n Deterministic, fully observable  single-state problem l Agent knows exactly which state it will be in; solution is a sequence n Non-observable  sensorless problem (conformant problem) l Agent may have no idea where it is; solution is a sequence n Nondeterministic and/or partially observable  contingency problem l percepts provide new information about current state l often interleave} search, execution n Unknown state space  exploration problem

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi * Depth-limited search = depth-first search with depth limit l, i.e., nodes at depth l have no successors n Recursive implementation:

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Iterative deepening search

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Iterative deepening search l =0

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Iterative deepening search l =1

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Iterative deepening search l =2

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Iterative deepening search l =3

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Iterative deepening search n Number of nodes generated in a depth-limited search to depth d with branching factor b: N DLS = b 0 + b 1 + b 2 + … + b d-2 + b d-1 + b d n Number of nodes generated in an iterative deepening search to depth d with branching factor b: n ?? Rewrite the number of node on IDS n For b = 10, d = 5, l N DLS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111 l N IDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 = 123,456 n Overhead = (123,456 - 111,111)/111,111 = 11%

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Properties of iterative deepening search n Complete? Yes n Time? (d+1)b 0 + d b 1 + (d-1)b 2 + … + b d = O(b d ) n Space? O(bd) n Optimal? Yes, if step cost = 1

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Summary of algorithms

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Repeated states n Failure to detect repeated states can turn a linear problem into an exponential one!

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Graph search

PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Summary n Problem formulation usually requires abstracting away real-world details to define a state space that can feasibly be explored n Variety of uninformed search strategies n Iterative deepening search uses only linear space and not much more time than other uninformed algorithms

Download ppt "PSU CS 370 – Artificial Intelligence Dr. Mohamed Tounsi Artificial Intelligence 3. Solving Problems By Searching."

Similar presentations