AI 2ndunit
AI 2ndunit
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.
o 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. 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 modeled as a Search problem and heuristic evaluation function, and
these are the two main factors which help to model and solve games in AI.
Imperfect information Battleships, blind, tic-tac-toe Bridge, poker, scrabble, nuclear war
o Perfect information: A game with the perfect information is that in which agents
can look into the complete board. Agents have all the information about the
game, and they can see each other moves also. Examples are Chess, Checkers,
Go, etc.
o Imperfect information: 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, such as tic-tac-toe, Battleship, blind, Bridge,
etc.
o Deterministic games: 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. Examples are chess, Checkers, Go, tic-tac-toe, etc.
o Non-deterministic games: Non-deterministic are those games which have
various unpredictable events and has a factor of chance or luck. This factor of
chance or luck is introduced by either dice or cards. These are random, and each
action response is not fixed. Such games are also called as stochastic games.
Example: Backgammon, Monopoly, Poker, etc.
Note: In this topic, we will discuss deterministic games, fully observable environment, zero-
sum, and where each agent acts alternatively.
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.
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.
65.5M
1.3K
Exception Handling in Java - Javatpoint
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.
The following figure is showing part of the game-tree for tic-tac-toe game. Following
are some key points of the game:
Example Explanation:
o From the initial state, MAX has 9 possible moves as he starts first. 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. Each player is doing his best to prevent another one from winning. MIN is
acting against Max in the game.
o So in the game tree, we have a layer of Max, a layer of MIN, and each layer is
called as Ply. 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. This game-tree is the whole
search space of possibilities that MIN and MAX are playing tic-tac-toe and taking
turns alternately.
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:
Initial call:
Minimax(node, 3, true)
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:
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.
o For node D max(-1,- -∞) => max(-1,4)= 4
o For Node E max(2, -∞) => max(2, 6)= 6
o For Node F max(-3, -∞) => max(-3,-5) = -3
o For node G max(0, -∞) = max(0, 7) = 7
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 4: Now it's a turn for Maximizer, and it will again choose the maximum of all nodes
value and find the maximum value for the root node. In this game tree, there are only 4
layers, hence we reach immediately to the root node, but in real games, there will be
more than 4 layers.
1. α>=β
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.
We have discussed the optimal solution in The Two Water Jug Puzzle. In this post,
a BFS based solution is discussed.
Here, we keep exploring all the different valid cases of the states of water in the jug
simultaneously until and unless we reach the required target water.
As provided in the problem statement, at any given state we can do either of the
following operations:
1. Fill a jug
2. Empty a jug
3. Transfer water from one jug to another until either of them gets completely filled or
empty.
Examples:
Input: X = 4, Y = 3, Z = 2
Output: {(0, 0), (0, 3), (3, 0), (3, 3), (4, 2), (0, 2)}
Explanation:
Step 1:- First we will fill the 4 litre jug completely with water.
Step 2:- Then optimal approach would be to empty water from 4-
litre jug into 3-litre (leaving 1L water in 4L jug and 3L completely
full). Hence we got 1L water.
Step 3:- Now, Empty water from 3L.
Step 4:- Pour the water from 4L jug into 3L jug Now 4L container
is completely empty and 1L water in present in 3L litre jug.
Step 5:- Fill the 4L jug with water completely again.
Step 6:- On transfering water from 4L jug to 3L jug, we will get
2L water in 4L jug which was our required quantity.
Input: X = 3, Y = 5, Z = 4
Output: 6
Explanation:
Step 1:- First we will fill the 5-litres jug to its maximum
capacity.
Step 2:- Then optimal approach would be to transfer 3-litres from
5-litres jug to 3-litres jugs.
Step 3:- Now, Empty the 3-litres jug.
Step 4:- Transfer 2L from 5L jug to 3-L jug.
Step 5:- Now, Fill 5-litres jug to its maximum capacity.
Step 6:- On Pouring water from 5L jug to 3L jug until it’s full we
will get 4L water in 5-litre jug which was our required quantity.
// using BFS
import java.util.*;
class Pair {
List<Pair> path;
this.j1 = j1;
this.j2 = j2;
path = new ArrayList<>();
this.j1 = j1;
this.j2 = j2;
path.addAll(_path);
throws java.lang.Exception
int jug1 = 4;
int jug2 = 3;
int target = 2;
}
private static void
boolean[][] visited
queue.offer(initialState);
while (!queue.isEmpty()) {
// water states
|| visited[curr.j1][curr.j2])
continue;
// mark current jugs state as visited
visited[curr.j1][curr.j2] = true;
if (curr.j1 == target) {
// path.
else {
}
int n = curr.path.size();
System.out.println(
System.out.println(
+ curr.path.get(i).j2);
return;
// Empty the other II. Fill the jug and let the
// consideration
// Now,
// untouched
// untouched
int amountTransferred
= Math.min(curr.j1, emptyJug);
j2 = curr.j2 - amountTransferred;
j1 = curr.j1 + amountTransferred;
Output
Path of states of jugs followed is :
0 , 0
0 , 3
3 , 0
3 , 3
4 , 2
0 , 2
Time Complexity: O(n*m).
Space Complexity: O(n*m). Where n and m are the quantity of jug1 and jug2,
respectively.