Unit IV - Knowledge Agents
Unit IV - Knowledge 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.
• Knowledge-based agents are composed of two main parts:
o Knowledge-base and
o Inference system.
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
Each time when the function is called, it performs its three operations:
A knowledge-based agent can be viewed at different levels which are given below:
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:
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 implement his knowledge and logic so that he can reach to the
destination.
Wumpus world:
• 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.
The rooms adjacent to the Wumpus room are smelly, so that it would have some
stench.
a. The room adjacent to PITs has a breeze, so if the agent reaches near to PIT,
then he will perceive the breeze.
b. There will be glitter in the room if and only if the room has gold.
c. 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:
Performance measure:
• +1000 reward points if the agent comes out of the cave with the gold.
• -1000 points penalty for being eaten by the Wumpus or falling into the pit.
• -1 for each action, and -10 for using an arrow.
• The game ends if either agent dies or came out of the cave.
Environment:
Actuators:
• Left turn,
• Right turn
• Move forward
• Grab
• Release
• Shoot.
Sensors:
• The agent will perceive the stench if he is in the room adjacent to the Wumpus.
(Not diagonally).
• The agent will perceive breeze if he is in the room directly adjacent to the Pit.
• The agent will perceive the glitter in the room where the gold is present.
• The agent will perceive the bump if he walks into a wall.
• When the Wumpus is shot, it emits a horrible scream which can be perceived
anywhere in the cave.
• These percepts can be represented as five element list, in which we will have
different indicators for each sensor.
• 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].
Properties:
Now we will explore the Wumpus world and will determine how the agent will find its
goal by applying logical reasoning.
Step1:
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.
Step2:
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.
Step3:
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].
Step4:
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:
• 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)
c) 3+3= 7(False proposition)
d) 5 is a prime number.
Syntax:
• The syntax of propositional logic defines the allowable sentences for the knowledge
representation. There are two types of Propositions:
Example:
Logical Connectives:
Semantics:
• The semantics defines the rules for determining the truth of a sentence with
respect to a particular model. In propositional logic, a model simply fixes the truth
value—true or false—for every proposition symbol.
T T T T T T
T F F T F F
F T F T T F
F F F F T T
• Atomic sentences are easy: True is true in every model and False is false in
every model.
• For complex sentences, we have five rules, which hold for any subsentences P
and Q in any model m :
• ¬P is true iff P is false in m.
Logical Equivalences:
• (P ∧ Q) ≡ (Q ∧ P) commutativity of ∧
(P ∨ Q) ≡ (Q ∨ P) commutativity of ∨
• ((P ∧ Q) ∧ R) ≡ (P ∧ (Q ∧ R)) associativity of ∧
((P ∨ Q) ∨ R) ≡ (P ∨ (Q ∨ R)) associativity of ∨
1. ¬(¬P) ≡ P double-negation elimination
Inference Rules:
• Modus Ponens Rule:
The Modus Ponens rule states that if P and and P → Q is true, then we can infer
that Q will be true. It can be represented as:
• And-Elimination:
And–Elimination rule state that if P∧ Q is true, then Q or P will also be true. It can
be represented as:
Resolution in PL:
A resolution algorithm is to first, (KB ∧ ¬P) is converted into CNF. Then, the resolution
rule is applied to the resulting clauses. Each pair that contains complementary literals
is resolved to produce a new clause, which is added to the set if it is not already
present. The process continues until one of two things happens:
• there are no new clauses that can be added, in which case KB does not entail P; or,
• two clauses resolve to yield the empty clause, in which case KB entails P.
Algorithm:
• 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. Consider
the following sentence, which we cannot represent using PL logic.
1. "Some humans are intelligent"
• To represent the above statements, PL logic is not sufficient, so we required some
more powerful logic, such as first-order logic.
• First-order logic is also known as Predicate logic or First-order predicate logic.
• To 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:
o Objects: A, B, people, numbers, colors, wars, theories, squares, pits,
wumpus, ......
o 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
o Function: Father of, best friend, third inning of, end of, ......
Variables x, y, z, a, b……
Connectives ∧, ∨, ¬, ⇒, ⇔
Quantifier ∀, ∃
Terms :
City(Delhi)
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).
Brothers(Ravi, Ajay).
Complex Sentences:
• Complex sentences are made by combining atomic sentences using
connectives.
Quantifiers:
Universal Quantifier:
• For all x
• For each x
• For every x.
Example:
It will be read as: There are all x where x is a man who drink coffee.
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 ∃, which resembles as inverted E. When it is
used with a predicate variable then it is called as an existential quantifier.
• If x is a variable, then existential quantifier will be ∃x or ∃(x). And it will be read as:
Example:
It will be read as: There are some x where x is a boy who is intelligent.
Properties of Quantifiers:
Examples of FOL:
∀x bird(x) →fly(x).
• ¬(∀ x p) ≡ ∃ x ¬p
• ¬(∃ x p) ≡ ∀ x ¬p
• ¬(¬P) ≡ P (double-negation elimination)
Resolution in FOL:
Inferences in FOL:
1. Let us begin with Suppose our knowledge base contains the standard folkloric
axiom stating that all greedy kings are evil:
∀ x King(x) ∧ Greedy(x) ⇒ Evil(x)
2. Universal Instantiation :
The rule of Universal Instantiation (UI for short) says that we can infer any sentence
obtained by substituting a ground term (a term without variables) for the variable. Let
SUBST(θ,α) denote the result of applying the substitution θ to the sentence α. Then
the rule is written as,
3. Existential Instantiation:
The variable is replaced by a single new constant symbol. The formal statement is as
follows: for any sentence α, variable v, and constant symbol k that does not appear
elsewhere in the knowledge base,
Unification:
• 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.
• 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.
E.g. Let's say there are two different expressions, P(x, y), and P(a, f(z)).
• 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:
Example :
Forward Chaining:
Properties of Forward-Chaining:
Algorithm:
Backward Chaining:
Algorithm:
Following are some main steps of the knowledge-engineering process. Using these
steps, we will develop a knowledge base which will allow us to reason about digital
circuit (One-bit full adder) which is given below:
The first step of the process is to identify the task, and for the digital circuit, there are
various reasoning tasks.
At the first level or highest level, we will examine the functionality of the circuit:
At the second level, we will examine the circuit structure details such as:
In the second step, we will assemble the relevant knowledge which is required for
digital circuits. So for digital circuits, we have the following required knowledge:
3. Decide on vocabulary:
• The next step of the process is to select functions, predicate, and constants to
represent the circuits, terminals, signals, and gates. Firstly we will distinguish the
gates from each other and from other objects. Each gate is represented as an
object which is named by a constant, such as, Gate(X1). The functionality of each
gate is determined by its type, which is taken as constants such as AND, OR, XOR,
or NOT. Circuits will be identified by a predicate: Circuit (C1).
For gate input, we will use the function In(1, X1) for denoting the first input terminal of
the gate, and for output terminal we will use Out (1, X1).
The function Arity(c, i, j) is used to denote that circuit c has i input, j output.
We use a unary predicate On (t), which is true if the signal at a terminal is on.
To encode the general knowledge about the logic circuit, we need some following
rules:
• If two terminals are connected then they have the same input signal, it can be
represented as:
∀ t1, t2 Terminal (t1) ∧ Terminal (t2) ∧ Connect (t1, t2) → Signal (t1) = Signal (2)
• Signal at every terminal will have either value 0 or 1, it will be represented as:
• Output of AND gate will be zero if and only if any of its input is zero.
∀ g Gate(g) ∧ Type(g) = XOR → Signal (Out(1, g)) = 1 ⇔ Signal (In(1, g)) ≠ Sign
al (In(2, g)).
• Output of NOT gate is invert of its input:
• Now we encode problem of circuit C1, firstly we categorize the circuit and its gate
components. This step is easy if ontology about the problem is already thought.
This step involves the writing simple atomics sentences of instances of concepts,
which is known as ontology.
For the given circuit C1, we can encode the problem instance in atomic sentences as
below:
Since in the circuit there are two XOR, two AND, and one OR gate so atomic sentences
for these gates will be:
In this step, we will find all the possible set of values of all the terminal for the adder
circuit. The first query will be:
What should be the combination of input which would generate the first output of circuit
C1, as 0 and a second output to be 1?
∃ i1, i2, i3 Signal (In(1, C1))=i1 ∧ Signal (In(2, C1))=i2 ∧ Signal (In(3, C1))= i3 ∧ Si
gnal (Out(1, C1)) =0 ∧ Signal (Out(2, C1))=1
Now we will debug the knowledge base, and this is the last step of the complete
process. In this step, we will try to debug the issues of knowledge base.