Knowledge-Based Agents in AI
Knowledge-Based Agents in AI
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.
When there is a need to add/update some new information or sentences in the knowledge-
based system, we require an inference system. Also, to know what information is already
known to the agent, we require the inference system. The technical words used for describing
the mechanism of the inference system are: TELL and ASK. When the agent solves a problem,
it calls the agent program each time. The agent program performs three things:
1. It TELLS the knowledge base what it has perceived from the environment.
2. It ASKS the knowledge base about the actions it should take?
3. It TELLS the action which is chosen, and finally, the agent executes that action.
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:
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.
PEAS description of Wumpus world:
To explain the Wumpus world we have given PEAS description as below:
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.
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.
Syntax: The statements given in a problem are represented via propositional symbols. Each
sentence consists of a single propositional symbol. The propositional symbol begins with an
uppercase letter and may be followed by some other subscripts or letters. We have two fixed
propositional symbols, i.e., True and False.
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): Its value is true, iff the value of P is false, and that of 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 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.
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.
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
are as follows:
1. Eliminate ⇔, replacing α ⇔ β with (α ⇒ β) ∧ (β ⇒ α).
(B1,1 ⇒ (P1,2 ∨ P2,1)) ∧ ((P1,2 ∨ P2,1) ⇒ B1,1) .
2. Eliminate ⇒, replacing α ⇒ β with ¬α ∨ β:
(¬B1,1 ∨ P1,2 ∨ P2,1) ∧ (¬(P1,2 ∨ P2,1) ∨ B1,1) .
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:
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.
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
Backward Chaining in Propositional Logic
A Backward chaining algorithm is a form of reasoning which starts with the goal and works
backwards chaining through rules to find the known facts that supports the goal.
Properties:
[Link] 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.
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
have truth values; hence, the sentence as a whole could be judged true even before the model is
complete. For example, the sentence (A ∨ B) ∧ (A ∨ C) is true if A is true, regardless of the values
of B and C. Early termination avoids examination of entire subtrees in the search space.
b)A pure symbol is a symbol that always appears with the same “sign” in all clauses. For example,
in the three clauses (A ∨ ¬B), (¬B ∨ ¬C), and (C ∨ A)
c) Unit clause heuristic: A unit clause was defined earlier as a clause with just one literal. For
example, if the model contains B = true, then (¬B ∨ ¬C) simplifies to ¬C, which is a unit clause.
DAYANANDA SAGAR COLLEGE OF ENGINEERING
(An Autonomous Institute Affiliated to VTU, Belagavi)
Shavige Malleshwara Hills, Kumaraswamy Layout, Bengaluru-560078
Dr. Rajeshwari.J
Professor
Dayananda Sagar College of Engineering
First-Order logic:
o First-order logic is another way of knowledge representation in
artificial intelligence. It is an extension to propositional logic.
o FOL is sufficiently expressive to represent the natural language
statements in a concise way.
o 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 a more easy way and can
also express the relationship between those objects.
o 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:
Variables x, y, z, a, b,....
Connectives ∧, ∨, ¬, ⇒, ⇔
Equality ==
Quantifier ∀, ∃
DAYANANDA SAGAR COLLEGE OF ENGINEERING
(An Autonomous Institute Affiliated to VTU, Belagavi)
Shavige Malleshwara Hills, Kumaraswamy Layout, Bengaluru-560078
Dr. Rajeshwari.J
Professor
Dayananda Sagar College of Engineering
Atomic sentences:
o 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.
o We can represent atomic sentences as Predicate (term1, term2,
......, term n).
o A term is a logical expression that refers to an object.
Complex Sentences:
o Complex sentences are made by combining atomic sentences using
connectives.
[Link] 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 ∀.
Note: In universal quantifier we use implication "→".
f x is a variable, then ∀x is read as:
o For all x
o For each x
o For every x.
Example:
All man drink coffee.
Let a variable x
DAYANANDA SAGAR COLLEGE OF ENGINEERING
(An Autonomous Institute Affiliated to VTU, Belagavi)
Shavige Malleshwara Hills, Kumaraswamy Layout, Bengaluru-560078
Dr. Rajeshwari.J
Professor
Dayananda Sagar College of Engineering
2. 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 ∃,
Note: In Existential quantifier we always use AND or Conjunction
symbol (∧).
Example:
Some students are intelligent.
3. Nested Quantifiers:
It is the nesting of the same type of quantifier. One predicate is nested under the
other predicate. Two quantifiers are nested if one is within the scope of the other.
4. Equality: We use the equality symbol to express that two terms refer to the same
object. For example, Eleveenth_President(India)= Dr. APJ Abdul Kalam. Here,
both LHS is equal to RHS. It means that both terms refer to the same entity/
person.
Equality can also be used with the negation to insist that two terns are not the
same object.
Richard has at least two brothers
∃x,y Brother(x, Richard) ∧ Brother (x, Richard) ∧ ¬(x==y)
Example 6: Some cows are black and some cows are white.
Solution: Let, x be the cows. Therefore, it will be represented as:
Ǝx: cows(x) → black(x) Ʌ white(x).
Substitution:
o Universal Generalization
o Universal Instantiation
o Existential Instantiation
o Existential introduction
1. Universal Generalization:
o Universal generalization is a valid inference rule which states that if premise P(c)
is true for any arbitrary element c in the universe of discourse, then we can have
a conclusion as ∀ x P(x).
Example: Let's represent, P(c): "A byte contains 8 bits", so for ∀ x P(x) "All bytes
contain 8 bits.", it will also be true.
2. Universal Instantiation:
o The UI rule state that we can infer any sentence P(c) by substituting a ground
term c (a constant within domain x) from ∀ x P(x) for any object in the
universe of discourse.
Example:1.
Example: 2.
3. Existential Instantiation:
Example:
4. Existential introduction
Generalized Modus Ponens can be summarized as, " P implies Q and P is asserted to be
true, therefore Q must be True."
According to Modus Ponens, for atomic sentences pi, pi', q. Where there is a substitution
θ such that SUBST (θ, pi',) = SUBST(θ, pi), it can be represented as:
t is a lifted version of Modus Ponen as it uplifts the Modus Ponens from ground
propositions to FOPL. Generalized Modus Ponen is more generalized than Modus Ponen.
It is because, in generailzed, the known facts and the premise of the implication are
matched only upto a substitution, instead of its exact match.
Example:
We will use this rule for Kings are evil, so we will find some x such that x is
king, and x is greedy so we can infer that x is evil.
DAYANANDA SAGAR COLLEGE OF ENGINEERING
(An Autonomous Institute Affiliated to VTU, Belagavi)
Shavige Malleshwara Hills, Kumaraswamy Layout, Bengaluru-560078
Dr. Rajeshwari.J
Professor
Dayananda Sagar College of Engineering
Unification
What is Unification?
o Unification is a process of making two different logical atomic expressions
identical by finding a substitution. Unification depends on the substitution
process.
o It takes two literals as input and makes them identical using substitution.
o Let Ψ1 and Ψ2 be two atomic sentences and 𝜎 be a unifier such that, Ψ1𝜎 = Ψ2𝜎,
then it can be expressed as UNIFY(Ψ1, Ψ2).
Substitution θ = {John/x} is a unifier for these atoms and applying this substitution,
and both expressions will be identical.
o The UNIFY algorithm is used for unification, which takes two atomic sentences
and returns a unifier for those sentences (If any exist).
o Unification is a key component of all first-order inference algorithms.
o It returns fail if the expressions do not match with each other.
o The substitution variables are called Most General Unifier or MGU.
DAYANANDA SAGAR COLLEGE OF ENGINEERING
(An Autonomous Institute Affiliated to VTU, Belagavi)
Shavige Malleshwara Hills, Kumaraswamy Layout, Bengaluru-560078
Dr. Rajeshwari.J
Professor
Dayananda Sagar College of Engineering
E.g.2. Let's say there are two different expressions, P(x, y), and P(a, f(z)).
In this example, we need to make both above statements identical to each other. For
this, we will perform the substitution.
o 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.
o 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].
Unification Algorithm:
Algorithm: Unify(Ψ1, Ψ2)
Step. 1: If Ψ1 or Ψ2 is a variable or constant, then:
a) If Ψ1 or Ψ2 are identical, then return NIL.
b) Else if Ψ1is a variable,
a. then if Ψ1 occurs in Ψ2, then return FAILURE
b. Else return { (Ψ2/ Ψ1)}.
c) Else if Ψ2 is a variable,
a. If Ψ2 occurs in Ψ1 then return FAILURE,
b. Else return {( Ψ1/ Ψ2)}.
d) Else return FAILURE.
Step.2: If the initial Predicate symbol in Ψ1 and Ψ2 are not same, then
return FAILURE.
Step. 3: IF Ψ1 and Ψ2 have a different number of arguments, then return
FAILURE.
Step. 4: Set Substitution set(SUBST) to NIL.
DAYANANDA SAGAR COLLEGE OF ENGINEERING
(An Autonomous Institute Affiliated to VTU, Belagavi)
Shavige Malleshwara Hills, Kumaraswamy Layout, Bengaluru-560078
Dr. Rajeshwari.J
Professor
Dayananda Sagar College of Engineering
Examples
the ―highest‖ common descendant of any two nodes is the result of applying their most
general unifier.
predicate with n arguments contains O(2n ) nodes (in our example, we have two
arguments, so our lattice has four nodes)
Repeated constants = slightly different lattice.
3. Forward Chaining
First-Order Definite Clauses:
A definite clause either is atomic or is an implication whose antecedent is a conjunction of positive
literals and whose consequent is a single positive literal. The following are first-order definite clauses:
Unlike propositional literals, first-order literals can include variables, in which case those variables
are assumed to be universally quantified.
Starting from the known facts, it triggers all the rules whose premises are satisfied,
adding their conclusions lo the known facts
The process repeats until the query is answered or no new facts are added. Notice that a fact is
not "new" if it is just renamingof a known fact.
We will use our crime problem to illustrate how FOL-FC-ASK works. The implication
sentences are (1), (4), (5), and (6). Two iterations are required:
On the first iteration, rule (1) has unsatisfied premises.
Rule (4) is satisfied with {x/Ml), and Sells (West, M1, Nono) is added. Rule (5) is satisfied with
{x/M1) and Weapon (M1) is added.
Rule (6) is satisfied with {x/Nono}, and Hostile (Nono) is added.
On the second iteration, rule (1) is satisfied with {x/West, Y/MI, z /Nono), and Criminal
(West) is added.
It is sound, because every inference is just an application of Generalized Modus Ponens, it is
completefor definite clause knowledge bases; that is, it answers every query whose answers are
entailed by any knowledge base of definite clauses
The algorithm will check all the objects owned by Nono in and then for each object, it could
check whether it is a missile. This is the conjunct ordering problem:
―Find an ordering to solve the conjuncts of the rule premise so that the total cost is minimized‖.
The most constrained variable heuristic used for CSPs would suggest ordering the conjuncts
to look for missiles first if there are fewer missiles than objects that are owned by Nono.
The connection between pattern matching and constraint satisfaction is actually very close. We
can view each conjunct as a constraint on the variables that it contains-for example, Missile(x)
is a unary constraint on x. Extending this idea, we can express everyfinite-domain CSP as a
single definite clause together with some associated ground facts. Matching a definite clause
against a set of facts is NP-hard
3. Irrelevant facts:
One way to avoid drawing irrelevant conclusions is to use backward chaining.
Another solution is to restrict forward chaining to a selected subset of rules
A third approach, is to rewrite the rule set, using information from the [Link] that only
relevant variable bindings-those belonging to a so-called magic set-are considered during
forward inference.
For example, if the goal is Criminal (West), the rule that concludes Criminal (x) will be
rewritten to include an extra conjunct that constrains the value of x:
The fact Magic (West) is also added to the KB. In this way, even if the knowledge base contains
facts about millions of Americans, only Colonel West will be considered during the forward
inference process.
4. Backward Chaining
This algorithm work backward from the goal, chaining through rules to find known facts that support
the proof. It is called with a list of goals containing the original query, and returns the set of all
substitutions satisfying the query. The algorithm takes the first goal in the list and finds every clause
in the knowledge base whose head, unifies with the goal. Each such clause creates a new recursive
call in which body, of the clause is added to the goal stack .Remember that facts are clauses with a
head but no body, so when a goal unifies with a known fact, no new sub goals are added to the stack
and the goal is solved. The algorithm for backward chaining and proof tree for finding criminal (West)
using backward chaining are given below.