Unit 2
Unit 2
Unit 2
Solutions
• 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.
AO * Algorithm
Step-1: Create an initial graph with a single node (start node).
Step-2: Transverse the graph following the current path, accumulating
node that has not yet been expanded or solved.
Step-3: Select any of these nodes and explore it. If it has no successors
then call this value- FUTILITY else calculate f'(n) for each of the
successors.
Step-4: If f'(n)=0, then mark the node as SOLVED.
Step-5: Change the value of f'(n) for the newly created node to reflect its
successors by back propagation.
Step-6: Whenever possible use the most promising routes, If a node is
marked as SOLVED then mark the parent node as SOLVED.
Step-7: If the starting node is SOLVED or value is greater
than FUTILITY then stop else repeat from Step-2.
Informed Search Algorithms
Hill Climbing Algorithm in Artificial Intelligence:-
• 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
neighbour has a higher value
Different regions in the state space landscape:
• Local Maximum: Local maximum is a state which is better than its neighbor states,
but there is also another state which is higher than it.
• Global Maximum: Global maximum is the best possible state of state space
landscape. It has the highest value of objective function.
• Current state: It is a state in a landscape diagram where an agent is currently
present.
• Flat local maximum: It is a flat space in the landscape where all the neighbor states
of current states have the same value.
• Shoulder: It is a plateau region which has an uphill edge.
Informed Search Algorithms
Types of Hill Climbing Algorithm:
• Simple hill Climbing:
• Steepest-Ascent hill-climbing:
• Stochastic hill Climbing:
1. Simple Hill Climbing:
• Simple hill climbing is the simplest way to implement a hill climbing algorithm. It
only evaluates the neighbor node state at a time and selects the first one which
optimizes current cost and set it as a current state. It only checks it's one
successor state, and if it finds better than the current state, then move else be in the
same state. This algorithm has the following features:
Step 1: Evaluate the initial state, if it is goal state then return success and Stop.
Step 2: Loop Until a solution is found or there is no new operator left to apply.
Step 3: Select and apply an operator to the current state.
Step 4: Check new state:
If it is goal state, then return success and quit.
Else if it is better than the current state then assign new state as a current state.
Else if not better than the current state, then return to step2.Step 5: Exit.
Informed Search Algorithms
Steepest-Ascent hill climbing:
• The steepest-Ascent algorithm is a variation of simple hill climbing algorithm. This
algorithm examines all the neighboring nodes of the current state and selects one
neighbor node which is closest to the goal state. This algorithm consumes more
time as it searches for multiple neighbors
Step 1: Evaluate the initial state, if it is goal state then return success and stop, else
make current state as initial state.
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.
Informed Search Algorithms
3. Stochastic hill climbing:
• 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.
Problems in Hill Climbing Algorithm:
1. Local Maximum: A local maximum is a peak state in the landscape which is better
than each of its neighboring states, but there is another state also present which is
higher than the local maximum.
• 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.
Informed Search Algorithms
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.
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.
Adversarial Search
• Searches in which two or more players with conflicting goals are trying to explore
the same search space for the solution, are called adversarial searches, often known
as Games.
Game tree:
• A game tree is a tree where nodes of the tree are the game states and Edges of the
tree are the moves by players. Game tree involves initial state, actions function, and
result Function.
Example: Tic-Tac-Toe game tree:
• The following figure is showing part of the game-tree for tic-tac-toe game.
Following are some key points of the game:
• There are two players MAX and MIN.
• Players have an alternate turn and start with MAX.
• MAX maximizes the result of the game tree
• MIN minimizes the result
Mini-Max Algorithm in Artificial Intelligence
• Min-Max algorithm is mostly used for game playing in AI. Such as Chess,
Checkers, tic-tac-toe, go, and various tow-players game. This Algorithm computes
the minimax decision for the current state.
• In this algorithm two players play the game, one is called MAX and other is called
MIN.
• Both the players fight it as the opponent player gets the minimum benefit while they
get the maximum benefit.
• Both Players of the game are opponent of each other, where MAX will select the
maximized value and MIN will select the minimized value.
• The minimax algorithm performs a depth-first search algorithm for the exploration
of the complete game tree.
• The minimax algorithm proceeds all the way down to the terminal node of the tree,
then backtrack the tree as the recursion.
Working of Min-Max Algorithm
Step 1:
Continue……………….
• Step2:
Continue…………..
• Step 3:
• Step 4:
Alpha-Beta Pruning
• The main drawback of the minimax algorithm is that it gets really slow for complex
games such as Chess, go, etc. This type of games has a huge branching factor, and
the player has lots of choices to decide. This limitation of the minimax algorithm
can be improved from alpha-beta pruning
• there is a technique by which without checking each node of the game tree we can
compute the correct minimax decision, and this technique is called pruning. This
involves two threshold parameter Alpha and beta for future expansion, so it is
called alpha-beta pruning.
• The two-parameter can be defined as:
– Alpha: The best (highest-value) choice we have found so far at any point along
the path of Maximizer. The initial value of alpha is -∞.
– Beta: The best (lowest-value) choice we have found so far at any point along
the path of Minimizer. The initial value of beta is +∞.
• The Alpha-beta pruning to a standard minimax algorithm returns the same move as
the standard algorithm does, but it removes all the nodes which are not really
affecting the final decision but making algorithm slow. Hence by pruning these
nodes, it makes the algorithm fast.
Alpha-Beta Pruning
Key points about alpha-beta pruning :(α>=β)
• The Max player will only update the value of alpha.
• The Min player will only update the value of beta.
• While backtracking the tree, the node values will be passed to upper nodes instead
of values of alpha and beta.
• We will only pass the alpha, beta values to the child nodes.
Working of Alpha-Beta Pruning:
Step 1:
Alpha-Beta Pruning
• Step 2&3:
Alpha-Beta Pruning
• Step 4:
Step 5&6:
Alpha-Beta Pruning
• Step 7:
• Step 8:
Crypt Arithmetic Problem
• Cryptarithmetic Problem is a type of constraint satisfaction problem where the game
is about digits and its unique replacement either with alphabets or other symbols.
In cryptarithmetic problem, the digits (0-9) get substituted by some possible
alphabets or symbols. The task in cryptarithmetic problem is to substitute each digit
with an alphabet to get the result arithmetically correct.
Crypt Arithmetic Problem
• The rules or constraints on a cryptarithmetic problem are as follows:
• There should be a unique digit to be replaced with a unique alphabet.
• The result should satisfy the predefined arithmetic rules, i.e., 2+2 =4, nothing else.
• Digits should be from 0-9 only.
• There should be only one carry forward, while performing the addition operation on
a problem.
• The problem can be solved from both sides, i.e., left hand side (L.H.S), or right
hand side (R.H.S)
• Let’s understand the cryptarithmetic problem as well its constraints better with the
help of an example:
• Given a cryptarithmetic problem, i.e., S E N D + M O R E = M O N E Y