FAI Module - 2
FAI Module - 2
Module - 2
Search Algorithms
Search
We will now outline the basic search algorithm, and then consider various
variations of this algorithm.
if Node is a goal
1
FAI Module - 2
Initially, the fringe contains a single node corresponding to the start state. In
this version we use only the OPEN list or fringe. The algorithm always picks
the first node from fringe for expansion. If the node contains a goal state, the
2
FAI Module - 2
path to the goal is returned. The path corresponding to a goal node can be
found by following the parent pointers. Otherwise all the successor nodes
are generated and they are added to the fringe.
The successors of the current expanded node are put in fringe. We will soon
see that the order in which the successors are put in fringe will determine the
property of the search algorithm.
We see that in the basic search algorithm, we have to select a node for
expansion. Which node should we select? Alternatively, how would we
place the newly generated nodes in the fringe? We will subsequently explore
various search strategies and discuss their properties,
Depending on the search problem, we will have different cases. The search
graph may be weighted or unweighted. In some cases we may have some
knowledge about the quality of intermediate states and this can perhaps be
exploited by the search algorithm. Also depending on the problem, our aim
may be to find a minimal cost path or any to find path as soon as possible.
3
FAI Module - 2
We will look at various search strategies and evaluate their problem solving
4
FAI Module - 2
2. Optimality: Does the solution have low cost or the minimal cost?
3. What is the search cost associated with the time and memory
required to find a solution?
a. Time complexity: Time taken (number of nodes expanded
b. Space complexity: Space used by the algorithm measured in
terms of the maximum size of fringe
Random Search
Random search is a technique where random combinations of the hyperparameters are used to
find the best solution for the built model. It is similar to grid search, and yet it has proven to
yield better results comparatively. The drawback of random search is that it yields high
variance during computing. Since the selection of parameters is completely random; and since
no intelligence is used to sample these combinations, luck plays its part.
5
FAI Module - 2
As random values are selected at each instance, it is highly likely that the whole of action space
has been reached because of the randomness, which takes a huge amount of time to cover
every aspect of the combination during grid search. This works best under the assumption that
not all hyperparameters are equally important. In this search pattern, random combinations of
parameters are considered in every iteration. The chances of finding the optimal parameter are
comparatively higher in random search because of the random search pattern where the model
might end up being trained on the optimised parameters without any aliasing.
The search problem will return as a solution a path to a goal node. Finding a
path is important in problems like path finding, solving 15-puzzle, and such
other problems. There are also problems like the N-queens problem for
which the path to the solution is not important. For such problems the search
problem needs to return the goal state only.
6
FAI Module - 2
Algorithm
The depth first search algorithm puts newly generated nodes in the front of
OPEN. This results in expanding the deepest node first. Thus the nodes in
OPEN follow a LIFO order (Last In First Out). OPEN is thus implemented
using a stack data structure.
DFS illustrated
B E
A D F H
C
G
7
FAI Module - 2
B C
D E D G
C F B F
G G H E G H
Let us now run Depth First Search on the search space given in Figure 34,
and trace its progress.
B C
D E D G
C F B F
G G H E G H
FRINGE: A
8
FAI Module - 2
B C
D E D G
C F B F
G G H E G H
FRINGE: B C
Step 3: Node B is removed from fringe, and its children D and E are pushed
in front of fringe.
A
B C
D E D G
C F B F
G G H E G H
FRINGE: D E C
Step 4: Node D is removed from fringe. C and F are pushed in front of fringe.
9
FAI Module - 2
B C
D E D G
C F B F
G G H E G H
Fringe : C F E C
B C
D E D G
C F B F
G G H E G H
FRINGE: G F E C
Step 6: Node G is expanded and found to be a goal node. The solution path
A-B-D-C-G is returned and the algorithm terminates.
10
FAI Module - 2
B C
D E D G
C F B F
G G H E G H
Goal!
FRINGE: G F E C
• Note that the time taken by the algorithm is related to the maximum
depth of the search tree. If the search tree has infinite depth, the
algorithm may not terminate. This can happen if the search space is
infinite. It can also happen if the search space contains cycles. The
latter case can be handled by checking for cycles in the algorithm.
Thus Depth First Search is not complete.
11
FAI Module - 2
Algorithm
if fringe is empty
return failure Node
remove-first
(fringe)if Node is agoal
12
FAI Module - 2
BFS illustrated
We will now consider the search space in Figure 1, and show how breadth
first search works on this graph.
Step 1: Initially fringe contains only one node corresponding to the source
state A.
B C
D E D G
C F B F
G G H E G H
FRINGE: A
Step 2: A is removed from fringe. The node is expanded, and its children B
and C are generated. They are placed at the back of fringe.
B C
D E D G
C F B F
G G H E G H
13
FAI Module - 2
FRINGE: B C
B C
D E D G
C F B F
G G H E G H
FRINGE : C D E
14
FAI Module - 2
Step 4: Node C is removed from fringe and is expanded. Its children D and
G are added to the back of fringe.
B C
D E D G
C F B F
G G H E G H
FRINGE: D E D G
Step 5: Node D is removed from fringe. Its children C and F are generated
and added to the back of fringe.
15
FAI Module - 2
B C
D E D G
C F B F
G G H E H
G
FRINGE: E D G C F
B C
D E D G
C F B F
G G H E G H
FRINGE: D G C F
B C
D E D G
C F B F
G G H E G H
16
FAI Module - 2
FRINGE: G C F B F
17
FAI Module - 2
B C
D E D G
C F B F
Goal!
G G H E G H
….
b children
….
18
FAI Module - 2
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.
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.
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.
In the informed search we will discuss two main algorithms which are given below:
19
FAI Module - 2
o A* Search Algorithm
20
FAI Module - 2
2 8 3 1 2 3
1 6 4 8 4
7 5 7 6 5
Initial State Goal state
Figure 1: 8 puzzle
The first picture shows the current state n, and the second picture the goal
state.
h(n) = 5
because the tiles 2, 8, 1, 6 and 7 are out of place.
21
FAI Module - 2
22
FAI Module - 2
Uniform Cost Search is a special case of the best first search algorithm. The
algorithm maintains a priority queue of nodes to be explored. A cost
function f(n) is applied to each node. The nodes are put in OPEN in the
order of their f values. Nodes with smaller f(n) values are expanded earlier.
The generic best first search algorithm is outlined below.
Greedy Search
In greedy search, the idea is to expand the node with the smallest estimated
cost to reach the goal.
Greedy algorithms often perform very well. They tend to find good
solutions quickly, although not always optimal ones.
23
FAI Module - 2
Figure : 1
Figure : 2
24
FAI Module - 2
Let us run the greedy search algorithm for the graph given in Figure 1. The
straight line distance heuristic estimates for the nodes are shown in Figure
2.
10.4 8.9
A D
10.4 8.9
D
A
10.4 6.9
A E
25
S
FAI 8.9 Module - 2
10.4
D
A
10.4 6.9
A E
6.7 3.0
B F
0
G
Goal H
Goal
15
15
D G
112
70 7
12 E
E
12
F
10
6 9
8 B 10
Start C
We will run greedy best first search on the problem in Figure 2. We use the
straight line heuristic. h(n) is taken to be the straight line distance from n to
the goal position.
The Nodes will be expanded in the following order
A-B-E-G-H
26
FAI Module - 2
A* Search
We will next consider the famous A* algorithm. This algorithm was
given by Hart, Nilsson & Rafael in 1968.
A* Searching Process:
27
FAI Module - 2
A* Search Implementation
A* Search Algorithm is one such algorithm that has been developed to help
us. A* was initially designed as a general graph traversal algorithm. It is
widely used in solving path finding problems in video games. Because of its
flexibility and versatility, it can be used in a wide range of contexts. A* is
formulated with weighted graphs, which means it can find the best path
involving the smallest cost in terms of distance and time. This makes A*
algorithm in artificial intelligence an informed search algorithm for best-first
search. Let us have a detailed look into the various aspects of A*.
28
FAI Module - 2
What is A* Algorithm
29
FAI Module - 2
To solve the problem, we need to map the intersections to the nodes (denoted
by the red dots) and all the possible ways we can make movements towards the
edges (denoted by the blue lines).
A denotes the starting point and B denotes the endpoint. We define the starting
and endpoint at the nodes A and B respectively.
If we use an uninformed search algorithm, it would be like finding a path that
is blind, while an informed algorithm for a search problem would take the path
that brings you closer to your destination. For instance, consider Rubik’s cube;
it has many prospective states that you can be in and this makes the solution
very difficult. This calls for the use of a guided search algorithm to find a
solution. This explains the importance of A*.
Unlike other algorithms, A* decides to take up a step only if it is convincingly
sensible and reasonable as per its functions. Which means, it never considers
any non-optimal steps. This is why A* is a popular choice for AI systems that
replicate the real world – like video games and machine learning.
The task is to take the unit you see at the bottom of the diagram, to the top of
it. You can see that there is nothing to indicate that the object should not take
the path denoted with pink lines. So it chooses to move that way. As and when
30
FAI Module - 2
it reaches the top it has to change its direction because of the ‘U’ shaped
obstacle. Then it changes the direction, goes around the obstacle, to reach the
top. In contrast to this, A* would have scanned the area above the object and
found a short path (denoted with blue lines). Thus, pathfinder algorithms like
A* help you plan things rather than waiting until you discover the problem.
They act proactively rather than reacting to a situation. The disadvantage is
that it is a bit slower than the other algorithms. You can use a combination of
both to achieve better results – pathfinding algorithms give bigger picture and
long paths with obstacles that change slowly; and movement algorithms for
local picture and short paths with obstacles that change faster.
Algorithm A*
OPEN = nodes on frontier. CLOSED =
expanded nodes. OPEN = {<s, nil>}
while OPEN is not empty
remove from OPEN the node <n,p> with minimum f(n)
place <n,p> on CLOSED
if n is a goal node,
return success (path p)
for each edge connecting n & m with cost c
if <m, q> is on CLOSED and {p|e} is cheaper than q
then remove n from
CLOSED, put
<m,{p|e}> on OPEN
else if <m,q> is on OPEN and {p|e} is cheaper than q
then replace q with {p|e}
else if m is not on OPEN
then put <m,{p|e}> on OPEN
return failure
Points to remember:
o A* algorithm returns the path which occurred first, and it does not
search for all remaining paths.
o The efficiency of A* algorithm depends on the quality of heuristic.
31
FAI Module - 2
32
FAI Module - 2
Hill climbing algorithm is a local search algorithm which continuously moves in the
direction of increasing elevation/value to find the peak of the mountain or best solution
to the problem. It terminates when it reaches a peak value where no neighbor has a
higher value.
o Hill climbing algorithm is a technique which is used for optimizing the mathematical problems. One of
the widely discussed examples of Hill climbing algorithm is Traveling-salesman Problem in which we
need to minimize the distance traveled by the salesman.
o It is also called greedy local search as it only looks to its good immediate neighbor state and not beyond
that.
o A node of hill climbing algorithm has two components which are state and value.
o Hill Climbing is mostly used when a good heuristic is available.
o In this algorithm, we don't need to maintain and handle the search tree or graph as it only keeps a single
current state.
o Generate and Test variant: Hill Climbing is the variant of Generate and Test method. The Generate
and Test method produce feedback which helps to decide which direction to move in the search space.
o Greedy approach: Hill-climbing algorithm search moves in the direction which optimizes the cost.
o No backtracking: It does not backtrack the search space, as it does not remember the previous states.
On Y-axis we have taken the function which can be an objective function or cost function, and state-
space on the x-axis. If the function on Y-axis is cost then, the goal of search is to find the global
minimum and local minimum. If the function of Y-axis is Objective function, then the goal of the
search is to find the global maximum and local maximum.
33
FAI Module - 2
Global Maximum: Global maximum is the best possible state of state space landscape. It has the
highest value of objective function.
Flat local maximum: It is a flat space in the landscape where all the neighbor states of current states
have the same value.
34
FAI Module - 2
o Step 1: Evaluate the initial state, if it is goal state then return success and Stop.
o Step 2: Loop Until a solution is found or there is no new operator left to apply.
o Step 3: Select and apply an operator to the current state.
o Step 4: Check new state:
a. If it is goal state, then return success and quit.
b. Else if it is better than the current state then assign new state as a current state.
c. Else if not better than the current state, then return to step2.
Step 5: Exit.
o Step 1: Evaluate the initial state, if it is goal state then return success and stop, else make current state
as initial state.
o Step 2: Loop until a solution is found or the current state does not change.
a. Let SUCC be a state such that any successor of the current state will be better than it.
b. For each operator that applies to the current state:
a. Apply the new operator and generate a new state.
b. Evaluate the new state.
c. If it is goal state, then return it and quit, else compare it to the SUCC.
d. If it is better than SUCC, then set new state as SUCC.
e. If the SUCC is better than the current state, then set current state to SUCC.
Step 5: Exit.
35
FAI Module - 2
Stochastic hill climbing does not examine for all its neighbor before moving. Rather, this search
algorithm selects one neighbor node at random and decides whether to choose it as a current state or
examine another state.
Solution: Backtracking technique can be a solution of the local maximum in state space landscape.
Create a list of the promising path so that the algorithm can backtrack the search space and explore
other paths as well.
2. Plateau: A plateau is the flat area of the search space in which all the neighbor states of the current
state contains the same value, because of this algorithm does not find any best direction to move. A
hill-climbing search might be lost in the plateau area.
Solution: The solution for the plateau is to take big steps or very little steps while searching, to solve
the problem. Randomly select a state which is far away from the current state so it is possible that the
algorithm could find non-plateau region.
36
FAI Module - 2
3. Ridges: A ridge is a special form of the local maximum. It has an area which is higher than its
surrounding areas, but itself has a slope, and cannot be reached in a single move.
Solution: With the use of bidirectional search, or by moving in different directions, we can improve
this problem.
Simulated Annealing:
A hill-climbing algorithm which never makes a move towards a lower value guaranteed to be
incomplete because it can get stuck on a local maximum. And if algorithm applies a random walk, by
moving a successor, then it may complete but not efficient. Simulated Annealing is an algorithm
which yields both efficiency and completeness.
In mechanical term Annealing is a process of hardening a metal or glass to a high temperature then
cooling gradually, so this allows the metal to reach a low-energy crystalline state. The same process is
used in simulated annealing in which the algorithm picks a random move, instead of picking the best
move. If the random move improves the state, then it follows the same path. Otherwise, the algorithm
follows the path which has a probability of less than 1 or it moves downhill and chooses another path.
37
FAI Module - 2
CSP:
• state is defined by variables Xi with values from domain Di
• goal test is a set of constraints specifying allowable combinations
of values for subsets of variables
Allows useful general-purpose algorithms with more power than standard search
algorithms
Example: Map-Coloring
Varieties of CSPs
✓ Discrete variables
✓ finite domains:
✓ infinite domains:
✓ e.g., job scheduling, variables are start/end days for each job
✓ Continuous variables
39
FAI Module - 2
Real-world CSPs
✓ Assignment problems
✓ Timetabling problems
✓ Factory scheduling
Game Playing
Mini-Max Algorithm
40
FAI Module - 2
Step-1: In the first step, the algorithm generates the entire game-tree and apply the
utility function to get the utility values for the terminal states. In the below tree
41
FAI Module - 2
diagram, let's take A is the initial state of the tree. Suppose maximizer takes first
turn which has worst-case initial value =- infinity, and minimizer will take next
turn which has worst-case initial value = +infinity.
Step 2: Now, first we find the utilities value for the Maximizer, its initial value is -
∞, so we will compare each value in terminal state with initial value of Maximizer
and determines the higher nodes values. It will find the maximum among the all.
For node D max(-1,- -∞) => max(-1,4)= 4
For Node E max(2, -∞) => max(2, 6)= 6
For Node F max(-3, -∞) => max(-3,-5) = -3
For node G max(0, -∞) = max(0, 7) = 7
Step 3: In the next step, it's a turn for minimizer, so it will compare all nodes value
with +∞, and will find the 3rd layer node values.
For node B= min(4,6) = 4
For node C= min (-3, 7) = -3
42
FAI Module - 2
Step 4: Now it's a turn for Maximizer, and it will again choose the maximum of all
nodes value and find the maximum value for the root node. In this game tree, there
are only 4 layers, hence we reach immediately to the root node, but in real games,
there will be more than 4 layers.
For node A max(4, -3)= 4
That was the complete workflow of the minimax two player game
Algorithm:
minimax(player,board)
if(game over in current board position)
return winner
children = all legal moves for player from this board
if(max's turn)
return maximal score of calling minimax on all the children else
(min's turn)
43
FAI Module - 2
44
FAI Module - 2
45