Unit 3-I BSC Ai

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

UNIT 3

Inference
We need intelligent computers in artificial intelligence to construct
new logic from old logic or evidence, therefore inference is the
process of drawing conclusions from data and facts.

Inference rules:
The templates for creating valid arguments are known as inference rules.
In artificial intelligence, inference rules are used to generate proofs, and a
proof is a series of conclusions that leads to the intended outcome. The
implication among all the connectives is vital in inference rules. Some
terms relating to inference rules are as follows:

1. Implication: It's one of the logical connectives, denoted by the


letters P → Q. It's a Boolean expression, to be precise.

2. Converse: The converse of implication is when the right-hand side


statement is applied to the left-hand side, and vice versa. It is
denoted by the letters Q → P.

3. Contrapositive: Contrapositive is the negation of converse, and it


can be expressed as ¬ Q → ¬ P.

4. Inverse: Inverse is the antithesis of implication. ¬ P → ¬ Q can be


used to symbolize it.

Some of the compound statements in the above term are equivalent to


each other, which we can verify using the truth table:

P Q P→Q Q→P ¬Q→¬P ¬P→¬Q

T T T T T T

T F F T F T

F T T F T F

F F T T T T

As a result from the above truth table, we can prove that P → Q is


equivalent to ¬ Q → ¬ P, and Q→ P is equivalent to ¬ P → ¬ Q.

Types of Inference rules:


1. Modus Ponens:
One of the most essential laws of inference is the Modus Ponens rule,
which asserts that if P and P → Q are both true, we can infer that Q will be
true as well. It's written like this:

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:

2.Modus Tollens:
According to the Modus Tollens rule if P→ Q is true and ¬ Q is true, then ¬
P will also true. It can be represented as:

Example:
Statement-1: "If I am sleepy then I go to bed" ==> P→ Q
Statement-2: "I do not go to the bed."==> ~Q
Statement-3: Which infers that "I am not sleepy" => ~P

Proof by Truth table:

3. Hypothetical Syllogism:
According to the Hypothetical Syllogism rule if P→R is true whenever P→Q
is true, and Q→R is true. It can be represented as the following notation:
Example:
Statement-1: Statement-1: If you have my home key then you can unlock
my home. P→Q
Statement-2: Statement-2: If you can unlock my home then you can take
my money. Q→R

Statement-3: Conclusion: If you have my home key then you can take my money. P→R

Proof by Truth table:

4. Disjunctive Syllogism:
According to the Disjunctive syllogism rule if P∨Q is true, and ¬P is true,
then Q will be true. It can be represented as:

Example:
Statement-1:Today is Sunday or Monday. ==>P∨Q
Statement-2:Today is not Sunday. ==> ¬P
Conclusion: Today is Monday. ==> Q

Proof by Truth table:

5. Addition:
According to the Addition rule which is one of the common inference rule,
If P is true, then P∨Q will be true.
Example:
Statement-1: I have a vanilla ice-cream. ==> P
Statement-2: I have Chocolate ice-cream.
Conclusion: I have vanilla or chocolate ice-cream. ==> (P∨Q)

Proof by Truth table:

6. Simplification:
According to the simplification rule if P∧ Q is true, then Q or P will also be
true. It can be represented as:

Proof by Truth table:

7. Resolution:
According to the Resolution rule if P∨Q and ¬ P∧R is true, then Q∨R will
also be true. It can be represented as

Proof by Truth table:


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.
Consider the following sentence, which we cannot represent using PL
logic.

o "Some humans are intelligent", or


o "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:
o First-order logic is another way of knowledge representation in artificial
intelligence. It is an extension to propositional logic.
o FOL is sufficiently expressive to represent the natural language
statements in a concise way.
o 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.
o First-order logic (like natural language) does not only assume that the
world contains facts like propositional logic but also assumes the following
things in the world:
o Objects: A, B, people, numbers, colors, wars, theories, squares,
pits, wumpus, ......
o Relations: It can be unary relation such as: red, round, is
adjacent, or n-any relation such as: the sister of, brother of, has
color, comes between
o Function: Father of, best friend, third inning of, end of, ......
o As a natural language, first-order logic also has two main parts:

a. Syntax

b. Semantics

Syntax of First-Order logic:


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:

ello Java Program for Beginners

Constant 1, 2, A, John, Mumbai, cat,....

Variables x, y, z, a, b,....

Predicates Brother, Father, >,....

Function sqrt, LeftLegOf, ....

Connectives ∧, ∨, ¬, ⇒, ⇔

Equality ==

Quantifier ∀, ∃
Atomic sentences:
o 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.
o 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).

Complex Sentences:
o Complex sentences are made by combining atomic sentences using
connectives.

First-order logic statements can be divided into two parts:

o Subject: Subject is the main part of the statement.


o 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:


o A quantifier is a language element which generates quantification, and
quantification specifies the quantity of specimen in the universe of
discourse.
o 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:

a. Universal Quantifier, (for all, everyone, everything)


b. Existential quantifier, (for some, at least one).
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:

o For all x
o For each x
o For every x.

Example:
All man drink coffee.

Let a variable x which refers to a cat so all x can be represented in UOD as


below:

∀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:
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:

o There exists a 'x.'


o For some 'x.'
o For at least one 'x.'

Example:
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.
Points to remember:
o The main connective for universal quantifier ∀(for all) is implication →.
o The main connective for existential quantifier ∃(there exist) is and ∧.

Properties of Quantifiers:
o In universal quantifier, ∀x∀y is similar to ∀y∀x.
o In Existential quantifier, ∃x∃y is similar to ∃y∃x.
o ∃x∀y is not similar to ∀y∃x.

Some Examples of FOL using quantifier:

1. All birds fly.


In this question the predicate is "fly(bird)."

∀x bird(x) →fly(x).
And since there are all birds who fly so it will be represented as follows.

2. Every man respects his parent.


In this question, the predicate is "respect(x, y)," where x=man, and

Since there is every man so will use ∀, and it will be represented as


y= parent.

∀x man(x) → respects (x, parent).


follows:

3. Some boys play cricket.

game. Since there are some boys so we will use ∃, and it will be
In this question, the predicate is "play(x, y)," where x= boys, and y=

∃x boys(x) → play(x, cricket).


represented as:

4. Not all students like both Mathematics and Science.


In this question, the predicate is "like(x, y)," where x= student, and

Since there are not all students, so we will use ∀ with negation,
y= subject.

¬∀ (x) [ student(x) → like(x, Mathematics) ∧ like(x,


so following representation for this:

Science)].

5. Only one student failed in Mathematics.


In this question, 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

∃(x) [ student(x) → failed (x, Mathematics) ∧∀ (y)


following representation for this:

[¬(x==y) ∧ student(y) → ¬failed (x, Mathematics)].


Free and Bound Variables:
The quantifiers interact with variables which appear in a suitable way.
There are two types of variables in First-order logic which are given below:

Free Variable: A variable is said to be a free variable in a formula if it


occurs outside the scope of the quantifier.

Example: ∀x ∃(y)[P (x, y, z)], where z is a free variable.

Bound Variable: A variable is said to be a bound variable in a formula if


it occurs within the scope of the quantifier.

Example: ∀x [A (x) B( y)], here x and y are the bound


variables.

Inference in First-Order Logic


Inference in First-Order Logic is used to deduce new facts or sentences
from existing sentences. Before understanding the FOL inference rule,
let's understand some basic terminologies used in FOL.

Substitution:

Substitution is a fundamental operation performed on terms and formulas.


It occurs in all inference systems in first-order logic. The substitution is
complex in the presence of quantifiers in FOL. If we write F[a/x], so it
refers to substitute a constant "a" in place of variable "x".

Note: First-order logic is capable of expressing facts about some or all objects in the
universe.

Equality:

First-Order logic does not only use predicate and terms for making atomic
sentences but also uses another way, which is equality in FOL. For this, we
can use equality symbols which specify that the two terms refer to the
same object.

Example: Brother (John) = Smith.

As in the above example, the object referred by the Brother (John) is


similar to the object referred by Smith. The equality symbol can also be
used with negation to represent that two terms are not the same objects.
Example: ¬(x=y) which is equivalent to x ≠y.

FOL inference rules for quantifier:


As propositional logic we also have inference rules in first-order logic, so
following are some basic inference rules in FOL:

o Universal Generalization
o Universal Instantiation
o Existential Instantiation
o Existential introduction

1. Universal Generalization:

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

o It can be represented as: .


o This rule can be used if we want to show that every element has a similar
property.
o 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.

2. Universal Instantiation:

o 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.
o The new KB is logically equivalent to the previous KB.
o As per UI, we can infer any sentence obtained by substituting a
ground term for the variable.
o 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.

o 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:

o King(John) ∧ Greedy (John) → Evil (John),


o King(Richard) ∧ Greedy (Richard) → Evil (Richard),
o King(Father(John)) ∧ Greedy (Father(John)) → Evil (Father(John)),

3. Existential Instantiation:

o Existential instantiation is also called as Existential Elimination, which is a


valid inference rule in first-order logic.
o It can be applied only once to replace the existential sentence.
o The new KB is not logically equivalent to old KB, but it will be satisfiable if
old KB was satisfiable.
o 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.
o The restriction with this rule is that c used in the rule must be a new term
for which P(c ) is true.

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

o The above used K is a constant symbol, which is called Skolem constant.


o The Existential instantiation is a special case of Skolemization process.
4. Existential introduction

o An existential introduction is also known as an existential generalization,


which is a valid inference rule in first-order logic.
o 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.

o It can be represented as:


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

What is Unification?
o Unification is a process of making two different logical atomic expressions
identical by finding a substitution. Unification depends on the substitution
process.
o It takes two literals as input and makes them identical using substitution.
o 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).
o Example: Find the MGU for Unify{King(x), King(John)}

Let Ψ1 = King(x), Ψ2 = King(John),

Substitution θ = {John/x} is a unifier for these atoms and applying this


substitution, and both expressions will be identical.

o The UNIFY algorithm is used for unification, which takes two atomic
sentences and returns a unifier for those sentences (If any exist).
o Unification is a key component of all first-order inference algorithms.
o It returns fail if the expressions do not match with each other.
o The substitution variables are called Most General Unifier or MGU.

E.g. Let's say there are two different expressions, P(x, y), and P(a, f(z)).

In this example, we need to make both above statements identical to


each other. For this, we will perform the substitution.
P(x,y).........(i)
P(a, f(z))......... (ii)

o Substitute x with a, and y with f(z) in the first expression, and it will be
represented as a/x and f(z)/y.
o With both the substitutions, the first expression will be identical to the
second expression and the substitution set will be: [a/x, f(z)/y].

Conditions for Unification:


Following are some basic conditions for unification:

o Predicate symbol must be same, atoms or expression with different


predicate symbol can never be unified.
o Number of Arguments in both expressions must be identical.
o Unification will fail if there are two similar variables present in the same
expression.

Unification Algorithm:
Implementation of the Algorithm
Step.1: Initialize the substitution set to be empty.

Step.2: Recursively unify atomic sentences:

a. Check for Identical expression match.


b. If one expression is a variable v i, and the other is a term t i which does not
contain variable vi, then:

a. Substitute ti / vi in the existing substitutions


b. Add ti /vi to the substitution setlist.
c. If both the expressions are functions, then function name must be
similar, and the number of arguments must be the same in both the
expression.
Resolution in FOL
Resolution
Resolution is a theorem proving technique that proceeds by building
refutation proofs, i.e., proofs by contradictions. It was invented by a
Mathematician John Alan Robinson in the year 1965.

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.

Note: To better understand this topic, firstly learns the FOL in AI.

Steps for Resolution:


1. Conversion of facts into first-order logic.
2. Convert FOL statements into CNF
3. Negate the statement which needs to prove (proof by contradiction)
4. Draw resolution graph (unification).

To better understand all the above steps, we will take an example in


which we will apply resolution.

Example:
a. John likes all kind of food.
b. Apple and vegetable are food
c. Anything anyone eats and not killed is food.
d. Anil eats peanuts and still alive
e. Harry eats everything that Anil eats.
Prove by resolution that:
f. John likes peanuts.

Step-1: Conversion of Facts into FOL


In the first step we will convert all the given statements into its first order
logic.

Step-2: Conversion of FOL into CNF

In First order logic resolution, it is required to convert the FOL into CNF as
CNF form makes easier for resolution proofs.

o Eliminate all implication (→) and rewrite

a. ∀x ¬ food(x) V likes(John, x)
b. food(Apple) Λ food(vegetables)
c. ∀x ∀y ¬ [eats(x, y) Λ ¬ killed(x)] V food(y)
d. eats (Anil, Peanuts) Λ alive(Anil)
e. ∀x ¬ eats(Anil, x) V eats(Harry, x)
f. ∀x¬ [¬ killed(x) ] V alive(x)
g. ∀x ¬ alive(x) V ¬ killed(x)
h. likes(John, Peanuts).
o Move negation (¬)inwards and rewrite

a. ∀x ¬ food(x) V likes(John, x)
b. food(Apple) Λ food(vegetables)
c. ∀x ∀y ¬ eats(x, y) V killed(x) V food(y)
d. eats (Anil, Peanuts) Λ alive(Anil)
e. ∀x ¬ eats(Anil, x) V eats(Harry, x)
f. ∀x ¬killed(x) ] V alive(x)
g. ∀x ¬ alive(x) V ¬ killed(x)
h. likes(John, Peanuts).
o Rename variables or standardize variables

a. ∀x ¬ food(x) V likes(John, x)
b. food(Apple) Λ food(vegetables)
c. ∀y ∀z ¬ eats(y, z) V killed(y) V food(z)
d. eats (Anil, Peanuts) Λ alive(Anil)
e. ∀w¬ eats(Anil, w) V eats(Harry, w)
f. ∀g ¬killed(g) ] V alive(g)
g. ∀k ¬ alive(k) V ¬ killed(k)
h. likes(John, Peanuts).
o Eliminate existential instantiation quantifier by elimination.
In this step, we will eliminate existential quantifier ∃, and this process is
known as Skolemization. But in this example problem since there is no
existential quantifier so all the statements will remain same in this step.
o Drop Universal quantifiers.
In this step we will drop all universal quantifier since all the statements are
not implicitly quantified so we don't need it.

a. ¬ food(x) V likes(John, x)
b. food(Apple)
c. food(vegetables)
d. ¬ eats(y, z) V killed(y) V food(z)
e. eats (Anil, Peanuts)
f. alive(Anil)
g. ¬ eats(Anil, w) V eats(Harry, w)
h. killed(g) V alive(g)
i. ¬ alive(k) V ¬ killed(k)
j. likes(John, Peanuts).

Note: Statements "food(Apple) Λ food(vegetables)" and "eats (Anil, Peanuts) Λ


alive(Anil)" can be written in two separate statements.

o Distribute conjunction ∧ over disjunction ¬.


This step will not make any change in this problem.
Step-3: Negate the statement to be proved

In this statement, we will apply negation to the conclusion statements,


which will be written as ¬likes(John, Peanuts)

Step-4: Draw Resolution graph:

Now in this step, we will solve the problem by resolution tree using
substitution. For the above problem, it will be given as follows:

Hence the negation of the conclusion has been proved as a complete


contradiction with the given set of statements.

Forward Chaining and backward chaining


in AI
In artificial intelligence, forward and backward chaining is one of the
important topics, but before understanding forward and backward
chaining lets first understand that from where these two terms came.

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:

a. Forward chaining
b. 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.

Properties of Forward-Chaining:

o It is a down-up approach, as it moves from bottom to top.


o It is a process of making a conclusion based on known facts or data, by
starting from the initial state and reaches the goal state.
o Forward-chaining approach is also called as data-driven as we reach to the
goal using available data.
o Forward -chaining approach is commonly used in the expert system, such
as CLIPS, business, and production rule systems.

Consider the following famous example which we will use in both


approaches:

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

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.

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 with the goal and works
backward, chaining through rules to find known facts that support the
goal.

Properties of backward chaining:

o It is known as a top-down approach.


o Backward-chaining is based on modus ponens inference rule.
o In backward chaining, the goal is broken into sub-goal or sub-goals to
prove the facts true.
o It is called a goal-driven approach, as a list of goals decides which rules
are selected and used.
o Backward -chaining algorithm is used in game theory, automated theorem
proving tools, inference engines, proof assistants, and various AI
applications.
o The backward-chaining method mostly used a depth-first
search strategy for proof.
Example:
In backward-chaining, we will use the same above example, and will
rewrite all the rules.

o American (p) ∧ weapon(q) ∧ sells (p, q, r) ∧ hostile(r) →


Criminal(p) ...(1)
Owns(A, T1) ........(2)
o Missile(T1)
o ?p Missiles(p) ∧ Owns (A, p) → Sells (Robert, p, A) ......(4)
o Missile(p) → Weapons (p) .......(5)
o Enemy(p, America) →Hostile(p) ........(6)
o Enemy (A, America) .........(7)
o American(Robert). ..........(8)

S. Forward Chaining Backward Chaining


No
.

1. Forward chaining starts from Backward chaining starts from the goal
known facts and applies and works backward through inference
inference rule to extract more rules to find the required facts that
data unit it reaches to the goal. support the goal.

2. It is a bottom-up approach It is a top-down approach

3. Forward chaining is known as Backward chaining is known as goal-


data-driven inference technique driven technique as we start from the
as we reach to the goal using the goal and divide into sub-goal to extract
available data. the facts.

4. Forward chaining reasoning Backward chaining reasoning applies a


applies a breadth-first search depth-first search strategy.
strategy.

5. Forward chaining tests for all the Backward chaining only tests for few
available rules required rules.

6. Forward chaining is suitable for Backward chaining is suitable for


the planning, monitoring, diagnostic, prescription, and debugging
control, and interpretation application.
application.
7. Forward chaining can generate Backward chaining generates a finite
an infinite number of possible number of possible conclusions.
conclusions.

8. It operates in the forward It operates in the backward direction.


direction.

9. Forward chaining is aimed for Backward chaining is only aimed for


any conclusion. the required data.

You might also like