Ai 4
Ai 4
• Text book: Stuart J. Russell and Peter Norvig, Artificial Intelligence, 3rd Edition,
Pearson, 2015
Representation Revisited
• The language of thought
• Combining the best of natural and formal languages.
Objects: people, houses, numbers, theories, Ronald McDonald, colors,
baseball games, wars, centuries . . .
Relations: these can be unary relations or properties such as red, round,
bogus, prime,multistoried . . ., or more general n-ary relations such as
brother of, bigger than, inside,part of, has color, occurred after, owns,
comes between, . . .
Functions: father of, best friend, third inning of, one more than,
beginning of .
Prof. Salma Itagi,Dept. of CSE,SVIT 3
1. “One plus two equals three.”
• Objects: one, two, three, one plus two; Relation: equals; Function: plus.
(“One plus two” is a name for the object that is obtained by applying the
function “plus” to the objects “one” and “two.” “Three” is another name
for this object.)
2. “Squares neighboring the wumpus are smelly.”
• Objects: wumpus, squares; Property: smelly; Relation: neighboring.
Complex sentence
¬Brother (LeftLeg(Richard), John)
Brother (Richard , John) ∧ Brother (John,Richard) King(Richard ) ∨ King(John)
¬King(Richard) ⇒ King(John)
1. First-order logic contains two standard quantifiers, called universal and existential
• “All kings are persons”
• ∀ x King(x) ⇒ Person(x) .
• Universal quantification makes statements about every object.
• A statement about some object in the universe without naming it, by using an
existential quantifier.
• King John has a crown on his head ∃ x Crown(x) ∧ OnHead(x, John) .
• “For all x, if x is a king, then x is a person.” The symbol x is called a variable.
• By convention, variables are lowercase letters.
• A variable is a term all by itself, and as such can also serve as the argument of a
function—for example, LeftLeg(x).
• A term with no variables is called a ground term.
Nested Quantifiers
• 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.”
5. A set is a subset of another set if and only if all of the first set’s members are
members of the second set:
∀ s1, s2 s1 ⊆ s2 ⇔ (∀x x∈ s1 ⇒ x∈ s2)
6. Two sets are equal if and only if each is a subset of the other:
∀ s1, s2 (s1 =s2) ⇔ (s1 ⊆ s2 ∧ s2 ⊆ s1)
7. An object is in the intersection of two sets if and only if it is a member of both sets:
∀ x, s1, s2 x∈ (s1 ∩ s2) ⇔ (x∈ s1 ∧ x∈s2) .
8. An object is in the union of two sets if and only if it is a member of either set:
∀ x, s1, s2 x∈ (s1 ∪ s2) ⇔ (x∈ s1 ∨ x∈s2) .
• Lists are similar to sets.
• The differences are that lists are ordered and the same element can appear
more than once in a list.
• We can use the vocabulary of Lisp for lists: Nil is the constant list with no
elements; Cons, Append, First, and Rest are functions; and Find is the
predicate that does for lists what Member does for sets. List? is a predicate
that is true only of lists.
• The empty list is [ ]. The term Cons(x, y), where y is a nonempty list, is
written [x|y].
• The term Cons(x, Nil) (i.e., the list containing the element x) is written as
[x].
• A list of several elements, such as [A,B,C], corresponds to the nested term
Cons(A, Cons(B, Cons(C, Nil))).
3. The WUMPUS WORLD
• At the highest level, one analyzes the circuit’s functionality. For example, does the circuit in Figure 8.6 actually
add properly?
• If all the inputs are high, what is the output of gate A2?
• For example, what are all the gates connected to the first input terminal?
• There are more detailed levels of analysis, including those related to timing delays, circuit area, power
consumption, production cost, and so on.
• Signals flow along wires to the input terminals of gates, and each gate produces a signal on the output
terminal that flows along another wire.
• To determine what these signals will be, we need to know how the gates transform their input signals.
• There are four types of gates: AND, OR, and XOR gates have two input terminals, and NOT gates
have one.
• First, we need to be able to distinguish gates from each other and from other objects.
• Each gate is represented as an object named by a constant, about which we assert that it is a gate
with, say, Gate(X1).
• The behavior of each gate is determined by its type: one of the constants AND,OR, XOR, or NOT.
• Because a gate has exactly one type, a function is appropriate: Type(X1)=XOR.
• Circuits, like gates, are identified by a predicate: Circuit(C1).
Decide on Vocabulary (cont..,)
• Next we consider terminals, which are identified by the predicate Terminal (x).
• A gate or circuit can have one or more input terminals and one or more output terminals.
• We use the function In(1,X1) to denote the first input terminal for gate X1.
• The function Arity(c, i, j) says that circuit c has i input and j output terminals.
• The connectivity between gates can be represented by a predicate, Connected, which takes two terminals as arguments, as in
Connected(Out(1,X1), In(1,X2)).
• One possibility is to use a unary predicate, On(t), which is true when the signal at a terminal is on.
• This makes it a little difficult, however, to pose questions such as “What are all the possible values of the signals at the output
terminals of circuit C1 ?”
• We therefore introduce as objects two signal values, 1 and 0, and a function Signal (t) that denotes the signal value for the terminal t.
Encode general knowledge of the domain
1. If two terminals are connected, then they have the same signal:
∀ t1, t2 Terminal (t1) ∧ Terminal (t2) ∧ Connected(t1, t2) ⇒ Signal
(t1)=Signal (t2) .
1. If two terminals are connected, then they have the same signal:
∀ t1, t2 Terminal (t1) ∧ Terminal (t2) ∧ Connected(t1, t2) ⇒ Signal (t1)=Signal (t2) .
Encode the specific problem instance
• First,
• we categorize the circuit and its component
gates:
• Circuit(C1) ∧ Arity(C1, 3, 2) •X1 (XOR): Computes A ⊕ B.
• Gate(X1) ∧ Type(X1)=XOR •X2 (XOR): Computes (A ⊕ B) ⊕ C_in to produce the
Sum output.
• Gate(X2) ∧ Type(X2)=XOR •A1 (AND): Computes A ∧ B.
• Gate(A1) ∧ Type(A1)=AND •A2 (AND): Computes C_in ∧ (A ⊕ B).
• Gate(A2) ∧ Type(A2)=AND •O1 (OR): Computes the Carry-out (C_out) as the OR
of the two AND gates' outputs.
• Gate(O1) ∧ Type(O1)=OR .
Then, we show the connections between them:
1. Connected(Out(1,X1), In(1,X2)) Connected(In(1,C1),
In(1,X1))
2. Connected(Out(1,X1), In(2,A2)) Connected(In(1,C1),
In(1,A1))
3. Connected(Out(1,A2), In(1,O1)) Connected(In(2,C1),
In(2,X1))
4. Connected(Out(1,A1), In(2,O1)) Connected(In(2,C1),
In(2,A1)) Gate(X1) ∧ Type(X1)=XOR
5.1. Connected(Out(1,X1),
Connected(Out(1,X2), Out(1,C1)) Connected(In(3,C1),
In(1,X2)) Gate(A1) ∧ Type(A1)=AND
In(2,X2))
•Out(1, X1): The output of the first XOR gate X1.
6. Connected(Out(1,O1),
•In(1, Out(2,C1))
X2): The first input of the Connected(In(3,C1),
second XOR gate X2.
statement. means that the output of X1 is connected to the first input of X2. In the full adder, X1 calculates A ⊕ B, and
•ThisIn(1,A2))
this output is used as an input to X2, which calculates (A ⊕ B) ⊕ C_in to produce the Sum.
2. Connected(In(1,C1), In(1,X1))
•In(1, C1): The first input of the circuit C1 (which has 3 inputs and 2 outputs, representing the 1-bit full adder).
•In(1, X1): The first input of XOR gate X1.
•This connection means that A (from the inputs of the full adder) is connected to the first input of XOR gate X1.
3. Connected(Out(1, X1), In(2, A2))
•Out(1, X1): The output of XOR gate X1.
•In(2, A2): The second input of AND gate A2.
•This means the output of X1 (which is A ⊕ B) is connected to the second input of the AND gate A2, where it is used as
(A ⊕ B) in the carry calculation.
Summary:
•The 1-bit full adder circuit involves three types of gates: XOR, AND, and OR.
•The inputs are A, B, and C_in (carry input), and the outputs are Sum and Carry-out.
•The various Connected statements describe how these gates interact with each other, ensuring the proper calculation of the
Sum and Carry-out.
•The Sum is calculated by XORing A, B, and C_in, and the Carry-out is calculated using AND and OR gates, based on the inputs.
The connections reflect the standard logic for a 1-bit full adder where:
•Sum = (A ⊕ B) ⊕ C_in.
•Carry-out = (A ∧ B) ∨ (C_in ∧ (A ⊕ B)).
Pose queries to the inference procedure
• What combinations of inputs would cause the first output of C1 (the sum bit) to be
0 and the second output of C1 (the carry bit) to be 1?
• ∃ i1, i2, i3 Signal (In(1, C1))=i1 ∧ Signal (In(2, C1))=i2 ∧ Signal (In(3,
C1))=i3∧ Signal (Out(1, C1))=0 ∧ Signal (Out(2, C1))=1 .
• The answers are substitutions for the variables i1, i2, and i3 such that the resulting
sentence is entailed by the knowledge base.
• ASKVARS will give us three such substitutions:
• {i1/1, i2/1, i3/0} {i1/1, i2/0, i3/1} {i1/0, i2/1, i3/1} .
{i1/1, i2/1, i3/0}: Sum = 1 ⊕ 1 ⊕ 0 = 0 (sum is 0) and carry-out = (1 ∧ 1) ∨ (1 ∧ 0) ∨ (1 ∧ 0) = 1 (carry-out is
1).
Cout=(A∧B)∨(B∧Cin)∨(A∧Cin)
• What are the possible sets of values of all the terminals for the adder circuit?
• ∃ i1, i2, i3, o1, o2 Signal (In(1, C1))=i1 ∧ Signal (In(2, C1))=i2
• ∧ Signal (In(3, C1))=i3 ∧ Signal (Out(1, C1))=o1 ∧ Signal (Out(2, C1))=o2 .
For example:
{i1=0, i2=0, i3=0, o1=0, o2=0}
Debug the knowledge base
• Various ways to see what kinds of erroneous behaviors emerge.
• For example, suppose we fail to read alternative semantics and hence forget to assert that 1 0.
• We can pinpoint the problem by asking for the outputs of each gate. For example, we can ask
• ∃ i1, i2, o Signal (In(1,C1))=i1 ∧ Signal (In(2,C1))=i2 ∧ Signal (Out(1,X1)) ,
• which reveals that no outputs are known at X1 for the input cases 10 and 01.
• Then, we look at the axiom for XOR gates, as applied to X1:
• Signal (Out(1,X1))=1 ⇔ Signal (In(1,X1)) = Signal (In(2,X1)) .
• If the inputs are known to be, say, 1 and 0, then this reduces to
• Signal (Out(1,X1))=1 ⇔ 1 0.
• Now the problem is apparent: the system is unable to infer that Signal (Out(1,X1))=1, so we need to tell it
that 1 0.
Summary
•The system is unable to infer the correct behavior of the XOR gate for certain input combinations, like 1 and 0.
• This failure to infer is due to the lack of knowledge about the relationship between those inputs (i.e., that 1 and 0
are different).
•By examining the axiom for the XOR gate and testing for the output at each gate, it becomes clear that the system
needs to be explicitly told about the condition 1 ≠ 0 in order to deduce the correct output.
•Once this information is provided, the system can correctly infer that Signal (Out(1, X1)) = 1 when the inputs are 1
and 0.
In essence, the problem is a missing or forgotten assertion that would allow the system to properly deduce the XOR
gate's output.
Artificial Intelligence-Module 4
Chapter 9
• For example, the three sentences given earlier are obtained with the substitutions {x/John}, {x/Richard }, and {x/Father
(John)}.
• In the rule for Existential Instantiation, the variable is replaced by a single new constant symbol. The formal statement is
as follows: for any sentence α, variable v, and constant symbol k that does not appear elsewhere in the knowledge base,
∃v α
SUBST({v/k}, α)
Step 2: Convert the logical operations (→ and ∃) into simpler logical connectives.
• "Person(x) → ∃y (FriendOf(y, x))" becomes "¬Person(x) ∨ ∃y FriendOf(y, x)"
• (implication is replaced by disjunction and negation).
Step 3: Replace existential quantifiers with propositional variables, e.g., FriendOf(a, b).
• Thus, the original statement can be reduced to a set of propositional logic clauses.
UNIFICATION AND LIFTING
• Unification is the process of finding a substitution for variables in two logical expressions so that the
expressions become identical. In essence, the goal is to make two formulas "look the same" by replacing
variables with terms (constants or functions).
Generalized Modus Ponens is a lifted version of Modus Ponens—it raises Modus Ponens from ground (variable-free)
propositional logic to first-order logic.
Premises
We have a series of n atomic sentences (propositions), denoted by:
•p1,p2,…, pn
•P1is: King(x)
•p2 is: Greedy(x)
Substitution (θ)
The substitution θ is given as:
•θ={x/John,y/John}
This substitution says:
•Replace x with John in all occurrences of x.
•Replace y with John in all occurrences of y.
Thus, when you apply this substitution to a formula, you replace the variables x and y with the constant "John."
Substitution Applied to the Conclusion (q)
The final part of the rule is applying the substitution θ to the consequent q, which is Evil(x) q is Evil(x) meaning
"x is evil."
•After applying the substitution θ={x/John,y/John}, you replace the variable x with "John".
Therefore:
•SUBST(θ,q) becomes Evil(John)
So, after substitution, the conclusion becomes Evil(John), which asserts that John is evil.
Here’s a complete breakdown of how this rule works in the context of the given example:
1.Premises:
1. p1:King(x) (John is a King)
2. p2:Greedy (John is Greedy)
3. The rule: If p1and p2 are true, then q (Evil(x)) is true.
2.Implication (Rule):
1. (p1∧p2⇒q) which reads: "If John is a King and greedy, then John is evil."
3.Substitution:
1. θ={x/John,y/John}, meaning replace all instances of x and y with "John."
4.Conclusion after applying the substitution:
1. q=Evil(x), and after applying θ , it becomes Evil(John).
Thus, the result of applying the substitution θ to q is that John is evil.
Unification
• UNIFY(Knows(John, x), Knows(John, Jane)) = {x/Jane}
• Substitution: x→Jane
• UNIFY(Knows(John, x), Knows(y, Bill )) = {x/Bill, y/John}
• Substitution: x→Bill,y→John
• UNIFY(Knows(John, x), Knows(y,Mother (y))) = {y/John, x/Mother
(John)}
• Substitution: y→John,x→Mother(John)
• UNIFY(Knows(John, x), Knows(x, Elizabeth)) = fail .
• Fail: The unification fails because of a contradiction in the substitution for x.
Example 1:
We want to unify the terms
Knows(John, x) and Knows(John, Jane).
•The operator Knows is the same for both terms.
•The first argument is John in both cases, so they
are already unified.
•The second argument is x in the first term and
Jane in the second term. We need to unify x with
Jane.
The unification is possible, and the substitution
becomes {x → Jane}.
FORWARD CHAINING
• The idea is simple: start with the atomic sentences in the knowledge base and apply Modus Ponens in the
forward direction, adding new atomic sentences, until no further inferences can be made.
• Definite clauses such as Situation ⇒ Response are especially useful for systems that make inferences in
response to newly arrived information.
• Forward Chaining is a method of reasoning used in rule-based systems and expert systems. It is a form of
data-driven reasoning where the inference engine starts with the available facts and applies inference rules to
extract new facts, progressively working forward through the knowledge base.
Eg:
The law says that it is a crime for an
American to sell weapons to hostile nations.
The country Nono, an enemy of America,
has some missiles, and all of its missiles
were sold to it by ColonelWest, who is
American.
American(x) ∧Weapon(y) ∧ Sells(x, y,
z) ∧ Hostile(z) ⇒ Criminal (x) .
Algorithm Steps Explained:
1. Initialize:
•The algorithm starts by initializing the new set to empty. This set will collect all newly inferred facts on each
iteration.
2. Repeat Until New Facts Are Inferred:
•The loop continues until no new facts are inferred, i.e., when the new set is empty. This ensures that the algorithm
will keep applying the rules to infer facts until no further progress can be made.
3. Standardizing Variables in Rules:
•For each rule in the knowledge base, the algorithm applies standardization. The function
STANDARDIZE-VARIABLES(rule) ensures that all variables in the rule are renamed so that no two rules share
the same variable names. This prevents variable conflicts during unification.
• Example: If two rules have variables x and y, STANDARDIZE-VARIABLES will rename them (e.g., x1 and
y1 ) to avoid confusion when performing unification.
4. Try to Match Rules with Known Facts:
•For each rule in the KB, it looks for substitutions θ that match the left-hand side (the premises) of the rule with
some existing facts in the KB.
•It checks all combinations of the premises p1 ,p2 ,…, pn from the KB (i.e., the left side of the rule).
•The algorithm compares the premises of the rule against the existing facts (which could be in KB or new facts) to
see if any of them unify with the premises. If a match is found, the rule’s conclusion q is derived using the
unification of those facts.
5. Apply Substitution to the Conclusion:
•If a match is found (i.e., a valid substitution θ is found), the algorithm applies the substitution to the conclusion q of
the rule:
• q' = SUBST(θ, q): This means the right-hand side (conclusion) of the rule is modified using the substitution
θ.
6. Check for Redundant Conclusions:
•After obtaining q′, the algorithm checks if q′already exists in the KB or in the set of newly inferred facts (new).
• If q′is already present, it is not added again.
• If q′ is new, it is added to the new set. This ensures that only new, unique facts are added to the knowledge
base.
7. Unify with the Query:
•After the fact q′ has been added to the new set, the algorithm checks if this fact unifies with the query α.
• φ = UNIFY(q', α): The function UNIFY tries to find a substitution φ that makes q′and α identical.
• If UNIFY succeeds (i.e., returns a non-failing substitution φ), then the query is provable from the
knowledge base, and the algorithm returns φ, the substitution that makes the query true.
8. Add New Facts to KB:
•If no valid unification is found for any q′, the new facts are added to the knowledge base KB.
• This is done so that these newly inferred facts can be used in the next iteration to infer further facts.
9. Return False if Query is Not Provable:
•If, after all iterations, no unification is found for α, meaning no substitution makes the query true, the algorithm
returns false. This means that the query α cannot be derived from the knowledge base.
FORWARD CHAINING
• The idea is simple: start with the atomic sentences in the knowledge base and apply Modus Ponens in the
forward direction, adding new atomic sentences, until no further inferences can be made.
• This method uses First Order Definite clause .
• A definite clause either is atomic or is an implication whose antecedent is a conjunction of positive literals and
whose consequent is a single positive literal.
For example
• King(x) ∧ Greedy(x) ⇒ Evil(x) .
• King(John) .
• Greedy(y) .
• The law says that it is a crime for an American to sell weapons to hostile nations.
The country Nono, an enemy of America, has some missiles, and all of its missiles
were sold to it by ColonelWest, who is American.
• We will prove that West is a criminal.
• Representation of this by using first order definite clauses.
• This knowledge base contains no function symbols and is therefore an instance of the class
of Datalog knowledge bases.
• Datalog is a language that is restricted to first-order definite clauses with no function
symbols.
A simple forward chaining algorithm
• Starting from the known facts, it triggers all the rules whose premises are satisfied,
adding their conclusions to the known facts. The process repeats until the query is
answered or no new facts are added.
• On the first iteration, rule 1 has unsatisfied premises.
• Rule 4 is satisfied with {x/M1}, and Sells(West,M1, Nono) is added.
• Rule 5 is satisfied with {x/M1}, and Weapon(M1) is added.
• Rule 6 is satisfied with {x/Nono}, and Hostile(Nono) is added.
• On the second iteration, rule 1 is satisfied with {x/West, y/M1, z/Nono}, and
Criminal (West) is added.
Steps:
1. Initialization
2. Loop until new is empty
3. Processing each rule
4. Finding applicable
premises
5. Checking for duplicates
6. Unifying with the query
7. Adding new facts to KB
8. Termination
Efficient Forward Chaining
• There are three possible sources of inefficiency.
• First, the “inner loop” of the algorithm involves finding all possible unifiers such that the premise of a rule
unifies with a suitable set of facts in the knowledge base. This is often called pattern matching and can be
very expensive.
• Second, the algorithm rechecks every rule on every iteration to see whether its premises are satisfied, even if
very few additions are made to the knowledge base on each iteration.
• Finally, the algorithm might generate many facts that are irrelevant to the goal.