Aiunit 2
Aiunit 2
INTELLIGENCE
UNIT-2 (GAME
PLAYING)
LECTURE NO:-1
1
The most common search technique in game playing is Minimax search procedure . It is
depth-first depth-limited search procedure. It is used for games like chess and tic-tac-
toe. Minimax algorithm uses two functions – MOVEGEN : It generates all the possible
moves that can be generated from the current position. STATICEVALUATION : It
returns a value depending upon the goodness from the viewpoint of two-player This
algorithm is a two player game, so we call the first player as PLAYER1 and second
player as PLAYER2. The value of each node is backed-up from its children. For
PLAYER1 the backed-up value is the maximum value of its children and for PLAYER2
the backed-up value is the minimum value of its children. It provides most promising
move to PLAYER1, assuming that the PLAYER2 has make the best move. It is a
recursive algorithm, as same procedure occurs at each
Figu
re 2: After backing-up of values We assume that PLAYER1 will start the game. 4 levels
are generated. The value to nodes H, I, J, K, L, M, N, O is provided by
STATICEVALUATION function. Level 3 is maximizing level, so all nodes of level 3 will
take maximum values of their children. Level 2 is minimizing level, so all its nodes will
2
take minimum values of their children. This process continues. The value of A is 23. That
means A should choose C move to win.
MINIMAX ALGORITHM
LECTURE NO:-1
3
Since this is a backtracking based algorithm, it tries all possible moves, then backtracks
and makes a decision.
Maximizer goes LEFT: It is now the minimizers turn. The minimizer now has a
choice between 3 and 5. Being the minimizer it will definitely choose the least among
both, that is 3
Maximizer goes RIGHT: It is now the minimizers turn. The minimizer now has a
choice between 2 and 9. He will choose 2 as it is the least among the two values.
Being the maximizer you would choose the larger value that is 3. Hence the optimal move
for the maximizer is to go LEFT and the optimal value is 3.
Now the game tree looks like below :
The above tree shows two possible scores when maximizer makes left and right moves.
It is the strategy used by combinational search that uses heuristic to speed up the search
strategy. The concept of Minimax strategy can be understood with the example of two
player games, in which each player tries to predict the next move of the opponent and tries
to minimize that function. Also, in order to win, the player always tries to maximize its
own function based on the current situation.
Mini-Max Algorithm in Artificial Intelligence
4
for the player assuming that opponent is also playing optimally.
o Mini-Max algorithm uses recursion to search through the game-tree.
o 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.
o In this algorithm two players play the game, one is called MAX and
other is called MIN.
5
UNIT-2 (GAME
PLAYING) ALPHA BETA
PRUNING LECTURE
NO:-2
Alpha-Beta Pruning :
A major issue with Minimax algorithm is that it can explore those parts of the tree that
are irrelevant, leads to the wastage of resources. Hence there must be a strategy to decide
which part of the tree is relevant and which is irrelevant and leave the irrelevant part
unexplored. Alpha-Beta pruning is one such kind of strategy.
The main goal of Alpha-Beta pruning algorithm is to avoid the searching those parts of the
tree that do not have any solution. The main concept of Alpha-Beta pruning is to use two
bounds named Alpha, the maximum lower bound, and Beta, the minimum upper bound.
These two parameters are the values that restrict the set of possible solutions. It compares
the value of the current node with the value of alpha and beta parameters, so that it can
move to the part of the tree that has the solution and discard the rest.
Description
Aplha-Beta pruning is a optimization technique used in minimax algorithm. The idea
behind this algorithm is cut off the branches of game tree which need not to be evaluated
as better move exists already.
This algorithm introduces two new fields −
Alpha − This is best value(maximum) that maximizer player can guarantee at current
level or its above level
Beta − This is the best value(minimum) that minimizer player can guarantee at the
current level or its above level.
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.
Let us understand the intuition behind this first and then we will formalize the
algorithm. Suppose, we have the following game tree:
6
In this case,
Minimax Decision = MAX{MIN{3,5,10}, MIN{2,a,b}, MIN{2,7,3}}
= MAX{3,c,2}
=3
You would be surprised!
How could we calculate the maximum with a missing value? Here is the trick.
MIN{2,a,b} would certainly be less than or equal to 2, i.e., c<=2 and hence
MAX{3,c,2} has to be 3.
The question now is do we really need to calculate c? Of course not.
We could have reached a conclusion without looking at those nodes. And this is
where alpha-beta pruning comes into the picture.
A few definitions:
Alpha: It is the best choice so far for the player MAX. We want to get the
highest possible value here.
Beta: It is the best choice so far for MIN, and it has to be the lowest possible
value.
Note: Each node has to keep track of its alpha and beta values. Alpha can be
updated only when it’s MAX’s turn and, similarly, beta can be updated only
when it’s MIN’s chance.
7
How does alpha-beta pruning work?
1. Initialize alpha = -infinity and beta = infinity as the worst possible cases.
The condition to prune a node is when alpha becomes greater than or
equal to beta.
2. Start with assigning the initial values of alpha and beta to root and since
alpha is less than beta we don’t prune it.
3. Carry these values of alpha and beta to the child node on the left. And
now from the utility value of the terminal state, we will update the values
of alpha and be, so we don’t have to update the value of beta. Again, we
don’t prune because the condition remains the same. Similarly, the third
child node also. And then backtracking to the root we set alpha=3 because
8
that is the minimum value that alpha can have.
4. Now, alpha=3 and beta=infinity at the root. So, we don’t prune. Carrying
this to the center node, and calculating MIN{2, infinity}, we get alpha=3
and beta=2.
5. Prune the second and third child nodes because alpha is now greater than
beta.
6. Alpha at the root remains 3 because it is greater than 2. Carrying this to
the rightmost child node, evaluate MIN{infinity,2}=2. Update beta to 2
and alpha remains 3.
7. Prune the second and third child nodes because alpha is now greater than
beta.
8. Hence, we get 3, 2, 2 at the left, center, and right MIN nodes, respectively.
And calculating MAX{3,2,2}, we get 3. Therefore, without even looking at
four leaves we could correctly find the minimax decision.
Pseudocode (Source: NPTEL Course):
evaluate (node, alpha, beta)
if node is a leaf
return the utility value of node
if node is a minimizing node
9
for each child of node
beta = min (beta, evaluate (child, alpha, beta))
if beta <= alpha
return beta
return beta
if node is a maximizing node
for each child of node
alpha = max (alpha, evaluate (child, alpha, beta))
if beta <= alpha
return alpha
return alpha
10
UNIT-2 (GAME
PLAYING) JUG
PROBLEM
LECTURE NO:-3
Water-Jug Problem
This problem is defined as:
“We are given two water jugs having no measuring marks on these. The capacities of jugs are
3 liter and 5 liter. It is required to fill the bigger jug with exactly 2 liter of water. The water
can be filled in a jug from a tap”.
In this problem, the start state is that both jugs are empty and the final state is that 5-liter
jug has exactly 2 liters of water. The production rules involve filling a jug with some
amount of water, filing the water from one jug to other or emptying
the jug. The search will be finding the sequence of production rules which transform
the initial state to final state.
In the water jug problem in Artificial Intelligence, we are provided with two
jugs: one having the capacity to hold 3 litres of water and the other has the
capacity to hold 5 litres of water. There is no other measuring equipment
available and the jugs also do not have any kind of marking on them. So, the
agent’s task here is to fill the 5 litres jug with 2 gallons of water by using only
these two jugs and no other material. Initially, both our jugs are empty.
So, to solve this problem, following set of rules were proposed:
Production rules for solving the water jug problem
State state
Here, let x denote the 5- litres jug and y denote the 3- litres jug.
1. (x,y) If x<5 (5,y) Fill the 4 litres jug completely
2. Initial
(x,y) if y<3 (x,3) Fill the 3 litres jug completely
S.No. Condition Final Description of action taken
3. (x,y) If x>0 (x-d,y) Pour some part from the 4 litres jug
4. (x,y) If y>0 (x,y-d) Pour some part from the 3 litres jug
5. (x,y) If x>0 (0,y) Empty the 5 litres jug
6. (x,y) If y>0 (x,0) Empty the 3 litres jug
Explanation:
The water jug problem can be solved with just two jugs – one that can hold 5 litres of
water and the other that can hold 3 litres of water, if there is also an unlimited supply of
water from a tap and a sink. Show the series of state diagrams that solve this problem.
Definition:
It is a normal chess game. In a chess problem, the start is the initial configuration of
chessboard. The final state is the any board configuration, which is a winning position for
any player. There may be multiple final positions and each board configuration can be
thought of as representing a state of the game. Whenever any player moves any piece, it
leads to different state of game.
The above figure shows a 3x3 chessboard with each square labeled with integers 1 to 9. We
simply enumerate the alternative moves rather than developing a general move operator
because of the reduced size of the problem.
Using a predicate called move in predicate calculus, whose parameters are the starting and
ending squares, we have described the legal moves on the board.
For example, move (1, 8) takes the knight from the upper left-hand corner to the middle of
the bottom row. While playing Chess, a knight can move two squares either horizontally
or vertically followed by one square in an orthogonal direction as long as it does not move
off the board.
The above predicates of the Chess Problem form the knowledge base for this problem. An
unification algorithm is used to access the knowledge base.
Suppose we need to find the positions to which the knight can move from a particular
location, square 2.The goal move (z, x) unifies with two different predicates in the
knowledge base, with the substitutions {7/x} and {9/x}. Given the goal move (2, 3), the
responsible is failure, because no move (2, 3) exists in the knowledge base.
UNIT-2 (GAME
PLAYING) TILES
PROBLEM LECTURE
NO:-5
This problem is also known as “Black and White Sliding Tiles Problem in
Artificial Intelligence “. There are 7 tiles, 3 are white and 3 are black and
there is a white space in the middle. All the black tiles are placed left and all
the white tiles are placed right
The Puzzle
The Sliding Tiles Puzzle was created by chess player and puzzle maker Sam Loyd (1841-
1911) in the 1870s. The puzzle consists of a N x M board shown in Figure 1, where each
cell could be represented as a number, a letter, an image or basically anything you can
think of.
The task in the sliding tiles puzzle is to rearrange the tiles of some starting state in a
manner in which all of them end up arranged matching another configuration known as
the goal state .
figure 2: Moving Tiles from Start State
to Goal State
To be able to reach the goal state a sequence of moves is required; one move consists of
swapping the empty tile with some other tile. The solution to the previous example would
be obtained as shown in Figure 3.
A logical question probably pops up into the reader's mind when he thinks of the starting
state; could I truly find the goal state from this initial state? Sam Loyd once offered $1,000
to anyone who could solve the puzzle in Figure 4.
Figure: 4 x 4 Board
Although several people claimed they found a solution, the reality is that the previous
puzzle has no solution. So, how do we know if a given initial configuration is solvable?
It has been proved that a configuration is solvable if the following conditions hold:
If the width is odd, every solvable state has an even number of inversions.
If the width is even, every solvable state has: a) an even number of inversions if the
empty tile is on an odd numbered row counting from the bottom; b) an odd number
of inversions if the empty tile is on an even numbered row counting from the
bottom.
An inversion occurs when some tile precedes another tile with a lower value; the goal state
has zero inversions. For instance, considering a 4 x 4 board the number 12 at the upper
left corner will have a total of 11 inversions as numbers 1 through 11 come after it. In
general, an inversion is a pair of tiles (a, b) such that a appears before b, but a is greater
than b.
Books-
1. Artificial Intelligence, 3rd Edn., E. Rich and K. Knight (TMH)
2. Artificial Intelligence, 3rd Edn., Patrick Henny Winston, Pearson Education.
3. Artificial Intelligence, Shivani Goel, Pearson Education.
4. Artificial Intelligence and Expert systems – Patterson, Pearson Education.