You are on page 1of 46

Solving problems by

searching
Outline
Problem-solving agents
Problem types
Problem formulation
Example problems
Basic search algorithms
Introduction
To build a system to solve a particular
problem, we need to do 04 things:
1. Define the problem precisely. Specify
Initial/final solutions
2. Analyze the problem. A few very important
features can have an immense impact on
the appropriateness of various possible
techniques for solving the problem
3. Isolate & represent the task knowledge
that is necessary to solve the problem
4. Choose the best problem-solving
technique (s) & apply it (them) to the
Production Systems
Since search forms the core of many intelligent processes, it is
useful to structure AI programs in a way that facilitates describing
& performing the search process.
Production systems provide such structures.
A production system consist of:
- A set of rules, each consisting of a left side (a pattern) that
determines the applicability of the rule & a right side that
describes the operation to be performed if the rule is applied
- One ore more knowledge/databases that contain whatever
information is appropriate for the particular task. Some parts of the
database may be permanent, while other parts of it may pertain
only to the solution of the current problem. The information in
these databases may be structured in any appropriate way.
- A control strategy that specifies the order in which the rules will be
compared to the database & a way of resolving the conflicts that
arise when several rules match at once
- A rule applier
Search
In general, an agent with several
immediate options of unknown value can
decide what to do by just examining
different possible sequences of actions that
lead to states of known value, and then
choosing the best sequence.
This process of looking for such a sequence
is called search.
A search algorithm takes a problem as
input & returns a solution in the form of an
action sequence.
Once a solution is found, the actions it
recommends can be carried out:
Problem-solving agents
Restricted form of general agent:

Note: this is offline problem solving; solution


executed\eyes closed.
Online problem solving involves acting without
Example: Romania
On holiday in Romania; currently in Arad.
Flight leaves tomorrow from Bucharest

Formulate goal:
be in Bucharest
Formulate problem:
states: various cities
actions: drive between cities

Find solution:
sequence of cities, e.g., Arad, Sibiu, Fagaras,
Bucharest
Example: Romania
Problem types
Deterministic, fully observable  single-state
problem
Agent knows exactly which state it will be in; solution is a
sequence
Non-observable  sensorless problem (conformant
problem)
Agent may have no idea where it is; solution is a sequence
Nondeterministic and/or partially observable 
contingency problem
percepts provide new information about current state
solution is a contingent plan or a policy
often interleave search, execution
Unknown state space  exploration problem
(“online”)
Example: vacuum world
Single-state, start in #5. Solution??
[Right; Suck]
Conformant, start in {1; 2; 3; 4; 5; 6; 7;
8}
e.g., Right goes to {2; 4; 6; 8}. Solution??
[Right; Suck; Left; Suck]

Contingency, start in #5
Murphy's Law: Suck can dirty a clean
carpet
Local sensing: dirt, location only.
Solution??

[Right; if dirt then Suck]


Single-state problem
formulation
A problem is defined by four items:
initial state e.g., “at Arad"
successor function S(x) = set of action - state pairs
e.g., S(Arad) = {<Arad  Zerind; Zerind>,…}

goal state, can be


explicit, e.g., x = “at Bucharest"
implicit, e.g., NoDirt(x)

path cost (additive)


e.g., sum of distances, number of actions executed, etc.
c(x; a; y) is the step cost, assumed to be  0

A solution is a sequence of actions


leading from the initial state to a goal state
Selecting a state space
Real world is absolutely complex
state space must be abstracted for problem solving

(Abstract) state = set of real states


(Abstract) action = complex combination of
real actions
e.g., “Arad  Zerind” represents a complex set of
possible routes, detours, rest stops, etc.
For guaranteed reliability, any real state "in
Arad“ must get to some real state "in Zerind"
 (Abstract) solution =
set of real paths that are solutions in the real world
Each abstract action should be “easier” than
the original problem
Vacuum world state space
graph
1 2

3 4

5 6

8
7

states??: integer dirt and robot locations (ignore


dirt amounts etc.)
actions??: Left, Right, Suck, NoOp
goal test??: no dirt
path cost??: 1 per action (0 for NoOp)
Example: The 8-puzzle

states??: integer locations of tiles (ignore intermediate


positions)
actions??: move blank left, right, up, down (ignore
unjamming etc.)
goal test??: = goal state (given)
path cost??: 1 per move
[Note: optimal solution of n-Puzzle family is NP-hard]
Example: robotic assembly

states??: real-valued coordinates of robot joint angles


parts of the object to be assembled
actions??: continuous motions of robot joints
goal test??: complete assembly with no robot
included!
path cost??: time to execute
Tree search algorithms
Basic idea:
offline, simulated exploration of state space
by generating successors of already-
explored states (i.e., expanding states)
Tree search example
Implementation: states vs.
nodes
A state is a (representation of) a physical configuration
A node is a data structure constituting part of a search tree
includes state, parent node, action, path cost g(x), depth
States do not have parents, children, depth, or path cost!

The EXPAND function creates new nodes, filling in the various


fields &
using the SUCCESSORFN of the problem to create the
corresponding states.
Implementation: general tree
search
Search strategies
A search strategy is defined by picking the order of
node expansion
Strategies are evaluated along the following dimensions:
Completeness--does it always find a solution if one exists?
Time complexity--number of nodes generated/expanded
Space complexity--maximum number of nodes in memory
Optimality--does it always find a least-cost solution?
Time and space complexity are measured in terms of
b--maximum branching factor of the search tree
d--depth of the least-cost solution
m--maximum depth of the state space (may be ∞)
Uninformed search strategies
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
Breadth-first
search
Expand shallowest Step 1
unexpanded node
Implementation:
fringe is a FIFO queue,
i.e., new successors go at
end

Step 2 Step 3 Step 4


BFS: an Example
 A breadth-first search
A (BFS) explores nodes
nearest the root before
exploring nodes further
B C away
 For example, after
D E F G searching A, then B, then C,
the search proceeds with
D, E, F, G
H I J K  Node are explored in the
order
L M N O P Q A B C D E F G H I J K L M N O P
Q
J will be found before N
23
How to do breadth-first
searching
Put the root node on a queue;
while (queue is not empty) {
remove a node from the queue;
if (node is a goal node) return success;
put all children of node onto the queue;
}
return failure;
Just before starting to explore level n, the
queue holds all the nodes at level n-1
In a typical tree, the number of nodes at
each level increases exponentially with the
depth
Memory requirements may be infeasible
24
Properties of breadth-first
search
Complete?? Yes (if b is infinite)
Time? 1+b+b2+b3+… +bd + b(bd-1) =
O(bd+1) i.e., exp. in d
Space? O(bd+1) (keeps every node in
memory)
Optimal? Yes (if cost = 1 per step); not
optimal in general

Space is the big problem; can easily generate


nodes at 100MB/sec
so 24hrs = 8640GB.
Pros of BFS
BFS will not get trapped exploring a blind alley. This contrasts
with DFS, which may follow a single, unfruitful path for a very
long time, perhaps forever, before the path actually
terminates in a state that has no successors
This is a particular problem of DFS (i.e., a state has a
successor that is also of its ancestors) unless special care is
expected to test for such a situation.
If there is a solution, then BDS is guaranteed to find it.
Furthermore, if there are multiple solutions, then a minimal
solution (minimum # of steps) will be found.
This is guaranteed by the fact that longer paths are never
explored until all shorter ones have already been examined
This contrasts with DFS, which may find a long path to a
solution in one part of the tree, when a shorter exists in some
other, unexplored part of thee tree
Uniform-cost search
Expand least-cost unexpanded node
Implementation:
fringe = queue ordered by path cost, lowest
first
 Equivalent to breadth-first if step costs all
equal
• Complete? Yes, if step cost ≥ ε
• Time? # of nodes with g ≤ cost of optimal
solution, O(bceiling(C*/ ε)) where C* is the cost of the
optimal solution
• Space? # of nodes with g ≤ cost of optimal
Depth-first search
 Expand deepest unexpanded node
 Implementation:
fringe = LIFO stack, i.e., put successors at
front
Depth-first search
DFS Example
 A depth-first search (DFS)
A explores a path all the way
to a leaf before backtracking
and exploring another path
B C  For example, after searching
A, then B, then D, the search
D E F G backtracks and tries another
path from B
 Node are explored in the
H I J K order
ABDEHLMNIOPCFG
L M N O P Q JKQ
 N will be found before J

30
How to do depth-first
searching
 Put the root node on a stack;
while (stack is not empty) {
remove a node from the stack;
if (node is a goal node) return success;
put all children of node onto the stack;
}
return failure;
At each step, the stack contains some nodes
from each of a number of levels
The size of stack that is required depends on the
branching factor b
While searching level n, the stack contains
approximately b*n nodes
When this method succeeds, it doesn’t give
the path 31
Properties of depth-first
search
Complete? No: fails in infinite-depth
spaces, spaces with loops
Modify to avoid repeated states along path
complete in finite spaces
Time? O(bm): terrible if m is much larger
than d
 but if solutions are dense, may be much
faster than breadth-first
 Space? O(bm), i.e., linear space!
 Optimal? No
Pros of DFS
DFS requires less memory since only the nodes on
the current path are stored
This contrast with BFS, where all of the tree that
has so far been generated must be stored
By chance, DFS may find a solution without
examine much of the search space at all
Contrast with BFS, in which all parts of the tree
must be examined to level n before any nodes on
level n+1 can be examined.
This is particularly significant if many acceptable
solutions exist,
DFS can stop when one of them is found
Comparative analysis (b = 3,d = 2)

FIFO

LIFO

Max-list-length = 1 + Max-list-length =
Depth-limited searching
Depth-first searches may be performed with a
depth limit:
 boolean limitedDFS(Node node, int limit, int depth) {
if (depth > limit) return failure;
if (node is a goal node) return success;
for each child of node {
if (limitedDFS(child, limit, depth + 1))
return success;
}
return failure;
}
 Since this method is basically DFS, if it succeeds
then the path to a goal node is in the stack

35
Iterative deepening search

-Iterative deepening is DFS to a fixed depth in the tree


being searched.
-If no solution is found up to this depth then the depth
to be searched is increased and the whole `bounded'
depth-first search begun again.
-It works by setting a depth of search -say, depth 1-
and doing depth-first search to that depth.
-If a solution is found then the process stops
-otherwise, increase the depth by, say, 1 and repeat
until a solution is found.
Iterative deepening search l
=0/1/2
Iterative deepening search l
=3
Properties of iterative deepening search
Complete? Yes
Time? (d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd)
Space? O(bd)
Optimal? Yes, if step cost = 1
Can be modified to explore uniform-cost tree

Number of nodes generated in a depth-limited search to


depth d with branching factor b:
 N(IDS) = (d)b + (d − 1)b2 + ・ ・ ・ + (1)bd

N(BFS) = b + b2 + … + bd + (bd+1 – b)

-BFS generates some nodes at depth d+1, IDS does


not.
BFS vs. IDS
IDS is faster than the BFS
For b = 10, d = 5,
 N(IDS) = 50 + 400 + 3, 000 + 20, 000 + 100,
000 = 123, 450
N(BFS) = 10 + 100 + 1, 000 + 10, 000 + 100,
000 = 1,111,10

In general, iterative deepening is the


preferred uninformed search method
when the search space is large and
the depth of the solution is not
known
Iterative deepening search

Worst Case: goal


d
DFS: 11 nodes
BFS: 04 nodes
IDS:04 nodes
Iterative deepening is a popular
method of search. Why?
-DFS can be implemented to be much cheaper than BFS in
terms of memory usage -but it is not guaranteed to find a
solution even where one is guaranteed.
-On the other hand, BFS can be guaranteed to terminate if
there is a winning state to be found & will always find the
`quickest' solution (in terms of how many steps need to be
taken from the root node). Very expensive method in terms of
memory usage.
-Iterative deepening is liked because it is an effective
compromise between the two other methods of search.
It is a form of DFS with a lower bound on how deep the search
can go.
Iterative deepening terminates if there is a solution.
It can produce the same solution that BFS would produce but
does not require the same memory usage (as for BFS).
Summary of algorithms
Repeated states
Failure to detect repeated states can turn a
linear problem into an exponential one!
Graph search
Summary
Problem formulation usually requires
abstracting away real-world details to define
a state space that can feasibly be explored
Variety of uninformed search strategies
Iterative deepening search uses only linear
space and not much more time than other
uninformed algorithms
Graph search can be exponentially more
efficient than tree search

You might also like