Upload
chandsek666
View
887
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Artificial Intelligence
Citation preview
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 l9 l
Example: Measuring problem!
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 l9 l
A cB
Example: Measuring problem!
• Another Solution:
A B C0 0 0 start0 5 03 2 03 0 23 5 23 0 7 goal
3 l 5 l9 l
A B C
Which solution do we prefer?
• Solution 1:
A B C0 0 0
start3 0 00 0 33 0 30 0 63 0 60 3 63 3 61 5 60 5 7
goal
• Solution 2:
A B C0 0 0
start0 5 03 2 03 0 23 5 23 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 SinkPump
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
SolutionF=2, O=9R=7, T=8Y=6, E=5N=0, I=1X=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 failureinitialize the search tree using the initial state problemloop do
if there are no candidates for expansion then return failurechoose a leaf node for expansion according to some strategyif the node contains a goal state then return the corresponding
solutionelse 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 algorithmsFunction 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 failurenode Remove-Front(nodes)if Goal-Test[problem] applied to State(node) succeeds then return nodenodes 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 SearchBreadth-First Search
Search
Breath-first searchExpand 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, Operators[problem]))
nodes := Insert-At-End-of-Queue(new-nodes)
end
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
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: (It 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 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, Operarors[problem])) nodes := Insert-At-Front-of-Queue(new-nodes)end
Depth-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
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