Unit 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 55

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

III – B.Tech – I – Sem (A, B, C & D Sections)

ARTIFICIAL INTELLIGENGE
(R18 Regulations)

By
Dr.Vijender Kr. Solanki
Dr.A.Nirmal Kumar

1
ARTIFICIAL INTELLIGENCE
UNIT – II
Search Algorithms
 Random search
 Search with closed and open list
 Depth first and Breadth first search
 Heuristic search
 Best first search
 A* algorithm
 Game Search
Search Algorithms in AI
Artificial Intelligence is the study of building agents that act
rationally. Most of the time, these agents perform some kind
of search algorithm in the background in order to achieve
their tasks.
• A search problem consists of:
– A State Space. Set of all possible states where you can be.
– A Start State. The state from where the search begins.
– A Goal Test. A function that looks at the current state returns
whether or not it is the goal state.
• The Solution to a search problem is a sequence of actions,
called the plan that transforms the start state to the goal
state.
• This plan is achieved through search algorithms.
Search Terminology
• Problem Space − It is the environment in which the search takes place. (A set
of states and set of operators to change those states)
• Problem Instance − It is Initial state + Goal state.
• Problem Space Graph − It represents problem state. States are shown by nodes
and operators are shown by edges.
• Depth of a problem − Length of a shortest path or shortest sequence of
operators from Initial State to goal state.
• Space Complexity − The maximum number of nodes that are stored in
memory.
• Time Complexity − The maximum number of nodes that are created.
• Admissibility − A property of an algorithm to always find an optimal solution.
• Branching Factor − The average number of child nodes in the problem space
graph.
• Depth − Length of the shortest path from initial state to goal state.
Single-player games
• Sudoku
• Crossword
Single-agent-path-finding Problems
• 3X3 eight-tile
• 4X4 fifteen-tile
• 5X5 twenty four tile puzzles
• Travelling Salesman Problem
• Rubik’s Cube
• Theorem Proving
Random Search
• Random search (RS) is a family of numerical optimization
methods that do not require the gradient of the problem to be
optimized, and RS can hence be used on functions that are
not continuous or differentiable. Such optimization methods
are also known as direct-search, derivative-free, or black-box
methods.
• The name "random search" is attributed to Rastrigin who
made an early presentation of RS along with basic
mathematical analysis. RS works by iteratively moving to
better positions in the search-space, which are sampled from
a hypersphere surrounding the current position.
Random Search Algorithm
• Let f: ℝn → ℝ be the fitness or cost function which must
be minimized. Let x ∈ ℝn designate a position or
candidate solution in the search-space. The basic RS
algorithm can then be described as:
• Initialize x with a random position in the search-space.
• Until a termination criterion is met (e.g. number of
iterations performed, or adequate fitness reached), repeat
the following:
– Sample a new position y from the hypersphere of a given
radius surrounding the current position x (see e.g. Marsaglia's
technique for sampling a hypersphere.)
– If f(y) < f(x) then move to the new position by setting x = y
Search with closed and open list
Pure Heuristic Search
• It expands nodes in the order of their heuristic values.
It creates two lists, a closed list for the already
expanded nodes and an open list for the created but
unexpanded nodes.
• In each iteration, a node with a minimum heuristic
value is expanded, all its child nodes are created and
placed in the closed list. Then, the heuristic function is
applied to the child nodes and they are placed in the
open list according to their heuristic value. The shorter
paths are saved and the longer ones are disposed.
TYPES OF SEARCH ALGORITHMS
I. Brute-Force Search Strategies (Uninformed / Blind)
• Breadth-First Search
• Depth-First Search
• Bidirectional Search
• Uniform Cost Search
• Iterative Deepening Depth-First Search
II. Informed (Heuristic) Search Strategies
• Pure Heuristic Search (Open and Closed List)
• Greedy Best First Search
• A * Search
III. Local Search Algorithms
• Hill-Climbing Search
• Local Beam Search
• Simulated Annealing
• Travelling Salesman Problem
UNINFORMED SEARCH ALGORITHMS
• The search algorithms in this section have no additional information on the
goal node other than the one provided in the problem definition. The plans
to reach the goal state from the start state differ only by the order and/or
length of actions. Uninformed search is also called Blind search.
• The following uninformed search algorithms are discussed in this section.
1. Depth First Search
2. Breath First Search
3. Uniform Cost Search
• Each of these algorithms will have:
 A problem graph, containing the start node S and the goal node G.
 A strategy, describing the manner in which the graph will be traversed
to get to G .
 A fringe, which is a data structure used to store all the possible states
(nodes) that you can go from the current states.
 A tree, that results while traversing to the goal node.
 A solution plan, which the sequence of nodes from S to G.
UNINFORMED SEARCH STRATEGIES

• Uninformed Search Strategies have no additional information about


states beyond that provided in the problem definition.
• Strategies that know whether one non goal state is "more promising"
than another are called Informed search or heuristic search strategies.

• There are six uninformed search strategies as given below.

1. Breadth-first search
2. Uniform-cost search
3. Depth-first search
4. Depth-limited search
5. Iterative deepening search
6. Bidirectional Search
BREADTH FIRST SEARCH

• Breadth-first search is a simple strategy in which the root node is


expanded first, then all 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.
• Breath-first-search is implemented by calling TREE-SEARCH with
an empty fringe that is a first-in-first-out(FIFO) queue, assuring that
the nodes that are visited first will be expanded first.
• In otherwards, calling TREE-SEARCH (problem, FIFO-QUEUE())
results in breadth-first-search.
• The FIFO queue puts all newly generated successors at the end of
the queue, which means that Shallow nodes are expanded before
deeper nodes.
Properties of BFS
Time complexity for BFS
• Assume every state has b successors.
• The root of the search tree generates b nodes at the first level,each of
which generates b more nodes,for a total of b2 at the second level.
• Each of these generates b more nodes,yielding b3 nodes at the third
level,and so on.
• Now suppose,that the solution is at depth d.
• In the worst case,we would expand all but the last node at level
d,generating bd+1 - b nodes at level d+1.
• Then the total number of nodes generated is
b + b2 + b3 + …+ bd + (bd+1 + b) = O(bd+1).
• Every node that is generated must remain in memory,because it is
either part of the fringe or is an ancestor of a fringe node.
• The space compleity is,therefore ,the same as the time complexity
UNIFORM-COST SEARCH
• Instead of expanding the shallowest node, uniform-cost search
expands the node n with the lowest path cost.
• uniform-cost search does not care about the number of steps a path
has, but only about their total cost.
• Properties of Uniform-cost-search:
DEPTH-FIRST-SEARCH
– Depth-first-search always expands the deepest node in the current fringe of the search
tree.
– The progress of the search is illustrated in figure.
– The search proceeds immediately to the deepest level of the search tree, where the
nodes have no successors.
– As those nodes are expanded, they are dropped from the fringe,
– so then the search "backs up" to the next shallowest node that still has unexplored
successors.
– This strategy can be implemented by TREE-SEARCH with a last-in-first- out
(LIFO) queue, also known as a stack.
– Depth-first-search has very modest memory requirements.
– It needs to store only a single path from the root to a leaf node along with the
remaining unexpanded sibling nodes for each node on the path.
– Once the node has been expanded, it can be removed from the memory, as soon as its
descendants have been fully explored.
– For a state space with a branching factor b and maximum depth m, depth- first-search
requires storage of only bm + 1 nodes.
INFORMED SEARCH ALGORITHMS
• Here, the algorithms have information on the goal state, which
helps in more efficient searching. This information is obtained
by something called a heuristic.
• In this section, we will discuss the following search
algorithms.
1. Greedy Search
2. A* Tree Search
3. A* Graph Search
• Search Heuristics: In an informed search, a heuristic is
a function that estimates how close a state is to the goal state.
For examples – Manhattan distance, Euclidean distance, etc.
(Lesser the distance, closer the goal.)
INFORMED SEARCH AND EXPLORATION
Informed (Heuristic) Search Strategies 
• Informed search strategy is one that uses problem-specific
knowledge beyond the definition of the problem itself.
• It can find solutions more efficiently than uninformed strategy.
Best-first search
• Best-first search is an instance of general TREE-SEARCH or
GRAPH-SEARCH algorithm in which a node is selected for
expansion based on an evaluation function f(n).
• The node with lowest evaluation is selected for expansion,
because the evaluation measures the distance to the goal.
• This can be implemented using a priority-queue, a data
structure that will maintain the fringe in ascending order of f-
values.
Heuristic functions
 
• A heuristic function or simply a heuristic is a function that ranks
alternatives in various search algorithms at each branching step basing on
an available information in order to make a decision which branch is to be
followed during a search.
• The key component of Best-first search algorithm is a heuristic
function,denoted by h(n):

h(n) = estimated cost of the cheapest path from node n to a goal node.
 
• For example,in Romania, one might estimate the cost of the cheapest path
from Arad to Bucharest via a straight-line distance from Arad to
Bucharest
• Heuristic function are the most common form in which additional
knowledge is imparted to the search algorithm.
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 a solution quickly.
• It evaluates the nodes by using the heuristic function f(n) = h(n).
• Taking the example of Route-finding problems in Romania, the
goal is to reach Bucharest starting from the city Arad.
• We need to know the straight-line distances to Bucharest from
various cities.
• For example, the initial state is In(Arad) ,and the straight line
distance heuristic hSLD(In(Arad)) is found to be 366.
• Vsing the straight-line distance heuristic hSLD ,the goal state can
be reached faster.
Values of hSLD - straight line distances to Bucharest
• The above figure shows the progress of greedy best-first search using h SLD
to find a path from Arad to Bucharest.
• The first node to be expanded from Arad will be Sibiu, because it is closer
to Bucharest than either Zerind or Timisoara.
• The next node to be expanded will be Fagaras, because it is closest.
• Fagaras in turn generates Bucharest, which is the goal.
Properties of Greedy Search
– Complete?? No-can get stuck in loops, e.g., Iasi ! Neamt ! Iasi !
Neamt !
Complete in finite space with repeated-state checking
– Time?? O(bm), but a good heuristic can give dramatic improvement
– Space?? O(bm)-keeps all nodes in memory
– Optimal?? No
• Greedy best-first search is not optimal, and it is incomplete.
• The worst-case time and space complexity is O(bm),where m is the
maximum depth of the search space.
A* Search
• A* Search is the most widely used form of best-first search. The evaluation
function f(n) is obtained 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).
• A* Search is both optimal and complete. A* is optimal if h(n) is an
admissible heuristic.
• The obvious example of admissible heuristic is the straight-line distance
hSLD.
• It cannot be an overestimate.
• A* Search is optimal if h(n) is an admissible heuristic - that is provided that
h(n) never overestimates the cost to reach the goal.
• An obvious example of an admissible heuristic is the straight-line distance
hSLD that we used in getting to Bucharest.
• The progress of an A* tree search for Bucharest is shown in above figure.
• The values of 'g ' are computed from the step costs shown in the
Romania,Also the values of hSLD are given in Figure Route Map of
Romania.
A* Tree Search
• A* Tree Search, or simply known as A* Search, combines the
strengths of uniform-cost search and greedy search. In this
search, the heuristic is the summation of the cost in UCS,
denoted by g(x), and the cost in greedy search, denoted
by h(x). The summed cost is denoted by f(x).
• Heuristic: The following points should be noted wrt heuristics
in A* search. f(x)=g(x)+h(x)
• Here, h(x) is called the forward cost, and is an estimate of the
distance of the current node from the goal node. And, g(x) is
called the backward cost, and is the cumulative cost of a node
from the root node. A* search is optimal only when for all
nodes, the forward cost for a node h(x) underestimates the
actual cost h*(x) to reach the goal. This property of A*
heuristic is called admissibility. Admissibility:
0<=h(x)<=h*(x)  
• Strategy: Choose the node with lowest f(x) value.
GAME PLAYING
Game Playing is one of the oldest sub-fields in AI. Game
playing involves abstract and pure form of competition that
seems to require intelligence. It is easy to represent the states
and actions. To implement the game playing very little world
knowledge is required.
• The most common used AI technique in game is search. Game
playing research has contributed ideas on how to make the
best use of time to reach good decisions.
• Game playing is a search problem defined by:
• Initial state of the game
• Operators defining legal moves
• Successor function
• Terminal test defining end of game states
• Goal test
• Path cost/utility/payoff function
• More popular games are too complex to solve, requiring the
program to take its best guess. “ for example in chess, the
search tree has 1040 nodes (with branching factor of 35). It is
the opponent because of whom uncertainty arises.
• Characteristics of game playing
1. There are always an “unpredictable” opponent:
– The opponent introduces uncertainty
– The opponent also wants to win
The solution for this problem is a strategy, which specifies
a move for every possible opponent reply.
2. Time limits:
Game are often played under strict time constraints
(eg:chess) and therefore must be very effectively handled.
Types of games
• There are basically two types of games
Deterministic games
Chance games
• Game like chess and checker are perfect information deterministic games whereas
games like scrabble and bridge are imperfect information. We will consider only
two player discrete, perfect information games, such as tic-tac-toe, chess, checkers
etc... . Two- player games are easier to imagine and think and more common to
play.
• Minimize search procedure
• Typical characteristic of the games is to look ahead at future position in order to
succeed. There is a natural correspondence between such games and state space
problems.
• In a game like tic-tac-toe
States-legal board positions
Operators-legal moves
Goal-winning position
• The game starts from a specified initial state and ends in position that can be
declared win for one player and loss for other or possibly a draw. Game tree is an
explicit representation of all possible plays of the game. We start with a 3 by 3
grid..
Search tree for tic-tac-toe
• The root node is an initial position of the game. Its successors are the positions
that the first player can reach in one move; their successors are the positions
resulting from the second player's replies and so on. Terminal or leaf nodes are
presented by WIN, LOSS or DRAW. Each path from the root ro a terminal
node represents a different complete play of the game. The moves available to
one player from a given position can be represented by OR links whereas the
moves available to his opponent are AND links.
• The trees representing games contain two types of nodes:
1. MAX- nodes (assume at even level from root)
2. MIN - nodes [assume at odd level from root)
• The leaves nodes are labeled WIN, LOSS or DRAW depending on whether
they represent a win, loss or draw position from Max‟s viewpoint. Once the
leaf nodes are assigned their WIN-LOSS or DRAW status, each nodes in the
game tree can be labeled WIN, LOSS or DRAW by a bottom up process.
• Game playing is a special type of search, where the intention of all players
must be taken into account.
INFORMED SEARCH ALGORITHMS

• So far we have talked about the uninformed search


algorithms which looked through search space for all
possible solutions of the problem without having any
additional knowledge about search space. But
informed search algorithm contains an array of
knowledge such as how far we are from the goal, path
cost, how to reach to goal node, etc. This knowledge
help agents to explore less to the search space and find
more efficiently the goal node.
• The informed search algorithm is more useful for
large search space. Informed search algorithm uses the
idea of heuristic, so it is also called Heuristic search.
Heuristics function: Heuristic is a function which
is used in Informed Search, and it finds the most
promising path. It takes the current state of the
agent as its input and produces the estimation of
how close agent is from the goal. The heuristic
method, however, might not always give the best
solution, but it guaranteed to find a good solution
in reasonable time. Heuristic function estimates
how close a state is to the goal. It is represented
by h(n), and it calculates the cost of an optimal
path between the pair of states. The value of the
heuristic function is always positive.
Admissibility of the heuristic function is given as:
h(n) <= h*(n)  
Here h(n) is heuristic cost, and h*(n) is the estimated cost. Hence
heuristic cost should be less than or equal to the estimated cost.
Pure Heuristic Search:
• Pure heuristic search is the simplest form of heuristic search
algorithms. It expands nodes based on their heuristic value h(n).
It maintains two lists, OPEN and CLOSED list. In the CLOSED
list, it places those nodes which have already expanded and in
the OPEN list, it places nodes which have yet not been
expanded.
• On each iteration, each node n with the lowest heuristic value is
expanded and generates all its successors and n is placed to the
closed list. The algorithm continues unit a goal state is found.
1.) Best-first Search Algorithm (Greedy Search):
Greedy best-first search algorithm always selects the path which
appears best at that moment. It is the combination of depth-first
search and breadth-first search algorithms. It uses the heuristic
function and search. Best-first search allows us to take the
advantages of both algorithms. With the help of best-first search,
at each step, we can choose the most promising node. In the best
first search algorithm, we expand the node which is closest to the
goal node and the closest cost is estimated by heuristic function,
f(n)= g(n)
h(n)= estimated cost from node n to the goal.

The greedy best first algorithm is implemented by the priority


queue.
Best first search algorithm:
• Step 1: Place the starting node into the OPEN list.
• Step 2: If the OPEN list is empty, Stop and return failure.
• Step 3: Remove the node n, from the OPEN list which has the
lowest value of h(n), and places it in the CLOSED list.
• Step 4: Expand the node n, and generate the successors of node
n.
• Step 5: Check each successor of node n, and find whether any
node is a goal node or not. If any successor node is goal node,
then return success and terminate the search, else proceed to Step
6.
• Step 6: For each successor node, algorithm checks for evaluation
function f(n), and then check if the node has been in either OPEN
or CLOSED list. If the node has not been in both list, then add it
to the OPEN list.
• Step 7: Return to Step 2.
Advantages:
• Best first search can switch between BFS and DFS by gaining the
advantages of both the algorithms.
• This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
• It can behave as an unguided depth-first search in the worst case
scenario.
• It can get stuck in a loop as DFS.
• This algorithm is not optimal.
Example:
Consider the below search problem, and we will traverse it using
greedy best-first search. At each iteration, each node is expanded
using evaluation function f(n)=h(n) , which is given in the below
table.
In this search example, we are using two lists which
are OPEN and CLOSED Lists. Following are the iteration for
traversing the above example.
• Expand the nodes of S and put in the CLOSED list
Initialization: Open [A, B], Closed [S]
Iteration 1: Open [A], Closed [S, B]
Iteration 2: Open [E, F, A], Closed [S, B]
                  : Open [E, A], Closed [S, B, F]
Iteration 3: Open [I, G, E, A], Closed [S, B, F]
                  : Open [I, E, A], Closed [S, B, F, G]
• Hence the final solution path will be: S----> B----->F----> G
• Time Complexity: The worst case time complexity of Greedy best first
search is O(bm).
• Space Complexity: The worst case space complexity of Greedy best first
search is O(bm). Where, m is the maximum depth of the search space.
• Complete: Greedy best-first search is also incomplete, even if the given
state space is finite.
• Optimal: Greedy best first search algorithm is not optimal.
2.) A* Search Algorithm:
A* search is the most commonly known form of best-first
search. It uses heuristic function h(n), and cost to reach
the node n from the start state g(n). It has combined
features of UCS and greedy best-first search, by which it
solve the problem efficiently. A* search algorithm finds
the shortest path through the search space using the
heuristic function. This search algorithm expands less
search tree and provides optimal result faster. A*
algorithm is similar to UCS except that it uses g(n)+h(n)
instead of g(n).
In A* search algorithm, we use search heuristic as well as the
cost to reach the node. Hence we can combine both costs as
following, and this sum is called as a fitness number.
Algorithm of A* search:
Step1: Place the starting node in the OPEN list.
Step 2: Check if the OPEN list is empty or not, if the list is empty then
return failure and stops.
Step 3: Select the node from the OPEN list which has the smallest value
of evaluation function (g+h), if node n is goal node then return success
and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into
the closed list. For each successor n', check whether n' is already in the
OPEN or CLOSED list, if not then compute evaluation function for n'
and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should
be attached to the back pointer which reflects the lowest g(n') value.
Step 6: Return to Step 2.
Advantages:
• A* search algorithm is the best algorithm than other
search algorithms.
• A* search algorithm is optimal and complete.
• This algorithm can solve very complex problems.
Disadvantages:
• It does not always produce the shortest path as it mostly
based on heuristics and approximation.
• A* search algorithm has some complexity issues.
• The main drawback of A* is memory requirement as it
keeps all generated nodes in the memory, so it is not
practical for various large-scale problems.
Example:
• In this example, we will traverse the given graph using the A*
algorithm. The heuristic value of all states is given in the below
table so we will calculate the f(n) of each state using the formula
f(n)= g(n) + h(n), where g(n) is the cost to reach any node from start
state.
• Here we will use OPEN and CLOSED list.
Solution:
Initialization: {(S, 5)}
Iteration1: {(S--> A, 4), (S-->G, 10)}
Iteration2: {(S--> A-->C, 4), (S--> A-->B, 7), (S-->G, 10)}
Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11), (S-->
A-->B, 7), (S-->G, 10)}
Iteration 4 will give the final result, as S--->A--->C--->G it
provides the optimal path with cost 6.

Points to remember:
• A* algorithm returns the path which occurred first, and it does
not search for all remaining paths.
• The efficiency of A* algorithm depends on the quality of
heuristic.
• A* algorithm expands all nodes which satisfy the condition f(n)
Complete: A* algorithm is complete as long as:
• Branching factor is finite.
• Cost at every action is fixed.
Optimal: A* search algorithm is optimal if it follows below two
conditions:
Admissible: the first condition requires for optimality is that h(n) should
be an admissible heuristic for A* tree search. An admissible heuristic is
optimistic in nature.
Consistency: Second required condition is consistency for only A* graph-
search.
• If the heuristic function is admissible, then A* tree search will always
find the least cost path.
Time Complexity: The time complexity of A* search algorithm depends
on heuristic function, and the number of nodes expanded is exponential
to the depth of solution d. So the time complexity is O(b^d), where b is
the branching factor.
Space Complexity: The space complexity of A* search algorithm
is O(b^d)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy