MODULE 4
KNOWLEDGE BASED AGENTS
• “Humans, it seems, know things and, what they know helps them do
things”
– These are not empty statements.
– They make strong claims about how the intelligence of humans is achieved—
not by purely reflex mechanisms but by processes of reasoning that operate
on internal representations of knowledge.
In AI, this approach to intelligence is embodied
in 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.
• For example,(knowledge & implementation level)
– Consider an “automated air conditioner.” The inbuilt knowledge stored in its
system is that “ It would adjust its temperature according to the weather.” This
represents the knowledge level of the agent. The actual working and its
adjustment define the implementation level of the knowledge-based agent.
• Knowledge-based agents are composed of two main parts:
– Knowledge-base and
– Inference system.
Another example?
A knowledge-based agent[central component] must able to
do the following:
• Represent states, actions, etc.
• Incorporate new percepts
• Update & Deduce the internal representation of the world
• Deduce appropriate actions.
Inference system
• Inference means deriving new sentences from old.
– A sentence is a proposition about the world.
Inference system allows us : -
– to add a new sentence to the knowledge base. – applies
logical rules to the KB to deduce new information. –
generates new facts so that an agent can update the KB.
– works mainly in two rules which are given as:
Forward chaining-FACT GOAL
Backward chaining- GOAL FACT
OPERATIONS PERFORMED BY KBA
Three operations which are performed by KBA
in order to show the intelligent behaviour:
• TELL: This operation tells the knowledge base
what it perceives from the environment.
• ASK: This operation asks the knowledge base
what action it should perform.
– extensive reasoning may be done about
» current state of the world,
» outcomes of possible action sequences, and so on.
• PERFORM: It performs the selected action.
• MAKE-PERCEPT-SENTENCE()
Returns a sentence which tells the perceived information by the agent at a given
time.
• MAKE-ACTION-QUERY()
Returns a sentence which tells what action the agent must take at the current
time. • MAKE-ACTION-SENTENCE()
Returns a sentence which tells an action is selected as well as
executed. The details of the inference mechanisms are hidden inside TELL and ASK
Wumpus World Problem
THE WUMPUS WORLD-Describe an environment
in which knowledge-based agents can show their
worth
The wumpus world is a cave consisting of rooms
connected by passage ways. Lurking somewhere in the
cave is the terrible wumpus, a beast that eats anyone who
enters its room. The wumpus can be shot by an agent, but
the agent has only one arrow. Some rooms contain
bottomless pits that will trap anyone who wanders into
these rooms (except for the wumpus, which is too big to
fall in). The only mitigating feature of this bleak
environment is the possibility of finding a heap of gold.
Although the wumpus world is rather tame by modern
computer game standards, it illustrates some important
points about intelligence
It is a simple world example to illustrate the
worth of a “knowledge-based agent” and to
“represent” knowledge representation
There are also some components which can help the agent to
navigate the cave
• 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 towards it, and
Wumpus will emit a horrible scream which can be heard anywhere in the
cave.
Assumptions
• 4×4 grid, tiles numbered (1,1) to (4,4)
• The agent starts in (1,1),agent is aware of the env (KB) • The beast
Wumpus sits at a random tile, unknown to the agent, • – a pile of
gold sits at another random tile, unknown to the agent, • – some
pits are located at random tiles, unknown to the agent. • – if the
agent enters the tile of the Wumpus, he will be eaten, • – if the
agent enters a pit, he will be trapped.
PEAS description
Performance measure
+1000 for climbing out of the cave with the gold,
–1000 for falling into a pit or being eaten by the wumpus, –1 for
each action taken and –10 for using up the arrow. The game
ends either when the agent dies or when the agent climbs out of
the cave.
Environment
A 4×4 grid of rooms.
– The agent always starts in the square labeled [1,1], facing
to the right.
– The locations of the gold and the wumpus are chosen
randomly, with a uniform distribution, from the squares
other than the start square.
» In addition, each square other than the start can be a pit, with
probability 0.2.
Actuators
• Left turn by 90 degree •
Right turn by 90 degree •
Move forward
• Grab
• Release
• Shoot.
Sensors
• The agent has five sensors, each of which gives a single bit of information – In
the square containing the wumpus and in the directly (not diagonally) adjacent
squares, the agent will perceive a Stench.
– In the squares directly adjacent to a pit, the agent will perceive a Breeze.
In the square where the gold is, the agent will perceive a Glitter.
– When an agent walks into a wall, it will perceive a Bump.
– When the wumpus is killed, it emits a woeful Scream that can be perceived
anywhere in the cave.
• Moves allowed” L , R ,U ,D , G(grab)
Start (1,1) Stench (1,2) & Breeze (2,1)
(2,1)[ breeze ]
(3,1) [pit] ?
(2,2) [pit ]?
Safely moves to (1,1)
Start from (1,2)Stench (2,2) & (1,3) ? W
Conclusion/Inference : - (2,2) IS SAFE !!
Start from (2,2) (2,3) & Breeze (3,2)
Move to (3,2) : Pit at (3,3) & (3,1) Move
back to (2,2) & explore (2,3) (2,3)
PERCEIVES GOLD !!!!!
Return via the same path agent went by
Uses ARROW (3,3)Pit , NO SCREAM
(1,3) Wumpus , SCREAM
The Wumpus world Properties
• Partially observable: The Wumpus world is partially observable because the
agent can only perceive the close environment such as an adjacent room.
• Deterministic: It is deterministic, as the result and outcome of the world are
already known.
• Sequential: The order is important, so it is sequential.
• Static: It is static as Wumpus and Pits are not moving.
• Discrete: The environment is discrete.
• One agent: The environment is a single agent as we have one agent only
and Wumpus is not considered as an agent
Propositional Logic (PL)
• 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:
It is Sunday.(True)
The Sun rises from West (False proposition)
3+3= 7(False proposition)
5 is a prime number.(True)
Some basic facts about PL.
• Boolean logic - works on 0 and 1.
• Use symbolic variables to represent the logic (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 (logical
operators)
• The propositions and connectives are the basic elements of the propositional logic. –
Connectives is a logical operator which connects two sentences.
• 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.
• Statements which are questions, commands, or opinions are not propositions such as "Where is
John", "How are you", "What is your name", are NOT PROPOSITIONS.
PROPOSTIONAL LOGIC-”REVISION”
• FORMAL PROOFS
• NORMAL FORMS
Left as an exercise to students
REVISION
• Negation: A sentence such as ¬ P is called negation of P.
– A literal can be either Positive literal or negative literal.
• Conjunction: A sentence which has ∧ connective such as, P ∧ Q is called a
conjunction.
– Example: Rohan is intelligent and hardworking. It can be written as,
P= Rohan is intelligent,
Q= Rohan is hardworking. → P∧ Q.
• Disjunction: A sentence which has ∨ connective, such as P ∨ Q. is called
disjunction, where P and Q are the propositions.
– Example: "Ritika is a doctor or Engineer",
Here P= Ritika is Doctor. Q= Ritika is Doctor, so we can write it as P ∨ Q.
• Implication: A sentence such as P → Q, is called an implication.
– Implications are also known as if-then rules. It can be represented as
If it is raining, then the street is wet.
Let P= It is raining, and Q= Street is wet, so it is represented as P → Q
• Biconditional: A sentence such as P⇔ Q is a Biconditional sentence, example If I
am breathing, then I am alive
P= I am breathing, Q= I am alive, it can be represented as P ⇔ Q.
Properties of Operators
• Commutativity:
– P∧ Q= Q ∧ P, or
– P ∨ Q = Q ∨ P.
• Associativity:
– (P ∧ Q) ∧ R= P ∧ (Q ∧ R),
– (P ∨ Q) ∨ R= P ∨ (Q ∨ R)
• Identity element:
– P ∧ True = P,
– P ∨ True= True.
• Distributive:
– P∧ (Q ∨ R) = (P ∧ Q) ∨ (P ∧ R).
– P ∨ (Q ∧ R) = (P ∨ Q) ∧ (P ∨ R).
• DE Morgan's Law:
– ¬ (P ∧ Q) = (¬P) ∨ (¬Q)
– ¬ (P ∨ Q) = (¬ P) ∧ (¬Q).
• Double-negation elimination:
– ¬ (¬P) = P.
Inference Rules
• 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.
Thus, if P→ Q is true and P is true then Q will
be true.
• Statement-1: "If I am sleepy then I go to bed" ==>
P→ Q
Statement-2: "I do not go to bed."==> ~Q
Statement-3: Which infers that "I am not sleepy"
=> ~P
• Statement-1: If you have my home key then you can
unlock my home. P→Q
Statement-2: If you can unlock my home then you
can take my money. Q→R
Conclusion: If you have my home key then you can
take my money. P→R
• Statement-1: Today is Sunday or Monday. ==>P∨Q
Statement-2: Today is not Sunday. ==> ¬P
Conclusion: Today is Monday. ==> Q
Limitations of Propositional logic:
Cannot represent relations like ALL, some, or none with propositional logic.
Example:
– All the girls are intelligent.
– Some apples are sweet.
• Propositional logic has limited expressive power.
– In propositional logic, we cannot describe statements in terms of their properties
or logical relationships.
"Some humans are intelligent", or
"Sachin likes cricket."
• To represent the above statements, PL logic is not
sufficient, so we required some more powerful logic,
such as FIRST-ORDER LOGIC.
Knowledge-base for Wumpus world
• Atomic proposition variable for Wumpus world: – Let
Pi,j be true if there is a Pit in the room [i, j]. – Let Bi,j be true
if agent perceives Breeze in [i, j] – Let Wi,j be true if there is
Wumpus in the square[i, j]. – Let Si,j be true if agent
perceives Stench in the square [i, j].
Some Propositional Rules for the
wumpus world:
Prove that Wumpus is in the room (1, 3)
1. Apply Modus Ponens with ¬S11 and R1
2. Apply Modus Ponens to ¬S21, and R2
3. Apply Modus Ponens to S12 and R4
4. Apply Unit resolution on W13 ∨ W12 ∨ W22 ∨W11 and ¬ W11
5. Apply Unit resolution on W13 ∨ W12 ∨ W22 and ¬ W22 6.
Apply Unit Resolution on W13 ∨ W12 and ¬ W12
~S11 ~S21
Proving W13 (R1) ~S11 => ~W11 ^ ~W12 ^
• Apply MP with ~S11 and R1: ~W21 ~W11 ^ ~W12 ^ ~W21
~W11 ^ ~W12 ^ ~W21
• Apply And-Elimination to this we get 3 W13 v W12 v W22 v W11
sentences: ~W11, ~W12, ~W21 R2
~W22, ~W21, ~W31
• Apply MP to ~S21 and R2, then applying
And-elimination: ~W22, ~W21, ~W31
• Apply MP to S12 and R4 we obtain:
• Apply Unit Resolution with (W13 v W12 v
W22) and ~W22 W13 v W12
• Apply UR with (W13 v W12) and ~W12
• Apply Unit resolution on (W13 v W12 v W22 S12 R4
v W11) and ~W11 W13 v W12 v W22
W13
• QED 52
(W13 v W12 v W22 v W11)
~W11 (W13 v W12 v W22) ~W22 W13 v W12
~W12 W13
Prove that Wumpus is in the room (1, 3)
Apply Modus Ponens with ¬S11 and R1:
• We will firstly apply MP rule with R1 which is
¬S11 → ¬ W11 ^ ¬ W12 ^ ¬ W21, and ¬S11 which will give the
output ¬ W11 ^ W12 ^ W12.
Apply And-Elimination Rule:
• After applying And-elimination rule to ¬ W11 ∧ ¬ W12 ∧ ¬ W21,
we will get three statements:
¬ W11, ¬ W12, and ¬W21.
Apply Modus Ponens to ¬S21, and R2:
• Now we will apply Modus Ponens to ¬S21 and R2 which is
¬S21 → ¬ W21 ∧¬ W22 ∧ ¬ W31, which will give the
Output as ¬ W21 ∧ ¬ W22 ∧¬ W31
• Apply And -Elimination rule:
Now again apply And-elimination rule to
¬ W21 ∧ ¬ W22 ∧¬ W31, We will get three statements:
¬ W21, ¬ W22, and ¬ W31.
• Apply MP to S12 and R4:
Apply Modus Ponens to S12 and R4 which is
S12 → W13 ∨ W12 ∨ W22 ∨.W11, we will get the
output as W13∨ W12 ∨ W22 ∨.W11.
• Apply Unit resolution on W13 ∨ W12 ∨ W22 ∨W11 and ¬ W11 :
After applying Unit resolution formula on W13 ∨ W12 ∨ W22
∨W11 and ¬ W11 we will get W13 ∨ W12 ∨ W22.
• Apply Unit resolution on W13 ∨ W12 ∨ W22 and ¬ W22 : After
applying Unit resolution on W13 ∨ W12 ∨ W22, and ¬W22, we will
get W13 ∨ W12 as output.
• Apply Unit Resolution on W13 ∨ W12 and ¬ W12 : • 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].
FOL-First Order Logic
Ex 1 : Parrots are green. Parrots(X) Green(X)
Ex 2: All kids like toys. like ( kids , toys ).
Ex 3: Some kids like toys. like ( kids , toys ).
Quantifiers! FOL
FIRST-ORDER LOGIC (FOL)
• Knowledge representation in artificial intelligence.
– Extension to propositional logic.
• Represent natural language statements in a concise way.
– (Predicate logic or First-order predicate logic)
• Powerful language
• First-order logic (like natural language) assumes:
– 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,……..
Syntax of FOL
– Constant symbols (i.e., the "individuals" in the world) E.g., Divya, 3 –
Function symbols (mapping individuals to individuals) E.g., father-of(Divya) =
John, color-of(Sky) = Blue
– Predicate symbols (mapping from individuals to truth values) E.g.,
greater(5,3), green(Grass), color(Grass, Green)
Syntax of FOL
• Variable symbols. E.g., x, y
• Connectives. Same as in PL: not (~), and (^), or (v),
implies (=>), if and only if (<=>)
• Quantifiers: Universal ( ∀) and
Existential ( ∃)
Basic elements of FOL syntax
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.
• Representation: - 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.
• Consider the statement: "x is an integer.", it consists of two parts,
the first part x is the subject of the statement and
second part "is an integer," is known as a
predicate.
Quantifiers in First-order logic:
Universal Quantifier, (for all, everyone, everything)
• For all x
• For each x
• For every x
∀x man(x) → drink (x, coffee).
It will be read as: There are all x where x is a man who drink coffee.
Existential quantifier, (for some, at least one)
• There exists a 'x.'
• For some 'x.'
• For at least one 'x.‘
∃x: kid(x) ∧ intelligent(x)
It will be read as: There are some x where x is a kid who is intelligent.
Points to remember:
• The main connective for universal quantifier ∀
is implication →
• The main connective for existential quantifier
∃ is and ∧
Properties of Quantifiers:
• In universal quantifier, ∀x∀y is similar to ∀y∀x.
• In Existential quantifier, ∃x∃y is similar to ∃y∃x.
• ∃x∀y is not similar to ∀y∃x.
Example FOL using quantifier:
• 1. All birds fly.
• In this question the predicate is "fly(bird)."
And since there are all birds who fly so it will be represented as follows.
∀x bird(x) →fly(x).
• 2. Every man respects his parent.
• In this question, the predicate is "respect(x, y)," where x=man, and y= parent.
Since there is every man so will use ∀, and it will be represented as follows:
∀x man(x) → respects (x, parent).
• 3. Some boys play cricket.
• In this question, the predicate is "play(x, y)," where x= boys, and y= game. Since there are some boys
so we will use ∃, and it will be represented as:
∃x boys(x) ∧ play(x, cricket).
Some more examples All students are smart.
∀ x ( Student(x) Smart(x) )
There exists a student.
∃ x Student(x).
There exists a smart student.
∃ x ( Student(x) ∧ Smart(x) )
Every student loves some student.
∀ x ( Student(x) ∃ y ( Student(y) ∧ Loves(x,y) )) Bill is a student.
Student(Bill)
Bill takes either Analysis or Geometry (but not both) Takes(Bill,
Analysis) ⇔ ¬ Takes(Bill, Geometry)
Bill takes Analysis or Geometry (or both).
Takes(Bill, Analysis) ∨ Takes(Bill,
Geometry) Bill takes Analysis and
Geometry. Takes(Bill, Analysis) ∧ Takes(Bill,
Geometry) Bill does not take Analysis.
¬ Takes(Bill, Analysis).
No student loves Bill.
¬ ∃ x ( Student(x) ∧ Loves(x, Bill) )
Bill has at least one sister.
∃ x SisterOf(x,Bill)
Bill has no sister.
¬ ∃ x SisterOf(x,Bill)
KNOWLEDGE ENGINEERING
• Imagine an education company wanting to automate the teaching of
children in subjects from biology to computer science (requiring to
capture the knowledge of teachers and subject matter experts SME)
• Oncologists choosing the best treatment for their patients (requiring
expertise and knowledge from information contained in medical
journals, textbooks, and drug databases).
Knowledge Engineering in FOL
The following steps are used to develop the
knowledge base:
1. Identify the problem or task.
2. Assemble the relevant knowledge to the given problem
or task. 3. Decide on a vocabulary of predicates, functions
and constants. 4. Encode the general knowledge about the
domain and a description of the specific problem instance.
5. Apply queries to the inference procedure and get the
answers. 6. Finally, debug the knowledge base.
• Identify the Task
– • Outline the range of questions
– • Determine available facts
– • What is needed to connect problems to answers?
• Assemble the Relevant Knowledge – •
Knowledge acquisition
– • Understand the scope of the knowledge base
– • How does the domain work?
• Decide on a Vocabulary
– • Determine vocabulary of predicates, functions, and constants
– • How is the knowledge base represented?
– ONTOLOGY-specification of the meanings of the symbols in an
information system i.e specification of a conceptualization •
Encode General knowledge
• Encode the general knowledge about the domain
• Encode the Problem
• Describe the specific problem instance using the general
knowledge
• Pose Queries
– • Let the inference procedure operate on the
axioms to derive facts
• Debugging
• Check the knowledge base for errors
Representing Facts in First-Order Logic
1. Lucy is a professor
2. All professors are people.
3. John is the dean.
4. Deans are professors.
5. All professors consider the dean a friend or don’t know him.
6. Everyone is a friend of someone.
7. People only criticize people that are not their friends.
8. Lucy criticized John .
The electronic circuits domain
One-bit full adder
The electronic circuits domain
1. Identify the task
o Does the circuit actually add properly? (circuit
verification) 2. Assemble the relevant knowledge
o Composed of wires and gates; Types of gates (AND, OR, XOR, NOT) o
Irrelevant: size, shape, color, cost of gates
3. Decide on a vocabulary
o Gate(X1)
o Terminal(x)
o Type(X1) = XOR
o Circuit(C1)
o Signal(t)-signal value at terminal t
o In(1,X1)-first i/p terminal at gate X1
The electronic circuits domain
4. Encode general knowledge of the domain
If two terminals are connected, then they have the same
signal: ∀t1,t2 Connected(t1, t2) ⇒ Signal(t1) = Signal(t2)
o ∀t Signal(t) = 1 ∨ Signal(t) = 0
o ∀t1,t2 Connected(t1, t2) ⇒ Connected(t2, t1)
o ∀g Type(g) = OR ⇒ Signal(Out(1,g)) = 1 ⇔ ∃n Signal(In(n,g)) = 1 o ∀g
Type(g) = AND ⇒ Signal(Out(1,g)) = 0 ⇔ ∃n Signal(In(n,g)) = 0 o ∀g Type(g)
= XOR ⇒ Signal(Out(1,g)) = 1 ⇔ Signal(In(1,g)) ≠ Signal(In(2,g)) o ∀g
Type(g) = NOT ⇒ Signal(Out(1,g)) ≠ Signal(In(1,g))
The electronic circuits domain
5. Encode the specific problem instance
Type(X1) = XOR Type(X2) = XOR
Type(A1) = AND Type(A2) = AND
Type(O1) = OR
Connected(Out(1,X1),In(1,X2)) Connected(In(1,C1),In(1,X1))
Connected(Out(1,X1),In(2,A2)) Connected(In(1,C1),In(1,A1))
Connected(Out(1,A2),In(1,O1)) Connected(In(2,C1),In(2,X1))
Connected(Out(1,A1),In(2,O1)) Connected(In(2,C1),In(2,A1))
Connected(Out(1,X2),Out(1,C1)) Connected(In(3,C1),In(2,X2))
Connected(Out(1,O1),Out(2,C1)) Connected(In(3,C1),In(1,A2))
The electronic circuits domain
6. Pose queries to the inference procedure
What are the possible sets of values of all the terminals for the adder
circuit?
∃i1,i2,i3,o1,o2Signal(In(1,C_1)) = i1 ∧ Signal(In(2,C1)) = i2 ∧ Signal(In(3,C1)) = i3 ∧
Signal(Out(1,C1)) = o1 ∧ Signal(Out(2,C1)) = o2
7. Debug the knowledge base