Module 1_search Methods
Module 1_search Methods
Problem-solving agents:
In Artificial Intelligence, Search techniques are universal problem-solving methods.
Rational agents or Problem-solving agents in AI mostly used these search strategies or
algorithms to solve a specific problem and provide the best result
Solution: It is an action sequence which leads from the start node to the goal
node.
Optimal Solution: If a solution has the lowest cost among all solutions.
Uninformed/ informed Search
Algorithms
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.
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, i.e.
f(n)= h(n).
where,
h(n) - estimated straight line distance from node n to goal
To implement the graph search procedure ,we will need to use two list of nodes.
OPEN- nodes that have been generated but have not been visited yet
1.The 1st step is to define the OPEN list with a single node, the
starting node.
2.The 2nd step is to check whether or not OPEN is empty. If it is
empty, then the algorithm returns failure and exits.
3.The 3rd step is to remove the node with the best score, n, from
OPEN and place it in CLOSED.
4.The 4th step “expands” the node n, where expansion is the
identification of successor nodes of n.
5.The 5th step then checks each of the successor nodes to see
whether or not one of them is the goal node. If any successor is the
goal node, the algorithm returns success and the solution, which
consists of a path traced backwards from the goal to the start node.
Otherwise, proceeds to the sixth step.
6.In 6th step, for every successor node, the algorithm applies the
evaluation function, f, to it, then checks to see if the node has been in
either OPEN or CLOSED. If the node has not been in either, it gets
added to OPEN.
7.Finally, the 7th step establishes a looping structure by sending the
algorithm back to the 2nd step. This loop will only be broken if the
algorithm returns success in step 5 or failure in step 2.
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.
Iteration 1:
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.
Initialization: {(S, 5)}
Points to remember:
Step 1
Step-1
Starting from node A, we first calculate the best path.
f(A-B) = g(B) + h(B) = 1+4= 5 , where 1 is the default cost value of travelling from A to B and 4
is the estimated cost from B to Goal state.
f(A-C-D) = g(C) + h(C) + g(D) + h(D) = 1+2+1+3 = 7 , here we are calculating the path cost as
both C and D because they have the AND-Arc. The default cost value of travelling from A-C is 1,
and from A-D is 1, but the heuristic value given for C and D are 2 and 3 respectively hence
making the cost as 7.
The minimum cost path is chosen i.e A-B.
Step-2
Using the same formula as step-1, the path is now calculated from the B node,
f(B-E) = 1 + 6 = 7.
f(B-F) = 1 + 8 = 9
Hence, the B-E path has lesser cost. Now the heuristics have to be updated since there is a
difference between actual and heuristic value of B. The minimum cost path is chosen and is
updated as the heuristic , in our case the value is 7. And because of change in heuristic of B
there is also change in heuristic of A which is to be calculated again.
f(A-B) = g(B) + updated((h(B)) = 1+7=8
Step-3
Comparing path of f(A-B) and f(A-C-D) it is seen that f(A-C-D) is smaller. Hence f(A-C-D) needs to
be explored.
Now the current node becomes C node and the cost of the path is calculated,
f(C-G) = 1+2 = 3
f(C-H-I) = 1+0+1+0 = 2
f(C-H-I) is chosen as minimum cost path,also there is no change in heuristic since it matches the
actual cost. Heuristic of path of H and I are 0 and hence they are solved, but Path A-D also needs
to be calculated , since it has an AND-arc.
f(D-J) = 1+0 = 1, hence heuristic of D needs to be updated to 1. And finally the f(A-C-D) needs to
be updated.
f(A-C-D) = g(C) + h(C) + g(D) + updated((h(D)) = 1+2+1+1 =5.
Also, the updated H(D) = 2, since P(D-J) = 1 + 1 = 2. By these updated values for H(C) and
H(D), the updated P(A-C-D) = 1 + 2 + 1 + 2 = 6. Let’s see an example below:
as we can see that path f(A⇢C+D) is get solved and this tree has become a
solved tree now.
In simple words, the main flow of this algorithm is that we have to find firstly
level 1st heuristic
value and then level 2nd and after that update the values with going upward
Means End Analysis (MEA)
Means end analysis is a technique used to solve problems in AI programs. This
technique combines forward and backward strategies to solve complex problems. With
these mixed strategies, complex problems can be tackled first, followed by smaller ones.
First, evaluate the difference between Initial State and final State.
Select the various operators which can be applied for each difference.
Apply the operator at each difference, which reduces the difference between the current
state and goal state.
Operator Subgoaling
In the MEA process, we detect the differences between the current state and goal state. Once
these differences occur, then we can apply an operator to reduce the differences. But sometimes
it is possible that an operator cannot be applied to the current state. So we create the subproblem
of the current state, in which operator can be applied, such type of backward chaining in which
operators are selected, and then sub goals are set up to establish the preconditions of the
operator
Algorithm is called Operator Subgoaling.
for Means-Ends Analysis:
Let's we take Current state as CURRENT and Goal State as GOAL, then following are the steps for
the MEA algorithm.
Step 1: Compare CURRENT to GOAL, if there are no differences between both then return Success
and Exit.
Step 2: Else, select the most significant difference and reduce it by doing the following steps until
the success or failure occurs.
Select a new operator O which is applicable for the current difference, and if there is no such
operator, then signal failure.
Attempt to apply operator O to CURRENT. Make a description of two states.
i) O-Start, a state in which O?s preconditions are satisfied.
ii) O-Result, the state that would result if O were applied In O-start.
If
(First-Part <------ MEA (CURRENT, O-START)
And
Example of Mean-Ends Analysis:
Let's take an example where we know the initial state and goal state as given below. In this
problem, we need to get the goal state by finding differences between the initial state and goal
state and applying operators.
Solution:
To solve the above problem, we will first find the differences between initial states and goal
states, and for each difference, we will generate a new state and will apply the operators. The
operators we have for this problem are:
Move
Delete
Expand
1. Evaluating the initial state: In the first step, we will evaluate the initial state and will compare
the initial and Goal state to find the differences between both states.
2. Applying Delete operator: As we can check the first difference is that in goal state there
is no dot symbol which is present in the initial state, so, first we will apply the Delete operator
to remove this dot.
3. Applying Move Operator: After applying the Delete operator, the new state occurs which
we will again compare with goal state. After comparing these states, there is another difference
that is the square is outside the circle, so, we will apply the Move Operator.
4. Applying Expand Operator: Now a new state is generated in the third step, and we will
compare this state with the goal state. After comparing the states there is still one difference
which is the size of the square, so, we will apply Expand operator, and finally, it will generate
the goal state.
Applications of Means End Analysis
Means end analysis can be applied in the following fields:
Organizational planning
Means end analysis is used in organizations to facilitate general management. It helps
organizational managers to conduct planning to achieve the objectives of the organization.
The management reaches the desired goal by dividing the main goals into sub-goals that are
linked with actionable tasks.
Business transformation
This technique is used to implement transformation projects. If there are any desired changes
in the current state of a business project, means end analysis is applied to establish the new
processes to be implemented. The processes are split into sub-processes to enhance effective
implementation.
Gap analysis
Gap analysis is the comparison between the current performance and the required
performance. Means end analysis is applied in this field to compare the existing technology
and the desired technology in organizations. Various operations are applied to fill the existing
gap in technology.
Problem Reduction in the Block Problem
You're given a table on which there are three blocks. And A is on table, B is on table, and
C is on A. This is the initial state. And you want to move these blocks, to the gold state. On
this configuration, so that C is on table, B is on C and
constraints.
You may move only one block at a time, so you can't pick both A and B together.
And second, you may only move a block that has nothing on top of it. So, you
cannot move block A in this configuration, because it has C on top of it.
Let us also suppose that we're given some operators in this world.
These operators essentially move some object to some location. For example, we could move
C to the table, or C onto B, or C onto A.
Not all the operators may be applicable in the current state. C is already on A, but in principle,
all these, all of these operators are available. Given these operators, and this initial state and
this goal state, write a sequence of operations that will move the blocks from the initial state
to the goal state.
Here is the initial state, here is the goal state. And the state space consists of all of the states
that could be potentially produced from the initial state by iterative application of the various
operators
There is one path, this is not the only path, but this is one path to go from the initial state to
the
Let goal state.
us see how this notion of path finding applies to our blocks world problem.
>From the initial state, here it is one path of going to the goal state. First, we put C on the
table. Then we put B on top C. And then we put A on top of B.
Consider this state, for example. There are several operations possible here.
How does the AI agent know which operation to select at this particular state?
One way of thinking about this is to talk in terms of differences. This chart illustrates the
differences between different states and the goal state. So, for example, if the current state
was this one then this red line illustrates the difference from the goal state. So we should pick
an operator that will help reduce the difference between the current state and the goal state.
So the reduction between the difference with the current state and the goal state is the end.
The application of the operator is the means.
That's why it's called the means-ends analysis. At any given state,
look at initial state and see that there are three differences between the initial state and the
goal state. First, A is on table here, but A should be on B.
B is on table here, but B should be on C. And third, C is on top of A here, the C should be on top,
on table there. So three differences. Here the number of operations are available to us. Nine
operations in particular. Let us do a means- end analysis. We can apply an operator that would
put C on table.
In which case the difference between the new state and the goal state will be two. We could
apply an operator that will put C on top of B, in that case the difference between the current
state and the goal state will still be three. Or we can apply the operator putting B on top of C,
in which case the distance between the current state and the goal state will be 2. Notice that
the notion of reducing differences now leads to two possible choices. One could go with this
state or with this one.
And the important thing to notice here is that with each different move the distance between
the current state and the goal state is decreasing, from three to two to one to zero. This is why
means-end analysis comes up with this path because at each time it reduces a difference
There are two main approaches to game playing in AI, rule-based systems and machine
learning-based systems.
The game trees are search trees with each level is a move from one of the players. We assume a
few things before proceeding further
1. Every player is allowed to play only one move in a single iteration. If he is allowed to take
multiple moves in special cases, we assume a single move consisting of cumulative effects
of those multiple moves.
2. There are only two players. In a typical two player game, each player owns one ply (level)
at alternate level. Again, for a multiple player game, the very solution can be extended.
3. The games are complete information games. Thus it is possible for us to apply static
evaluation function to probable moves and ascertain complete information about the board
position to be used in making a decision.
4. Both opponents (players) goals are exactly opposite to each other. When one player
makes a decision at one layer, next layer is second player’s prerogative. Thus, what the first
player tries to maximize at one layer, the second player tries to minimize at next layer. That
is why they are sometimes denoted as Max and Min layers
An example game tree is depicted in figure below . This game is known as tic-tac-toe
This game is played by two players, one mark his move by X while the other by 0. There is a
matrix of 3 X 3 which is completely empty in the beginning. When the game begins, both the
players take turn in marking one of the cells of the matrix. When a player is able to mark three
consecutive cells before other player, he is a winner. Three consecutive cells can be marked in
total 8 different ways; three columns, three rows and two diagonals. Figure below shows eight
different ways one can win. For simplicity the opponent moves are not shown. The aim of the
player is to reach to any one of such states. Another important aim of a player is to make sure
that opponent cannot achieve similar state and win (so you lose). In fact it is also possible to
reach to a state where none of the players has three consecutive marks and none wins. That is
known as draw.
We would like to reach to the state mentioned as W in figure (where we have three
cross covering the leftmost column). We would like to pick up each node in a way that
we can reach to that node by the time 5th move is taken. Let us assume that the
game has reached to 3rd level. We would like to pick up move mentioned as 2 to
reach to the state W. Can we do that? We cannot. It is the opponent’s move. He will
have to decide that move. If he is not a novice with the game, he won’t choose that
move as he can understand that if he does so, you can win. He, instead, would choose
move 1, which will prevent you to win (and also give him a chance to win if you forget
to block the central cell in the next move).
We want to build the search algorithm which works differently at alternate levels. On
odd levels(1,3,5.. ) etc, the algorithm should act like normal search algorithm and pick
up the state which maximizes our chances of winning. For all even levels (2, 4, 6…),
we will have to remember that it is our opponents turn so we need to pick up the
worst move for us (as opponent is likely to choose that). Thus we will have to choose
Max state at one level and Min state at another level. Now you can see why they are
called Min and Max levels.
O o X
X O
A zero-sum game is one in which no wealth is created or destroyed. So, in a two-player zero-
sum game, whatever one player wins, the other loses. Therefore, the player share no common
interests. There are two general types of zero-sum games: those with perfect information and
those without.
In a game with perfect information, every player knows the results of all previous moves. Such
games include chess, tic-tac-toe, and Nim. In games of perfect information, there is at least
one "best" way to play for each player. This best strategy does not necessarily allow him to
win but will minimize his losses. For instance, in tic-tac-toe, there is a strategy that will allow
you to never lose, but there is no strategy that will allow you to always win. Even though
there is an optimal strategy, it is not always possible for players to find it. For instance, chess
is a zero-sum game with perfect information, but the number of possible strategies is so large
that it is not possible for our computers to determine the best strategy.
In games with imperfect information, the players do not know all of the previous moves.
Often, this occurs because the players play simultaneously. Example –Throwing Dice
The familiar game of Rock, Paper, Scissors is played like this: at the same time, two players
display one of three symbols: a rock, paper, or scissors. A rock beats scissors, scissors beat
paper by cutting it, and paper beats rock by covering it. In this simulation, the computer has two
different strategies that it can follow.
Rock, paper, scissors is an example of a zero-sum game without perfect information. Whenever
one player wins, the other loses. We can express this game using a payoff matrix that explains
what one player gains with each strategy the players use. In the payoff matrix below, the rows
represent player 1's possible strategies while the columns represent player 2's possible
strategies. 1 represents a win for player 1, 0 a tie, and -1 a loss for player 1. So, for instance, the
upper left entry is a 0 because if both players display rocks, they tie. The upper middle entry is a
-1 because if player 1 displays a rock and player 2 displays paper, player 2 wins, and therefore,
player 1 loses.
Player 2
Rock Paper Scissors
Player 1 Rock 0 -1 1
Pape 1 0 -1
Scissors -1 1 0
Mini-Max Algorithm
Mini-Max algorithm uses recursion to search through the game-tree.
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:
• The working of the minimax algorithm can be easily described using an example. Below
we have taken an example of game-tree which is representing the two-player game.
• In this example, there are two players one is called Maximizer and other is called
Minimizer.
Maximizer will try to get the Maximum possible score, and Minimizer will try to get the
minimum possible score
.
• This algorithm applies DFS, so in this game-tree, we have to go all the way through the
leaves to reach the terminal nodes.
• At the terminal node, the terminal values are given so we will compare those value and
backtrack the tree until the initial state occurs. Following are the main steps involved in
solving the two-player game tree:
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 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.
Alpha-Beta Pruning
Alpha-beta pruning is a modified version of the minimax algorithm. It is an optimization
technique for the minimax algorithm.
As we have seen in the minimax search algorithm that the number of game states it has to
examine are exponential in depth of the tree. Since we cannot eliminate the exponent, but we
can cut it to half. Hence 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. It is also called as Alpha-Beta Algorithm.
Alpha-beta pruning can be applied at any depth of a tree, and sometimes it not only prune
the tree leaves but also entire sub-tree.
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 +∞.
Condition for Alpha-beta pruning:
The main condition which required for alpha-beta pruning is:
α>=β
Step 1: At the first step the, Max player will start first move from node A where α= -∞ and β=
+∞, these value of alpha and beta passed down to node B where again α= -∞ and β= +∞, and
Node B passes the same value to its child D.
Step 2: At Node D, the value of α will be calculated as its turn for Max. The value of α is compared
with firstly 2 and then 3, and the max (2, 3) = 3 will be the value of α at node D and node value will
also 3.
Step 3: Now algorithm backtrack to node B, where the value of β will change as this is a turn of
Min, Now β= +∞, will compare with the available subsequent nodes value, i.e. min (∞, 3) = 3,
hence at node B now α= -∞, and β= 3.
In the next step, algorithm traverse the next successor of Node B which is node E, and the
values of α= -∞, and β= 3 will also be passed.
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.