IAI UNIT-II Games
IAI UNIT-II Games
IAI UNIT-II Games
Games
Adversarial Search
Zero-Sum Game
o Zero-sum games are adversarial search which involves pure
competition.
o One player of the game tries to maximize one single value, while
another player tries to minimize it.
o 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.
following elements:
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.
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, action’s 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:
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 players play 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 aware of the tic-tac-toe and playing the
best play. Each player is doing his best to prevent the other 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 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
is discovered.
In each 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:
Mini-Max Algorithm in Artificial Intelligence:
o The Mini-Max algorithm is mostly used for game playing in AI. Such
as Chess, Checkers, tic-tac-toe, go, and various two-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
the other is called MIN.
o Both the players fight it as the opponent player gets the minimum
benefit while they get the maximum benefit.
o Both Players of the game are opponents of each other, where MAX
will select the maximized value and MIN will select the minimized
value.
o The minimax algorithm proceeds all the way down to the terminal
node of the tree, then backtrack the tree as the recursion.
Step-1: In the first step, the algorithm generates the entire game-tree and
applies 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 all.
The main drawback of the minimax algorithm is that it gets really slow for
complex games such as Chess, go, etc. This type of game has a huge
branching factor, and the player has lots of choices to decide. This limitation
of the minimax algorithm can be improved from alpha-beta pruning.
Alpha-Beta Pruning
o Alpha-beta pruning is a modified version of the minimax algorithm. It is an
o 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.
o 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.
a. Alpha: The best (highest-value) choice we have found so far at any point
b. Beta: The best (lowest-value) choice we have found so far at any point
The Alpha-beta pruning to a standard minimax algorithm returns the same move as the
standard algorithm does, but it removes all the nodes which are not really affecting the
final decision but making algorithm slow. Hence by pruning these nodes, it makes the
algorithm fast.
α>=β
Key points about alpha-beta pruning:
o The Max player will only update the value of alpha.
o The Min player will only update the value of beta.
o While backtracking the tree, the node values will be passed to upper nodes instead of
values of alpha and beta.
o We will only pass the alpha, beta values to the child nodes.
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
o Worst ordering: In some cases, alpha-beta pruning algorithm does not prune any of the
leaves of the tree, and works exactly as minimax algorithm. In this case, it also consumes
more time because of alpha-beta factors, such a move of pruning is called worst
ordering. In this case, the best move occurs on the right side of the tree. The time
complexity for such an order is O(bm).
o 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
CSPs represents the objects in problem as a collection of finite constraints over variables, which
is solved by constraint satisfaction methods.
Applications:
➢ Map coloring
➢ Line drawing interpretation
➢ Scheduling problems
➢ Floor planning for VLSI
A CSP consists of
Key Concepts
variables.
Constraint Graph:
values can be eliminated from any domain. The primary goal is to reduce
Example
Consider a simple CSP with two variables, X and Y, each with domains {1, 2,
domains as follows:
the domains of variables, thereby narrowing down the possible values and
explicit, thus reducing the search space and making it easier to find a
solution.
Steps in Constraint Propagation:
Initialization:
Propagation:
a variable is reduced, this new information can further reduce the domains
Arc Consistency:
every value in its domain satisfies the binary constraints with some value in
2}.
Iteration:
AC-3 algorithm:
➢ AC-3 maintains a queue of arcs which initially contains all the arcs in
the CSP.
➢ AC-3 then pops off an arbitrary arc (Xi, Xj) from the queue and makes
Xi arc-consistent with respect to Xj.
➢ If this leaves Di unchanged, just moves on to the next arc;
➢ But if this revises Di, then add to the queue all arcs (Xk, Xi) where Xk is
a neighbor of Xi.
➢ If Di is revised down to nothing, then the whole CSP has no consistent
solution, return failure;
➢ Otherwise, keep checking, trying to remove values from the domains
of variables until no more arcs are in the queue.
➢ The result is an arc-consistent CSP that have the same solutions as the
original one but have smaller domains.
The complexity of AC-3:
Assume a CSP with n variables, each with domain size at most d, and
with c binary constraints (arcs). Checking consistency of an arc can be done
in O(d2) time, total worst-case time is O(cd3).
Node Consistency:
Ensures that each variable’s value satisfies the unary constraints on that
variable.
Path Consistency:
ensuring that constraints are satisfied over paths in the constraint graph.
Generalized Arc Consistency (GAC):
variables.
In Sudoku, each cell is a variable, and the domain is the numbers 1 to 9. The
constraints are:
uniqueness.
solutions.
optimizing resources.
Backtracking search for CSPs
➢ Backtracking search, a form of depth-first search, is commonly used for solving CSPs.
Inference can be interwoven with search.
➢ Commutativity: CSPs are all commutative. A problem is commutative if the order of
application of any given set of actions has no effect on the outcome.
➢ Backtracking search: A depth-first search that chooses values for one variable at a
time and backtracks when a variable has no legal values left to assign.
➢ Backtracking algorithm repeatedly chooses an unassigned variable, and then tries all
values in the domain of that variable in turn, trying to find a solution. If an
inconsistency is detected, then BACKTRACK returns failure, causing the previous call
to try another value.
➢ There is no need to supply BACKTRACKING-SEARCH with a domain-specific initial
state, action function, transition model, or goal test.
➢ BACKTRACKING-SARCH keeps only a single representation of a state and alters that
representation rather than creating a new one.
Knowledge-Based Agents
o An intelligent agent needs knowledge about the real world for taking decisions
and reasoning to act efficiently.
o Knowledge-based agents are those agents who have the capability of maintaining
an internal state of knowledge, reason over that knowledge, update their
knowledge after observations and take actions. These agents can represent the
world with some formal representation and act intelligently.
o Knowledge-base and
o Inference system.
perceiving the environment. The input is taken by the inference engine of the agent and
which also communicate with KB to decide as per the knowledge store in KB. The
Inference system
Inference means deriving new sentences from old. Inference system allows us to add a
new sentence to the knowledge base. A sentence is a proposition about the world.
Inference system applies logical rules to the KB to deduce new information.
Inference system generates new facts so that an agent can update the KB. An inference
o Forward chaining
o Backward chaining
1. TELL: This operation tells the knowledge base what it perceives from the
environment.
2. ASK: This operation asks the knowledge base what action it should perform.
function KB-AGENT(percept):
persistent: KB, a knowledge base
t, a counter, initially 0, indicating time
TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
Action = ASK(KB, MAKE-ACTION-QUERY(t))
TELL(KB, MAKE-ACTION-SENTENCE(action, t))
t=t+1
return action
The knowledge-based agent takes percept as input and returns an action as output. The
agent maintains the knowledge base, KB, and it initially has some background
knowledge of the real world. It also has a counter to indicate the time for the whole
process, and this counter is initialized with zero.
Each time when the function is called, it performs its three operations:
o Third agent program TELLS the KB that which action was chosen.
was executed.
1. Knowledge level
Knowledge level is the first level of knowledge-based agent, and in this level, we need to
specify what the agent knows, and what the agent goals are. With these specifications,
we can fix its behavior. For example, suppose an automated taxi agent needs to go from
a station A to station B, and he knows the way from A to B, so this comes at the
knowledge level.
2. Logical level:
At this level, we understand that how the knowledge representation of knowledge is
stored. At this level, sentences are encoded into different logics. At the logical level, an
encoding of knowledge into logical sentences occurs. At the logical level we can expect
3. Implementation level:
This is the physical representation of logic and knowledge. At the implementation level
agent perform actions as per logical and knowledge level. At this level, an automated
taxi agent actually implements his knowledge and logic so that he can reach to the
destination.
Approaches to designing a knowledge-based agent:
There are mainly two approaches to build a knowledge-based agent:
behavior as a program code. Which means we just need to write a program that already
encodes the desired behavior or agent.
However, in the real world, a successful agent can be built by combining both
Example:
1. a) It is Sunday.
4. d) 5 is a prime number.
connectives.
o The propositions and connectives are the basic elements of the propositional
logic.
o A proposition formula which has both true and false values is calledcontingency.
o Statements which are questions, commands, or opinions are not propositions
such as "Where is Rohini", "How are you", "What is your name", are not
propositions.
a. Atomic Propositions
b. Compound propositions
single proposition symbol. These are the sentences which must be either true or false.
Example:
Example:
sentence logically. We can create compound propositions with the help of logical
connectives. There are mainly five connectives, which are given as follows:
conjunction.
Example: Rohan is intelligent and hardworking. It can be written as,
P= Rohan is intelligent,
Q= Rohan is hardworking. → P∧ Q.
Truth Table:
In propositional logic, we need to know the truth values of propositions in all possible
scenarios. We can combine all the possible combination with logical connectives, and
the representation of these combinations in a tabular format is called Truth table.
Following are the truth table for all logical connectives:
Truth table with three propositions:
We can build a proposition composing three propositions P, Q, and R. This truth table is
made-up of 8n Tuples as we have taken three proposition symbols.
Precedence of connectives:
Just like arithmetic operators, there is a precedence order for propositional connectors
or logical operators. This order should be followed while evaluating a propositional
problem. Following is the list of the precedence order for operators:
Precedence Operators
Logical equivalence is one of the features of propositional logic. Two propositions are said to be logically
equivalent if and only if the columns in the truth table are identical to each other.
Let's take two propositions A and B, so for logical equivalence, we can write it as A⇔B. In below truth table we
can see that column for ¬A∨ B and A→B, are identical hence A is Equivalent to B
Properties of Operators:
o Commutativity:
o P∧ Q= Q ∧ P, or
o P ∨ Q = Q ∨ P.
o Associativity:
o (P ∧ Q) ∧ R= P ∧ (Q ∧ R),
o (P ∨ Q) ∨ R= P ∨ (Q ∨ R)
o Identity element:
o P ∧ True = P,
o P ∨ True= True.
o Distributive:
o P∧ (Q ∨ R) = (P ∧ Q) ∨ (P ∧ R).
o P ∨ (Q ∧ R) = (P ∨ Q) ∧ (P ∨ R).
o DE Morgan's Law:
o ¬ (P ∧ Q) = (¬P) ∨ (¬Q)
o ¬ (P ∨ Q) = (¬ P) ∧ (¬Q).
o Double-negation elimination:
o ¬ (¬P) = P.
Inference:
In artificial intelligence, we need intelligent computers which can create new logic from
old logic or by evidence, so generating the conclusions from evidence and facts is
termed as Inference.
Inference rules:
Inference rules are the templates for generating valid arguments. Inference rules are
applied to derive proofs in artificial intelligence, and the proof is a sequence of the
In inference rules, the implication among all the connectives plays an important role.
Following are some terminologies related to inference rules:
o Converse: The converse of implication, which means the right-hand side proposition
goes to the left-hand side and vice-versa. It can be written as Q → P.
Hence from the above truth table, we can prove that P → Q is equivalent to ¬ Q → ¬ P,
and Q→ P is equivalent to ¬ P → ¬ Q.
1. Modus Ponens:
The Modus Ponens rule is one of the most important rules of inference, and it states
that if P and P → Q is true, then we can infer that Q will be true. It can be represented as:
Example:
Hence, we can say that, if P→ Q is true, and P is true then Q will be true.
Proof by Truth table:
2. Modus Tollens:
The Modus Tollens rule state that if P→ Q is true and ¬ Q is true, then ¬ P will also
true. It can be represented as:
The Hypothetical Syllogism rule state that if P→R is true whenever P→Q is true, and
Q→R is true. It can be represented as the following notation:
Example:
Statement-1: If you have my home key then you can unlock my home. P→Q
Statement-2: If you can unlock my home then you can take my money. Q→R
Conclusion: If you have my home key then you can take my money. P→R
4. Disjunctive Syllogism:
The Disjunctive syllogism rule state that if P∨Q is true, and ¬P is true, then Q will be true.
5. Addition:
The Addition rule is one the common inference rule, and it states that If P is true, then
Example:
Statement: I have a vanilla ice-cream. ==> P
Proof by Truth-Table:
6. Simplification:
The simplification rule state that if P∧ Q is true, then Q or P will also be true. It can be
represented as:
Proof by Truth-Table:
7. Resolution:
The Resolution rule state that if P∨Q and ¬ P∧R is true, then Q∨R will also be true. It can
be represented as
Proof by Truth-Table:
Proof by Resolution
Resolution
• p is true or q is true
• and we also know that p is false or r is true
• then it must be the case that q is true or r is true.
1. r -> u == (NOT r OR u)
2. u -> NOT w == (NOT u OR NOT w)
3. NOT r -> NOT w == (r OR NOT w)
We then use resolution on the hypotheses to derive the conclusion (NOT w):
1. NOT r OR u Premise
3. r OR NOT w Premise
7. QED
We can combine resolution with proof by contradiction (where we assert the negation of what
we wish to prove, and from that premise derive FALSE) to direct our search towards smaller and
smaller clauses, with the goal of producing FALSE.
Proof by contradiction:
(NOT p -> 0) == p
We use proof by contradiction to drive our search for a proof; we are looking for the smallest
possible goal clause (false), so any use of equivalences or resolution that brings us to simpler
expressions is working towards that goal.
We can redo the previous proof (about Joe and his umbrella) using proof by contradiction with
resolution:
1. NOT r OR u Premise
3. r OR NOT w Premise
4. w Negation of conclusion
If either C173 or C220 is required, then all students will take computer science. C173 and C240
are required. Prove that all students will take computer science.