Chapter 3 Solving Problems by Searching1

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 96

Chapter - III

Solving Problems by Searching


Outline / objectives
Problem-solving agents

Problem types

Problem formulation

Example problems
Problem solving
We want:
To automatically solve a problem

We need:
A representation of the problem
Algorithms that use some strategy to
solve the problem defined in that
representation
States
A problem is defined by its elements and their
relations.
A state is a representation of those elements in a
given moment.
In each instant of a problem, those elements have
specific descriptors (How to select them?) and
relations.
Two special states are defined:
– Initial state (starting point)
– Final state (goal state)
State modification: successor function
A successor function is needed to move
between different states.
A successor function is a description of
possible actions, a set of operators. It is a
transformation function on a state
representation, which convert it into another
state.
The successor function defines a relation of
accessibility among states.
State space
The state space is the set of all states
reachable from the initial state.
It forms a graph (or map) in which the
nodes are states and the arcs between
nodes are actions.
A path in the state space is a sequence
of states connected by a sequence of
actions.
Problem solution
A solution in the state space is a path from
the initial state to a goal state.

Path/solution cost: function that assigns a


numeric cost to each path, the cost of
applying the operators to the states.

Solution quality is measured by the path


cost function, and an optimal solution has
the lowest path cost among all solutions.
Problem solving agents
Goal formulation, based on the current situation and the

agent’s performance measure, is the first step in problem

solving

Goal is a set of states. The agent’s task is to find out

which sequence of actions will get it to a goal state

Problem formulation is the process of deciding what

sorts of actions and states to consider, given a goal


Problem solving agents Cont’d..
An agent can decide what to do by first
examining different possible sequences of
actions that lead to states of known value,
and then choosing the best sequence,
Looking for such a sequence is called
search,
A search algorithm takes a problem as input
and returns a solution in the form of action
sequence,
Problem solving agents Cont’d..
After formulating a goal and a problem to
solve the agent calls a search procedure to
solve it,
The basic algorithm for problem-solving
agents consists of 3 phases: “formulate,
search, execute” design for the agent,
formulate the problem, search for a solution
and execute the solution.
Problem solving agents Cont’d..
The aim of the problem-solving agent
is therefore to perform a sequence of
actions that change the environment so
that it ends up in one of the goal states.
Once the solution has been
executed, the agent will formulate a
new goal.
Problem types
Deterministic, fully observable  single-state problem
 Agent knows exactly which state it will be in;

Nondeterministic and/or partially observable 


contingency problem
 percepts provide new information about current state

Non-observable  sensorless problem


 Agent may have no idea where it is;

Unknown state space  exploration problem


 states and actions of the environment are initially unknown
Example: Vacuum-Cleaner Env’t

There is a vacuum-cleaner agent that can be


located in one of the two locations, A or B
4 actions available to it: to move right, move
left, suck or NoOp, and its goal is to clean both
locations.
Deterministic, fully observable  single-state problem

fully-observable: if the vacuum-


cleaner agent could perceive its own
location and whether or not there is dirt
in both locations.
Deterministic: if the next state of the
environment could be predicted from
the current state and action.
Nondeterministic and/or partially observable
 contingency problem
occur when the agent can obtain new
information from its sensors after acting.
This can be the case because the
environment is partially-observable, or
because the environment is nondeterministic
(i.e. stochastic or strategic).
Solutions to contingency problems are
harder to find.
Non-observable  sensorless problem
if the vacuum-cleaner agent did not
receive any percepts from the
environment.
the agent does not know what state the
environment is in
use a belief state to indicate what set of
real ‘physical’ states it thinks it might
be in, based on actions it has taken.
Unknown state space  exploration problem

states and actions of the


environment are initially unknown
An extreme version of the
contingency problem
The agent must therefore explore
the environment to discover them.
Problem Formulation in Single State
Example: Romania
On holiday in Romania; currently in Arad.

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 Formulation…. Cont’d

Initial state: the state in


which the agent starts in –
e.g. In(Arad)
Problem Formulation…. Cont’d
Actions: A description of the possible actions

available to the agent

– Successor function – returns a set of <action,

successor> pairs

– e.g. {<Go(Sibiu),In(Sibiu)>,

<Go(Zerind), In(Zerind)>}
Problem Formulation…. Cont’d

Goal test determines whether a given state

is a goal state – e.g.{In(Bucharest)}


Problem Formulation…. Cont’d
Path cost function that assigns a numeric
cost to each path.
 The cost of a path can be described as the sum
of the costs of the individual actions along the
path –
step cost – e.g. to go Bucharest
c(Arad, AradZerind, Zerind) = 75, c(Arad,
AradSibiu, Sibiu) = 140,
c(Arad, AradTimisoara, Timisoara) = 118, etc.
Example: The 8-puzzle

states?
actions?
goal test?
path cost?
Example: The 8-puzzle

states? locations of tiles


actions? move blank left, right, up, down
goal test? = goal state (given)
path cost? 1 per move
Vacuum world state space graph

 states?
 actions?
 goal test?
 path cost?
Vacuum world state space graph

states? dirt and robot location


actions? Left, Right, Suck, NoOp
goal test? no dirt at all locations
path cost? 1 per action
Exercise: Water Pouring
Given a 4 gallon bucket and a 3 gallon
bucket, how can we measure exactly 2
gallons into one bucket?
There are no markings on the bucket
You must fill each bucket completely
Water Pouring
Initial state:
The buckets are empty
Represented by the tuple ( 0 0 )

Goal state:
One of the buckets has two gallons of water in it
Represented by either ( x 2 ) or ( 2 x )

Path cost:
1 per unit step
Water Pouring
Actions and Successor Function
Fill a bucket
(x y) -> (3 y)
(x y) -> (x 4)
Empty a bucket
(x y) -> (0 y)
(x y) -> (x 0)
Pour contents of one bucket into
another
Water Pouring
(0,0)

(4,0) (0,3)

(1,3) (4,3) (3,0)

(1,0) (0,1)
(3,3) (4,2)

(4,1)
(2,3)

(2,0) (0,2)
Exercise: Eight Queens
States:
Any arrangement of 0 to 8 queens on the
board
Initial state:
No queens on the board
Successor function:
Add a queen to an empty square
Goal Test:
8 queens on the board and none are attacked
Solution for 8 Queens
Part two
Outline
Basic search Strategies

Uninformed Search Strategies

BFS

UCS

DFS

DLS
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
 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 ∞)
Search Strategy Techniques
Uninformed Search Strategy: a searching technique
which have no additional information about the distance
from current state to the goal.
 is a class of general purpose search algorithms that operate in
a brute-force way.

Informed Search Strategy: is a another technique which


have additional information about the estimate distance
from the current state to the goal.
 uses prior knowledge about problem,hence very efficient
Uninformed search strategies
Uninformed search strategies use only the
information available in the problem definition.
Breadth-first search

Depth-first search

Depth Limited Search

Uniform-cost search
Breadth-first search
The root node is expanded first, then all the successors of
the root node, and their successors and so on
In general, all the nodes are expanded at a given depth in the
search tree before any nodes at the next level are expanded
Expand shallowest unexpanded node
Implementation:
– fringe is a FIFO queue,
– the nodes that are visited first will be expanded first
–All newly generated successors will be put at the end of
the queue
– Shallow nodes are expanded before deeper nodes
Breadth-first Search
A

Successors: B,C,D
Initial state
D
B C

E F G H I J

K L M N O P Q R

Goal state
S T U
Visited:
The fringe is the data structure we use to store all of the
Fringe: A (FIFO) nodes that have been generated
Breadth-first Search
A
Next node
D
Successors: E,F B C

E F G H I J

K L M N O P Q R

S T U Visited: A

Fringe: B,C,D (FIFO)


Breadth-first Search
A

Successors: K,L
D
B C
Next node

E F G H I J

K L M N O P Q R

Visited: A, B, C, D
S T U

Fringe: E,F,G,H,I,J (FIFO)


Breadth-first Search
A

Successors: M D
B C
Next node

E F G H I J

K L M N O P Q R

Visited: A, B, C, D, E
S T U

Fringe: F,G,H,I,J,K,L (FIFO)


Breadth-first Search
A

Successors: N D
B C
Next node

E F G H I J

K N O P Q R
L M

Visited: A, B, C, D, E, F
S T U

Fringe: G,H,I,J,K,L,M (FIFO)


Breadth-first Search
A

Successors: O D
B C
Next node

E F G H I J

K L M N O P Q R

S T U Visited: A, B, C, D, E, F,
G

Fringe: H,I,J,K,L,M,N (FIFO)


Breadth-first Search
A

Successors: P,Q
D
B C
Next node

E F G H I J

K L M N O P Q R

Visited: A, B, C, D, E, F,
S T U
G, H

Fringe: I,J,K,L,M,N,O (FIFO)


Breadth-first Search
A

Successors: R
D
B C
Next node

E F G H I J

K L M N O P Q R

Visited: A, B, C, D, E, F,
S T U G, H, I

Fringe: J,K,L,M,N,O,P,Q (FIFO)


Breadth-first Search
A

Successors: S
D
B C

E F G H I J
Next node

K L M N O P Q R

Visited: A, B, C, D, E, F,
S T U
G, H, I, J

Fringe: K,L,M,N,O,P,Q,R (FIFO)


Breadth-first Search
A

Successors: T D
B C

E F G H I J
Next node

K L M N O P Q R

Visited: A, B, C, D, E, F,
S T U
G, H, I, J, K

Fringe: L,M,N,O,P,Q,R,S (FIFO)


Breadth-first Search
A

Successors: T D
B C

E F G H I J
Next node

K L M N O P Q R

Visited: A, B, C, D, E, F,
S T U
G, H, I, J, K

Fringe: L,M,N,O,P,Q,R,S (FIFO)


Breadth-first Search
A

Successors: D
B C

E F G H I J

K L M N O P Q R

Next node
Visited: A, B, C, D, E, F,
S T U
G, H, I, J, K, L

Fringe: M,N,O,P,Q,R,S,T (FIFO)


Breadth-first Search
A
Goal state achieved
Successors: D
B C

E F G H I J

K L M N O P Q R

Next node
Visited: A, B, C, D, E, F,
S T U G, H, I, J, K, L, M

Fringe: N,O,P,Q,R,S,T (FIFO)


Properties of breadth-first search
Complete? Yes (if b is finite)

Time? 1+b+b2+b3+… +bd + b(bd-1) = O(bd+1)

Space? O(bd+1) (keeps every node in memory)

Optimal? Yes (if cost = 1 per step)

Space is the bigger problem (more than time)


Uniform-cost search (UCS)
Uniform cost search is a search algorithm used to
traverse, and find the shortest path in weighted trees
and graphs.
Uniform Cost Search or UCS begins at a root node
and will continually expand nodes, taking the node
with the smallest total cost from the root until it
reaches the goal state.

Uniform cost search doesn't care about how many


steps a path has, only the total cost of the path.
UCS with all path costs equal to one is identical to
breadth first search.
Uniform Cost Search

A
10
1
S 5 B 5 D
5
15
C

Similar to BFS except that it sorts (ascending order) the


nodes in the fringe according to the cost of the node.
where cost is the path cost.
Uniform Cost Search
Fringe = [S0]
A Queue
10
1
S 5 B 5 D
5
15
C
Next Node=Head of Fringe=S, S is not
goal
Updated Fringe=[A1,B5,C15]
Queue Successor(S)={C,B,A}=expand(S) but
sort them according to path cost.
Uniform Cost Search
Fringe = [S0]
A Queue
10
1
S 5 B 5 D
5
15
C
Next Node=Head of Fringe=S, S is not
goal
Updated Fringe=[A1,B5,C15]
Queue Successor(S)={C,B,A}=expand(S) but
sort them according to path cost.
Uniform Cost Search
Fringe = [A1,B5,C15]
A
10
1
S 5 B 5 D
5
15
C
Next Node=Head of Fringe=A, A is not
Updated Fringe=[B5,D11,C15] goal
Successor(A)={D}=expand(A)
Sort the queue according to path cost.
Uniform Cost Search
Fringe = [B5,D11,C15]
A
10
1
S 5 B 5 D
5
15
C
Next Node=Head of Fringe=B, B is not
goal
Updated Fringe=[D10,D11,C15]
Successor(B)={D}=expand(B)
Sort the queue according to path cost.
Uniform Cost Search
Fringe = [D10,D11,C15]
A
10
1
S 5 B 5
D
5
15
C
Next Node=Head of Fringe=D,
Always finds the
cheapest solution D is a GOAL (cost 10 = 5+5)
SBD
Depth First Search - Method
Expand Root Node First
Explore one branch of the tree before
exploring another branch
If a leaf node do not represent a goal
state, search backtracks up to the next
highest node that has an unexplored
path
DFS
Depth-first search (DFS) is an
algorithm for traversing or searching a
tree, tree structure, or graph.
One starts at the root (selecting some
node as the root in the graph case) and
explores as far as possible along each
branch before backtracking.
Depth-First Search
A

Successors: B,C,D
Initial state
D
B C

E F G H I J

K L M N O P Q R

S T Goal state U Visited:

Fringe: A (LIFO)
Depth-First Search
A
Successors: E,F
D
B C

E F G H I J

K L M N O P Q R

S T U
Visited: A
Fringe: B,C,D (LIFO)
Depth-First Search
A

Successors: K,L
D
B C

E F G H I J

K L M N O P Q R

Visited: A, B
S T U

Fringe: E,F,C,D (LIFO)


Depth-First Search
A

Successors: S D
B C

E F G H I J

K L M N O P Q R

S T U Visited: A, B, E

Fringe: K,L,F,C,D (LIFO)


Depth-First Search
A

Successors: D
B C

E F G H I J

K L M N O P Q R

S T U Visited: A, B, E, K

Fringe: S,L,F,C,D (LIFO)


Depth-First Search
A

Successors: T D
B C

E F G H I J

K L M N O P Q R

Visited: A, B, E, K, S
S T U
Backtracking
Fringe: L,F,C,D (LIFO)
Depth-First Search
A

Successors: D
B C

E F G H I J

K L M N O P Q R

Visited: A, B, E, K, S, L
S T U

Fringe: T,F,C,D (LIFO)


Depth-First Search
A

Successors: M
D
B C

E F G H I J

K L M N O P Q R

Visited: A, B, E, K, S, L, T
S T U
Backtracking
Fringe: F,C,D (LIFO)
Depth-First Search
A

Successors: D
B C

E F G H I J

K L M N O P Q R

Visited: A, B, E, K, S, L, T,
S T U F

Fringe: M,C,D (LIFO)


Depth-First Search
A

Successors: G,H
D
B C

E F G H I J

K L M N O P Q R

Visited: A, B, E, K, S, L, T,
F, M
S T U
Backtracking
Fringe: C,D (LIFO)
Depth-First Search
A

Successors: N D
B C

E F G H I J

K L M N O P Q R

Visited: A, B, E, K, S, L, T,
F, M, C
S T U

Fringe: G,H,D (LIFO)


Depth-First Search
A

Goal state achieved


Successors: D
B C
Finished search
E F G H I J

K L M N O P Q R

Visited: A, B, E, K, S, L, T,
S T U F, M, C, G

Fringe: N,H,D (LIFO)


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(bd): terrible if m is much larger than d
Space? O(bm), i.e., linear space!
Optimal? No

Depth limited search
Like Depth first search, but the search is limited to a
predefined depth.

The depth of each state is recorded as it is generated. When


picking the next state to expand, only those with depth less or
equal than the current depth are expanded.

Once all the nodes of a given depth are explored, the current
depth is incremented.
Properties of depth limited search

Incomplete: if solution is below


cut-off depth l
Optimal: if solution is above cut-
off depth l
Time complexity: O(bl)
Space complexity: O(bl)
Summary of Uninformed Tree Search Algorithms

Criterion BFS UCS DFS DLS


Complete? Yes Yes No No
Optimal? Yes Yes No No
Time O(bd+1) O(b[C*/ε]) O(bm) O(bl)
Space O(bd+1) O(b[C*/ε]) O(bm) O(bl)
Informed (Heuristic) Search
Best-first search
Greedy best-first search
A* search
Hill-climbing search
Simulated annealing search
Informed (Heuristic)
A node is selected for expansion
based on an evaluation function
that estimates cost to goal.
Best-first search
General approach of informed search:
Best-first search: node is selected for expansion based on
an evaluation function f(n)
Idea: evaluation function measures distance to the
goal.
Choose node which appears best
Implementation:
fringe is queue sorted in decreasing order of desirability.
Special cases: greedy Best-first search, A* search
A heuristic function: h(n)
 [dictionary]“A rule of thumb,
simplification, or educated guess that
reduces or limits the search for solutions in
domains that are difficult and poorly
understood.”
h(n) = estimated cost of the cheapest path
from node n to goal node.
If n is goal then h(n)=0
h =straight-line distance heuristic.
SLD
h
SLD can NOT be computed from the problem description itself
 In this example f(n)=h(n)
 Expand node that is closest to goal= Greedy best-first search
Greedy best-first search example

Arad (366)

Assume that we want to use greedy search to solve the


problem of travelling from Arad to Bucharest.
The initial state=Arad
Greedy search example
Arad

Sibiu(253)

Timisoara Zerind(374)
(329)
The first expansion step produces:
Sibiu, Timisoara and Zerind
Greedy best-first will select Sibiu.
Greedy search example
Arad

Sibiu

Arad Rimnicu Vilcea


Fagaras Oradea
(366) (193)
(176) (380)

If Sibiu is expanded we get:


Arad, Fagaras, Oradea and Rimnicu Vilcea
Greedy best-first search will select: Fagaras
Greedy search example

Arad

Sibiu

Fagaras

Sibiu Bucharest
(253) (0)

If Fagaras is expanded we get:


Sibiu and Bucharest
Goal reached !!
Yet not optimal (see Arad, Sibiu, Rimnicu Vilcea, Pitesti)
Greedy search, evaluation
Completeness: NO ( DF-search)
Time complexity?
 Worst-case DF-search
O(b m )
(with m is maximum depth of search space)
Good heuristic can give dramatic improvement.
Space complexity?


Keeps all nodes in memory
Optimality? NO
Same as DF-search O(b m )
A* search
Best-known form of best-first search.
Idea: avoid expanding paths that are already
expensive.
Evaluation function f(n)=g(n) + h(n)
g(n) the cost (so far) to reach the node.
h(n) estimated cost to get from the node to the
goal.
f(n) estimated total cost of path through n to goal.
A* search
A* search uses an admissible heuristic
A heuristic is admissible if it never
overestimates the cost to reach the goal
Are optimistic

Formally:
1. h(n) <= h*(n) where h*(n) is the true cost from n
2. h(n) >= 0 so h(G)>= 0 for any goal G.

e.g. hSLD(n) never overestimates the actual road distance


Properties of A*
Complete? Yes
Time/Space? Exponential b d

Optimal? Yes
Hill-climbing search
Hill-climbing search is a local search algorithm
that iteratively improves a solution by making
incremental changes that maximize a heuristic
evaluation function.
It repeatedly selects the neighbor with the highest
heuristic value, moving towards the direction of
increasing values. However, it may get stuck in
local optima and struggle with plateaus.
Simulated annealing search
Simulated annealing search is a probabilistic
optimization algorithm inspired by the annealing
process in metallurgy.
It explores the search space by randomly accepting
moves that improve the solution or moves to less
optimal solutions based on a temperature parameter.
Over time, the temperature decreases, reducing the
likelihood of accepting worse solutions, ultimately
converging towards the global optimum.
?

You might also like