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

Lecture 2

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