Lecture 2


Published on

Artificial Intelligence

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Lecture 2

  1. 1. Everyday – search examples <ul><li>Searching for the shortest route to RP? </li></ul><ul><li>Searching for your keys? </li></ul><ul><li>Searching for classes to take? </li></ul><ul><li>Searching for where the party is? </li></ul><ul><li>Searching for the best way to pack your car/truck when you move? </li></ul>
  2. 2. Industry – search examples <ul><li>Searching for ways to break a code? </li></ul><ul><li>Searching for ways to configure wireless antennae? </li></ul><ul><li>Searching for ways to set up the pipeline to transport oil/gas/water? </li></ul><ul><li>Searching for ways to schedule your workers? </li></ul><ul><li>Searching for ways to configure the shop floor? </li></ul>
  3. 3. Today’s lecture <ul><li>Uninformed search </li></ul><ul><ul><li>Why is it called ‘uninformed’? </li></ul></ul><ul><ul><li>What are the search techniques? </li></ul></ul><ul><ul><li>How does the algorithm work? </li></ul></ul><ul><ul><li>How do you code these in lisp? </li></ul></ul>
  4. 4. Search <ul><li>Basic definitions </li></ul><ul><li>Basic terms </li></ul>
  5. 5. Problem solving by search Represent the problem as STATES and OPERATORS that transform one state into another state. A solution to the problem is an OPERATOR SEQUENCE that transforms the INITIAL STATE into a GOAL STATE . Finding the sequence requires SEARCHING the STATE SPACE by GENERATING the paths connecting the two.
  6. 6. Example: Measuring problem– water jug problem! <ul><li>Problem: Using these three buckets, measure 7 liters of water. </li></ul>3 l 5 l 9 l
  7. 7. Example: Measuring problem! A c B A B C 0 0 0 3 0 0 3 0 3 0 0 6 3 0 6 0 3 6 3 3 6 1 5 6 0 5 7 3 l 5 l 9 l
  8. 8. Example: Measuring problem! <ul><li>Another Solution: </li></ul><ul><li>A B C </li></ul><ul><li>0 0 0 start </li></ul><ul><li>0 5 0 </li></ul><ul><li>3 2 0 </li></ul><ul><li>3 0 2 </li></ul><ul><li>3 5 2 </li></ul><ul><li>3 0 7 goal </li></ul>A B C 3 l 5 l 9 l
  9. 9. Which solution do we prefer? <ul><li>Solution 1: </li></ul><ul><li>A B C </li></ul><ul><li>0 0 0 start </li></ul><ul><li>3 0 0 </li></ul><ul><li>0 0 3 </li></ul><ul><li>3 0 3 </li></ul><ul><li>0 0 6 </li></ul><ul><li>3 0 6 </li></ul><ul><li>0 3 6 </li></ul><ul><li>3 3 6 </li></ul><ul><li>1 5 6 </li></ul><ul><li>0 5 7 goal </li></ul><ul><li>Solution 2: </li></ul><ul><li>A B C </li></ul><ul><li>0 0 0 start </li></ul><ul><li>0 5 0 </li></ul><ul><li>3 2 0 </li></ul><ul><li>3 0 2 </li></ul><ul><li>3 5 2 </li></ul><ul><li>3 0 7 goal </li></ul>
  10. 10. Ok…Let’s review <ul><li>What was the initial state? </li></ul><ul><li>What was the goal state? </li></ul><ul><li>What was the set of operations that took us from the initial state to the goal state? </li></ul><ul><li>What is the path that, if followed, would get us from the initial state to the goal state? </li></ul><ul><li>What would be the STATE SPACE? </li></ul>
  11. 11. Basic concepts (1) <ul><li>State : finite representation of the world that you want to explore at a given time. </li></ul><ul><li>Operator : a function that transforms a state into another ( also called rule, transition, successor function, production, action). </li></ul><ul><li>Initial state : The problem at the beginning. </li></ul><ul><li>Goal state : desired end state (can be several) </li></ul><ul><li>Goal test : test to determine if the goal has been reached. </li></ul><ul><li>Solution Path : The sequence of actions that get you from the initial state to the goal state. </li></ul>
  12. 12. Basic concepts (2) <ul><li>Reachable goal : a state for which there exists a sequence of operators to reach it. </li></ul><ul><li>State space : set of all reachable states from initial state (possibly infinite). </li></ul><ul><li>Cost function : a function that assigns a cost to each operation. </li></ul><ul><li>Performance (not for ALL uninformed): </li></ul><ul><ul><li>cost of the final operator sequence </li></ul></ul><ul><ul><li>cost of finding the sequence </li></ul></ul>
  13. 13. Problem formulation <ul><li>The first task is to formulate the problem in terms of states and operators </li></ul><ul><li>Some problems can be naturally defined this way, others not! </li></ul><ul><li>Formulation makes a big difference! </li></ul><ul><li>Examples: </li></ul><ul><ul><li>water jug problem, tic-tac-toe, 8-puzzle, 8-queen problem, cryptoarithmetic </li></ul></ul><ul><ul><li>robot world, travelling salesman, parts assembly </li></ul></ul>
  14. 14. Example 1: water jug (1) 9 5 Given 3 jugs (9, 5 and 3 liters), a water pump, and a sink, how do you get exactly 7 liters into the 9 liter jug? <ul><li>State : (x y z) for liters in jugs 1, 2 , and 3 integers 0 to 9 assigned to all possible permutations of 1 2 3 </li></ul><ul><li>Operations : empty jug, fill jug, EX. (fill (0 5 0)) </li></ul><ul><li>Initial state : (0 0 0) </li></ul><ul><li>Goal state : (x x 7) </li></ul><ul><li>Solution seqence (5 0 0 (0 5 0 (0 0 0 etc….) </li></ul>Jug 2 Jug 3 Sink Pump 3 Jug 1
  15. 15. Example 2: cryptoarithmetic F O R T Y + T E N + T E N S I X T Y Assign numbers to letters so that the sum is correct 2 9 7 8 6 + 8 5 0 + 8 5 0 3 1 4 8 6 <ul><li>State space: All letters and all numbers assigned to the letters </li></ul><ul><li>Operations : replace all occurrences of a letter with a digit not already there </li></ul><ul><li>Initial State : Letters that make words, integers </li></ul><ul><li>Goal State: only digits, sum is correct </li></ul><ul><li>Solution : F= 2, etc. see above </li></ul>Solution F=2, O=9 R=7, T=8 Y=6, E=5 N=0, I=1 X=4
  16. 16. Example 4: 8-queens <ul><li>State : any arrangement of up to 8 queens on the board </li></ul><ul><li>Operation: add a queen (incremental), move a queen (fix-it) </li></ul><ul><li>Initial state: no queens on board </li></ul><ul><li>Goal state: 8 queens, with no queen is attacked </li></ul><ul><li>Solution Path: The set of operations that allowed you to get to the The board that you see above at the indicated positions. </li></ul>
  17. 17. Example: 8-puzzle <ul><li>State: </li></ul><ul><li>Operators: </li></ul><ul><li>Goal test: </li></ul><ul><li>Solution path: </li></ul>start state goal state
  18. 18. Example: 8-puzzle <ul><li>Operators: moving blank left, right, up, down (ignore jamming) </li></ul><ul><li>Goal test: goal state </li></ul><ul><li>State: integer location of tiles (ignore intermediate locations) </li></ul><ul><li>Solution: move 4 tile to blank, move 1 tile blank, etc. </li></ul>start state goal state
  19. 19. A different Problem <ul><li>http://www.cs.wisc.edu/~jgast/cs540/sample/8puzzle.html </li></ul><ul><li>Initial State </li></ul><ul><li>State: </li></ul><ul><li>Operators: </li></ul><ul><li>Goal test: </li></ul>
  20. 20. How do we represent the problem in Lisp? Data structures? <ul><li>State: a list of lists, or a series of property lists </li></ul><ul><li>Node: </li></ul><ul><ul><li>state, depth level </li></ul></ul><ul><ul><li># of predecesors, list of cconnected nodes </li></ul></ul><ul><ul><li># of successors, list of cconnected nodes </li></ul></ul><ul><li>Edge: the cdr of the list or the get of the property…may also have a cost associated with it. </li></ul><ul><li>Operation: taking things off the list (or getting the property of a node, matching function </li></ul><ul><li>Queue or stack or list of lists to keep states to be expanded </li></ul>
  21. 21. Tree for water jug problem (0,0,0) (0,3,0) (4,0, 0) (0,0,0) (1,3,0) (4,3,0) (0,0,0) (3,0,0) (0,3,0) (1,0,0) (4,0,0) (4,3,0)     (4,3,0)
  22. 22. Search algorithms <ul><li>Function General-Search( problem , strategy ) returns a solution , or failure </li></ul><ul><li>initialize the search tree using the initial state problem </li></ul><ul><li>loop do </li></ul><ul><li>if there are no candidates for expansion then return failure </li></ul><ul><li>choose a leaf node for expansion according to some strategy </li></ul><ul><li>if the node contains a goal state then return the corresponding solution </li></ul><ul><li>else expand the node and add resulting nodes to the search tree </li></ul><ul><li>end </li></ul><ul><li>Basic idea: </li></ul><ul><li>offline, systematic exploration of simulated state-space by generating successors of explored states (expanding) </li></ul>
  23. 23. Implementation of search algorithms <ul><li>Function General-Search(problem, Queuing-Fn) returns a solution, or failure </li></ul><ul><li>nodes  make-queue(make-node(initial-state[problem])) </li></ul><ul><li>loop do </li></ul><ul><li>if node is empty then return failure </li></ul><ul><li>node  Remove-Front(nodes) </li></ul><ul><li>if Goal-Test[problem] applied to State(node) succeeds then return node </li></ul><ul><li>nodes  Queuing-Fn(nodes, Expand(node, Operators[problem])) </li></ul><ul><li>end </li></ul>Queuing-Fn( queue , elements ) is a queuing function that inserts a set of elements into the queue and determines the order of node expansion . Varieties of the queuing function produce varieties of the search algorithm.
  24. 24. Evaluation of search strategies <ul><li>Search algorithms are commonly evaluated according to the following four criteria: </li></ul><ul><ul><li>Completeness: does it always find a solution if one exists? </li></ul></ul><ul><ul><li>Time complexity: how long does it take as a function of number of nodes? </li></ul></ul><ul><ul><li>Space complexity: how much memory does it require? </li></ul></ul><ul><ul><li>Optimality: does it guarantee the least-cost solution? </li></ul></ul><ul><li>Time and space complexity are measured in terms of: </li></ul><ul><ul><li>b – max branching factor of the search tree </li></ul></ul><ul><ul><li>d – depth of the least-cost solution </li></ul></ul><ul><ul><li>m – max depth of the state-space (may be infinity) </li></ul></ul>
  25. 25. Uninformed search strategies <ul><li>Use only information available in the problem formulation </li></ul><ul><li>Breadth-first </li></ul><ul><li>Depth-first </li></ul><ul><li>Depth-limited </li></ul><ul><li>Iterative deepening </li></ul><ul><li>Uniform Cost </li></ul><ul><li>Bi-Directional </li></ul>
  26. 26. <ul><li>Breadth-First Search </li></ul>Search
  27. 35. Breath-first search Expand the tree in successive layers, uniformly looking at all nodes at level n before progressing to level n+1 function Breath-First-Search( problem ) returns solution nodes := Make-Queue(Make-Node(Initial-State( problem )) loop do if nodes is empty then return failure node := Remove-Front (nodes) if Goal-Test[ problem ] applied to State( node ) succeeds then return node new-nodes := Expand (node, O perators [problem])) nodes := Insert-At-End-of-Queue (new-nodes) end
  28. 36. Another Breath-first search S A D B D A E C E E B B F D F B F C E A C G G C G F 14 19 19 17 17 15 15 13 G 25 11
  29. 37. Properties of breadth-first search <ul><li>Completeness: (Does it always find a solution?) </li></ul><ul><li>Time complexity: (How long does it take?) </li></ul><ul><li>Space complexity: (How much memory does it take?) </li></ul><ul><li>Optimality: (I t always finds the shortest path) </li></ul>
  30. 38. Properties of breadth-first search <ul><li>Completeness: Yes, if b is finite </li></ul><ul><li>Time complexity: O(b d ) , i.e., exponential in d (Rem: b is no. of branches) </li></ul><ul><li>Space complexity: O(b d ) , keeps every node in memory </li></ul><ul><li>Optimality: Yes, if cost = 1 per step; not optimal in general </li></ul>
  31. 39. Depth-first
  32. 47. Depth first search Dive into the search tree as far as you can, backing up only when there is no way to proceed function Depth-First-Search( problem ) returns solution nodes := Make-Queue(Make-Node(Initial-State( problem )) loop do if nodes is empty then return failure node := Remove-Front (nodes) if Goal-Test[ problem ] applied to State( node ) succeeds then return node new-nodes := Expand (node, O perarors [problem])) nodes := Insert-At-Front-of-Queue (new-nodes) end
  33. 48. Depth-first search S A D B D A E C E E B B F D F B F C E A C G G C G F 14 19 19 17 17 15 15 13 G 25 11
  34. 49. Properties of depth-first search <ul><li>Completeness: No, fails in infinite state-space </li></ul><ul><li>Time complexity: O(b m ) </li></ul><ul><li>Space complexity: O(bm) </li></ul><ul><li>Optimality: No – it may never find the path! </li></ul>
  35. 50. Examples <ul><li>Graphs </li></ul><ul><li>http://www.cs.duke.edu/csed/jawaa/JAWAA.html </li></ul>
  36. 51. More Examples <ul><li>Graphs </li></ul><ul><li>http://www.cs.duke.edu/csed/jawaa/JAWAA.html </li></ul>
  37. 52. Lisp Code for Depth First <ul><li>Creating the tree: </li></ul><ul><li>(defun addbranches (location branches) </li></ul><ul><li>(setf (get location 'branch) branches)) </li></ul><ul><li>(addbranches 'root '(a b)) </li></ul><ul><li>(addbranches 'a '(c d)) </li></ul><ul><li>(addbranches 'b '(e)) </li></ul><ul><li>(addbranches 'c '(f)) </li></ul><ul><li>(addbranches 'e '(g h i)) </li></ul>
  38. 53. <ul><li>Finding the node: </li></ul><ul><li>(defun match (element pattern) </li></ul><ul><li>(eq element pattern)) </li></ul><ul><li>Expanding the node: </li></ul><ul><li>(defun morepaths (path) </li></ul><ul><li>(mapcar (lambda (nextpath) (cons nextpath path)) </li></ul><ul><li>(get (car path) 'branch))) </li></ul>
  39. 54. <ul><li>The Search </li></ul><ul><li>(defun depth-first (tree pattern) </li></ul><ul><li>(let (current paths) </li></ul><ul><li>(setq paths (list (list tree))) </li></ul><ul><li>(loop </li></ul><ul><li>(setq current (car paths)) </li></ul><ul><li>(cond </li></ul><ul><li>((null paths) (return nil)) </li></ul><ul><li>((match (car current) pattern) </li></ul><ul><li>(return (reverse current))) </li></ul><ul><li>(t (setq paths </li></ul><ul><li>(append (morepaths current) </li></ul><ul><li>(cdr paths)))))))) </li></ul>
  40. 55. Properties of search strategies <ul><li>Completeness </li></ul><ul><ul><li>guarantees to find a solution if a solution exists, or return fail if none exists </li></ul></ul><ul><li>Time complexity </li></ul><ul><ul><li># of operations applied in the search </li></ul></ul><ul><li>Space complexity </li></ul><ul><ul><li># of nodes stored during the search </li></ul></ul>
  41. 56. Where are we? <ul><li>Make very certain that you can by now write simple lisp functions. Examples: take two numbers (i.e. write a function of two arguments) and sum them </li></ul><ul><li>take a list of two numbers (i.e. write a function of one argument) and sum them </li></ul><ul><li>take an arbitrary list of numbers and sum them </li></ul><ul><li>take a list of numbers and return a list of all the numbers which were negative </li></ul>