AI Notes Unit II
AI Notes Unit II
ADVERSARIAL SEARCH
Adversarial search is a game-playing technique where the agents are surrounded
by a competitive environment. A conflicting goal is given to the agents
(multiagent). These agents compete with one another and try to defeat one
another in order to win the game. Such conflicting goals give rise to the
adversarial search. Here, game-playing means discussing those games
where human intelligence and logic factor is used, excluding other factors such
as luck factor. Tic-tac-toe, chess, checkers, etc., are such type of games
where no luck factor works, only mind works.
Games:
Minmax Algorithm
Alpha-beta Pruning.
Minimax Algorithm
Players will be two namely:
Algorithm:
Propagate the values from the leaf node till the current position following the
minimax strategy.
Make the best move from the choices.
For example, in the above figure, the two players MAX and MIN are
there. MAX starts the game by choosing one path and propagating all the nodes of
that path. Now, MAX will backtrack to the initial node and choose the best path
where his utility value will be the maximum. After this, its MIN chance. MIN will
also propagate through a path and again will backtrack, but MIN will choose the
path which could minimize MAX winning chances or the utility value.
So, if the level is minimizing, the node will accept the minimum value
from the successor nodes. If the level is maximizing, the node will accept
the maximum value from the successor.
Function minmax(N)
Begin
If Nis a leaf then
Return the payoff(rewards) of the leaf
Else
Let N1,N2… b ethe successor of the node
If N is the minnode then
Return min(Minimax(N1), Minimax(N2)….)
Else
Return max(Minimax(N1), Minimax(N2)….)
NOTE: Extra Information: The utility value is just some arbitrary value that the
player receives when arriving at a certain state in the game. For instance, in Tic-
tac-toe, your utility function could simply be 1 for a win, 0 for a tie, or -1 for a loss.
Running minmax on this would at best find a set of actions that result in 1 (a win).
Another example would be chess (not that you can feasibly run minimax on a
game of chess). Say your utility function comes from a certain number that is
based on the value of the piece you captured or lost
Alpha-beta Pruning
The method used in alpha-beta pruning is that it cutoff the search by exploring
less number of nodes. It makes the same moves as a minimax algorithm does, but
it prunes the unwanted branches using the pruning technique
Alpha-beta pruning works on two threshold values, i.e., α (alpha) and β (beta).
α: It is the best highest value, a MAX player can have. It is the lower bound,
which represents negative infinity value.
β: It is the best lowest value, a MIN player can have. It is the upper bound
which represents positive infinity.
So, each MAX node has α-value, which never decreases, and each MIN node has β-
value, which never increases.
After completing one part, move the achieved β-value to its upper node and
fix it for the other threshold value, i.e., α.
Now, its P turn, he will pick the best maximum value. P will move to explore
the next part only after comparing the values with the current α-value. If the
value is equal or greater than the current α-value, then only it will be
replaced otherwise we will prune the values.
The steps will be repeated unless the result is not obtained.
Note:
Utility can be thought of as a way to “score” each possible
move based on its potential to result in a win. How utility is
calculated is entirely up to the programmer. It can
incorporate a large variety of factors and weigh them as
the programmer sees fit. For instance, number of blank
spaces on the board, the location of the opponent’s current
pieces, the location of our current pieces, how close we are
to a winning formation, etc. all might be factors to consider
in calculating the utility of a particular move. Let’s take tic-
tac-toe, for example, which can have relatively simple
utility measures. The figure below displays a tic-tac-toe
board midway through the game with a very simple
(probably not optimal) utility rule. We can see that it’s X’s
turn, and there are only 3 possible moves, and hence, 3
child nodes. For each possible move, utility is calculated
using the below utility rule.
An Inference Engine
Knowledge base: A Knowledge base represents the actual facts which
exist in the real world. It is the central component of a knowledge-based
agent. It is a set of sentences which describes the information related to
the world.
Inference Engine: It is the engine of a knowledge-based system which
allows to infer new knowledge in the system.
MAKE-ACTION-QUERY()
This function returns a sentence which tells what action the agent must take at the
current time.
MAKE-ACTION-SENTENCE()
This function returns a sentence which tells an action is selected as well as
executed.
Let’s understand the working of these functions under the Inference engine with
the help of the below function:
Knowledge base:
Knowledge-base is a central component of a knowledge-based agent, it is also known as KB. It
is a collection of sentences (here 'sentence' is a technical term and it is not identical to
sentence in English). These sentences are expressed in a language which is called a knowledge
representation language.
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.
The Wumpus world is a cave which has 4/4 rooms connected with passageways. So there are
total 16 rooms which are connected with each other. We have a knowledge-based agent who
will go forward in this world. The cave has a room with a beast which is called Wumpus, who
eats anyone who enters the room. The Wumpus can be shot by the agent, but the agent has a
single arrow. In the Wumpus world, there are some Pits rooms which are bottomless, and if
agent falls in Pits, then he will be stuck there forever. The exciting thing with this cave is that in
one room there is a possibility of finding a heap of gold. So the agent goal is to find the gold
and climb out the cave without fallen into Pits or eaten by Wumpus. The agent will get a reward
if he comes out with gold, and he will get a penalty if eaten by Wumpus or falls in the pit.
Following is a sample diagram for representing the Wumpus world. It is showing some rooms
with Pits, one room with Wumpus and one agent at (1, 1) square location of the world.
There are also some components which can help the agent to navigate the
cave. These components are given as follows:
a. The rooms adjacent to the Wumpus room are smelly, so that it would have some stench.
b. The room adjacent to PITs has a breeze, so if the agent reaches near to PIT, then he will
perceive the breeze.
c. There will be glitter in the room if and only if the room has gold.
d. The Wumpus can be killed by the agent if the agent is facing to it, and Wumpus will emit
a horrible scream which can be heard anywhere in the cave.
Performance measure:
o +1000 reward points if the agent comes out of the cave with the gold.
o -1000 points penalty for being eaten by the Wumpus or falling into the pit.
o The game ends if either agent dies or came out of the cave.
Environment:
o A 4*4 grid of rooms.
o The agent initially in room square [1, 1], facing toward the right.
o Location of Wumpus and gold are chosen randomly except the first square [1,1].
o Each square of the cave can be a pit with probability 0.2 except the first square.
Actuators:
o Left turn,
o Right turn
o Move forward
o Grab
o Release
o Shoot.
Sensors:
o The agent will perceive the stench if he is in the room adjacent to the Wumpus. (Not
diagonally).
o The agent will perceive breeze if he is in the room directly adjacent to the Pit.
o The agent will perceive the glitter in the room where the gold is present.
o The agent will perceive the bump if he walks into a wall.
o When the Wumpus is shot, it emits a horrible scream which can be perceived anywhere
in the cave.
o These percepts can be represented as five element list, in which we will have different
indicators for each sensor.
o Example if agent perceives stench, breeze, but no glitter, no bump, and no scream then
it can be represented as:
[Stench, Breeze, None, None, None].
Now we will explore the Wumpus world and will determine how the agent will find its
goal by applying logical reasoning.
Initially, the agent is in the first room or on the square [1,1], and we already know that this
room is safe for the agent, so to represent on the below diagram (a) that room is safe we will
add symbol OK. Symbol A is used to represent agent, symbol B for the breeze, G for Glitter or
gold, V for the visited room, P for pits, W for Wumpus.
At Room [1,1] agent does not feel any breeze or any Stench which means the adjacent squares
are also OK.
Agent's second Step:
Now agent needs to move forward, so it will either move to [1, 2], or [2,1]. Let's suppose agent
moves to the room [2, 1], at this room agent perceives some breeze which means Pit is around
this room. The pit can be in [3, 1], or [2,2], so we will add symbol P? to say that, is this Pit
room?
Now agent will stop and think and will not make any harmful move. The agent will go back to
the [1, 1] room. The room [1,1], and [2,1] are visited by the agent, so we will use symbol V to
represent the visited squares
At the third step, now agent will move to the room [1,2] which is OK. In the room [1,2] agent
perceives a stench which means there must be a Wumpus nearby. But Wumpus cannot be in
the room [1,1] as by rules of the game, and also not in [2,2] (Agent had not detected any
stench when he was at [2,1]). Therefore agent infers that Wumpus is in the room [1,3], and in
current state, there is no breeze which means in [2,2] there is no Pit and no Wumpus. So it is
safe, and we will mark it OK, and the agent moves further in [2,2]
At room [2,2], here no stench and no breezes present so let's suppose agent decides to move
to [2,3]. At room [2,3] agent perceives glitter, so it should grab the gold and climb out of the
cave.
Propositional Logic
There should be proper knowledge representation and reasoning. The knowledge should be
represented properly. Otherwise the AI system will do wrong analysis. If we don’t represent
properly then there will be syntax and semantics error. Output of the propositional logic is either
true or false but not both.
Syntax and semantics define a way to determine the truth value of the sentence.
implication. It returns true if both sides satisfy one another, else returns
false. This can also be denoted as (≡).
Name Symbol
Parenthesis/ Brackets ()
Negation/not ¬ or ~
Conjuction/and Ʌ
Disjunction/or V
Implication →
Semantics: It defines the rules to determine the truth of a sentence with respect
to a specific model. A semantic should be able to compute the truth value of any
given sentence.
There are following five rules regarding the semantics of the complex
sentences P and Q in a given model m :
¬P: Its value will be false, iff it is true in the model m.
(P Ʌ Q): Its value is true, iff both P and Q are true in m.
(P v Q): Its value is true, iff either P is true, or Q is true in m.
(P ⇔ Q): The value will be true, iff P and Q value is either true or false in the given
(P=> Q): Its value is true, iff the value of P is false, and that of Q is true in m.
model m.
Note: Here, iff means if and only if.
These five connectives can also be understood with the help of the below
described truth table:
Q=It is raining.
It is represented as (P→Q).
It is represented as (A Ʌ B).
It is represented as P → ( ~Q)
It is represented as (X V Y).
It is represented as (A ⇔ B).
There can be many examples of Propositional logic.
A → B = ~A → ~B
Contrapositive Law
~A → ~B (Converse of Inverse)
Implication Removal A → B = ~A V B
Biconditional Removal A ⇔ B = (A → B) Ʌ (B → A)
Absorption Law A Ʌ (A V B) ≡ A AV (A Ʌ B) ≡A
Table defining the rules used in Propositional logic where A, B, and C represents some
arbitrary sentences.
Validity: If a sentence is valid in all set of models, then it is a valid sentence. Validity is
also known as tautology, where it is necessary to have true value for each set of
model.
Satisfiability: If a sentence is true atleast for some set of values, it is a satisfiable
sentence.
Let’s understand validity and satisfiability with the help of examples:
Example 1:
(P V Q) → (P Ʌ Q)
P Q PVQ PɅQ (P V Q) → (P Ʌ Q)
Example 2:
A B A→B (A → B) Ʌ A ((A → B) Ʌ A) → B
((A → B) Ʌ A) → B
So, it is clear from the truth table that the given expression is valid as well as satisfiable.
Inference Rules in Proposition Logic
Inference rules are those rules which are used to describe certain conclusions. The inferred
conclusions lead to the desired goal state.
Rule: If P→Q is given, where P is positive, then Q value will also be positive.
Example: If Sheero is intelligent, then Sheero is smart. Sheero is intelligent. Prove that
Sheero is smart.
Solution: Let, A= Sheero is intelligent.
B= Sheero is smart.
To prove: A→B.
By using Modus Ponen rule, A→B where A is positive. Hence, the value of B will be
true. Therefore, Sheero is smart.
Syllogism: It is a type of logical inference rule which concludes a result by using
deducting reasoning approach. It is a valid deductive argument having two premises
and a conclusion.
Rule: If there are three variables say P, Q, and R where
P→Q and Q→R then P→R.
Example: Given a problem statement:
If Ram is the friend of Shyam and Shyam is the friend of Rahul, then Ram is the friend of
Rahul.
Solution: Let, P= Ram is the friend of Shyam.
Q= Shyam is the friend of Rahul.
R= Ram is the friend of Rahul.
It can be represented as: If (P→Q) Ʌ (Q→R)= (P→R).
Resolution Method in AI
Resolution method is an inference rule which is used in both Propositional as well as First-
order Predicate Logic in different ways. This method is basically used for proving the
satisfiability of a sentence. In resolution method, we use Proof by Refutation technique to
prove the given statement.
The key idea for the resolution method is to use the knowledge base and negated goal to
obtain null clause(which indicates contradiction). Resolution method is also called Proof by
Refutation. Since the knowledge base itself is consistent, the contradiction must be
introduced by a negated goal. As a result, we have to conclude that the original goal is true.
In propositional logic, resolution method is the only inference rule which gives a new clause
when two or more clauses are coupled together.
The process followed to convert the propositional logic into resolution method contains
the below steps:
Convert the given axiom into clausal form,
Apply and proof the given goal using negation rule.
Use those literals which are needed to prove.
Iteratively apply resolution to the set and add the resolvent to the set
Continue until no further resolvents can be obtained or a null clause can be obtained.
In propositional logic, the resolution method is applied only to those clauses which are
disjunction of literals. There are following steps used to convert into CNF:
4) Finally, using distributive law on the sentences, and form the CNF as:
(A1 V B1) Ʌ (A2 V B2) Ʌ …. Ʌ (An V Bn).
We illustrate the procedure by converting the sentence B1,1 ⇔ (P1,2 ∨ P2,1) into CNF. The steps
We should prove that Negation of Goal (¬R): It will not rain is false then R which is a goal it
is raining is true.
After applying Proof by Refutation (Contradiction) on the goal, the problem is solved, and it
has terminated with a Null clause ( Ø ). Hence, the goal is achieved. Thus, It is not raining.
Note: We can have many examples of Proposition logic which can be proved with the help of
Propositional resolution method.
Horn clauses:
literals and whose conclusion is a single positive literal. For example, the definite clause (¬L1,1 ∨
Every definite clause can be written as an implication whose premise is a conjunction of positive
¬Breeze ∨ B1,1) can be written as the implication (L1,1 ∧ Breeze) ⇒ B1,1. In the implication
form, the sentence is easier to understand: it says that if the agent is in [1,1] and there is a breeze,
then [1,1] is breezy.
Inference with Horn clauses can be done through the forward-chaining and backwardchaining
algorithms.
Example: For example, if I am indoors and hear rain starting to fall, it might occur to me that the
picnic will be canceled.
Inference with Horn clauses can be done through the forward-chaining and backwardchaining
algorithms.
Forward Chaining
Forward Chaining is the process which works on the basis of available data to make certain
decisions. Forward chaining is the process of chaining data in the forward direction. In forward
chaining, we start with the available data and use inference rules to extract data until the goal
is reached. Forward chaining is the concept of data and decision. From the available data,
expand until a decision is made.
--It is a form of reasoning which starts with atomic sentences in the knowledge base and applies the
inference rules in the forward direction to extract more data until the goal is reached.
Properties
--It is a process of making the conclusion based on known facts or data by starting from the initial
state and reach the goal.
--It is also called as data driven because we reach the goal using the available data.
Backward Chaining
Properties:
1.Backward chaining is based on modus ponen inference rule.
2. In Backward chaining goal is broken into subgoals to prove the facts are true.
3. It is a goal driven approach as goal decides which goal is selected or used.
One approach based on backtracking search, and one local hill-climbing search. These algorithms
are part of the “technology” of propositional logic. The algorithms we describe are for checking
satisfiability:
a) Early termination: The algorithm detects whether the sentence must be true or false, even with a
partially completed model. A clause is true if any literal is true, even if the other literals do not yet
complete. For example, the sentence (A ∨ B) ∧ (A ∨ C) is true if A is true, regardless of the values
have truth values; hence, the sentence as a whole could be judged true even before the model is
of B and C. Early termination avoids examination of entire subtrees in the search space.
example, if the model contains B = true, then (¬B ∨ ¬C) simplifies to ¬C, which is a unit clause.
c) Unit clause heuristic: A unit clause was defined earlier as a clause with just one literal. For