0% found this document useful (0 votes)
22 views18 pages

Aiunit 2

The document discusses game playing in artificial intelligence. It covers topics like minimax search procedure, alpha-beta pruning, and how these techniques are used to develop algorithms that can learn to play games and make decisions to win. Game playing is an important domain of AI that involves developing programs to play games using techniques like minimax search and machine learning approaches.

Uploaded by

Cbra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views18 pages

Aiunit 2

The document discusses game playing in artificial intelligence. It covers topics like minimax search procedure, alpha-beta pruning, and how these techniques are used to develop algorithms that can learn to play games and make decisions to win. Game playing is an important domain of AI that involves developing programs to play games using techniques like minimax search and machine learning approaches.

Uploaded by

Cbra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

ARTIFICIAL

INTELLIGENCE

UNIT-2 (GAME
PLAYING)
LECTURE NO:-1

Game Playing is an important domain of artificial intelligence. Games don’t require


much knowledge; the only knowledge we need to provide is the rules, legal moves and the
conditions of winning or losing the game. Both players try to win the game. So, both of
them try to make the best move possible at each turn. Searching techniques like
BFS(Breadth First Search) are not accurate for this as the branching factor is very high,
so searching will take a lot of time. So, we need another search procedures that improve.
 Generate procedure so that only good moves are generated.
 Test procedure so that the best move can be explored first.
Game playing is a popular application of artificial intelligence that involves the
development of computer programs to play games, such as chess, checkers, or Go. The
goal of game playing in artificial intelligence is to develop algorithms that can learn how
to play games and make decisions that will lead to winning outcomes.
1. One of the earliest examples of successful game playing AI is the chess program
Deep Blue, developed by IBM, which defeated the world champion Garry Kasparov in
1997. Since then, AI has been applied to a wide range of games, including two-player
games, multiplayer games, and video games.
2. There are two main approaches to game playing in AI: rule-based systems and
machine learning-based systems. Rule-based systems use a set of fixed rules to play the
game, whereas machine learning-based systems use algorithms to learn from
experience and make decisions based on that experience.
3. In recent years, machine learning-based systems have become increasingly
popular, as they are able to learn from experience and improve over time, making
them well-suited for complex games such as Go. For example, AlphaGo, developed by
DeepMind, was the first machine learning-based system to defeat a world champion in
the game of Go.
Game playing in AI is an active area of research and has many practical applications,
including game development, education, and military training. By simulating game
playing scenarios, AI algorithms can be used to develop more effective decision-making
systems for real-world applications.

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

level. Figure 1: Before backing-up of values

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

Minimax Algorithm in Game


Minimax is a kind of backtracking algorithm that is used in decision making and game
theory to find the optimal move for a player, assuming that your opponent also plays
optimally. It is widely used in two player turn-based games such as Tic-Tac-Toe,
Backgammon, Mancala, Chess, etc.
In Minimax the two players are called maximizer and minimizer. The maximizer tries to
get the highest score possible while the minimizer tries to do the opposite and get the
lowest score possible.
Every board state has a value associated with it. In a given state if the maximizer has
upper hand then, the score of the board will tend to be some positive value. If the
minimizer has the upper hand in that board state then it will tend to be some negative
value. The values of the board are calculated by some heuristics which are unique for
every type of game.
Example1:
Consider a game which has 4 final states and paths to reach final state are from root to 4
leaves of a perfect binary tree as shown below. Assume you are the maximizing player and
you get the first chance to move, i.e., you are at the root and your opponent at next
level. Which move you would make as a maximizing player considering that your
opponent also plays optimally?

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

o Mini-max algorithm is a recursive or backtracking algorithm which is


used in decision-making and game theory. It provides an optimal move

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.

Working of Min-Max Algorithm:


o 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.
o In this example, there are two players one is called Maximizer and
other is called Minimizer.
o Maximizer will try to get the Maximum possible score, and Minimizer
will try to get the minimum possible score.
o This algorithm applies DFS, so in this game-tree, we have to go all the
way through the leaves to reach the terminal nodes.
o 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:

Limitation of the minimax Algorithm:


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

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

Pour some water from the 3


(4, y-[4-
7. (x,y) If (x+y)<7 gallon jug to fill the four
x])
gallon jug 11

(x-[3- Pour some water from the 4 gallon jug to


8. (x,y) If (x+y)<7
y],y) fill the 3 gallon jug.
Pour all water from 3 litres jug to the 5
9. (x,y) If (x+y)<5 (x+y,0)
litres jug
Pour all water from the 5 litres jug to
10. (x,y) if (x+y)<3 (0, x+y)
the 3 litres jug
The listed production rules contain all the actions that could be performed by
the agent in transferring the contents of jugs. But, to solve the water jug
problem in a minimum number of moves, following set of rules in the given
sequence should be performed:
Solution of water jug problem according to the production rules:

S.No. 5 litres jug contents 3 litres jug contents Rule followed


1. 0 litres 0 litres Initial state
2. 0 litres 3 litres Rule no.2
3. 3 litres 0 litres Rule no. 9
4. 3 litres 3 litres Rule no. 2
5. 4 litres 2 litres Rule no. 7
6. 0 litres 2 litres Rule no. 5
7. 2 litres 0 litres Rule no. 9
On reaching the 7th attempt, we reach a state which is our goal state.
Therefore, at this state, our problem is solved.

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.

 Fill 5-litres jug to its maximum capacity.


 Transfer 3-litres from 5-litres jug to 3-litres jugs.
 Empty the 3-litres jug.
 Transfer 2-litres from 5-litres jug to 3-litres jug.
 Fill 5-litres jug to its maximum capacity.
 Pour water to 3L jug from 5L jug until it’s full.
Lecture-4
Chess 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 all possible moves of figure are as follows.


Move (1, 8) move (6, 1)

Move (1, 6) move (6, 7)

Move (2, 9) move (7, 2)

Move (2, 7) move (7, 6)

Move (3, 4) move (8, 3)

Move (3, 8) move (8, 1)

Move (4, 1) move (9, 2)

Move (4, 3) move (9, 4)

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.

Figure 1: A 3 x 3 Sliding Tiles Puzzles


Board

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.

Figure 3: Moving From Start to Goal Requires Moving Two Tiles

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.

You might also like