Chapter 4- KB
Chapter 4- KB
1
Knowledge Base Agent
Knowledge base agent is an agent that perform action using the
knowledge it has and reason about their action using its inference
procedure.
2
Cont…
• There must mechanisms to derive new sentences from old ones.
• This process is known as inferencing or reasoning.
• Inference must obey the primary requirement that the new
sentences should follow logically from the previous ones.
• Logic is the primary vehicle for representing and reasoning
about knowledge
Declarative approach to building an agent (or other system):
Tell it what it needs to know (Knowledge base)
Ask what it knows
Answers should follow from the KB
3
Knowledge-based agent program
• It takes a percept as input, returns an action and
maintains a kb (i.e., initial background
knowledge)
• Each time the agent program is called, it does
three things:
– It TELLs the kb what it perceives
– It ASKs the kb what action it should perform
– The agent program TELLs the kb which action was
chosen, and the agent executes the action
4
Cont…
• A generic knowledge-based agent
• function KB-AGENT(percept) returns an action
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
5
Knowledge Bases Agent
The agent must be able to:
– Represent states of the world, actions, etc.
– Incorporate new percepts (facts and rules)
– Deduce hidden properties of the world
– Deduce appropriate actions
– Update internal representations of the world
6
Knowledge Bases Agent
Representation of state of the world using KB
Example of KB written in that represent a state of a
world in which only Azieb is living (PROLOG
representation)
Moreover, the required information are gender and age
given name
FACTS
1.female(azieb).
2.age(azieb, 18).
7
Example
Agent should incorporate new percept
For example,
Azieb get maried with Melaku, they have two kids selam and solomon
This can be reflected by incorporating the new percepts
FACTS RULE
1.female(azieb). 1. married(X,Y):-married(Y,X).
2.Age(azieb, 18). 2. father(X,Y):-male(X),parent(X,Y).
3.Married(azieb, melaku).
3. mother(X,Y):-
4.male(melaku). female(X),parent(X,Y).
5.female(selam).
4. wife(X,Y):-parent(X,Z),parent(Y,Z).
6.male(solomon).
5. brother(X,Y):-male(X),
7.parent(melaku,selam). parent(Z,X),parent(Z,Y).
8.parent(azieb,selam).
6. sister(X,Y):-female(X),
9.parent(melaku,solomon). parent(Z,X),parent(Z,Y).
10.parent(azieb,solomon).
11.age(melaku,26).
12.age(selam, 5).
8
13.age(solomon, 1).
Example
The agent should deduce hidden portion of the world like Azieb and Selam
are beautiful.
If one asks questions like
?brother(aster,belay).
Married(melaku, azieb).
The system must respond correctly
Deduce appropriate action to query.
If one asks questions like
who is the sister of whom,
who is the father of belay,
who is the mother of Selam, etc
The system must respond correctly
Agent should update internal representation of the world.
For example, if died(melaku) is given we should modify any fact
that tells us about live history about melaku. 9
Knowledge representation
Knowledge representation refers to the technique how to express
the available facts and rules inside a computer so that agent will
use it to perform well.
Knowledge representation consists of:
– Syntax (grammar): possible physical configuration that
constitute a sentence (fact or rule) inside the agent
architecture.
• For example one possible syntax rule may be every
sentence must end with full stop.
– Semantics (concept): determine the facts in the world to
which the sentence refers
• Without semantics a sentence is just a sequence of
characters or binary sequences
• Semantic defines the meaning of the sentence
10
Knowledge representation
E.g., In the language of arithmetic
– x+2 ≥ y is a sentence; x2+y > {} is not a sentence
– x+2 ≥ y is true iff the number x+2 is not less than the number
y
– x+2 ≥ y is true in a world where x = 7, y = 1
– x+2 ≥ y is false in a world where x = 0, y = 6
12
Logic
Logic is the study of the principles of reasoning and arguments
towards the truth of a given conclusion given premises.
13
Types of Logic
In mathematics there are different kinds of logics.
Some of these according to order of their generality are
Propositional logic
First order logic
Second order logic and more
First order logic can be used to design, represent or infer for any
environment in the real world.
14
Propositional logic (PL)
Proposition is statement which is either true or false but not both at
any time.
A statement is a sentence which is either true or false.
PL uses declarative sentences only
PL doesn’t involve quantifiers.
Not all sentences are statement (interrogatives, imperatives and
exclamatory)
Proposition can be conditional or unconditional
Examples
Socrates is mortal (unconditional)
If the winter is severe, students will not succeed. (conditional)
All are the same iff their color is black (conditional)
In Propositional logic, symbols represent the whole Proposition.
Examples:
M = Socrates is mortal
W = winter is sever
S = students will not succeed 15
Con’t…
There are two types of Propositions:
i. Atomic Propositions
⚫ simple propositions.
⚫ It consists of a single proposition symbol.
⚫ These are the sentences which must be either true or false.
Example:
a. 2+2 is 4, it is an atomic proposition as it is a true fact.
Compound propositions
ii.
24-Dec-24 16
Propositional logic (PL)
Proposition symbols can be combined using Boolean connectives
to generate new Proposition with complex meaning
Symbols involved in PL:
Logical constants (TRUE and FALSE)
Proposition symbols (also called atomic symbols) like M, W, S
Logical connectives
(negation),
(conjunction),
(disjunction),
(bi-implication or equivalence),
(implication) and parenthesis
17
Propositional logic (PL)
Rules
Logical constants and propositional symbols are sentence by
them selves
Wrapping parenthesis around a sentence yield a sentence like
(P V Q)
Literal are atomic symbols or negated atomic symbols
Complex sentence can be formed by combining simpler
sentences with logical connectors
PL connector priority
Priority of logical connectives from highest to lowest
Parenthesis
Negation
Conjunction
Disjunction
Implication
Bi-implication 18
Types of Sentence
Given a sentence α, this sentence according to the world
considered can be
Valid (tautology)
Invalid (contradiction)
Satisfiable (neither valid nor invalid)
Validity (tautology)
A sentence is valid iff it is true under any interpretations in all
possible world
Example: x>4 or x<=4;
Water boils at 100 degree centigrade
Human has two legs (may not be valid)
Books have page number (may not be valid)
19
Satisfiability
A sentence is satisfiable iff there is some interpretation in some
world for which it is true.
Every valid sentence is satisfiable
Example: x+2 = 20
Every students of AI are in their class
A sentence which is not satisfiable is unsatisfiable (contradiction
or invalid).
20
Entailment
Entailment means that one thing follows from another:
It can be represented by ╞ symbol (double turnstyle)
KB ╞ α shows α can be entailed from KB
Knowledge base, KB entails sentence α if and only if α is true in
all worlds where KB is true
E.g., the KB containing “the Giants won” and “the Reds won”
entails “Either the Giants won or the Reds won”
E.g., x+y = 4 entails 4 = x+y
E.g., x+y = 4 can’t entails x= 2 and y = 2
Entailment is a relationship between sentences (i.e., syntax)
that is based on semantics
21
Inference Procedure
An inference procedure is a procedure used as reasoning engine.
It can do:
1. Given KB, generate new sentence α that can be entailed by KB
and we call the inference procedure entail α
2. Given KB and α, it will prove whether α is entailed by KB or not
KB ├i α means sentence α can be derived from KB by procedure i
(|- is called turnstyle or single turnstyle)
Inference Procedure property
Soundness: inference procedure i is said to be sound:
if whenever KB ├i α, it is also true that KB╞ α
A proof system is sound if everything that is provable is in fact true
(if KB ├i α then KB╞ α )
Completeness: inference procedure i is said to be complete if
whenever KB╞ α, it is also true that KB ├i α
A proof system is complete if everything that is true has a proof (if
KB╞ α then KB ├i α )
22
Inference Procedure property
The record of operation of a sound inference procedure is
called a proof
Soundness of an inference can be established through truth
table.
For example the inference procedure that entails P from a KB
P which Qconsists¬P
of PQ & Q isPnot
P^Q ∨ sound
Q 𝑃as
→shown
𝑄 bellow
PQ
25
Normal Forms of Logical expression
• Clausal normal form (disjunction normal form):
– it is a set of one or more literals connected with the
disjunction operator (disjunction of literals).
– It doesn’t allow negation marker except as a prefix to a
symbol
Example ~P Q ~R is a clausal form
– According to the definition a single symbol or negation of a
symbol is in clausal form
26
Normal Forms of Logical expression
• Conjunctive normal forms (CNF):
– Simply defined as a sequences of one or more clauses
connected by the conjunction operator
– Or it is conjunction of clauses
– Or conjunction of disjunction of literals
Example (A B) (C D)
– Note: connector is needed if we have two or more
elementary unit to connect to each other to form complex
expression
– This is the basis for the generalized resolution inference
procedure
27
Inference procedure and normal forms
The inference procedure that we have seen before are all sound
If KB is represented in CNF, the generalized resolution inference
procedure is complete
If KB is represented in Horn form, the generalized modes ponens
algorithm is complete
It can be proved that every sentence of human language can be
represented using logic as CNF. However, it is not possible in
Horn form.
Therefore, CNF is a more powerful representation technique for
knowledge
But, Horn form representation of knowledge is easily
understandable and convenient. It also require polynomial time
inference procedure
28
Logical equivalence (1)
Two sentences are logically equivalent iff they have the same truth
value in all possible world
Equivalently α ≡ ß iff α╞ β and β╞ α
29
Logical equivalence (2)
24-Dec-24 31
Generalized Resolution for PL
Given any two clauses A and B, if there are any literal P1 in A which
has a complementary literal P2 in B, delete P1 and P2 from A and B
and construct a disjunction of the remaining clauses.
The clause constructed is called the resolvent of A and B.
– For example, consider the following clauses
A: PQR
B: ~P Q M
C: ~Q S
From clause A and B, if we remove P and ~P, it resolves into clause
like
D : Q R Q M Q R M .
If Q of clause D and ~Q of clause C resolved, we get clause
E: R M S
32
Generalized Resolution for PL
Resolution can also be used to prove a sentence whether it is valid
or not.
That can be done using direct method or indirect method
(refutation)
For example Given a KB consists of the sentences
A: P Q R
B: ~P R
C: ~Q
We need to prove R
Method One (Direct)
Entail D: Q R from A and B
Entail E: R from D and C
Note: in order to apply resolution for proving a theory, make sure
first all the knowledge is in its clausal form
33
Predicate (First-Order) Logic
– We used propositional logic as our representation language
because it is one of the simplest languages that demonstrates
all the important points
24-Dec-24 34
Cont…
24-Dec-24 35
Cont…
• Examples:
24-Dec-24 36
Cont…
• Although FOL commits to the existence of objects and
relations,
It does not make an ontological commitment to such
things as:
Categories,
Time,
Events, which also seem to show up in most facts
about the world
24-Dec-24 37
SYNTAX AND SEMANTICS of FOL
• In propositional logic every expression is a sentence, which
represents a fact
• FOL has sentences, but it also has terms, which represent objects
24-Dec-24 40
Cont…
• 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.
e.g. Brother(Richard, John)
has value true or false
We can represent atomic sentences as:
• Predicate (term1, term2, , term n).
Example: Abdi and Ali are brothers: => Brothers(Abdi, Ali)
• Tom is a cat: => cat (Tom).
Atomic sentences can have arguments that are complex terms:
e.g. Married(FatherOf (Richard),MotherOf (John))
24-Dec-24 41
Cont…
• Complex sentences
24-Dec-24 42
Quantifiers
• FOL contains two standard quantifiers, called universal and
existential
Universal quantification ()
(x)P(x) means that P holds for all values of x in the
domain associated with that variable
E.g., (x) dolphin(x) mammal(x)
Existential quantification ()
( x)P(x) means that P holds for some value of x in the
domain associated with that variable
E.g., ( x) mammal(x) lays-eggs(x)
Permits one to make a statement about some object without
naming it
24-Dec-24 43
Cont…
• Universal quantifiers are often used with “implies” to form
“rules”:
24-Dec-24 44
Cont…
• Existential quantifiers are usually used with “and” to
specify a list of properties about an individual:
24-Dec-24 45
Cont…
• Nested quantifiers:
• We will often want to express more complex sentences
using multiple quantifiers
• For example, "For all x and all y, if x is the parent of y then
y is the child of x" becomes
x,y Parent(x,y) => Child(y,x)
• x,y is equivalent to x y.
• Similarly, the fact that a person's brother has that person as
a sibling is expressed by:
x,y Brother(x,y) => Sibling(y,x)
24-Dec-24 46
Cont…
• Quantifier Scope:
(x)(y)P(x, y) ↔ (y)(x) P( x, y)
– one is also saying that there does not exist someone who likes
them; and vice versa:
24-Dec-24 48
Cont…
• Equality
For example:
Father(John) = Henry
24-Dec-24 49
Higher-order logic
• Higher-order logic allows us to quantify over relations and
functions as well as over objects
• For example, in higher-order logic, we can say that two objects are
equal if and only if all properties applied to them are equivalent:
x, y (x = y) & (p p(x) p(y))
• Or we could say that two functions are equal if and only if they
have the same value for all arguments:
f,g (f = g) ( x f(x) = g(x))
• Higher-order logics have strictly more expressive power than first-
order logic
• But undecidable: there isn’t an effective algorithm to decide
whether all sentences are valid
24-Dec-24 50
What is Higher-Order Logic (HOL)
• Higher-Order Logic (HOL) extends first-order logic (FOL) by
allowing quantification over predicates and functions, not just
individuals or objects.
• First-Order Logic (FOL):
In FOL, the variables represent individuals (objects) in the
domain.
• For example:
– ∀x likes(x,y) means "every individual xxx likes some
individual y."
• Higher-Order Logic (HOL):
In HOL, you can quantify not only over individuals, but also over
predicates and functions. This adds another layer of abstraction.
– Example: ∀P P(John), where P is a predicate, and we are
quantifying over the predicate itself.
51
Key Differences B/n FOL and HOL:
Feature First-Order Logic (FOL) Higher-Order Logic (HOL)
Quantifies over individuals Quantifies over predicates,
Quantification
(objects). functions, and individuals.
Can express more complex
Limited to properties and relationships, such as
Expressiveness
relations of individuals. quantifying over functions and
sets.
∀f likes(f,x), where f is a
Examples ∀x likes(x,y)
function)
Complex, involving
Simple, no quantification over
Syntax quantification over
functions/predicates.
functions/predicates.
Higher-order logic is
Decidability First-order logic is decidable.
undecidable in general.
52
Examples of Higher-Order Logic:
• Quantifying over predicates:
– ∀P P(x) : This means "for every predicate P, P holds for
x."
– Here, PPP is a predicate variable, which can stand for
any predicate (such as "is a student", "is a parent",
etc.).
• Quantifying over functions:
– ∀f (f(x)=x+1)): This means "for every function f, f(x)
equals x+1."
– In this case, f is a function variable that represents any
function that takes x as an input.
53
Example with Functions:
• Let’s say we have the function f that maps people
to their ages.
• f:Person→N, where f(x) gives the age of person x.
• A higher-order logic statement might look like
this:
∀f ∀x (f(x)>0) This means "for every function f
and every person x, the age of x (given by f(x) is
greater than zero."
54
Use Cases of Higher-Order Logic:
• Mathematics: Higher-order logic is commonly used in set theory,
theories of functions, and mathematical logic, where we need to
quantify over sets, functions, and predicates.
56
Expressing uniqueness
• Sometimes we want to say that there is a single, unique
object that satisfies a certain condition
! x king(x)
24-Dec-24 57
Cont…
• “Every country has exactly one ruler”
c country(c) ! r ruler(c,r)
dead( x ruler(freedonia,x))
24-Dec-24 58
The End of chapter four
24-Dec-24 59
Converting to CNF
Converting the following sentence to CNF:
a ~ b ↔ c ≡ (a ~ b) ↔ c
60