Unit 2
Unit 2
Unit 2
in
UNIT-II
KNOWLEDGE BASED AGENTS
A knowledge-based agent is a specific form of artificial
intelligence that utilizes a stored repository of information, referred
to as a knowledge base, to arrive at decisions. This agent draws upon
its stored world knowledge, incorporating facts, methodologies, and
cognitive abilities, to draw solutions.
Architecture of a knowledge-based agent
Dept. of BCA
Click to Edit
Knowledge base
• The knowledge base acts as a repository of facts, rules, processes,
and other necessary data that the agent uses to guide its decision-
making process. The knowledge base contains data from the past
experiences of the agent as well as knowledge of the world, its
environment, and other factors the agent is trained for.
Inference engine
• The inference engine is the agent's component that applies logical
rules to the knowledge base to deduce additional information. It
Dept. of BCA
Click to Edit essentially acts as a system of logical deduction that utilizes the
contents of the knowledge base to reach decisions.
• These two components collaborate to enable the agent to observe
its environment, interpret the input, and ascertain the correct
action based on its knowledge and logical deduction capabilities.
Approaches to designing a knowledge-based agent:
There are mainly two approaches to build a knowledge-based agent:
1. 1. Declarative approach: We can create a knowledge-based agent
by initializing with an empty knowledge base and telling the agent
all the sentences with which we want to start with. This approach
is called Declarative approach.
2. 2. Procedural approach: In the procedural approach, we directly
Dept. of BCA
encode desired behavior as a program code. Which means we just
Click to Edit need to write a program that already encodes the desired behavior
or agent.
The Wumpus World in Artificial intelligence
• 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
Dept. of BCA
Click to Edit
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.
Dept. of BCA
Click to Edit
https://thiagodnf.github.io/wumpus-world-simulator/#
https://primaryobjects.github.io/wumpus/
There are also some components which can help the
agent to navigate the cave. These components are given
as follows:
• Example:
Dept. of BCA
Click to Edit 1. It is Sunday.
2. The Sun rises from West (False proposition)
3. 3+3= 7(False proposition)
4. 5 is a prime number. (True proposition)
• Syntax of propositional logic:
• The syntax of propositional logic defines the allowable
sentences for the knowledge representation. There are two
types of Propositions:
1. Atomic Propositions
2. Compound propositions
• Atomic Proposition: Atomic propositions are the simple
propositions. It consists of a single proposition symbol.
Dept. of BCA
Click to Edit These are the sentences which must be either true or false.
1. 2+2 is 4, it is an atomic proposition as it is a true fact.
2.
"The Sun is cold" is also a proposition as it is a false fact.
• Compound proposition: Compound propositions are
constructed by combining simpler or atomic
propositions, using parenthesis and logical connectives.
• Example:
1. a) "It is raining today, and street is wet."
2. b) "Ankit is a doctor, and his clinic is in Mumbai."
Dept. of BCA
Click to Edit
Logical Connectives:
Logical connectives are used to connect two simpler propositions or representing
a sentence logically. We can create compound propositions with the help of
logical connectives. There are mainly five connectives, which are given as
follows:
Dept. of BCA Example: Rohan is intelligent and hardworking. It can be written as,
Click to Edit
P=Rohan is intelligent,
Q= Rohan is hardworking. → P∧ Q.
Dept. of BCA
Click to Edit
Truth Table
Dept. of BCA
Click to Edit
Logical equivalence:
Dept. of BCA
Click to Edit
Logical equivalence is one of the features of
propositional logic. Two propositions are said to be
logically equivalent if and only if the columns in the
truth table are identical to each other.
Let's take two propositions A and B, so for logical
equivalence, we can write it as A⇔B. In below truth table
we can see that column for ¬A∨ B and A→B, are
identical hence A is Equivalent to B.
Dept. of BCA
Click to Edit
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)
Dept. of BCA
Click to Edit 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:
Dept. of BCA
Click to Edit ¬ (¬P) = P.
First-Order Logic in Artificial intelligence
• In the topic of Propositional logic, we have seen that how to
represent statements using propositional logic.
• But unfortunately, 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.
Dept. of BCA
• Consider the following sentence, which we cannot represent
Click to Edit using PL logic.
• "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.
First-Order logic:
• First-order logic is another way of knowledge
representation in artificial intelligence. It is an extension
to propositional logic.
• FOL is sufficiently expressive to represent the natural
language statements in a concise way.
• First-order logic is also known as Predicate logic or
First-order predicate logic. First-order logic is a
Dept. of BCA
Click to Edit
powerful language that develops information about the
objects in a more easy 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, ......
• As a natural language, first-order logic also has two main parts:
• Syntax
• Semantics
• Syntax of First-Order logic:
Dept. of BCA
Click to Edit
• The syntax of FOL determines which collection of symbols is a
logical expression in first-order logic. The basic syntactic
elements of first-order logic are symbols. We write statements in
short-hand notation in FOL.
• Basic Elements of First-order logic:
• Following are the basic elements of FOL syntax:
Constant 1, 2, A, John, Mumbai, cat,....
Variables x, y, z, a, b,....
Connectives ∧, ∨, ¬, ⇒, ⇔
Dept. of BCA
Click to Edit
Equality ==
Quantifier ∀, ∃
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.
• 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
Dept. of BCA binds two atoms together in a statement.
Click to Edit
• We can represent atomic sentences as Predicate (term1,
term2, ......, term n).
• Example: Ravi and Ajay are brothers: =>
Brothers(Ravi,Ajay).
Chinky is a cat: => cat (Chinky).
Click to Edit
Complex Sentences:
• Complex sentences are made by combining atomic
sentences using connectives.
Ex-She studied hard so that she could get a scholarship.
Dept. of BCA
Click to Edit
Quantifiers in First-order logic:
• A quantifier is a language element which generates
quantification, and quantification specifies the quantity
of specimen in the universe of discourse(set of all
elements or objects)
• These are the symbols that permit to determine or
identify the range and scope of the variable in the logical
expression. There are two types of quantifier:
Dept. of BCA
Click to Edit
• Universal Quantifier, (for all, everyone, everything)
• Existential quantifier, (for some, at least one).
1. Universal Quantifier (for all, everyone, everything)
In artificial intelligence and logic, the universal quantifier is a
concept used in first-order logic to express statements that apply to
all elements within a given domain. It is denoted by the symbol "∀"
(for all). It specifies that the s
Here's an example to illustrate the use of the universal quantifier in
AI:
• Statement: "All humans are mortal."
• In first-order logic with the universal quantifier, this statement
Dept. of BCA can be represented as:
Click to Edit
• Breaking it down:
• ∀x: For all elements x,
• (Human(x) → Mortal(x)): If x is a human, then x is mortal.
2. Existential quantifier (for some, at least one).
• In artificial intelligence and logic, the existential quantifier is a
concept used in first-order logic to express statements that assert the
existence of at least one element in a given domain satisfying a
particular condition.
• It is denoted by the symbol "∃" (there exists). The existential
quantifier indicates that there is at least one individual element for
which a specified statement is true.
• Here's an example to illustrate the use of the existential quantifier in
AI:
Dept. of BCA
Click to Edit • Statement: "There exists a smart student."
• In first-order logic with the existential quantifier, this statement can
be represented as:
• Breaking it down:
• ∃x: There exists an element x,
• (Student(x) ∧ Smart(x)): Such that x is a student and x is smart.
Some Examples of 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).
Dept. of BCA
Click to Edit
Propositional versus first-order inference
Aspect Propositional Logic First-Order Logic
Propositions, Variables,
Propositions (True/False
Syntax Quantifiers, Predicates,
statements)
Functions
More expressive, can
Expressiveness Limited expressiveness represent complex
relationships
Resolution, Unification,
Truth tables, Model Semantic Tableau,
Inference Methods
checking Automated Theorem
Provers
Dept. of BCA
Detailed representation of
Click to Edit Scope Declarative statements relationships, attributes, and
quantification
Knowledge representation,
Applications Basic rule-based systems expert systems, natural
language understanding
Can be computationally
Simpler and more
Complexity intensive due to handling
computationally efficient
variables and quantifiers
Rules of Inference in First order Logic
1. Modus Ponens:
The Modus Ponens rule is one of the most important rules of inference,
and it states that if P and P → Q is true, then we can infer that Q will be
true. It can be represented as:
Dept. of BCA
Click to Edit
2. Modus Tollens:
The Modus Tollens rule state that if P→ Q is true and if ¬ Q is true,
then ¬ P will also true. It can be represented as:
Dept. of BCA
Click to Edit
4. Universal Instantiation:
If you have a statement that says something is true for all instances ,
like “For all x, P(x),” and you have a specific individual “a”, you can
conclude that “P(a)” is true.
Ex- If all dogs bark, then Fido also barks.
Give: All dogs(∀x Dog(x)) bark(P(x)).
Conclusion : Fido barks (Dog(Fido) Bark(Fido)).
Dept. of BCA
Click to Edit
5. Existential Instantiation:
If you have a statement that says something exists, like “ There exists
an x such that P(x),” and you introduce a new variable “a”, you can
conclude that “P(a)” is true.
Given: “At least one student in the class speaks German”
Inference: “ Jerry is a student in the class who speaks German”.
Dept. of BCA
6. Existential Generalisation: If you have a statement that’s
Click to Edit true for a specific individual “a”, like P(a), you can generalise it to say
that something exists for all intances or “ There exists an x such that
P(x)”.
Ex- Given- “One student scored the highest marks in the exam”
Statement-” There exists a student who scored the highest marks in the
exam”
Click to Edit
Suppose the database contains the fact parent(john, mary). If you query parent(X,
mary), Prolog uses unification to infer that X = john.
Click to Edit
Unifying variable
Dept. of BCA We unify the variable “x” in Expression A with “Riya “ in Expression B. This results
Click to Edit in the substitution x=Riya.
We also unify the variable “y’ in Expression B with the constant “Apple”. This gives
us the substitution y= Apple.
Applying Substitutions
After substitutions, Expression A becomes “Eats(Riya, Apple)”.
Expression B remains the same: “Eats(Riya, Apple)”.
Rule 1: If a student has completed all required courses, then the student is
eligible for graduation.
Rule 2: If a student has completed Course A, Course B, and Course C, then the
student has completed all required courses.
Click to Edit
Initial facts:
Student X has completed Course A.
Student X has completed Course B.
Student X has completed Course C
Now, let's use forward chaining to determine if Student X is eligible for graduation
Since Student X has completed Course A and Course B, course C mark the fact that Student
Student X has completed all required courses (fact from Rule 2).
Since Student X has completed all required courses, mark the fact that Student X is eligible
for graduation.
Conclusion: Based on the forward chaining process, the system concludes that Student X is
Rules:
If a student has completed all required courses, then the student is eligible for graduation.
If a student has completed Course A, Course B, and Course C, then the student has
completed all required courses.
Click to Edit
Initial facts:
Student X has completed Course A.
Student X has completed Course B.
Student X has completed Course C
Now, let's use backward chaining to determine if the goal (Student X
is eligible for graduation) can be satisfied:
Apply Rule 2: The goal is satisfied if Student X has completed Course A, Course B, and Course C.
Conclusion:
Click to Edit
Based on backward chaining, the system determines that Student X is eligible for graduation because the
goal can be satisfied using Rule 2 and the available facts about Student X's completed courses.
In backward chaining, the system starts with the goal and works backward through the rules and facts to
establish whether the goal can be achieved. It's a top-down approach that begins with the final objective
and traces the path backward to the available information.
Click to Edit
Resolution in First Order Logic
In artificial intelligence and logic, resolution is a rule of inference
used for automated theorem proving in propositional and first-order
logic. The resolution rule is particularly effective in resolving
(disproving) the negation of a statement by finding a contradiction
between clauses. This process is used in logic programming,
knowledge representation, and reasoning systems.