AI Unit-2
AI Unit-2
RCA-403
Syllabus
UNIT-I INTRODUCTION: - Introduction to Artificial Intelligence, Foundations and
History of Artificial Intelligence, Applications of Artificial Intelligence, Intelligent
Agents, Structure of Intelligent Agents. Computer vision, Natural Language
Possessing.
UNIT-II INTRODUCTION TO SEARCH: - Searching for solutions, uniformed
search strategies, informed search strategies, Local search algorithms and
optimistic problems, Adversarial Search, Search for Games, Alpha - Beta
pruning.
UNIT-III KNOWLEDGE REPRESENTATION & REASONING: - Propositional
logic, Theory of first order logic, Inference in First order logic, Forward &
Backward chaining, Resolution, Probabilistic reasoning, Utility theory, Hidden
Markov Models (HMM), Bayesian Networks.
Syllabus
UNIT-IV MACHINE LEARNING: - Supervised and unsupervised learning,
Decision trees, Statistical learning models, learning with complete data - Naive
Bayes models, Learning with hidden data – EM algorithm, Reinforcement
learning.
Problem solving is the method to reach the desired goal or finding a solution to a
given situation. This method of solving problem through AI involves the process of
defining the search space, deciding start state and goal state and then finding the
path from start state to goal state through search space.
The movement from start state to goal state is guided by set of rules
specifically designed for that particular problem called the production rules. The
production rules are the valid moves described by the problems
Introduction to Search: Problem solving through AI
7 4 2 1 2 3
1 5 8 4
6 3 8 7 6 5
Introduction to Search: Problem solving through AI
Example: 8 Puzzle Problem
States: It specifies the location of each of the 8 tiles and the blank in one of the nice
squares.
Initial state: Any state can be designated as the initial state.
Goal: Many goal configurations are possible one such is shown in the figure
Legal moves ( or state): They generate legal states that result from trying the four
actions-
• Blank moves left
• Blank moves right
• Blank moves up
• Blank moves down
Path cost: Each step costs 1, so the path cost is the number of steps in the path.
Introduction to Search: Problem solving through AI
Example: 8 Puzzle Problem (Search/ State Space Representation)
Introduction to Search: Problem solving through AI
PRODUCTION SYSTEM
Problem Representation in AI
Assumptions:
• We can fill a jug from
the pump.
• We can pour water out
of a jug to the ground.
• We can pour water from
one jug to another.
• There is no measuring
device available.
The production rules for
“WATER-JUG” Problem
are formulated as →
PRODUCTION SYSTEM
Problem Representation in AI
ASSUMPTION: Consider that both the missionaries (M) and cannibals(C) are on
the same side of the river.
PRODUCTION SYSTEM
Problem Representation in AI
The production rules for
“Missionaries &
Cannibals” Problem are
formulated as →
PRODUCTION SYSTEM
Problem Representation in AI
One solution is applying
the rules in the sequence
(5,2,7,10,3,6,3,10,7,10,7).
The solution is presented
in the RHS table →
PRODUCTION SYSTEM
Problem Representation in AI
Water-Jug Problem:
Statement: We are given 2 jugs, a 4-litre one and a 3-litre one. Neither have any
measuring markers on it. There is a pump that can be used to fill the jugs with water.
How can we get exactly 2 litres of water in to the 4-liter jugs?
PRODUCTION SYSTEM
Cryptarithmetic Problem
A Constraint Satisfaction Problem where the digits of some numbers are
represented by letters (or symbols). Each letter represents a unique digit. The goal is
to find the digits such that a given mathematical equation is verified
Constraints are:
• No two letters have same value
• Sum of digits must be as shown in the problem
• There should be only one carry forward
Letter Digit
Example 1: T O
G O T 2
----------------------- O 1
O U T G 8
(1) (0) (2) U 0
Letter Digit
Example 2: S E N D S
M O R E E
_______________________________
N
M O N E Y
D
Example 3: C R O S S M
R O A D S O
________________________________ R
D A N G E R
Y
Example 4: B A S E
B A L L
________________________________
G A M E S
AI & Search Process
Searching can be defined as a sequence of steps that transforms the initial state to
the goal state. The searching process in AI can be broadly classified into two major
types:
1. Brute Force Search or uninformed or blind search.
2. Heuristic Search or informed search.
Measuring Problem-Solving Performance
• Completeness
• Optimality
• Time Complexity
• Space Complexity
AI & Search Process
Uninformed or Blind search or Brute Force Search: Uninformed search
algorithms do not have additional information about state or search space other than
how to traverse the tree, so it is also called blind search.
8-Puzzle Problem
Uninformed Search Techniques
Breadth First Search (BFS)
Advantages:
• Finds the path of minimal length to the goal - If there is more than one solution
then BFS can find the minimal one that requires less number of steps.
• If there is a solution, BFS will definitely find it out.
Disadvantages:
• Amount of memory is proportional to the number of nodes stored,
• If the solution is farther away from the root, breath first search will consume
lot of time.
Exercise 1: Consider the following State
Space Search. Here node ‘A’ is the source or
start or initial node and node ‘G’ is the goal
node. Use a breadth-first search to find G in
the following search tree
Exercise 2:
Uninformed Search Techniques
Performance Measure of Breadth First Search (BFS)
• Uniform-cost search expands nodes in order of their cost from the root.
• Uniform-cost is guided by path cost rather than path length like in BFS
• The algorithms starts by expanding the root, then expanding the node with the
lowest cost from the root, the search continues in this manner for all nodes.
The nodes are stored in a priority queue.
• Uniform Cost Search can also be used as Breadth First Search if all the edges
are given a cost of 1.
Uninformed Search Techniques
Uniform Cost Search (UCS): Consider the following State Space Search
Advantages:
• Guaranteed to find the least-cost solution. Uniform cost search is optimal
because at every state the path with the least cost is chosen.
Disadvantages:
• Exponential storage required.
• Open list must be kept sorted (as a priority queue).
• It does not care about the number of steps involve in searching and only
concerned about path cost. Due to which this algorithm may be stuck in an
infinite loop.
Uninformed Search Techniques
Performance Measure of Uniform Cost Search (UCS)
• Completeness: It is obvious that UCS is complete if the cost of each step
exceeds some small positive integer, this to prevent infinite loops..
• Optimality: UCS is always optimal in the sense that the node that it always
expands is the node with the least path cost.
• Space complexity: UCS is guided by path cost rather than path length so it is
hard to determine its complexity in terms of b and d, so if we consider C to be
the cost of the optimal solution, and every action costs at least e, then the
algorithm worst case is O(bC/e).
• Time Complexity: O(bC/e)
• DFS progresses by expanding the first child node of the search tree that
appears and thus going deeper and deeper until a goal node is found, or until it
hits a node that has no children. Then the search backtracks, returning to the
most recent node it hasn’t finished exploring. DFS investigated in 19th century
by French Mathematician Charles Pierre as a strategy for solving mazes.
• DFS is implemented using STACK (Last In First Out)
Uninformed Search Techniques
Depth First Search (DFS): Consider the following State Space Search
A
B C
B F G
B F M
B F
B K L
Advantages:
• DFS consumes very less memory space
Disadvantages:
• There is no guarantee of finding the goal node
Uninformed Search Techniques
Performance Measure of Depth First Search(DFS)
Note: b is branching factor and l is the limited depth (l < d) as d is depth of tree
Uninformed Search Techniques
Depth-First Iterative Deepening Search
• It is a search strategy resulting when you combine BFS and DFS, thus
combining the advantages of each strategy, taking the completeness and
optimality of BFS and the modest memory requirements of DFS.
• IDS works by looking for the best search depth d, thus starting with depth
limit 0 and make a BFS and if the search failed it increase the depth limit by 1
and try a BFS again with depth 1 and so on – first d = 0, then 1 then 2 and so
on – until a depth d is reached where a goal is found.
Uninformed Search Techniques
Depth-First Iterative Deepening Search Example:
Uninformed Search Techniques
Performance Measure of Depth-First Iterative Deepening Search (IDS)
S→A→C, 5
S→B→F→G1,5
S→B→F→G1,5 S→B→F→I, 6
F
S→B→F→I, 6 S→A→D, 6
S→B→E→G1, 6
S→B→E→H, 7
Heuristic (Informed) Search Techniques
Example 2
Path is SACBHI
Heuristic (Informed) Search Techniques
Example 3: Consider the given state space graph. Find the path and path cost
using Best First Search
Heuristic (Informed) Search Techniques
Example 4: Find the path using Best First Search!!!
Heuristic (Informed) Search Techniques
Example 5: Find the path using Best First Search!!!
Heuristic (Informed) Search Techniques
Best First Search:
Advantages:
• It is more efficient than that of BFS and DFS.
• Time complexity of Best first search is much less than Breadth first search.
• The Best first search allows us to switch between paths by gaining the
benefits of both breadth first and depth first search. Because, depth first is
good because a solution can be found without computing all nodes and
Breadth first search is good because it does not get trapped in dead ends.
Disadvantages:
• Sometimes, it covers more distance than our consideration
Heuristic (Informed) Search Techniques
A* Algorithm
• Starting from a specific starting node of a graph, it aims to find a path to the given
goal node having the smallest cost
• A* Algorithm is the specialization of Best First Search in which the cost associated
with a node is f(n) = g(n) + h(n), where g(n) is the cost of the path from the initial
state to node n and h(n) is the heuristic estimate or the cost or a path from node n to
a goal. Thus, f(n) estimates the lowest total cost of any solution path going through
node n. At each point, a node with lowest f value is chosen for expansion.
• A* algorithm guides an optimal path to a goal if the heuristic function h(n) is
admissible, meaning it never overestimates actual cost
• The * represents that the algorithm is admissible as it guarantees to give optimal
solution.
8 Puzzle Problem:
Example of
Heuristic Value/
Function
Heuristic (Informed) Search Techniques
A* Algorithm
Where,
• f(n) = evaluation function.
• g(n) = actual cost of current node from start node.
• h(n) = heuristic value i.e., estimated cost of current node from goal node.
Heuristic (Informed) Search Techniques
A* Algorithm: Consider the following graph, the numbers written on edges
represent the distance between the nodes and the numbers written on nodes
represent the heuristic value.
Find the most cost-effective path to reach from start state A to final state J using A*
Algorithm.
Heuristic (Informed) Search Techniques
A* Algorithm: Consider the following graph, the numbers written on edges
represent the distance between the nodes and the numbers written on nodes
represent the heuristic value.
Find the most cost-effective path to reach from start state S to final state G using
A* Algorithm.
Example 1
Example 2: Apply the steps of A* algorithm to find the shortest path from A to Z
Example 3: Apply the steps of A* algorithm to find the shortest path from S to F or M
Heuristic (Informed) Search Techniques
A* Algorithm:
Advantages:
• It is complete and optimal.
• It is the best algorithm, there is no other optimal algorithm guaranteed to
expand fewer nodes than A*
Disadvantages:
• Although being the best path finding algorithms, but A* search algorithm
does not produce the shortest path always because it heavily depends/ relies
on heuristics.
Heuristic (Informed) Search Techniques
A* Algorithm:
Advantages:
• It is complete and optimal.
• It is the best algorithm, there is no other optimal algorithm guaranteed to
expand fewer nodes than A*
Disadvantages:
• Although being the best path finding algorithms, but A* search algorithm
does not produce the shortest path always because it heavily depends/ relies
on heuristics.
Admissibility of A* Algorithm
J
(1)
I
(1)
Heuristic (Informed) Search Techniques
AO* Algorithm:
Advantages:
• It is an efficient method to explore a solution path
• Solution is guaranteed by using AO* algorithm
Disadvantages:
• Sometimes for unsolvable nodes, it can’t find the optimal path. Also, AO*
does not explore all the solution path once it got solution
Adversarial Search
It relates to competitive environment in which the agent goals are conflict giving
rise to adversarial search.
There are two methods for game playing:
1. Min-Max Procedure
2. Alpha-Beta Pruning (Cut-offs)
Min-Max Search: Min-Max strategy is a simple strategy for two player game.
Here, one player is called “maximizer” and the other called “minimizer.
Maximizer tries to maximize its score while minimizer tries to minimize
maximizer’s score. The minimax algorithm performs a depth-first search algorithm
for the exploration of the complete game tree.
It is also assumed that the maximizer makes the first move (not essential, as
minimizer can also make first move). The maximizer, always tries to go a position
where the static evaluation function value is the maximum positive value.
Adversarial Search
Min-Max Search: Consider the following tree graph example
The maximizer being the player to make the first move, and will move to node D
because the static evaluation function value for that is maximum. . The same above
figure shows that if the minimizer has to make the first move, it will go to node B
because the static evaluation function value at that node will be advantageous to it.
Once the static evaluation function is applied at the leaf nodes, backing up values
can begin. First we compute the backed-up values at the parents of the leaves.
Step-1: In the first step, the algorithm Working of Min-Max Algorithm
generates the entire game-tree and apply the
utility function to get the utility values for the
terminal states. In the given tree 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
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
Adversarial Search - Alpha-Beta Pruning
The method that we are going to look in this article is called alpha-beta pruning. If
we apply alpha-beta pruning to a standard minimax algorithm, it returns the same
move as the standard one, but it removes (prunes) all the nodes that are possibly not
affecting the final decision.
Alpha-Beta pruning is a search algorithm that seeks to decrease the number
of nodes that are evaluated by mini-max algorithm in its search tree.
• α is a value which is best for Max player (highest value)
• β is a value which is best for Min player (lowest value)
Each node will keep its α-β values, and pruning done by following way:
• For Min node, if β<= α of max ancestor, PRUNE.
• For Max node, if β>= α of min ancestor, PRUNE.
Working of Alpha-Beta Pruning
Step 4: At node E, Max will take its turn, and the value of alpha will change. The
current value of alpha will be compared with 5, so max (-∞, 5) = 5, hence at node E α= 5
and β= 3, where α>=β, so the right successor of E will be pruned, and algorithm will not
traverse it, and the value at node E will be 5.
Step 5: At next step, algorithm again backtrack
the tree, from node B to node A. At node A, the
value of alpha will be changed the maximum
available value is 3 as max (-∞, 3)= 3, and β=
+∞, these two values now passes to right
successor of A which is Node C.
At node C, α=3 and β= +∞, and the same values
will be passed on to node F.
Step 6: At node F, again the value of α will be
compared with left child which is 0, and
max(3,0)= 3, and then compared with right
child which is 1, and max(3,1)= 3 still α
remains 3, but the node value of F will become
1.
Step 7: Node F returns the node value 1 to node
C, at C α= 3 and β= +∞, here the value of beta
will be changed, it will compare with 1 so min
(∞, 1) = 1. Now at C, α=3 and β= 1, and again it
satisfies the condition α>=β, so the next child of
C which is G will be pruned, and the algorithm
will not compute the entire sub-tree G.
Step 8: C now returns the value of 1 to A here
the best value for A is max (3, 1) = 3. Following
is the final game tree which is the showing the
nodes which are computed and nodes which has
never computed. Hence the optimal value for
the maximizer is 3 for this example.
Adversarial Search - Alpha-Beta Pruning
Consider the following state space graph, what’s the value of Root?
Adversarial Search - Alpha-Beta Pruning
The optimal value of the maximizer will be 3.
Example 1: What nodes could have been pruned from the search
using alpha-beta pruning
Example 2: What nodes could have been pruned from the search
using alpha-beta pruning
Example 3: Consider the following state space graph