1. Home
  2. Explore
  3. Computer Science
  4. AI Knowledge & Reasoning

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

AI Knowledge & Reasoning

🧠 Knowledge-Based Agents & Logic

This section explores knowledge-based agents in AI, focusing on how artificial intelligence can represent and reason with knowledge to draw conclusions and solve problems. Unlike search problems that focus on actions in an environment, this approach emphasizes leveraging known facts to infer new information.

💡 The Essence of Knowledge & Reasoning

Human intelligence heavily relies on knowledge and the ability to reason about it. We use known facts to draw conclusions and understand how to act or discover new information. AI aims to replicate this by building knowledge-based agents that can represent knowledge internally and use algorithms to reason.

❓ Example: Harry Potter Deduction

Consider a simple deductive reasoning problem:

  1. If it didn't rain, then Harry visited Hagrid today.

  2. Harry visited Hagrid or Dumbledore today, but not both.

  3. Harry visited Dumbledore today.

From these three facts, a human can deduce:

  • From (2) and (3): Harry must not have visited Hagrid today. (Because he visited Dumbledore, and not both.)

  • From (1) and the previous deduction: If "Harry visited Hagrid today" is false, then "it didn't rain" must also be false. Therefore, it did rain today.

This process of using logic to take known information and reach new conclusions is the core of what AI agents need to achieve.

📜 Propositional Logic

To enable computers to reason logically, we need a formal system. Propositional logic is a fundamental type of logic based on statements about the world, called propositions.

⚛️ Propositional Symbols

  • Propositional symbols are basic assertions, often represented by letters like P, Q, R.

  • Each symbol represents a fact or sentence about the world.

    • Example: P could mean "it is raining."

    • Example: Q could mean "Harry visited Hagrid today."

  • Each symbol can be either true or false.

🔗 Logical Connectives

To combine propositional symbols into more complex statements, we use logical connectives:

Symbol

Name

Meaning

¬\neg¬

Not

Inverts the truth value of a proposition.

∧\wedge∧

And

True if both propositions are true.

∨\vee∨

Or

True if at least one proposition is true (inclusive or).

→\rightarrow→

Implication

"If P, then Q" (P implies Q). True unless P is true and Q is false.

↔\leftrightarrow↔

Biconditional

"P if and only if Q." True if P and Q have the same truth value.

Truth Tables for Logical Connectives

Truth tables define the meaning of each connective by showing all possible combinations of truth values for its inputs and the resulting output.

  • Not (¬\neg¬)

    P

    ¬\neg¬P

    False

    True

    True

    False

    ¬\neg¬P is the opposite of P. If P is "it is raining," then ¬\neg¬P is "it is not raining."

  • And (∧\wedge∧)

    P

    Q

    P ∧\wedge∧ Q

    False

    False

    False

    False

    True

    False

    True

    False

    False

    True

    True

    True

    P ∧\wedge∧ Q is true only when both P and Q are true.

  • Or (∨\vee∨)

    P

    Q

    P ∨\vee∨ Q

    False

    False

    False

    False

    True

    True

    True

    False

    True

    True

    True

    True

    P ∨\vee∨ Q is true if P is true, or Q is true, or both are true (inclusive OR).

  • Implication (→\rightarrow→)

    P

    Q

    P →\rightarrow→ Q

    False

    False

    True

    False

    True

    True

    True

    False

    False

    True

    True

    True

    P →\rightarrow→ Q (P implies Q) means "If P is true, then Q must also be true." It is only false if P is true and Q is false. If P is false, the implication makes no claim about Q, so it's considered true.

  • Biconditional (↔\leftrightarrow↔)

    P

    Q

    P ↔\leftrightarrow↔ Q

    False

    False

    True

    False

    True

    False

    True

    False

    False

    True

    True

    True

    P ↔\leftrightarrow↔ Q (P if and only if Q) is true only when P and Q have the same truth value. It implies P →\rightarrow→ Q and Q →\rightarrow→ P.


🌎 Models & Knowledge Bases

To bridge logical statements with the real world, AI uses models and knowledge bases.

🗺️ Models

  • A model assigns a truth value (true or false) to every propositional symbol.

  • A model represents a possible world or a specific state of affairs.

  • If there are NNN propositional symbols, there are 2N2^N2N possible models.

    • Example: If P is "it is raining" and Q is "it is a Tuesday", a model could be:

      • P = True (it is raining)

      • Q = False (it is not a Tuesday)

    • There are 22=42^2 = 422=4 possible models for these two symbols.

🧠 Knowledge Base (KB)

  • A knowledge base (KB) is a set of sentences in propositional logic that an AI agent knows to be true about the world.

  • The AI uses this KB to draw conclusions.

➡️ Entailment & Inference

The ultimate goal is for the AI to use its knowledge to infer new, true information.

⛓️ Entailment (⊨\models⊨)

  • Alpha entails Beta (α⊨β\alpha \models \betaα⊨β) means: In every model (possible world) where sentence α\alphaα is true, sentence β\betaβ is also true.

  • If α⊨β\alpha \models \betaα⊨β, then knowing α\alphaα to be true guarantees that β\betaβ is also true.

    • Example: "It is a Tuesday in January" entails "It is January."

💡 Inference

  • Inference is the process of deriving new sentences from old ones based on logical rules.

  • The AI's task is to determine if its KB entails a query (α\alphaα). That is, can we conclude α\alphaα is true based on the KB alone?

🧩 Inference Example

Propositional Symbols:

  • P: It is a Tuesday.

  • Q: It is raining.

  • R: Harry will go for a run.

Knowledge Base (KB):

  1. P ∧¬\wedge \neg∧¬Q →\rightarrow→ R (If it is a Tuesday AND it is not raining, THEN Harry will go for a run.)

  2. P (It is a Tuesday.)

  3. ¬\neg¬Q (It is not raining.)

Inference Process:

  • From (2) and (3), we know P is true and $\neg$Q is true.

  • Therefore, P $\wedge \neg$Q is true.

  • Since P $\wedge \neg$Q $\rightarrow$ R (from 1) and P $\wedge \neg$Q is true, by the definition of implication, R must be true.

  • Conclusion: R (Harry will go for a run) is true.

🔎 Model Checking Algorithm

One way to perform inference is model checking, which systematically checks all possible worlds.

⚙️ How Model Checking Works

To determine if KB $\models \alpha$:

  1. Enumerate all possible models: List every combination of true/false assignments for all propositional symbols.

  2. For each model:

    • Check if the knowledge base (KB) is true in that model.

    • If the KB is true, then check if the query (α\alphaα) is also true in that model.

  3. If α\alphaα is true in every model where KB is true, then KB $\models \alpha$. Otherwise, there is no entailment.

💻 Model Checking in Practice (Python Example)

A logic.py library can define classes for symbols and connectives (e.g., Symbol, Not, And, Or, Implication).

Harry Potter Example:

  • Symbols: rain, hagrid, dumbledore

  • Knowledge Base (knowledge variable):

    • Implication(Not(rain), hagrid): If it's not raining, Harry visited Hagrid.

    • Or(hagrid, dumbledore): Harry visited Hagrid or Dumbledore.

    • Not(And(hagrid, dumbledore)): Harry didn't visit both Hagrid and Dumbledore.

    • dumbledore: Harry visited Dumbledore.

A model_check(knowledge, query) function would recursively explore all 2N2^N2N possible truth assignments for rain, hagrid, dumbledore and verify the entailment. For the Harry Potter example, model_check(knowledge, rain) would return True.

🏗️ Knowledge Engineering

  • Knowledge engineering is the process of taking a real-world problem and distilling it into knowledge that is representable by a computer using logical symbols and formulas.

  • This allows general-purpose problems to be solved by inference algorithms.

🎲 Clue Game Example (Knowledge Engineering)

Goal: Determine the murderer, room, and weapon in the game Clue.

Propositional Symbols:

  • Mustard, Plum, Scarlet (for characters)

  • Ballroom, Kitchen, Library (for rooms)

  • Knife, Revolver, Wrench (for weapons)

Initial Knowledge (KB):

  1. Or(Mustard, Plum, Scarlet): One person is the murderer.

  2. Or(Ballroom, Kitchen, Library): One room is where it happened.

  3. Or(Knife, Revolver, Wrench): One weapon was used.

  4. (Additional rules for exclusivity, e.g., if Mustard is the murderer, Plum is not)

Game Play (adding knowledge):

  • If you have the Mustard card: knowledge.add(Not(Mustard))

  • If a guess (e.g., Scarlet, Library, Wrench) is revealed to be false (one of the cards is not in the envelope): knowledge.add(Or(Not(Scarlet), Not(Library), Not(Wrench)))

By adding more knowledge (cards seen, deductions), the model checking algorithm can eventually deduce the solution (e.g., "Yes, Miss Scarlet," "Yes, Library," "Yes, Knife").

🧩 Logic Puzzle Example (Hogwarts Houses)

Goal: Assign each person (Gildaroy, Minerva, Pomona, Horace) to a unique house (Gryffindor, Hufflepuff, Ravenclaw, Slytherin).

Propositional Symbols:

  • GildaroyGryffindor, MinervaHufflepuff, etc. (16 symbols: 4 people x 4 houses). Each is true if the person is in that house.

Initial Knowledge (KB):

  1. Every person belongs to a house: Or(GildaroyGryffindor, GildaroyHufflepuff, ...) for each person.

  2. Only one house per person: Implication(MinervaGryffindor, Not(MinervaHufflepuff)) for all pairs.

  3. Every house has only one person: Implication(MinervaGryffindor, Not(GildaroyGryffindor)) for all pairs.

Given Information (adding knowledge):

  • Or(GildaroyGryffindor, GildaroyRavenclaw)

  • Not(PomonaSlytherin)

  • MinervaGryffindor

Model checking, though slow, can deduce the unique assignment for each person to their house.

🐢 Limitations of Model Checking

  • Computational Complexity: Model checking requires enumerating 2N2^N2N possible models for NNN propositional symbols. This becomes intractable for larger numbers of variables.

  • For complex problems with many variables, more efficient inference methods are needed.


📚 Inference Rules

Inference rules are patterns that allow us to derive new true sentences from existing true sentences without enumerating all models.

📝 Structure of Inference Rules

  • Premise(s): Statement(s) above a horizontal line that are known to be true.

  • Conclusion: Statement below the line that can be derived.

📏 Common Inference Rules

  1. Modus Ponens

    • Premises: α→β\alpha \rightarrow \betaα→β, α\alphaα

    • Conclusion: β\betaβ

    • English: "If it is raining, then Harry is inside. It is raining. Therefore, Harry is inside."

  2. And Elimination

    • Premise: α∧β\alpha \wedge \betaα∧β

    • Conclusion: α\alphaα (or β\betaβ)

    • English: "Harry is friends with Ron and Hermione. Therefore, Harry is friends with Hermione."

  3. Double Negation Elimination

    • Premise: ¬(¬α)\neg(\neg \alpha)¬(¬α)

    • Conclusion: α\alphaα

    • English: "It is not true that Harry did not pass the test. Therefore, Harry passed the test."

  4. Implication Elimination

    • Premise: α→β\alpha \rightarrow \betaα→β

    • Conclusion: ¬α∨β\neg \alpha \vee \beta¬α∨β

    • English: "If it is raining, then Harry is inside. This means either it is not raining, or Harry is inside."

    • This rule is crucial for converting implications into disjunctions (OR statements).

  5. Biconditional Elimination

    • Premise: α↔β\alpha \leftrightarrow \betaα↔β

    • Conclusion: (α→β)∧(β→α)(\alpha \rightarrow \beta) \wedge (\beta \rightarrow \alpha)(α→β)∧(β→α)

    • English: "It is raining if and only if Harry is inside. This means if it is raining then Harry is inside, AND if Harry is inside then it is raining."

  6. De Morgan's Laws (for moving negations)

    • Not (A and B) →\rightarrow→ (Not A or Not B)

      • Premise: ¬(α∧β)\neg(\alpha \wedge \beta)¬(α∧β)

      • Conclusion: ¬α∨¬β\neg \alpha \vee \neg \beta¬α∨¬β

      • English: "It is not true that both Harry and Ron passed the test. Therefore, either Harry did not pass the test, or Ron did not pass the test."

    • Not (A or B) →\rightarrow→ (Not A and Not B)

      • Premise: ¬(α∨β)\neg(\alpha \vee \beta)¬(α∨β)

      • Conclusion: ¬α∧¬β\neg \alpha \wedge \neg \beta¬α∧¬β

      • English: "It is not true that Harry or Ron passed the test. Therefore, Harry did not pass the test, AND Ron did not pass the test."

  7. Distributive Laws (for rearranging AND/OR)

    • And over Or:

      • Premise: α∧(β∨γ)\alpha \wedge (\beta \vee \gamma)α∧(β∨γ)

      • Conclusion: (α∧β)∨(α∧γ)(\alpha \wedge \beta) \vee (\alpha \wedge \gamma)(α∧β)∨(α∧γ)

    • Or over And:

      • Premise: α∨(β∧γ)\alpha \vee (\beta \wedge \gamma)α∨(β∧γ)

      • Conclusion: (α∨β)∧(α∨γ)(\alpha \vee \beta) \wedge (\alpha \vee \gamma)(α∨β)∧(α∨γ)

⚙️ Theorem Proving as Search

  • Inference rules can be used in a search problem framework to prove theorems:

    • Initial State: The initial knowledge base (KB) (set of known sentences).

    • Actions: Applying any valid inference rule to the current set of sentences.

    • Transition Model: The new KB after adding the conclusion of an applied inference rule.

    • Goal Test: Check if the query (α\alphaα) is present in the KB.

    • Path Cost: Number of inference rules applied (steps in the proof).


🚀 Resolution

Resolution is a powerful inference rule that forms the basis of a common and complete inference algorithm.

🤝 The Resolution Rule

  • Unit Resolution: If P $\vee$ Q is true, and $\neg$P is true, then Q must be true.

    • English: "Ron is in the Great Hall or Hermione is in the library. Ron is not in the Great Hall. Therefore, Hermione is in the library."

  • Generalized Resolution:

    • Premises: P $\vee$ Q (where Q can be Q1 $\vee$ Q2 ...) and $\neg$P $\vee$ R (where R can be R1 $\vee$ R2 ...)

    • Conclusion: Q $\vee$ R

    • English: "Ron is in the Great Hall or Hermione is in the library. Ron is not in the Great Hall or Harry is sleeping. Therefore, Hermione is in the library or Harry is sleeping."

  • Complementary Literals: The key is finding a propositional symbol (P) and its negation ($\neg$P) across two clauses. These "resolve" and are eliminated.

📝 Clauses & Conjunctive Normal Form (CNF)

  • Literal: A propositional symbol (e.g., P) or its negation (e.g., $\neg$P).

  • Clause: A disjunction of literals (literals connected by $\vee$ OR).

    • Example: P $\vee \neg$Q $\vee$ R

  • Conjunctive Normal Form (CNF): A logical sentence that is a conjunction of clauses (clauses connected by $\wedge$ AND).

    • Example: (A $\vee$ B $\vee$ C) $\wedge$ (D $\vee \neg$E) $\wedge$ (F $\vee$ G)

    • Any logical sentence can be converted into CNF.

Converting to CNF

  1. Eliminate Biconditionals: A $\leftrightarrow$ B →\rightarrow→ (A $\rightarrow$ B) $\wedge$ (B $\rightarrow$ A)

  2. Eliminate Implications: A $\rightarrow$ B →\rightarrow→ $\neg$A $\vee$ B

  3. Move ¬\neg¬ Inwards: Use De Morgan's Laws (e.g., $\neg$(A $\wedge$ B) →\rightarrow→ $\neg$A $\vee \neg$B) and Double Negation Elimination ($\neg \neg$A →\rightarrow→ A).

  4. Distribute ∨\vee∨ over ∧\wedge∧: Use distributive law to ensure $\wedge$ are on the outside (e.g., A $\vee$ (B $\wedge$ C) →\rightarrow→ (A $\vee$ B) $\wedge$ (A $\vee$ C)).

    • Example CNF Conversion: Convert (P $\vee$ Q) $\rightarrow$ R to CNF.

    1. Eliminate implication: $\neg$(P $\vee$ Q) $\vee$ R

    2. Move ¬\neg¬ inwards (De Morgan's): ($\neg$P $\wedge \neg$Q) $\vee$ R

    3. Distribute ∨\vee∨ over ∧\wedge∧: ($\neg$P $\vee$ R) $\wedge$ ($\neg$Q $\vee$ R)

      is is now in CNF.

👻 The Empty Clause

  • If P and $\neg$P are resolved, and there are no other literals in the clauses, the result is the empty clause (represented as ⊥\perp⊥).

  • The empty clause is always false because P $\wedge \neg$P is a contradiction.

🎯 Inference by Resolution Algorithm

To determine if KB $\models \alpha$:

  1. Proof by Contradiction: Assume the query is false. We want to prove KB $\wedge \neg \alpha$ leads to a contradiction.

  2. Convert to CNF: Convert the entire statement KB $\wedge \neg \alpha$ into Conjunctive Normal Form. This gives a set of clauses.

  3. Repeated Resolution:

    • Continuously take any two clauses from the set.

    • If they contain complementary literals (e.g., P in one, $\neg$P in another), resolve them to produce a new clause.

    • Add this new clause to the set (after factoring to remove duplicate literals).

    • Repeat this process until:

      • Empty Clause Produced: If the empty clause (⊥\perp⊥) is generated, it means KB $\wedge \neg \alpha$ is a contradiction. Therefore, KB $\models \alpha$ (the query is true).

      • No New Clauses: If no new clauses can be generated, and the empty clause has not been produced, then KB $\not\models \alpha$ (the query is not entailed).

Resolution Example

Knowledge Base: (A $\vee$ B) $\wedge$ ($\neg$B $\vee$ C) $\wedge \neg$C Query: A

  1. Assume ¬\neg¬A: Add $\neg$A to the KB.

    • Clauses: (A $\vee$ B), ($\neg$B $\vee$ C), ($\neg$C), ($\neg$A)

  2. Resolve (¬\neg¬B ∨\vee∨ C) and (¬\neg¬C):

    • Complementary literals: C and $\neg$C

    • New clause: ($\neg$B)

  3. Resolve (A ∨\vee∨ B) and (¬\neg¬B):

    • Complementary literals: B and $\neg$B

    • New clause: (A)

  4. Resolve (A) and (¬\neg¬A):

    • Complementary literals: A and $\neg$A

    • New clause: The empty clause (⊥\perp⊥)

  5. Conclusion: Since the empty clause is produced, KB $\models$ A. The knowledge base entails A.

🌟 First-Order Logic

First-order logic (FOL) is more expressive than propositional logic, allowing us to represent complex ideas more concisely, especially when dealing with objects and relationships.

🚧 Limitations of Propositional Logic

  • Redundancy: In problems like the Hogwarts puzzle, we needed many propositional symbols (e.g., MinervaGryffindor, MinervaHufflepuff) to represent simple relationships.

  • Lack of Structure: Propositional logic treats each symbol as an independent fact, making it hard to express general rules about objects.

🔑 Key Concepts in First-Order Logic

FOL introduces different types of symbols:

  1. Constant Symbols: Represent objects (e.g., people, houses).

    • Examples: Minerva, Pomona, Gryffindor, Slytherin.

  2. Predicate Symbols: Represent relations or properties that hold true or false for constants.

    • Examples: Person(Minerva) (Minerva is a person), House(Gryffindor) (Gryffindor is a house).

    • BelongsTo(Minerva, Gryffindor) (Minerva belongs to Gryffindor).

Examples of FOL Sentences

  • Person(Minerva): Minerva is a person.

  • House(Gryffindor): Gryffindor is a house.

  • Not(House(Minerva)): Minerva is not a house.

  • BelongsTo(Minerva, Gryffindor): Minerva belongs to Gryffindor.

    • This is more concise: one BelongsTo predicate handles all person-house relationships, unlike needing a separate propositional symbol for each combination.

🌐 Quantifiers

FOL introduces quantifiers to express ideas about collections of objects.

  1. Universal Quantification (∀\forall∀): "For all" or "For every".

    • Symbol: Upside-down A ($\forall$)

    • Example: $\forall$x (BelongsTo(x, Gryffindor) $\rightarrow$ Not(BelongsTo(x, Hufflepuff)))

    • Meaning: For all objects x, if x belongs to Gryffindor, then x does not belong to Hufflepuff. (Anyone in Gryffindor is not a Hufflepuff.)

  2. Existential Quantification (∃\exists∃): "There exists" or "For some".

    • Symbol: Backwards E ($\exists$)

    • Example: $\exists$x (House(x) $\wedge$ BelongsTo(Minerva, x))

    • Meaning: There exists an object x such that x is a house, AND Minerva belongs to x. (Minerva belongs to a house.)

Combining Quantifiers

Quantifiers can be combined to express complex rules:

  • $\forall$x (Person(x) $\rightarrow$ $\exists$y (House(y) $\wedge$ BelongsTo(x, y)))

  • Meaning: For all x, if x is a person, then there exists a y such that y is a house and x belongs to y. (Every person belongs to a house.)

📈 Beyond First-Order Logic

  • There are even more powerful logics (e.g., second-order logic, higher-order logic) that allow expressing increasingly complex ideas.

  • The core goal remains knowledge representation and reasoning to enable AI agents to understand and infer information.


⏭️ Next Steps: Embracing Uncertainty

While current methods focus on what AI knows for sure, future exploration will delve into uncertainty, allowing AI to reason with probabilities and likelihoods rather than just absolute truth or falsehood. This will enable even more sophisticated intelligent systems.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent