Chapter3 Notes NEP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

CHAPTER - 3

Logical Agents:
In this chapter we design agent-based system that can represent a complex world. The new
representation about the world can be derived by using process of inference and these new
representations to deduce what to do. Also develop logic as a general class of representation to
support knowledge-based agent.
Knowledge-Based Agent in Artificial intelligence
The main component of knowledge -based agent is its of knowledge-base(KB).
A knowledge base is a set of representations of facts of the world.
Each individual representation is called a sentence.
The sentences are expressed in language termed as knowledge representation language.
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.
Knowledge-based agents are composed of two main parts:
1. Knowledge-base and
2. Inference system.
A knowledge-based agent must able to do the following:
An agent should be able to represent states, actions, etc.
An agent Should be able to incorporate new percept’s
An agent can update the internal representation of the world
An agent can deduce the internal representation of the world
An agent can deduce appropriate actions.
The architecture of knowledge-based agent:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 1


CHAPTER - 3

The above diagram is representing a generalized architecture for a knowledge-based agent. The
knowledge-based agent (KBA) takes input from the environment by perceiving the environment.
The input is taken by the inference engine of the agent and which also communicate with KB to
decide as per the knowledge store in KB. The learning element of KBA regularly updates the KB
by learning new knowledge.
Knowledge base: Knowledge-base is a central component of a knowledge-based agent, it is also
known as KB. It is a collection of sentences (here 'sentence' is a technical term and it is not
identical to sentence in English). These sentences are expressed in a language which is called a
knowledge representation language. The Knowledge-base of KBA stores fact about the
world.
Knowledge-base is required for updating knowledge for an agent to learn with experiences and
take action as per the knowledge
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. An inference system
works mainly in two rules which are given as:
Forward chaining
Backward chaining
Operations Performed by KBA
Following are 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.
Perform: It performs the selected action.
A generic knowledge-based agent:
Following is the structure outline of a generic knowledge-based agents program:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 2


CHAPTER - 3

The knowledge-based agent takes percept as input and returns an action as output. The agent
maintains the knowledge base, KB, and it initially has some background knowledge of the real
world. It also has a counter to indicate the time for the whole process, and this counter is
initialized with zero.
Each time when the function is called, it performs its three operations:
Firstly it TELLs the KB what it perceives.
Secondly, it asks KB what action it should take
Third agent program TELLS the KB that which action was chosen.
The MAKE-PERCEPT-SENTENCE generates a sentence as setting that the agent perceived the
given percept at the given time.
The MAKE-ACTION-QUERY generates a sentence to ask which action should be done at the
current time.
MAKE-ACTION-SENTENCE generates a sentence which asserts that the chosen action was
executed.
Various levels of knowledge-based agent:
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
behaviour. 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:
At this level, we understand that how the knowledge representation of knowledge is stored. At
this level, sentences are encoded into different logics. At the logical level, an encoding of
knowledge into logical sentences occurs. At the logical level we can expect to the automated taxi
agent to reach to the destination B.
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.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 3


CHAPTER - 3

THE WUMPUS WORLD EXAMPLE


The Wumpus world is an example of a world which describe an environment in which
knowledge-based agent can show their worth.
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.
There are some rooms contains pits. And if agent falls in pits, then he will be stuck there
forever. In this cave, there is a possibility of finding a heap of gold.
So, the goal of agent 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 come out with gold. If agent falls in the pit or eaten by
Wumpus then he will get penalty.
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:
1. 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.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 4


CHAPTER - 3

2. There will be glitter in the room if and only if the room has gold.
3. 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:
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:
A 4*4 grid of rooms.
The agent initially in room square [1, 1], facing toward the right.
Location of Wumpus and gold are chosen randomly except the first square [1,1].
Each square of the cave can be a pit with probability 0.2 except the first square.
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.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 5


CHAPTER - 3

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].
Exploring the Wumpus world:
Now we will explore the Wumpus world and will determine how the agent will find its goal by
applying logical reasoning.
Agent's First step:
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.

Agent's second Step:


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.
Agent's third step:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 6


CHAPTER - 3

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].

Agent's fourth step:


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.

Logic:
Logic is defined as proof or validation of a reason. Knowledge base consist of sentence and these
sentences are expressed according to the syntax. Syntax is a formal description of the structure of
program in a given language. The notion of syntax must be in well forms for example “x+y=10”
is a well-formed but “xyz+=” is not in well formed.
A logic can also define the semantic or meaning of sentence. Semantic is a formal description of
the programs in a given language. The semantics of the language defines the truth of each
sentence with respect to each possible world. For example, consider the sentence “x+y=10” is
true in a world where possible value of x is 5 and y is 5, but false where x is 1 and y is1. In
standard logic each sentence must be either true or false only.
When we need to be precise, we will use the term model in place of possible world. If a sentence
α is true in a model, then m is a model of α and M(α) notation is used to mean the set of all
models of α.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 7


CHAPTER - 3

Logical reasoning is the process through which any statement is verified. It is relation of logical
entailment (Entailment is the relationship between two sentences where the truth of one (A)
requires the truth of the other (B)) between sentences.
It can be denoted by α |=β and mean that the sentence α entails the sentence β.
The entailment is defined as α |=β if and only if, in every model in which α is true then β is also
true and can be written as α |=β iff M(α) ⊆ M(β)
This analysis can be applied on Wumpus world reasoning example.
Propositional logic in Artificial intelligence
Propositional logic is a simplest form of logic in which knowledge is represented as proposition.
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
Following are some basic facts about propositional logic:
Propositional logic is also called Boolean logic as it works on 0 and 1.
In propositional logic, we use symbolic variables to represent the logic, and we can use
any symbol for a 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.
These connectives are also called logical operators.
The propositions and connectives are the basic elements of the propositional logic.
Connectives can be said as 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.
A proposition formula which has both true and false values is called
Statements which are questions, commands, or opinions are not propositions such as
"Where is Rohini", "How are you", "What is your name", are not propositions.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 8


CHAPTER - 3

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
1. Atomic Proposition: Atomic propositions are the 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.
b) "The Sun is cold" is also a proposition as it is a false fact.
2. Compound proposition: Compound propositions are constructed by combining simpler or
atomic propositions, using parenthesis and logical connectives.
Example:
a) "It is raining today, and street is wet."
b) "Ankit is a doctor, and his clinic is in Mumbai."

Semantics:
The semantic defines the rules for determining the truth of a sentence with respect to particular
model. In a propositional logic, model fixes the truth value as true or false for every proposition
symbol.
Suppose if the sentence in the knowledge base makes use of the proposition symbols such as
P1,2, P2,2 and P3,1 then model is as follows
M1 = {P1,2=false, P2,2=false, P3,1=false}
With these three symbols there are 23 =8 possible models.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 9


CHAPTER - 3

The semantic must specify how to compute the truth value of any sentence and it done
recursively. By using atomic sentences and five connectives all sentences are constructed. So,
it’s necessary to specify how to compute the truth of atomic sentence and sentence which are
formed with each of the five connectives.
The atomic sentences are easy
True is true and False is false in every model.
The truth value of every other proposition symbol must be specified directly in the
model. For example, in the model m1 given earlier, P1;2 is false.
There are five rules for complex sentence, that hold any substances P and Q in any model m:
For any sentence s and any model m, the sentence :s is true in m, if and only if s is false
in m.
¬ P is true iff P is false in m.
P∧ Q is true iff both P and Q are true in m.
P ∨ Q is true iff either P or Q is true in m.
P=>Q is true unless P is true and Q is false in m.
P ⇔ Q is true iff P and Q are both true or both false in m.
For complex sentences:

A Simple Knowledge Base:


Now consider immutable aspects of the Wumpus world with the following symbol for each
location [x,y] such as
Px,y is true if there is a pit in [x,y]
Wx,y is true if there is Wumpus in [x,y] dead or alive
Bx,y is true if the agent perceives a breeze in [x,y]

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 10


CHAPTER - 3

Sx,y is true if the agent perceive a stench in {x,y}


Now consider the following situations and label each sentence Ri so it can be referred further.
1) there is no pit in [1,1]
R1: ¬ P1,1
2) A square is breeze if and only if there is pit in a neighboring square R2: B1,1 ⇔ (P1,2 V P2,1)
R3: B2,1 ⇔ (P1,1 V P2,2 V P3,1)
3) Now the breeze percept has been included for the first two squares
R4: ¬ B1,1
R5: ¬ B2,1
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:
1. Negation: A sentence such as ¬ P is called negation of P. A literal can be either Positive
literal or negative literal.
2. 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.
3. 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.
4. 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
5. Biconditional: A sentence such as P⇔ Q is a Biconditional sentence, example If I am
breathing, then I am alive

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 11


CHAPTER - 3

P= I am breathing, Q= I am alive, it can be represented as P ⇔ Q.


Following is the summarized table for Propositional Logic Connectives:

Truth Table:
In propositional logic, we need to know the truth values of propositions in all possible scenarios.
We can combine all the possible combination with logical connectives, and the representation of
these combinations in a tabular format is called Truth table. Following are the truth table for all
logical connectives:

Truth table with three propositions:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 12


CHAPTER - 3

Precedence of connectives:
Just like arithmetic operators, there is a precedence order for propositional connectors or logical
operators. This order should be followed while evaluating a propositional problem. Following is
the list of the precedence order for operators:
Precedence Operators
First Precedence Parenthesis
Second Precedence Negation
Third Precedence Conjunction(AND)
Fourth Precedence Disjunction(OR)
Fifth Precedence Implication
Six Precedence Biconditional
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

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).

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 13


CHAPTER - 3

P ∨ (Q ∧ R) = (P ∨ Q) ∧ (P ∨ R).
DE Morgan's Law:
¬ (P ∧ Q) = (¬P) ∨ (¬Q)
¬ (P ∨ Q) = (¬ P) ∧ (¬Q).
Double-negation elimination:
¬ (¬P) = P.
Limitations of Propositional logic:
We 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.

REASONING PATTERNS IN PROPOSITIONAL LOGIC


(Propositional theorem proving)
We can determine entailment by model checking (enumerating models, introduced above) or
theorem proving.
Theorem proving:
Applying rules of inference directly to the sentences in our knowledge base to construct a proof
of the desired sentence without consulting models
The entailment can be done by theorem proving. If the number of models is large but the length
of the proof is short then theorem proving can be more efficient as compare to model checking.
Logical equivalence - Two sentences α and β are logically equivalent if they are true in the
same set of models. It can be denoted by α ≡ β. Or any two sentences α and β are
equivalent only if each of them entails the other and denoted by α ≡ β if and only if α |= β and
β |= α.
Validity- a sentence is true in all model then it is valid.
Deduction theorem- it is derived from the definition of entailment, which was known to the
ancient Greeks for any sentences α and β, α |= β if and only if the sentence (α ⇒ β) is valid

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 14


CHAPTER - 3

Satisfiability- A sentence is satisfiable if it is true in or satisfied by some model. It can be


checked by enumerating the possible models until one is found that satisfies the sentence.

Rules of Inference in Artificial intelligence


In artificial intelligence, we need intelligent computers which can create new logic from old
logic or by evidence, so generating the conclusions from evidence and facts is termed as
Inference.
Inference rules: Inference rules are the templates for generating valid arguments. Inference
rules are applied to derive proofs in artificial intelligence, and the proof is a sequence of the
conclusion that leads to the desired goal.
Types of Inference rules:
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:

Example:
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.
Hence, we can say that, if P→ Q is true and P is true then Q will be true.
Proof by Truth table:

It indicates that any sentence of the form α ⇒ β and α are given, then the sentence β can be
inferred For example, if (Wumpus Ahead ∧ Wumpus Alive) => Shoot and (Wumpus Ahead ∧
Wumpus Alive) are given then shoot can be inferred.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 15


CHAPTER - 3

2) And-elimination:
It says that from conjunction any conjuncts can be inferred.

For example, from the sentence (Wumpus Ahead ∧ Wumpus Alive) the inference, “Wumpus
Alive” can be drawn.
3) Monotonicity:
it says that the set of entailed sentences can only increases as information is added to the
knowledge base. For any sentence α and β
if KB|= α then KB ∧ β|= α
Monotonicity means inference rules can be applied whenever suitable premises are found in the
knowledge base. the conclusion of the rule must follow regardless of what else is in the
knowledge base.
Proof by Resolution:
Resolution is a theorem proving technique that proceeds by proofs by contradictions ( In logic
and mathematics, proof by contradiction is a form of proof that establishes the truth or the
validity of a proposition, by showing that assuming the proposition to be false leads to a
contradiction.)
Resolution method is basically used for proving the satisfiability of a sentence.
Resolution is used, if there are various statements are given, and we need to prove a conclusion
of those statements. Unification is a key concept in proofs by resolutions. Resolution is a single
inference rule which can efficiently operate on the conjunctive normal form or clausal form.
Clause: Disjunction of literals (an atomic sentence) is called a clause. It is also known as a unit
clause.
Conjunctive Normal Form: A sentence represented as a conjunction of clauses is said to be
conjunctive normal form or CNF.
The resolution inference rule:
Resolution can resolve two clauses if they contain complementary literals, which are assumed to
be standardized apart so that they share no variables.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 16


CHAPTER - 3

Where li and mj are complementary literals. This rule is also called the binary (Unit)
resolution rule because it only resolves exactly two literals.
So, resolution takes two clauses and generate a new clause. The new clause containing all the
literals of the two original clause except two complementary literals. The resulting clause should
contain only one copy of each literal is the technical aspect of resolution rule. The removal of
multiple copies of literals is called factoring.
A resolution Algorithm:
It takes the input as a knowledge base and α and its output is either true or false.
First (KB ∧ ¬ α) is converted into CNF. Then the resolution rule is applied to the resulting
clauses. Each pair in which complementary literals are there is resolved to produce a new clause
and it is added to the new set if it is not there.
The procedure continues until one of the two things happens
first, there are no new clauses that can be added in which KB does not entails α or second two
clauses resolve to yield the empty clause in which case KB entails α.
The resolution algorithm is as shown below.

Example: Prove that Wampus is present in location [1,3]


EFFECTIVE PROPOSITIONAL MODEL CHECKING
Model-checking approach
Enumerate the models (assignments of true or false to every relevant proposition symbol), check
that α is true in every model in which KB is true.
Example:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 17


CHAPTER - 3

TT-ENTAILS?: A general algorithm for deciding entailment in propositional logic, performs a


recursive enumeration of a finite space of assignments to symbols.
Sound and complete.
Time complexity: O(2n)
Space complexity: O(n), if KB and α contain n symbols in all.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 18


CHAPTER - 3

For better inferences efficient algorithm is needed that based on model checking. Algorithm
approach can be backtracking search or hill-climbing search.
A Complete Backtracking Algorithm:
This algorithm is also called as Davis-Putnam algorithm or DPLL algorithm. It takes as input a
sentence in conjunction normal form, which is set of clauses. It is recursive depth first
enumeration of possible models. The algorithm is improvement over earlier general inferencing
algorithm. There are three improvements over the simple scheme TT-ENTAILS.
There are three improvements over the simple scheme TT-ENTAILS.
1) Early termination: The algorithm finds whether the sentence must be true or false. A clause
is true if any literal is true. A sentence is false if any clause is false, which occurs when each of
its literal is false.
2) Pure symbol heuristic: it is a symbol that always appears with same “sign” in all clauses. If a
sentence has a model, then it has a model with pure symbol assigned so as to make literal true.
3) Unit clause heuristic: it is clause with just one literal. One unit clause can create another unit
clause. The only literal in unit clause is true.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 19


CHAPTER - 3

Local search algorithms:


Local search algorithms can be applied directly to the SAT problem, provided that choose the
right evaluation function. (We can choose an evaluation function that counts the number of
unsatisfied clauses.)
These algorithms take steps in the space of complete assignments, flipping the truth value of one
symbol at a time.
The space usually contains many local minima, to escape from which various forms of
randomness are required.
Local search methods such as WALKSAT can be used to find solutions. Such algorithm are
sound but not complete.
WALKSAT: one of the simplest and most effective algorithms

On every iteration, the algorithm picks an unsatisfied clause, and chooses randomly between 2
ways to pick a symbol to flip:
Either a. a “min-conflicts” step that minimizes the number of unsatisfied clauses in the new state;
Or b. a “random walk” step that picks the symbol randomly.
When the algorithm returns a model, the input sentence is indeed satifiable;
When the algorithm returns failure, there are 2 possible causes:
Either a. The sentence is unsatisfiable;
Or b. We need to give the algorithm more time.
AGENT BASED ON PROPOSITIONAL LOGIC
we have learned so far in order to construct agents that operate using propositional logic. We will
look at two kinds of agents: those which use inference algorithms and a knowledge base, like the

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 20


CHAPTER - 3

generic knowledge-based agent and those which evaluate logical expressions directly in the form
of circuits.
Inference based agent -it requires the separate copies of its knowledge base for every time
step. Inference based agent takes exponential time for inferencing and huge time
requirements for completeness. It is easy to represent in propositional logic language. It
requires large memory as it stores previous percepts or states.
Circuit based agent- it doesn’t require separate copy of its KB for every time step. Circuit
based agent takes linear time for inferencing which is dependent on circuit size. For a
complete circuit CBA will take exponential time with respect to circuit size so it is
incomplete. It is easy to describe and construct knowledge base.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 21


CHAPTER - 3

First-Order Logic, Inference in First-Order Logic – 1:


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. Consider the
following sentence, which we cannot represent 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 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 & SEMANTICS OF FIRST-ORDER LOGIC
We begin this section by specifying the way in which the possible worlds of first-order logic
reflect the ontological (What is ontology in AI? In AI, an ontology is a specification of the
meanings of the symbols in an information system. That is, it is a specification of a

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 22


CHAPTER - 3

conceptualization. It is a specification of what individuals and relationships are assumed to exist


and what terminology is used for them.) commitment to objects and relations.
Then we introduce the various elements of the language, explaining their semantics as we go
along.
Models for First-Order Logic:
Models of a logical language are the formal structures that constitute the possible worlds under
consideration. Models for propositional logic link proposition symbols to predefined truth
values. Models for first-order logic have objects. Every possible world must contain at least one
object.
Below figure shows a model with five objects:
1. Richard the Lionheart, King of England;

2. his younger brother, the evil King John;

3. the left legs of Richard;

4. the left legs of John;

5. and a crown.

The objects in the model may be related in various ways. In the figure, Richard and John are
brothers.
Thus, the brotherhood relation in this model is the set
{ <Richard the Lionheart, King John>, <King John, Richard the Lionheart> }
The “brother” and “on head” relations are binary relations that is, they relate pairs of objects.
The model also contains unary relations, or properties:
the “person” property is true of both Richard and John; and the “crown” property is true only for
the crown.
Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 23
CHAPTER - 3

Certain kinds of relationships are considered as functions, in that a given object must be related
to exactly one object.
For example, each person has one left leg, so the model has a unary “left leg” function that
includes the following mappings:
<Richard the Lionheart>→ Richard’s left leg
<King John> → John’s left leg.

Symbols and Interpretations:


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.
The symbols, therefore, come in three kinds:
1. Constant symbols:
Constants symbols refer to objects in a universe of discourse. Objects can be anything like
integers, people, real numbers and geometric shapes etc.
Example: Richard, John
2. Predicate symbols:
A predicate symbol represents a property of or relation between terms that can be true or false.
Each predicate symbol has an associated arity (Arity is the number of arguments or operands
taken by a function, operation or relation in logic, mathematics) indicating its number of
arguments.
Brother, OnHead, Person, King, and Crown, etc. are predicate symbols.
Example:
King(John) is a unary predicate which is having one arguments.
Brother(John, Richard) is a Binary predicate which is having two arguments.
We write statements in short-hand notation in FOL.
3. Function symbols:
Function constants refer to functions like mother, age, LeftLeg, plus and times.
Each function symbol has an associated arity indicating its number of arguments.
Example:
mother(Jane) - here, mother has arity one (unary)
while in times(3,2) - times has arity two(binary).

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 24


CHAPTER - 3

Figure: The syntax of first-order logic, specified in Backus–Naur form and Operator precedences
are specified from highest to lowest.
Like proposition symbols, the choice of names is entirely up to the user & every model must
provide the information required to determine if any given sentence is true or false.
In addition to its objects, relations, and functions, each model includes an interpretation that
specifies exactly which objects, relations and functions are referred to by the constant, predicate,
and function symbols. One possible interpretation for our example is as follows:
Interpretation:
Richard refers to Richard the Lionheart and John refers to the evil King John.
Brother refers to the brotherhood relation; OnHead refers to the “on head” relation that holds
between the crown and King John; Person, King, and Crown refer to the sets of objects that are
persons, kings, and crowns.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 25


CHAPTER - 3

LeftLeg refers to the “left leg” function.


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,....

Predicates Brother, Father, >,....

Function sqrt, LeftLegOf, ....

Connectives ∧, ∨, ¬, ⇒, ⇔

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.
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).
Atomic sentences can also have complex terms as arguments. Such as,
Married(Father (Ravi),Mother (Ajay))
states that Ravi’s father is married to Ajay’s mother.
Complex Sentences:
Complex sentences are made by combining atomic sentences using connectives.
Logical connectives can be used to construct more complex sentences, with the same
syntax and semantics as in propositional calculus. Logical connectives like ¬, ∧, ∨, ⇒,
⇔ can be used.
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.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 26


CHAPTER - 3

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:


A quantifier is a language element which generates quantification, and quantification
specifies the quantity of specimen in the universe of discourse.
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:
1. Universal Quantifier, (for all, everyone, everything)
2. Existential quantifier, (for some, at least one).
1 Universal 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 ∀, which resembles an inverted A.
Note: In universal quantifier we use implication "→".
If x is a variable, then ∀x is read as:
For all x
For each x
For every x.
Example 1:
The rule, “All kings are persons,” is written in first-order logic as
∀ x King(x) ⇒ Person(x)
Thus, the sentence will be read as, “For all x, if x is a king, then x is a person.” The symbol x is
called variable. By convention, variables are lowercase letters.
Variable:
A variable is a term by itself.
It can also serve as the argument of a function for example, LeftLeg(x).
A term with no variables is called a ground term.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 27


CHAPTER - 3

Example 2
All man drink coffee.
Let a variable x which refers to a man so all x can be represented as below:
x1 drinks coffee.
x2 drinks coffee.
x3 drinks coffee. .
xn drinks coffee.
We can write it as: ∀x man(x) → drink (x, coffee).
It will be read as: For all x, if x is a man, then x drinks coffee.
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 ∃, which resembles as inverted E. When it is used with a
predicate variable then it is called as an existential quantifier.
Note: In Existential quantifier we always use AND or Conjunction symbol (∧).
If x is a variable, then existential quantifier will be ∃x or ∃(x).
And it will be read as:
There exists a 'x.'
For some 'x.'
For at least one 'x.'
Example 1:
King John has a crown on his head, we write
∃ x: Crown(x) ∧ OnHead(x, John)
That is, at least one of the following is true:
i. Richard the Lionheart is a crown ∧ Richard the Lionheart is on John’s head;
ii. King John is a crown ∧ King John is on John’s head;
iii. Richard’s left leg is a crown ∧ Richard’s left leg is on John’s head;
iv. John’s left leg is a crown ∧ John’s left leg is on John’s head;
v. The crown is a crown ∧ the crown is on John’s head.
The fifth assertion is true in the model.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 28


CHAPTER - 3

Example 2:
Some boys are intelligent.
∃x: boys(x) ∧ intelligent(x)
It will be read as: There are some x where x is a boy who is intelligent.
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.
Nested quantifiers:
More complex sentences can be expressed using multiple quantifiers. The simplest case is where
the quantifiers are of the same type.
For example,
“Brothers are siblings”
can be written as ∀ x ∀ y Brother (x, y) ⇒ Sibling(x, y)
Siblinghood is a symmetric relationship, we can write ∀ x, y Sibling(x, y) ⇔ Sibling(y, x
In other cases, we will have mixtures.
“Everybody read few books” means that for every person, there is some book that person
read:
∀ x ∃ y Read(x, y).
On the other hand, to say “There is someone who is liked by everyone,”
we write ∃ y ∀ x Likes(x, y) The order of quantification is therefore very important. It
becomes clearer if we insert parentheses.
Connections between ∀ and ∃:
The two quantifiers are actually connected with each other, through negation. Asserting that
everyone dislikes XYZ is the same as asserting there does not exist someone who likes them, and
vice versa:
∀ x ¬Likes(x, XYZ ) is equivalent to ¬∃ x Likes(x, XYZ)
We can go one step further: “Everyone likes ice cream” means that there is no one who does not
like ice cream:
∀ x Likes(x, IceCream) is equivalent to ¬∃ x ¬Likes(x, IceCream)

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 29


CHAPTER - 3

Because ∀ is a conjunction over the universe of objects and ∃ is a disjunction, quantifiers obey
De Morgan’s rules.
The De Morgan rules for quantified and unquantified sentences are as follows:

Examples of First-Order Logic:


Some Examples of First-Order Logic using quantifiers:
1. All birds fly.
 In this statement 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 statement, 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 statement, 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)
4. Not all students like both Mathematics and Science.
 In this statement, the predicate is like(x, y), where x= student, and y= subject.
 Since there are not all students, so we will use ∀ with negation, so following
representation for this:
¬∀ (x) [ student(x) → like(x, Mathematics) ∧ like(x, Science)]

5. Only one student failed in Mathematics.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 30


CHAPTER - 3

 In this statement, the predicate is failed(x, y), where x= student, and y= subject.
 Since there is only one student who failed in Mathematics, so we will use following
representation for this:
∃(x) [ student(x) → failed (x, Mathematics) ∧∀ (y) [¬(x==y) ∧ student(y) → ¬failed (x,
Mathematics)]
Equality:
Equality symbol can be used to signify that two terms refer to the same object.
For example, Father (John)=Henry says that the object referred to by Father (John) and
the object referred to by Henry are the same.
The equality symbol can be used to state facts about a given function, as we just did for
the Father symbol. It can also be used with negation to insist that two terms are not the
same object. To say that Richard has at least two brothers, we would write
∃ x, y Brother (x,Richard ) ∧ Brother (y,Richard ) ∧¬(x=y)
The sentence ∃ x, y Brother (x,Richard ) ∧ Brother (y,Richard ) does not have the
intended meaning. If x is John & y is also John the sentence will not be valid because
Richard’s both the brothers will not be having the same name as John, therefore x and y
both should be different. For this purpose, ¬(x=y) is added. The notation x ≠ y is
sometimes used as an abbreviation for ¬(x=y).

USING FIRST-ORDER LOGIC


Now we have defined an expressive logical language, it is time to learn how to use it. In this
section, we provide representations of some simple domains. In knowledge representation, a
domain is just some part of the world about which we wish to express some knowledge. We
begin with a brief description of the TELL/ASK interface for first-order knowledge bases.
Then we look at the domains of
1. Family relationships
2. Numbers
3. Sets and lists
4. The Wumpus world

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 31


CHAPTER - 3

Assertions and Queries In First-Order Logic:


1. Assertions:
Sentences are added to a knowledge base using TELL, exactly as in propositional logic. Such
sentences are called assertions.
For example, we can assert that John is a king, Richard is a person, and all kings are persons:
TELL(KB, King(John)) .
TELL(KB, Person(Richard)) .
TELL(KB, ∀ x King(x) ⇒ Person(x)) .
2. Queries:
We can ask questions from the knowledge base using ASK.
For example, ASK(KB, King(John)) Above query returns true.
Questions asked with ASK are called queries or goals.
ASK(KB, Person(John)) This query also returns true using above assertions.
We can ask quantified queries, such as ASK(KB, ∃ x Person(x)) .
The answer is true, but we do not know the value of x makes the sentence true. If we
want to know what value of x makes the sentence true, we will use a different function
which is called ASKVARS.
ASKVARS(KB, Person(x))
Above function yields a stream of answers.
In this case there will be two answers: {x/John} and {x/Richard}. Such an answer is
called a substitution or binding list. Which means in above query x can be substituted
with John or Richard.
1 The Kinship Domain:
The first example we consider is the domain of family relationships, or kinship.
This domain includes facts such as “Elizabeth is the mother of Charles” and “Charles is the
father of William” and rules such as “One’s grandmother is the mother of one’s parent”.
We have following in Kinship domain according to First-Order logic.
1. Objects:
objects in kinship domain are people.
2. Unary predicates:
Male and Female.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 32


CHAPTER - 3

3. Binary Predicates:
Kinship relations parenthood, brotherhood, marriage, and so on are represented by binary predicates:
Parent, Sibling, Brother, Sister, Child, Daughter, Son, Spouse, Wife, Husband, Grandparent, Grandchild,
Cousin, Aunt, and Uncle.
4. Functions: Mother and Father, because every person has exactly one of each of these (at least according
to nature’s design).
We can go through each function and predicate, writing down what we know in terms of the other
symbols.
Example:
1. One’s mother is one’s female parent:
∀ m, c Mother (c)=m ⇔ Female(m) ∧ Parent(m, c)
2. One’s husband is one’s male spouse:
∀ w, h Husband(h,w) ⇔ Male(h) ∧ Spouse(h,w)
3. Male and female are disjoint categories:
∀ x Male(x) ⇔ ¬Female(x)
4. Parent and child are inverse relations:
∀ p, c Parent(p, c) ⇔ Child (c, p)
5. A grandparent is a parent of one’s parent:
∀ g, c Grandparent (g, c) ⇔ ∃p Parent(g, p) ∧ Parent(p, c)
6. A sibling is another child of one’s parents:
∀ x, y Sibling(x, y) ⇔ x = y ∧ ∃p Parent(p, x) ∧ Parent(p, y)
We could go on for several examples like this. Each of these sentences can be viewed as an
axiom of the kinship domain.
The axioms define the Mother function and the Husband, Male, Parent, Grandparent, and Sibling
predicates.
This is a natural way in which we can build up the representation of a domain. Axioms can also
be “just plain facts,” such as Male(Jim) and Spouse(Jim, Laura) etc. Not all logical sentences
about a domain are axioms.
Some are theorems that is, they are entailed by the axioms.
For example, consider the assertion that siblinghood is symmetric: ∀ x, y Sibling(x, y) ⇔
Sibling(y, x)

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 33


CHAPTER - 3

The Wumpus World:


The first-order axioms for Wumpus world are much more concise, capturing in a natural way
exactly what we want to say. Wumpus agent receives a percept sequence with five elements. The
corresponding first-order sentence stored in the knowledge base must include both the percept
and the time at which it occurred; otherwise, the agent will get confused about when it saw what.
We use integers for time steps.
A typical percept sentence would be Percept ([Stench, Breeze, Glitter, None, None], 5)
Here, Percept is a binary predicate, and Stench and so on are constants placed in a list.
The actions in the wumpus world can be represented by logical terms:
Turn(Right ), Turn(Left ), Forward , Shoot , Grab, Climb.
To determine which action is best, the agent program executes the query
ASKVARS(∃ a BestAction(a, 5))
Which returns a binding list such as {a/Grab}.
The agent program can then return Grab as the action to take.
The raw percept data implies certain facts about the current state.
For example: ∀ t, s, g, m, c Percept ([s, Breeze, g,m, c], t) ⇒ Breeze(t) ,
∀ t, s, b, m, c Percept ([s, b, Glitter,m, c], t) ⇒ Glitter (t) , and so on.
Notice the quantification over time t. In propositional logic, we would need copies of
each sentence for each time step.

Inference Rules for Quantifiers:


Like a propositional logic we also have inference rules in first-order logic. Following are some
basic inference rules in First-Order logic:
1. Universal Instantiation
2. Existential Instantiation
3. Universal Generalization
4. Existential Generalization
1 Universal Instantiation:
Universal instantiation is also called as universal elimination or UI is a valid inference
rule. It can be applied multiple times to add new sentences.
The new KB is logically equivalent to the previous KB.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 34


CHAPTER - 3

As per UI, we can infer any sentence obtained by substituting a ground term for the
variable.
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.
It can be represented as:

Example: 1
IF "Every person like ice-cream"=> ∀x P(x). So we can infer that
"John likes ice-cream" => P(c)
Example: 2.
Let's take a famous example,
"All kings who are greedy are Evil." So let our knowledge base contains this detail as in the form
of FOL:
∀x king(x) ∧ greedy (x) → Evil (x),
So, from this information, we can infer any of the following statements using Universal
Instantiation:
King(John) ∧ Greedy (John) → Evil (John),
King(Richard) ∧ Greedy (Richard) → Evil (Richard),
King(Father(John)) ∧ Greedy (Father(John)) → Evil (Father(John)),
Here, the substitutions are {x/John }, {x/Richard }, and {x/Father (John )}.
2 Existential Instantiation:
Existential instantiation is also called as Existential Elimination, which is a valid
inference rule in first-order logic.
It can be applied only once to replace the existential sentence.
The new knowledge base is not logically equivalent to old knowledge base, but it will be
satisfiable if old knowledge base was satisfiable.
This rule states that one can infer P(c) from the formula given in the form of ∃x P(x) for a
new constant symbol c.
The restriction with this rule is that c used in the rule must be a new term for which P(c)
is true.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 35


CHAPTER - 3

It can be represented as:

Example:
From the given sentence: ∃x Crown(x) ∧ OnHead(x, John),
So we can infer: Crown(K) ∧ OnHead( K, John), as long as K does not appear in the
knowledge base.
The above used K is a constant symbol, which is called Skolem constant.
The Existential instantiation is a special case of Skolemization process.
3. Universal Generalization:
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).

It can be represented as: .


This rule can be used if we want to show that every element has a similar property.
In this rule, x must not appear as a free variable.
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.
4 Existential Generalization:
An existential introduction is also known as an existential generalization, which is a valid
inference rule in first-order logic.
This rule states that if there is some element c in the universe of discourse which has a
property P, then we can infer that there exists something in the universe which has the
property P.
It can be represented as:

Example:
Let's say that,
"Ravi got good marks in English."
"Therefore, someone got good marks in English."

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 36


CHAPTER - 3

UNIFICATION AND LIFTING:


A First-Order Inference Rule- Generalized Modus Ponens Rule:
For the inference process in FOL, we have a single inference rule which is called Generalized
Modus Ponens. It is lifted version of Modus ponens.
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:

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.
Here let say,
p1' is king(John) p1 is king(x)
p2' is Greedy(y) p2 is Greedy(x)
θ is {x/John, y/John} q is evil(x)
SUBST(θ, q) is Evil(John) .
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)
Lifted inference rules require finding substitutions that make different logical expressions
look identical. This process is called unification and is a key component of all first-order
inference algorithms.
The algorithm takes two sentences and returns a unifier for them if one exists:
UNIFY(p, q)= θ where SUBST(θ, p)= SUBST(θ, q) .
Let us look at some examples of how UNIFY should behave. Suppose we have a query
AskVars (Knows (John , x)): whom does John know?

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 37


CHAPTER - 3

Answers to this query can be found by finding all sentences in the knowledge base that unify
with Knows(John , x). Here are the results of unification with four different sentences that might
be in the knowledge base:
UNIFY(Knows (John, x), Knows (John, Jane)) = {x/Jane }
UNIFY(Knows (John, x), Knows (y, Bill )) = {x/Bill, y/John }
UNIFY(Knows (John, x), Knows (y, Mother (y))) = {y/John , x/Mother (John )}
UNIFY(Knows (John, x), Knows (x, Elizabeth )) = fail .
The last unification fails because x cannot take on the values John and Elizabeth at the same
time. Now, remember that Knows(x, Elizabeth) means “Everyone knows Elizabeth,” so we
should be able to infer that John knows Elizabeth.
The problem arises only because the two sentences happen to use the same variable name, x. The
problem can be avoided by standardizing apart one of the two sentences being unified, which
means renaming its variables to avoid name clashes.
For example, we can rename x in Knows(x, Elizabeth) to x17 (a new variable name) without
changing its meaning.
Now the unification will work:
UNIFY(Knows(John, x), Knows(x17, Elizabeth)) = {x/Elizabeth, x17/John} .
There is one more complication:
we said that UNIFY should return a substitution that makes the two arguments look the same.
But there could be more than one such unifier.
For example,
UNIFY(Knows(John, x), Knows(y, z)) could return {y/John, x/z} or {y/John, x/John, z/John}.
The first unifier gives Knows(John, z) as the result of unification, whereas the second gives
Knows(John, John).
The second result could be obtained from the first by an additional substitution {z/John}; we say
that the first unifier is more general than the second, because it places fewer restrictions on the
values of the variables.
It turns out that, for every unifiable pair of expressions, there is a single most general unifier (or
MGU) that is unique up to renaming and substitution of variables. (For example, {x/John} and
{y/John} are considered equivalent, as are {x/John, y/John} and {x/John, y/x}.)
In this case it is {y/John, x/z}.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 38


CHAPTER - 3

Forward Chaining, Backward Chaining:


Inference engine: The inference engine is the component of the intelligent system in artificial
intelligence, which applies logical rules to the knowledge base to infer new information from
known facts. The first inference engine was part of the expert system. Inference engine
commonly proceeds in two modes, which are:
1. Forward chaining
2. Backward chaining
Horn Clause and Definite clause:
Horn clause and definite clause are the forms of sentences, which enables knowledge base to use
a more restricted and efficient inference algorithm. Logical inference algorithms use forward and
backward chaining approaches, which require KB in the form of the first-order definite clause.
Definite clause: A clause which is a disjunction of literals with exactly one positive literal is
known as a definite clause or strict horn clause.
Horn clause: A clause which is a disjunction of literals with at most one positive literal is
known as horn clause. Hence all the definite clauses are horn clauses.
Example: (¬ p V ¬ q V k). It has only one positive literal k.
It is equivalent to p ∧ q → k.

A. Forward Chaining
Forward chaining is also known as a forward deduction or forward reasoning method when using
an inference engine. Forward chaining is a form of reasoning which start with atomic sentences
in the knowledge base and applies inference rules (Modus Ponens) in the forward direction to
extract more data until a goal is reached.
The Forward-chaining algorithm starts from known facts, triggers all rules whose premises are
satisfied, and add their conclusion to the known facts. This process repeats until the problem is
solved.
Example:
"As per the law, it is a crime for an American to sell weapons to hostile nations. Country A, an
enemy of America, has some missiles, and all the missiles were sold to it by Robert, who is an
American citizen."
Prove that "Robert is criminal."

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 39


CHAPTER - 3

To solve the above problem, first, we will convert all the above facts into first-order definite
clauses, and then we will use a forward-chaining algorithm to reach the goal.
Facts Conversion into FOL:
It is a crime for an American to sell weapons to hostile nations. (Let's say p, q, and r are
variables)
American (p) ∧ weapon(q) ∧ sells (p, q, r) ∧ hostile(r) → Criminal(p) ...(1)
Country A has some missiles. ?p Owns(A, p) ∧ Missile(p). It can be written in two
definite clauses by using Existential Instantiation, introducing new Constant T1.
Owns(A, T1) ......(2)
Missile(T1) .......(3)
All of the missiles were sold to country A by Robert.
?p Missiles(p) ∧ Owns (A, p) → Sells (Robert, p, A) ......(4)
Missiles are weapons.
Missile(p) → Weapons (p) .......(5)
Enemy of America is known as hostile.
Enemy(p, America) →Hostile(p) ........(6)
Country A is an enemy of America.
Enemy (A, America) .........(7)
Robert is American
American(Robert). ..........(8)
Forward chaining proof:
Step-1:
In the first step we will start with the known facts and will choose the sentences which do not
have implications,
such as: American(Robert), Enemy(A, America), Owns(A, T1), and Missile(T1). All these facts
will be represented as below.

Step-2: At the second step, we will see those facts which infer from available facts and with
satisfied premises.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 40


CHAPTER - 3

Rule-(1) does not satisfy premises, so it will not be added in the first iteration.
Rule-(2) and (3) are already added.
Rule-(4) satisfy with the substitution {p/T1}, so Sells (Robert, T1, A) is added, which infers
from the conjunction of Rule (2) and (3).
Rule-(6) is satisfied with the substitution(p/A), so Hostile(A) is added and which infers from
Rule-(7).

Step-3:
At step-3, as we can check Rule-(1) is satisfied with the substitution {p/Robert, q/T1, r/A}, so we
can add Criminal(Robert) which infers all the available facts. And hence we reached our goal
statement.

Hence it is proved that Robert is Criminal using forward chaining approach.


B. Backward Chaining:
Backward-chaining is also known as a backward deduction or backward reasoning method when
using an inference engine. A backward chaining algorithm is a form of reasoning, which starts

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 41


CHAPTER - 3

with the goal and works backward, chaining through rules to find known facts that support the
goal.
Example:
In backward-chaining, we will use the same above example, and will rewrite all the rules.
American (p) ∧ weapon(q) ∧ sells (p, q, r) ∧ hostile(r) → Criminal(p) ...(1)
Owns(A, T1) ........(2)
Missile(T1)
?p Missiles(p) ∧ Owns (A, p) → Sells (Robert, p, A) ......(4)
Missile(p) → Weapons (p) .......(5)
Enemy(p, America) →Hostile(p) ........(6)
Enemy (A, America) .........(7)
American(Robert). ..........(8)
Backward-Chaining proof:
In Backward chaining, we will start with our goal predicate, which is Criminal(Robert), and then
infer further rules.
Step-1:
At the first step, we will take the goal fact. And from the goal fact, we will infer other facts, and
at last, we will prove those facts true. So our goal fact is "Robert is Criminal," so following is the
predicate of it.

Step-2:
At the second step, we will infer other facts form goal fact which satisfies the rules. So as we can
see in Rule-1, the goal predicate Criminal (Robert) is present with substitution {Robert/P}. So
we will add all the conjunctive facts below the first level and will replace p with Robert.
Here we can see American (Robert) is a fact, so it is proved here.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 42


CHAPTER - 3

Step-3:t At step-3, we will extract further fact Missile(q) which infer from Weapon(q), as it
satisfies Rule-(5). Weapon (q) is also true with the substitution of a constant T1 at q.

Step-4:
At step-4, we can infer facts Missile(T1) and Owns(A, T1) form Sells(Robert, T1, r) which
satisfies the Rule- 4, with the substitution of A in place of r. So these two statements are proved
here.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 43


CHAPTER - 3

Step-5:
At step-5, we can infer the fact Enemy(A, America) from Hostile(A) which satisfies Rule- 6.
And hence all the statements are proved true using backward chaining.

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 44

You might also like