AI Notes
AI Notes
KNOWLEDGE REPRESENTATION
Knowledge-Based agents
An intelligent agent needs knowledge about the real world for taking decisions and
reasoning to act efficiently.
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.
Knowledge-based agents are composed of two main parts:
Knowledge-base and
Inference system.
A knowledge-based agent must able to do the following:
An agent should be able to represent states, actions, etc.
An agent Should be able to incorporate new precepts
An agent can update the internal representation of the world
An agent can perform appropriate actions.
The architecture of knowledge-based agent:
1. function KB-AGENT(percept):
2. persistent: KB - a knowledge base, t - a counter initially 0, indicating time
3. TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
4. Action = ASK(KB, MAKE-ACTION-QUERY(t))
5. TELL(KB, MAKE-ACTION-SENTENCE(action, t))
6. t = t + 1
7. return action
Each time when the function is called, it performs its three operations:
Firstly it TELLs the KB what it perceives.
Secondly, it ASKS KB what action it should take
Third agent program TELLS the KB that which action was chosen.
The MAKE-PERCEPT-SENTENCE generates a sentence as setting that the agent
perceived the given percept at the given time.
The MAKE-ACTION-QUERY generates a sentence to ask which action should be
done at the current time.
MAKE-ACTION-SENTENCE generates a sentence which asserts that the chosen
action was executed.
Problem definition: 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.
There are also some components which can help the agent to navigate the cave. These
components are given as follows:
The rooms adjacent to the Wumpus room are smelly, so that it would have some stench.
The room adjacent to PITs has a breeze, so if the agent reaches near to PIT, then he will
perceive the breeze.
There will be glitter in the room if and only if the room has gold.
The Wumpus can be killed by the agent if the agent is facing to it.
Propositional Logic
Propositional logic (PL) is the simplest form of logic where all the statements are made
by propositions.
A proposition is a declarative statement which is either true or false. It is a technique of
knowledge representation in logical and mathematical form.
Example:
a) It is Sunday.
b) The Sun rises from West (False proposition)
Following are some basic facts about propositional logic:
Propositional logic is also called Boolean logic as it works on 0 and 1.
In propositional logic, we use symbolic variables to represent the logic, and we can use
any symbol for a representing a proposition, such A, B, C, P, Q, R, etc.
Propositions can be either true or false, but it cannot be both.
Propositional logic consists of an object, relations or function, and logical connectives.
These connectives are also called logical operators.
A proposition formula which is always true is called tautology, and it is also called a
valid sentence.
A proposition formula which is always false is called Contradiction.
A proposition formula which has both true and false values is called
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 square is breezy if and only if there is a pit in a neighbouring square. This has to be
stated for each square; for now, we include just the relevant squares:
Now we include the breeze percept for the first two squares visited in the specific world
the agent is in
The knowledge base, then, consists of sentences R1 through R5.It can also be
considered as a single sentence-the conjunction R1 A Rz A R3 A R4 A R5-because it
asserts that all the individual sentences are true.
Inference
For propositional logic, models are assignments of true or false to every proposition symbol.
Returning to our wumpus-world example, the relevant proposition symbols are B1,1, B2,1, P1,1,
P1,2,P2,1, P2,2 and P3,1.With seven symbols, there are 27=128 possible models; in three of these,
KB is true.
Modus Ponens:
Example:
Statement-1: "If I am sleepy then I go to bed" ==> P→ Q
Statement-2: "I am sleepy" ==> P
Conclusion: "I go to bed." ==> Q.
Hence, we can say that, if P→ Q is true and P is true then Q will be true.
Modus Tollens:
And elimination:
AND Elimination:
The simplification rule state that if P∧ Q is true, then Q or P will also be true. It can be represented as:
NDRKFGC HASSAN
Hhassa
Biconditional elimination :
P > Q can be written as PQ A QP
Or
PQ A QP can be written as P > Q
Resolution :
Atomic proposition variable for Wumpus world:
o Let Pi,j be true if there is a Pit in the room [i, j].
o Let Bi,j be true if agent perceives breeze in [i, j], (dead or alive).
o Let Wi,j be true if there is wumpus in the square[i, j].
o Let Si,j be true if agent perceives stench in the square [i, j].
o Let Vi,j be true if that square[i, j] is visited.
o Let Gi,j be true if there is gold (and glitter) in the square [i, j].
o Let OKi,j be true if the room is safe.
We can prove that wumpus is in the room (1, 3) using propositional rules which we have derived for the wumpus world and using
inference rule.
We will firstly apply MP rule with R1 which is ¬S 11 → ¬ W 11 ^ ¬ W12 ^ ¬ W21, and ¬S11 which will give the output ¬ W 11 ^ W12 ^ W12.
After applying And-elimination rule to ¬ W11 ∧ ¬ W12 ∧ ¬ W21, we will get three statements:
¬ W11, ¬ W12, and ¬W21.
Now we will apply Modus Ponens to ¬S 21 and R2 which is ¬S21 → ¬ W21 ∧¬ W22 ∧ ¬ W31, which will give the Output as ¬ W21 ∧ ¬
W22 ∧¬ W31
NDRKFGC HASSAN
Hhassa
o Apply And -Elimination rule:
Now again apply And-elimination rule to ¬ W21 ∧ ¬ W22 ∧¬ W31, We will get three statements:
¬ W21, ¬ W22, and ¬ W31.
Apply Modus Ponens to S12 and R4 which is S12 → W13 ∨. W12 ∨. W22 ∨.W11, we will get the output as W13∨ W12 ∨ W22 ∨.W11.
After applying Unit resolution formula on W 13 ∨ W12 ∨ W22 ∨W 11 and ¬ W11 we will get W 13 ∨ W12 ∨ W 22.
After applying Unit resolution on W13 ∨ W12 ∨ W22, and ¬W22, we will get W13 ∨ W12 as output.
After Applying Unit resolution on W13 ∨ W12 and ¬ W12 , we will get W13 as an output, hence it is proved that the Wumpus is in the
room [1, 3].
NDRKFGC HASSAN
Hhassa
Agent Based on Propositional Logic
Example proof by deduction (Wumpus World): Let us take a condition which says there is
breeze in [2,2] square and there is no breeze in [2,2] square.
By applying inference rules, we have to prove any from the above condition.
The steps are given below:
NDRKFGC HASSAN
Hhassa
NDRKFGC HASSAN
Hhassa
From the above proof it is proved that there is no pit in [2,1], [2,3], [1,2] and [3,2] square.
That means there is no breeze in [2,2] square.
First-Order Logic
In propositional logic, we can only represent the facts, which are either true or false. PL
is not sufficient to represent the complex sentences or natural language statements. The
propositional logic has very limited expressive power.
First-Order logic:
First-order logic is another way of knowledge representation in artificial intelligence.
It is an extension to propositional logic.
NDRKFGC HASSAN
Hhassa
FOL is sufficiently expressive to represent the natural language statements in a conciseway.
First-order logic is also known as Predicate logic or First-order predicate logic. First-
order logic is a powerful language that develops information about the objects in an
easier way and can also express the relationship between those objects.
First-order logic (like natural language) does not only assume that the world contains
facts like propositional logic but also assumes the following things in the world:
Objects: A, B, people, numbers, colors, wars, theories, squares, pits, wumpus, ......
Relations: It can be unary relation such as: red, round, is adjacent, or n-any relation such
as: the sister of, brother of, has color, comes between
Function: Father of, best friend, third inning of, end of, ......
Variables x, y, z, a, b,....
Connectives 𝖠, ∨, ¬, ⇒, ⇔
Equality ==
Quantifier ∀, ∃
NDRKFGC HASSAN
Hhassa
The objects in the model may be related in various ways. In the figure, Richard and John are
brothers.
Thus, the brotherhood relation in this model is the set { (Richard the Lionheart, King John),
(King John, Richard the Lionheart) ).
Atomic sentences:
Atomic sentences are the most basic sentences of first-order logic. These sentences are formed
from a predicate symbol followed by a parenthesis with a sequence of terms.
We can represent atomic sentences as Predicate (term1, term2, ...... , term n).
Example: Ravi and Ajay are brothers: => Brothers(Ravi, Ajay).
Complex Sentences:
Complex sentences are made by combining atomic sentences using connectives.
First-order logic statements can be divided into two parts:
Subject: Subject is the main part of the statement.
Predicate: A predicate can be defined as a relation, which binds two atoms together in a
statement.
Example: >(1,2)V≤(1,2) => True
NDRKFGC HASSAN
Hhassa
Sisters(geeta,seta)˄sisters(geeta,leela)
Universal Quantifier:
Universal quantifier is a symbol of logical representation, which specifies that the
statement within its range is true for everything or every instance of a particular thing.
The Universal quantifier is represented by a symbol ∀, which resembles an inverted A.
If x is a variable, then ∀x is read as:
o For all x
o For each x
o For every x.
Existential Quantifier:
Existential quantifiers are the type of quantifiers, which express that the statement within its
scope is true for at least one instance of something. It is denoted by the logical operator ∃. If x
is a variable, then existential quantifier will be ∃x or ∃(x). And it will be read as:
o There exists a 'x.'
o For some 'x.'
o For at least one 'x.'
Let us take a variable x which can take the value of “student”. Let us take a predicate student(x)
which is true if x is a student. Similarly let us take another predicate likes(x,y) which is true if
x likes y.
NDRKFGC HASSAN
Hhassa
∃(x) student(x)˄likes(x,y) [y is ice cream]
There exists some x such that x is a student and also likes ice cream.
Some Examples of FOL using quantifier:
Properties of Quantifiers:
Equality
We can use the equality symbol to make statements to the effect that two terms refer to the
same object. For example,
Father(John) = Henry
says that the object referred to by Father(John) and the object referred to by Henry are the same.
Nested quantifiers
1. “Brothers are siblings” can be written as ∀ x ∀ y Brother (x, y) ⇒ Sibling(x, y)
2. siblinghood is a symmetric relationship, we can write ∀ x, y Sibling(x, y) ⇔ Sibling(y, x) .
3. “Everybody loves somebody” means that for every person, there is someone that person
loves: ∀ x ∃ y Loves(x, y) .
4. On the other hand, to say “There is someone who is loved by everyone,” we write ∃ y ∀ x
Loves(x, y) .
Connections between ∀ and ∃
The De Morgan rules for quantified and unquantified sentences are as follows:
NDRKFGC HASSAN
Hhassa
∀ x ¬P ≡ ¬∃ x P
¬(P ∨ Q) ≡ ¬P ∧ ¬Q
¬∀ x P ≡ ∃ x ¬P
¬(P ∧ Q) ≡ ¬P ∨ ¬Q
∀ x P ≡ ¬∃ x ¬P
P ∧ Q ≡ ¬(¬P ∨ ¬Q)
∃ x P ≡ ¬∀ x ¬P
P ∨ Q ≡ ¬(¬P ∧ ¬Q) .
Using FOL
Sentences are added to a knowledge base using TELL, exactly as in propositional logic. Such
sentences are called assertions. For example, we can assert that John is a king and that kings
are persons:
We can ask questions of the knowledge base using ASK. For example,
We will use the normal vocabulary of set theory as syntactic sugar. The empty set is a constant
written as { }. There is one unary predicate, Set, which is true of sets. The binary predicates are x∈
s (x is a member of set s) and s1 ⊆ s2 (set s1 is a subset, not necessarily proper, of set s2). The
binary functions are s1 ∩ s2 (the intersection of two sets), s1 ∪ s2 (the union of two sets), and {x|s}
(the set resulting from adjoining element x to set s).
One possible set of axioms is as follows:
1. The only sets are the empty set and those made by adjoining something to a set:
∀ s Set(s) ⇔ (s = { }) ∨ (∃ x, s2 Set(s2) ∧ s = {x|s2}) .
2. The empty set has no elements adjoined into it. In other words, there is no way to
decompose { } into a smaller set and an element:
¬∃ x, s {x|s} = { } .
3. Adjoining an element already in the set has no effect:
∀ x, s x∈ s ⇔ s = {x|s} .
4. The only members of a set are the elements that were adjoined into it. We express this
recursively, saying that x is a member of s if and only if s is equal to some set s2 adjoined
with some element y, where either y is the same as x or x is a member of s2:
∀ x, s x∈ s ⇔ ∃ y, s2 (s = {y|s2} ∧ (x = y ∨ x∈ s2)) .
5. A set is a subset of another set if and only if all of the first set’s members are members of
the second set:
∀ s1, s2 s1 ⊆ s2 ⇔ (∀ x x∈ s1 ⇒ x∈ s2) .
6. Two sets are equal if and only if each is a subset of the other:
∀ s1, s2 (s1 = s2) ⇔ (s1 ⊆ s2 ∧ s2 ⊆ s1) .
The agent’s location changes over time, so we write At(Agent, s, t) to mean that the agent is at
square s at time t. We can fix the wumpus’s location with
∀t At(Wumpus, [2, 2], t).
We can then say that objects can only be at one location at a time:
∀ x, s1, s2, t At(x, s1, t) ∧ At(x, s2, t) ⇒ s1 = s2 .
Given its current location, the agent can infer properties of the square from properties of its current
percept. For example, if the agent is at a square and perceives a breeze, then that square is breezy:
∀ s, t At(Agent, s, t) ∧ Breeze(t) ⇒ Breezy(s)
NDRKFGC HASSAN
Hhassa
Unification and Lifting
Unification is a process of making two different logical atomic expressions identical by
finding a substitution. Unification depends on the substitution process.
It takes two literals as input and makes them identical using substitution.
The UNIFY algorithm is used for unification, which takes two atomic sentences and
returns a unifier for those sentences (If any exist).
Unification is a key component of all first-order inference algorithms.
It returns fail if the expressions do not match with each other.
The substitution variables are called Most General Unifier or MGU.
NDRKFGC HASSAN
Hhassa
In this example, we need to make both above statements identical to each other. For this, we
will perform the substitution.
Substitute x with a, and y with f(z) in the first expression, and it will be represented as
a/x and f(z)/y.
With both the substitutions, the first expression will be identical to the second
expression and the substitution set will be: [a/x, f(z)/y].
Algorithm
Step.1: Initialize the substitution set to be empty.
Step.2: Recursively unify atomic sentences:
i. Check for Identical expression match.
ii. If one expression is a variable vi, and the other is a term ti which does not contain
variable vi, then:
I. Substitute ti / vi in the existing substitutions
II. Add ti /vi to the substitution setlist.
III. If both the expressions are functions, then function name must be similar, and
the number of arguments must be the same in both the expression.
Example1:
Given: Knows(Ram, X) is a predicate Whom does Ram knows?
The UNIFY algorithm will search all the related sentences in the knowledge base, which could
unify with Knows(Ram, X)
Example 2:. Find the MGU of {p(f(a), g(Y)) and p(X, X)}
Example 3: Find the MGU of {p(b, X, f(g(Z))) and p(Z, f(Y), f(Y))}
NDRKFGC HASSAN
Hhassa
S2 => { p(b, f(g(b)), f(g(b)); p(b, f(g(b)), f(g(b))} Unified Successfully.
Predicate symbol must be same, atoms or expression with different predicate symbol
can never be unified.
Example: UNIFY Knows (Ram,X) with Brother (Laxman, Ram)
Fails because predicate is different
Number of Arguments in both expressions must be identical.
Example: UNIFY hate (Marcus) with hate (Marcus, john)
Fails because number of arguments are different
Unification will fail if there are two similar variables present in the same expression.
Example: UNIFY Knows (Ram, X) with Knows (X, Raman)
NDRKFGC HASSAN
Hhassa
Inference Engine
The inference engine is the component of the expert system in AI, which applies logical
rules to the knowledge base to infer new information from known facts.
Forward and Backward chaining is the strategies used by the inference engine in
making the deductions.
Example of Inference chain
Rule 1: IF X is true
THEN A is true
Rule 2: IF A is true
AND B is true
AND C is true
THEN Y is true
Rule 3: IF Y is true
AND D is true
THEN Z is true
Forward Chaining:
Forward chaining is a form of reasoning which start with atomic sentences in the
knowledge base and applies inference rules (Modus Ponens) in the forward direction to
extract more data until a goal is reached.
Properties of Forward-Chaining:
It is a down-up approach, as it moves from bottom to top.
It is a process of making a conclusion based on known facts or data, by starting from
the initial state and reaches the goal state.
Forward-chaining approach is also called as data-driven as we reach to the goal using
available data.
Example:
Rule 1: IF student is excellent in academics
AND he is good in sports
THEN he is all rounder (Goal)
Rule 2: IF student gets marks > 80
AND he is good in genera knowledge
THEN he is excellent in academics
Rule 3: IF student is making centuries
THEN he is good in sports
NDRKFGC HASSAN
Hhassa
Given following facts:
1. Student gets marks > 80
2. Student is making centuries
3. He is good in general knowledge
Explanation:
In forward chaining we need to check the given facts in the IF statement, the first given
fact is present in the Rule 2 (i.e Student gets marks > 80).
In the Rule 2 along with the first given fact, third given fact is also present (i.e, He is
good in general knowledge).
From the Rule 2, it is inferred that he is excellent in academics (the subgoal).
Now remaining fact should be checked (i.e, student is making centuries)
This fact is present in the Rule 3. From that it is inferred that he is good in sports
(subgoal).
The two inferred subgoals are given in the Rule 1 IF, AND statement.
From this it is inferred that he is all rounder, which is the final goal.
Example:
"As per the law, it is a crime for an American to sell weapons to hostile nations. Country A, an enemy of America, has some
missiles, and all the missiles were sold to it by Robert, who is an American citizen."
Step-1:
o In the first step we will start with the known facts and will choose the sentences which do not have implications, such
as: American(Robert), Enemy(A, America), Owns(A, T1), and Missile(T1). All these facts will be represented as below.
o
NDRKFGC HASSAN
Hhassa
o Step-2:
o At the second step, we will see those facts which infer from available facts and with satisfied premises.
o Rule-(1) does not satisfy premises, so it will not be added in the first iteration.
o Rule-(2) and (3) are already added.
o Rule-(4) satisfy with the substitution {p/T1}, so Sells (Robert, T1, A) is added, which infers from the conjunction of Rule (2)
and (3).
o Step-3:
o At step-3, as we can check Rule-(1) is satisfied with the substitution {p/Robert, q/T1, r/A}, so we can add
Criminal(Robert) which infers all the available facts. And hence we reached our goal statement.
Backward Chaining
A backward chaining algorithm is a form of reasoning, which starts with the goal and works
backward, chaining through rules to find known facts that support the goal.
Properties of backward chaining:
It is known as a top-down approach.
Backward-chaining is based on modus ponens inference rule.
In backward chaining, the goal is broken into sub-goal or sub-goals to prove the facts
true.
It is called a goal-driven approach, as a list of goals decides which rules are selected
and used.
Example:
Rule 1: IF student is excellent in academics
AND he is good in sports
NDRKFGC HASSAN
Hhassa
THEN he is all rounder (Goal)
Rule 2: IF student gets marks > 80
AND he is good in genera knowledge
THEN he is excellent in academics
Rule 3: IF student is making centuries
THEN he is good in sports
Given following facts:
1. Student gets marks > 80
2. Student is making centuries
3. He is good in general knowledge
Explanation:
In Backward chaining from the goal we need to reach the given facts. Here the goal is
he is all rounder.
First we need to check whether goal is present in given facts, if not the statements in IF,
AND condition should be treated as subgoals. In the given example IF student is
excellent in academics and AND he is good in sports are the subgoals.
Next check the subgoals in IF statement, student is excellent in academics is present
in Rule 2 inferred part, so from that we got two given facts i.e student gets marks >
80 and he is good in genera knowledge.
Next remaining subgoal (i.e he is good in sports) is checked in inferred part; it is
present in Rule 3. From this we got one more fact i.e, student is making centuries.
Finally we got all the given facts from the inferred statements.
Example 2:
In backward-chaining, we will use the same above example, and will rewrite all the rules.
NDRKFGC HASSAN
Hhassa
Step-1:
o At the first step, we will take the goal fact. And from the goal fact, we will infer other facts, and at last, we will prove those
facts true. So our goal fact is "Robert is Criminal," so following is the predicate of it.
Step-2:
o At the second step, we will infer other facts form goal fact which satisfies the rules. So as we can see in Rule -1, the goal
predicate Criminal (Robert) is present with substitution {Robert/P}. So we will add all the conjunctive facts below the first level
and will replace p with Robert.
o Here we can see American (Robert) is a fact, so it is proved here.
Step-3:t At step-3, we will extract further fact Missile(q) which infer from Weapon(q), as it satisfies Rule-(5). Weapon (q) is also true with
the substitution of a constant T1 at q.
o Step-4:
At step-4, we can infer facts Missile(T1) and Owns(A, T1) form Sells(Robert, T1, r) which satisfies the Rule- 4, with the substitution of A in
place of r. So these two statements are proved here.
NDRKFGC HASSAN
Hhassa
Step-5:
At step-5, we can infer the fact Enemy(A, America) from Hostile(A) which satisfies Rule- 6. And hence all the statements are proved
true using backward chaining.
1. Forward chaining starts from known facts and Backward chaining starts from the goal and works
applies inference rule to extract more data backward through inference rules to find the
unit it reaches to the goal. required facts that support the goal.
5. Forward chaining tests for all the available Backward chaining only tests for few required
rules rules.
6. Forward chaining is suitable for the planning, Backward chaining is suitable for diagnostic,
monitoring, control, and interpretation prescription, and debugging application.
application.
7. Forward chaining can generate an infinite Backward chaining generates a finite number of
number of possible conclusions. possible conclusions.
9. Forward chaining is aimed for any conclusion. Backward chaining is only aimed for the required
data.
NDRKFGC HASSAN
Hhassa