FAI Module 2
FAI Module 2
Optimality: If a solution found for an algorithm is guaranteed to be the best solution (lowest path
cost) among all other solutions, then such a solution for is said to be an optimal solution.
Time Complexity: Time complexity is a measure of time for an algorithm to complete its task.
Space Complexity: It is the maximum storage space required at any point during the search, as the
complexity of the problem.
Types of search algorithms
Based on the search problems we can classify the search algorithms into uninformed (Blind search)
search and informed search (Heuristic search) algorithms.
Uninformed/Blind Search:
The uninformed search does not contain any domain knowledge such as closeness, the location of the
goal. It operates in a brute-force way as it only includes information about how to traverse the tree
and how to identify leaf and goal nodes. Uninformed search applies a way in which search tree is
searched without any information about the search space like initial state operators and test for the
goal, so it is also called blind search. It examines each node of the tree until it achieves the goal node.
Informed search algorithms use domain knowledge. In an informed search, problem information is
available which can guide the search. Informed search strategies can find a solution more efficiently
than an uninformed search strategy. Informed search is also called a Heuristic search.
A heuristic is a way which might not always be guaranteed for best solutions but guaranteed to find a
good solution in reasonable time.
Informed search can solve much complex problem which could not be solved in another way.
1. Greedy Search
2. A* Search
Random Search:
Random search is a technique where random combinations of the hyper parameters 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. Below is a visual description of search pattern of the random
search.
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 hyper parameters 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.
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.
Example of 8 Puzzle Problem using :-
Depth – First – Search traversal and Breadth - First - Search traversal :
Uninformed Search Methods:
Breadth- First -Search: Breadth First Search
Algorithm
Breadth first search
if fringe is empty
return failure Node
remove-first
(fringe)ifNode is agoal
BFS Illustrated
We will now consider the search space in Figure1, 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
Step2: 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
FRINGE: BC
Step3: Node B is removed from fringe and is expanded. Its children D, E are generated
And put at the back of fringe.
A
B C
D E D G
C F B F
G G H E G H
FRINGE: C DE
Step4: 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 EDG
Step5: Node D is removed from fringe. Its children C and F are generated and added
to the back of fringe.
A
B C
D E D G
C F B F
G G H E H
FRINGE:E DGC F
B C
D E D G
C F B F
G G H E G H
FRINGE:DG CF
B C
D E D G
C F B F
G G H E G H
FRINGE:GCFBF
A
B C
D E D G
C F B F
Goal!
G G H E G H
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
A
B C
D E D G
C F B F
G G H E G H
B C
D E D G
C F B F
G G H E G H
FRINGE:A
B C
D E D G
C F B F
G G H E G H
FRINGE:B C
Step3: 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:DE C
Step4: Node D is removed from fringe. C and F are pushed in front of fringe.
B C
D E D G
C F B F
G G H E G H
Fringe:CFEC
B C
D E D G
C F B F
G G H E G H
FRINGE:G FE C
Step6: 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.
B C
D E D G
C F B F
G G H E G H
Goal!
FRINGE:GFEC
HeuristicSearch
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 until a goal state is found.
In the informed search we will discuss two main algorithms which are given below:
• Best First Search Algorithm
• A* Search Algorithm
2 8 3 1 2 3
1 6 4 8 4
7 5 7 6 5
Figure: 8puzzle
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.
//
Let us consider the below example.
A* Algorithm Concepts and Implementation
What is an A* Algorithm?
It is a searching algorithm that is used to find the shortest path between an initial and a
final point.
It is a handy algorithm that is often used for map traversal to find the shortest path to be
taken. A* was initially designed as a graph traversal problem, to help build a robot that
can find its own course. It still remains a widely popular algorithm for graph traversal.
It searches for shorter paths first, thus making it an optimal and complete algorithm. An
optimal algorithm will find the least cost outcome for a problem, while a complete
algorithm finds all the possible outcomes of a problem.
Another aspect that makes A* so powerful is the use of weighted graphs in its
implementation. A weighted graph uses numbers to represent the cost of taking each
path or course of action. This means that the algorithms can take the path with the least
cost, and find the best route in terms of distance and time.
A heuristic algorithm sacrifices optimality, with precision and accuracy for speed, to solve
problems faster and more efficiently.
All graphs have different nodes or points which the algorithm has to take, to reach the final
node. The paths between these nodes all have a numerical value, which is considered as the
weight of the path. The total of all paths transverse gives you the cost of that route.
Initially, the Algorithm calculates the cost to all its immediate neighbouring nodes, n, and
chooses the one incurring the least cost. This process repeats until no new nodes can be chosen
and all paths have been traversed. Then, you should consider the best path among them. If f(n)
represents the final cost, then it can be denoted as :
g(n) = cost of traversing from one node to another. This will vary from node to node
h(n) = heuristic approximation of the node's value. This is not a real value but an approximation
cost
Consider the weighted graph depicted above, which contains nodes and the distance between
them. Let's say you start from A and have to go to D.
Now, since the start is at the source A, which will have some initial heuristic value. Hence, the
results are
f(A) = g(A) + h(A)
f(A) = 0 + 6 = 6
f(A-B) = 1 + 4
f(A-C) = 5 + 2
Now take the path to the destination from these nodes, and calculate the weights :
f(A-B-D) = (1+ 7) + 0
f(A-C-D) = (5 + 10) + 0
It is clear that node B gives you the best path, so that is the node you need to take to reach the
destination.
The text below represents the pseudo code of the Algorithm. It can be used to implement the
algorithm in any programming language and is the basic logic behind the Algorithm.
• If it does not reach the destination node, then consider a node with the lowest f-score in the
open list
We are finished
• Else :
Put the current node in the list and check its neighbours
• If the neighbour has a lower g value than the current node and is in the closed list:
Replace neighbour with the lower g value and change the neighbour’s parent to the current node.
Step2: 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
Step3: 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
Step4: 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 than4layers.
For node A max(4,-3)=4
That was the complete work flow 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'sturn)
return minimal score of calling minimax on all the children
Properties of Mini-Max Algorithm:
• Complete- Min-Max algorithm is Complete. It will definitely
find a solution (if exist), in the finite search tree.
• Optimal- Min-Max algorithm is optimal if both opponents
are playing optimally.
• Time complexity- As it performs DFS for the game-tree, so the
time complexity of Min-Max algorithm is O(bm), where b is
branching factor of the game-tree, and m is the maximum depth
of the tree.
• Space Complexity- Space complexity of Mini-max algorithm is
also similar to DFS which is O(bm).