AI Unit-2
AI Unit-2
1. Introduction
2. problem reduction
3. game playing
4. alpha-beta pruning
5. two-player perfect information games
Adversarial Search Adversarial search is a search, where we examine the problem which arises
when we try to plan ahead of the world and other agents are planning against us. o In previous
topics, we have studied the search strategies which are only associated with a single agent that
aims to find the solution which often expressed in the form of a sequence of actions. o But,
there might be some situations where more than one agent is searching for the solution in the
same search space, and this situation usually occurs in game playing.
The environment with more than one agent is termed as multi-agent environment, in which
each agent is an opponent of other agent and playing against each other. o Each agent needs to
consider the action of other agent and effect of that action on their performance. o So, 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. o Games are modelled
as a Search problem and heuristic evaluation function, and these are the two main factors which
help to model and solve games in AI.
Types of Games in AI:
Perfect information:
o A game with the perfect information is that in which agents can look into the complete board.
o Agents have all the information about the game, and they can see each other moves also.
o Examples are Chess, Checkers, Go, etc. o Imperfect information:
o If in a game agents do not have all information about the game and not aware with what's
going on, such type of games are called the game with imperfect information o such as tic-tac-
toe, Battleship, blind, Bridge, etc.
Deterministic games:
o Deterministic games are those games which follow a strict pattern and set of rules for the
games, and there is no randomness associated with them.
o Examples are chess, Checkers, Go, tic-tac-toe, etc.
Non-deterministic games:
o Non-deterministic are those games which have various unpredictable events and has a factor
of chance or luck.
o This factor of chance or luck is introduced by either dice or cards.
o These are random, and each action response is not fixed. o Such games are also called as
stochastic games. Example: Backgammon, Monopoly, Poker, etc.
Zero-Sum Game o Zero-sum games are adversarial search which involves pure competition.
o In Zero-sum game each agent's gain or loss of utility is exactly balanced by the losses or
gains of utility of another agent.
o One player of the game try to maximize one single value, while other player tries to minimize
it.
o Each move by one player in the game is called as ply.
o Chess and tic-tac-toe are examples of a Zero-sum game.
Zero-sum game: Embedded thinking The Zero-sum game involved embedded thinking in
which one agent or player is trying to figure out:
o What to do.
o How to decide the move
o Needs to think about his opponent as well
o The opponent also thinks what to do Each of the players is trying to find out the response of
his opponent to their actions. This requires embedded thinking or backward reasoning to solve
the game problems in AI.
Formalization of the problem:
A game can be defined as a type of search in AI which can be formalized of the following
elements:
o Initial state: It specifies how the game is set up at the start.
o Player(s): It specifies which player has moved in the state space.
o Action(s): It returns the set of legal moves in state space.
o Result(s, a): It is the transition model, which specifies the result of moves in the state space.
o Terminal-Test(s): Terminal test is true if the game is over, else it is false at any case. The state
where the game ends is called terminal states.
o Utility(s, p): A utility function gives the final numeric value for a game that ends in terminal
states s for player p. It is also called payoff function. For Chess, the outcomes are a win, loss,
or draw and its payoff values are +1, 0, ½. And for tic-tac-toe, utility values are +1, -1, and 0.
Problem Reduction:
Definition: The process of decomposing a complex problem into a set of sub problems and
then integrating all the sub solutions to get the solution of the given complex problem is known
as problem reduction.
Example for problem reduction:
1) Towers of Hanoi problem
2) AND-OR Graph
3) AO* Algorithm
1)Tower of Hanoi problem:
2. AND-OR Graph:
The AND-OR GRAPH (or tree) is useful for representing the solution of problems that can
solved by decomposing them into a set of smaller problems, all of which must then be solved.
This decomposition, or reduction, generates arcs that we call AND arcs. One AND arc may
point to any number of successor nodes, all of which must be solved in order for the arc to
point to a solution. Just as in an OR graph, several arcs may emerge from a single node,
indicating a variety of ways in which the original problem might be solved. This is why the
structure is called not simply an AND-graph but rather an AND-OR graph (which also happens
to be an AND-OR tree)
EXAMPLE FOR AND-OR GRAPH
ALGORITHM:
Let G be a graph with only starting node INIT.
Repeat the followings until INIT is labeled SOLVED or h(INIT) > FUTILITY
a) Select an unexpanded node from the most promising path from INIT (call it NODE)
b) Generate successors of NODE. If there are none, set h(NODE) = FUTILITY (i.e., NODE
is unsolvable); otherwise for each SUCCESSOR that is not an ancestor of NODE do the
following:
i. Add SUCCESSSOR to G.
ii. If SUCCESSOR is a terminal node, label it SOLVED and set h(SUCCESSOR) = 0.
iii. If SUCCESSPR is not a terminal node, compute its h
c) Propagate the newly discovered information up the graph by doing the following: let S
be set of SOLVED nodes or nodes whose h values have been changed and need to have values
propagated back to their parents. Initialize S to Node. Until S is empty repeat the followings:
i. Remove a node from S and call it CURRENT.
ii. Compute the cost of each of the arcs emerging from CURRENT. Assign minimum
cost of its successors as its h.
iii. Mark the best path out of CURRENT by marking the arc that had the minimum cost
in step ii
iv. Mark CURRENT as SOLVED if all of the nodes connected to it through new labeled
arc have been labeled SOLVED
v. If CURRENT has been labeled SOLVED or its cost was just changed, propagate its
new cost back up through the graph. So add all of the ancestors of CURRENT to S.
EXAMPLE: 1
STEP 1:
3. AO* Search Algorithm:
AO* search Algorithm is based on problem decomposition (Breakdown problem into small
pieces) When a problem can be divided or decomposed into a set of sub problems,
where each sub problem can be solved separately and for each subproblem,
sub solution is evaluated and a combination of these sub solutions will be a whole
solution,
AND OR graphs or AND OR trees are used for representing this solution.
Problem Reduction In Artificial Intelligence
AO* is informed search algorithm, works based on heuristic.
We already know about the divide and conquer strategy, a solution to a problem can be
obtained by decomposing it into smaller sub-problems.
Each of this sub-problem can then be solved to get its sub solution.
These sub solutions can then recombined to get a solution as a whole.
That is called is Problem Reduction.
AND-OR graphs or AND – OR trees are used for representing the solution.
This method generates arc which is called as AND-OR arcs.
One AND arc may point to any number of successor nodes, all of which must be solved
in order for an arc to point to a solution.
AND-OR graph is used to represent various kind of complex problem solutions.
AO* search algorithm is based on AND-OR graph so ,it is called AO* search algorithm.
Example: In Following figure,
we have taken example of Goal: Acquire TV Set.
This goal or problem is subdivided into two subproblems or sub goals like
i. STEAL TV SET
ii. Earn some money, Buy TV SET.
SO to solve this problem if we select second alternative of earn some Money, then along with
that Buy TV SET also need to select as it is part of AND graph.
Whereas First alternative : Steal Tv Set is forming OR Graph
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: o There are two players MAX and
MIN. o Players have an alternate turn and start with MAX. o MAX maximizes the result of the
game tree o MIN minimizes the result.
Example Explanation: o From the initial state, MAX has 9 possible moves as he starts first. o
MAX place x and MIN place o, and both player plays alternatively until we reach a leaf node
where one player has three in a row or all squares are filled. o Both players will compute each
node, minimax, the minimax value which is the best achievable utility against an optimal
adversary. o Suppose both the players are well aware of the tic-tac-toe and playing the best
play. o Each player is doing his best to prevent another one from winning. o MIN is acting
against Max in the game.
So in the game tree, we have a layer of Max, a layer of MIN, and each layer is called as Ply. o
Max place x, then MIN puts o to prevent Max from winning, and this game continues until the
terminal node. o In this either MIN wins, MAX wins, or it's a draw. o This game-tree is the
whole search space of possibilities that MIN and MAX are playing tic-tac-toe and taking turns
alternately.
Hence adversarial Search for the minimax procedure works as follows: o It aims to find the
optimal strategy for MAX to win the game. o It follows the approach of Depth-first search. o
In the game tree, optimal leaf node could appear at any depth of the tree. o Propagate the
minimax values up to the tree until the terminal node discovered.
In a given game tree, the optimal strategy can be determined from the minimax value of each
node, which can be written as MINIMAX(n). MAX prefer to move to a state of maximum
value and MIN prefer to move to a state of minimum value then:
Game Playing:
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 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.
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.
Example:
Mini-Max Algorithm:
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.
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
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 been computed.
Hence the optimal value for the maximizer is 3 for this example.
Move Ordering in Alpha-Beta pruning:
The effectiveness of alpha-beta pruning is highly dependent on the order in which each
node is examined. Move order is an important aspect of alpha-beta pruning.
It can be of two types:
Worst ordering
Ideal ordering
Worst ordering:
o In some cases, alpha-beta pruning algorithm does not prune any of the leaves of the
tree, and works exactly as minimax algorithm.
o In this case, it also consumes more time because of alpha-beta factors, such a move of
pruning is called worst ordering.
o In this case, the best move occurs on the right side of the tree.
o The time complexity for such an order is O(bm).
Ideal ordering:
The ideal ordering for alpha-beta pruning occurs when lots of pruning happens
in the tree, and best moves occur at the left side of the tree.
We apply DFS hence it first search left of the tree and go deep twice as minimax
algorithm in the same amount of time.
Complexity in ideal ordering is O(bm/2).
Rules to find good ordering:
Following are some rules to find good ordering in alpha-beta pruning:
o Occur the best move from the shallowest node.
o Order the nodes in the tree such that the best nodes are checked first.
o Use domain knowledge while finding the best move.
o Ex: for Chess, try order: captures first, then threats, then forward moves, backward
moves.
o We can bookkeep the states, as there is a possibility that states may repeat.
Some of the examples of alpha beta pruning:
Two-player perfect information games:
Explain any two player examples like tic toc toe,Min Max,AO* Search,Alpha Beta pruning.