0% found this document useful (0 votes)
11 views58 pages

Ai CH - 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 58

Chapter Three

Basics of search

1
Problem Solving Agents
• This chapter describes one kind of goal-
based agent called a problem-solving
agent.
• Goal formulation: -
• We will consider a goal to be a set of
world states—exactly those states in which
the goal is satisfied. The agent’s task is to
find out how to act, now and in the future,
so that it reaches a goal state.
2
• Problem formulation: - is the process of deciding
what actions and states to consider, given a goal.
– Here we may have unknown environment so our agent
has no choice but to try one of the actions at random.
• The process of looking for a sequence of actions
that reaches the goal is called search.
• A search algorithm takes a problem as input and
returns a solution in the form of an action
sequence. Once a solution is found, the actions it
recommends can be carried out. This is called the
execution phase.
• Thus, we have a simple:
formulate search execute 3
Well-defined problems and solutions

Example:-

4
Count…
• A problem can be defined formally by five
components:
– Initial State: - The initial state that the agent starts
in. For example, the initial state for the above
figures can be described as A. In(A)
– Actions: - Description of possible actions
available to the agent. Given a particular state s,
ACTIONS(s) returns the set of actions that can
be executed in s. We say that each of these actions
is applicable in s.
• Example: - In (A), the applicable actions are {Go (C),
Go (D), Go E)}.
5
Count…
– Transition Model: - A description of what
each action does. Specified by a function
RESULT(s,a) that returns the state that
results from doing action a in state s. We
also use the term successor to refer to any
state reachable from a given state by a
single action.
• Example: - RESULT (In (A), Go (E)) = In (E)

6
Count…
– Goal state: - Determines whether a given state is a
goal state or not.
– Path cost: -Is a function that assigns a numeric cost
to each path. It reflects agent’s performance
measure.
• The preceding elements define a problem and can be
gathered into a single data structure that is given as
input to a problem-solving algorithm.
• A solution to a problem is an action sequence that leads
from the initial state to a goal state. Solution quality is
measured by the path cost function, and an optimal
solution has the lowest path cost among all solutions.
7
Count…
• Together, the initial state, actions, and
transition model implicitly define the state
space of the problem—the set of all states
reachable from the initial state by any
sequence of actions.
• The state space forms a directed network or
graph in which the nodes are states and the
links between nodes are actions.
• A path in the state space is a sequence of
states connected by a sequence of actions.
8
Searching for solutions
• A solution is an action sequence, so search
algorithms work by considering various possible
action sequences.
– The possible action sequences starting at the initial
state form a search tree with the initial state at the
root; the branches are actions and the nodes
correspond to states in the state space of the problem.
• A leaf node is a node with no children in the
tree. The set of all leaf nodes available for
expansion at any given point is called the
frontier.
9
Measuring problem-solving performance
• We can evaluate an algorithm’s performance in
four ways:
– COMPLETENESS: - Is the algorithm guaranteed
to find a solution when there is one?
– OPTIMALITY: - Does the strategy find the
optimal solution?
– TIMECOMPLEXITY: - How long does it take to
find a solution?
– SPACECOMPLEXITY: -How much memory is
needed to perform the search?

10
Count…
• Time and space complexity is expressed in
terms of three quantities:
– b: maximum number of successors of any
node (OR maximum branching factor of the
search tree)
– d: the depth of the shallowest goal need (OR
depth of the least cost solution )
– m: the maximum length of any path in the
state space (OR maximum depth of the sate
space )
11
Uninformed (Blind) Search

• These types of search strategies have no


additional information about states beyond that
provided in the problem definition.
• All they can do is generate successors and
distinguish a goal state from a non-goal state.
– Breadth-first search (BFS)
– Uniform-cost search
– Depth-first search
– Depth limited Search
– Iterative deepening depth-first search (IDS)
12
Breadth-first search (BFS)

• It is a simple strategy in which the root node is


expanded first, then all the successors of the
root node are expanded next, then 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.

13
Count…
• Breadth-first search is an instance of the
general graph-search algorithm in which the
shallowest unexpanded node is chosen for
expansion.
• This is achieved very simply by using a FIFO
queue for the frontier.
• Goal test is done when a node is generated
rather than when it is selected for expansion.

14
15
Performance of breadth- first search
• Completeness: - yes it is complete if we have a
finite branching factor. i.e. if we have some finite
branching factor then we have finite depth.
• Optimality: -Breadth-first search is optimal if
the path cost is a non decreasing function of the
depth of the node.
• Time complexity: - if every state has b
successors and the solution is at depth d.
– The total number of node generated is: -
b+b2+b3+…+bd=O(bd)
16
Count…
• Space complexity: - for breadth- first search
every node generated remains in memory.
– There will be O(bd-1)) nodes in the explored set and
O(bd) nodes in the frontier.
– So the space complexity is O(bd) i.e. it is dominant
by the size of the frontier.

17
Uniform-cost search

• It expands the node with the lowest path cost


g(n).
• This is done by storing the frontier as a priority
queue ordered by g.
• The two basic differences between Breadth First
search and uniform cost search is: -
– The goal test is applied to a node when it is selected
for expansion rather than when it is first generated.
– The test is added in case a better path is found to a
node in the frontier.
18
Count…

• Example: -

19
Count…
• From the above figure the successor of A are C
and B, with cost 80 and 99 respectively.
• The least cost node is C so we expand it next we
will find D with cost 97 so we add the two
80+97=177.
• Then we compare it with B, B=99<177 therefore
we expand B and get E with cost 99+211=310.
• At this point we compare 310 and 177 so we will
expand D and get 80+97+101=278.
• The algorithm chooses 278 because it is the least.
20
Performance of uniform- cost search
• Completeness:- Uniform-cost search does not
care about the number of steps a path has, but
only about their total cost.
• Therefore, it will get stuck in an infinite loop if
there is a path with an infinite sequence of
zero-cost actions.
• Completeness is guaranteed, if provided with
the cost of every step exceeds some small
positive constant 

21
Count…
• Optimality: - it is optimal in general because of
two reasons:
– When a node n is selected for expansion the
optimality path to that node has been found.
– Step costs are non- negative paths never getting
shorter as nodes are added.
• By these two reasons we can imply that
uniform- cost search expands nodes in order of
their optimal path cost.
• Hence, the first node selected for expansion
must be the optimal solution.
22
Count…
• Time and space complexity: - the algorithms
worst case time and space complexity will be
• O (b1+[C*/ ]) where,
–  - is the cost of every action
– C*- the cost of the optimal solution

23
Depth-first search

• This algorithm always expands the deepest


node in the current frontier of the search tree
then proceeds to the deepest level of the search
three.
• The depth-fist search algorithm is an instance
of the graph-search algorithm.

24
Count…
• Depth-first search uses a LIFO queue. A LIFO
stack means that the most recently generated
node is chosen for expansion.
• This must be the deepest unexpanded node
because it is one deeper than its parent—
which, in turn, was the deepest unexpanded
node when it was selected.

25
26
27
Performance of Depth- first search
• Completeness:- The graph-search version,
which avoids repeated states and redundant
paths, is complete in finite state spaces
because it will eventually expand every node.
• The tree-search version, on the other hand, is
not complete.
• If we have infinite state space the both the tree
and graph search version are not complete.

28
Count…
• Optimality: - Whether we are using graph or
tree search version to apply to depth first
search it is not optimal consider the following
figure:

29
Count…
• If node C is a goal state the depth first search
will explore the entire left sub tree even if
node C is a goal node.
• If G were also a goal node state the depth first
search would return it as a solution instead of
C which would be a better solution; hence,
depth-first search is not optimal.

30
Count…
• Time Complexity: - it may generate all the nodes
in the search tree. It is O(bm) , b is branching factor
and m is the maximum depth of any node.
• Space complexity: - its tree search version has
advantage over breadth first search in its space
complexity.
• Depth first search removes a node form memory
once all its descendents have been expanded.
• Therefore, its space complexity will be O (bm).

31
Depth limited Search
• Depth first search suffers from non termination
when the length of a path in the search tree is
infinite, so we perform depth first search to a
limited depth which is called Depth limited
search.
• This search precedes the search by declaring
nodes at depth l are treated as if they have no
successors.
• This helps to solve the infinite path problem.

32
Performance of Depth- limited search
• Unfortunately, it also introduces an additional
source of incompleteness if we choose l<d,
that is, the shallowest goal is beyond the depth
limit. (This is likely when d is unknown.)
• Depth-limited search will also be non optimal
if we choose l >d.
• Its time complexity is O(b l) and its space
complexity isO(b l).
• Depth-first search can be viewed as a special
case of depth-limited search with l =∞.
33
Iterative deepening depth-first search (IDS)

• It is a general strategy, often used in


combination with depth-first tree search that
finds the best depth limit.
• It does this by gradually increasing the limit—
first 0, then1, then2, and soon—until a goal is
found.
• Iterative deepening combines the benefits of
depth-first and breadth-first search.

34
35
Performance of iterative deepening depth first search

• Completeness: - Like breadth-first search, it is


complete when the branching factor is finite
and optimal when the path cost is a non
decreasing function of the depth of the node.
• Optimality: - if states are of the same cost
then it is optimal
• Time complexity: - O (bd)
• Space complexity: - Like depth-first search,
its memory requirements are modest
36
Informed (Heuristic) Search Strategies
• Informed search uses problem-specific knowledge
beyond the definition of the problem itself—can find
solutions more efficiently than can an uninformed
strategy.
• The general approach we consider is called best-first
search.
• Best-first search is algorithm in which a node is
selected for expansion based on an evaluation
function, f(n).
– The evaluation function is construed as a cost estimate, so
the node with the lowest evaluation is expanded first.
– Most best-first algorithms include as a component of f a
heuristic function, denoted h(n):
37
Count…
• h (n)=estimated cost of the cheapest path from
the state at node n to a goal state.
• Heuristic functions are the most common form
in which additional knowledge of the problem
is imparted to the search algorithm. H(n)=0 if
n is the goal node 3

38
Count…
• Heuristic functions are the most common form in
which additional knowledge of the problem is
imparted to the search algorithm.
• For now, we consider them to be arbitrary,
nonnegative, problem-specific functions, with one
constraint: if n is a goal node, then h(n )=0.

39
Greedy best-first search

• Greedy best-first search tries to expand the node that is


closest to the goal, on the grounds that this is likely to lead
to a solution quickly.
• Thus, it evaluates nodes by using just the heuristic
function; that is, f(n)=h(n).
• Let us see how this works for route finding problems in
Romania;
• we use the straight line distance heuristic, which we will
call hSLD.
• If the goal is Bucharest, we need to know the straight-line
distances to Bucharest, shown in the table below.
40
41
42
43
Performance of Greedy best-first tree
search
• Completeness
– Greedy best-first tree search is also incomplete even in a
finite state space, much like depth-first search.
• Consider the problem of getting from Iasi to Fagaras.
The heuristic suggests that Neamt be expanded first
because it is closest to Fagaras, but it is a dead end.
• The solution is to go first to Vaslui—a step that is
actually farther from the goal according to the heuristic
—and then to continue to Urziceni, Bucharest, and
Fagaras.
• The algorithm will never find this solution, however,
because expanding Neamt puts Iasi back into the
frontier, Iasi is closer to Fagaras thanVaslui is, and so
Iasi will be expanded again, leading to an infinite loop.
44
Count…
• The graph search version is complete infinite
spaces, but not in infinite ones.
• Optimality
– It is not optimal, however: the path via Sibiu and
Fagaras to Bucharest is 32 kilometers longer than
the path through Rimnicu Vilcea and Pitesti. This
shows why the algorithm is called“greedy”—at
each step it tries to get as close to the goal as it
can.

45
Count…
• Time and space complexity:
– The worst-case time and space complexity for the
tree version is O(bm),where m is the maximum
depth of the search space.
• With a good heuristic function, however, the
complexity can be reduced substantially.
• The amount of the reduction depends on the
particular problem and on the quality of the
heuristic.

46
A* search: Minimizing the total
estimated solution cost
• It evaluates nodes by combining g(n), the cost to reach
the node, and h(n), the cost to get from the node to the
goal:
f(n)=g(n)+h(n)
• Since g(n) gives the path cost from the start node to
node n ,and h(n ) is the estimated cost of the cheapest
path from n to the goal,
– we have f (n )= estimated cost of the cheapest solution
through n.
• The algorithm is identical to uniform cost- search
except that A* uses g + h instead of g.
47
Example

48
49
50
51
Performance of A*
• A* is optimal and complete.
• Conditions for optimality
– Admissibility (if we apply A* using THREE-
SEARCH strategy)
• The first condition we require for optimality is that h(n)
be an admissible heuristic.
• An admissible heuristic is one that never overestimates
the cost to reach the goal.
• Because g(n) is the actual cost to reach n along the
current path, and f(n)=g(n)+h(n),we have as an
immediate consequence that f(n) never overestimates
the true cost of a solution along the current path
through n. 52
Count…
– Consistency (if we apply A* using GRAPH-
SEARCH strategy )
• A heuristic h(n) is consistent if, for every node n
and every successor n’ of n generated by any
action a, the estimated cost of reaching the goal
from n is no greater than the step cost of getting
to n’ plus the estimated cost of reaching the goal
from n’:
h(n)≤c(n,a,n’)+h(n’)

53
Memory bounded heuristic Search

• Recursive best-first search (RBFS) it uses the f-


limit variable to keep track of the f- value of the best
alternative path available from any ancestor of the
current node.
• If the current node exceeds this limit the recursion
unwinds back to the alternative path and replace the
value of f-value by backed up value (best f value of it
children).

54
55
56
Performance of Recursive best-first search (RBFS)

• Optimal: -RBFS is optimal algorithm if the


heuristic function h(n) is admissible.
• Space Complexity: - linear in the depth of the
deepest optimal solution.
• Time complexity: - is difficult to characterize
because of two reasons
• It depends on the accuracy of heuristic functions
• Depends on how often the best path changes as
nodes are expanded.
57
I Suggest You to read more about search
strategies from other sources!!!

I
THANK YOU!!!

58

You might also like