Algorithms, Pseudocode, or Code
Algorithms, Pseudocode, or Code
Ch 5:
function Backtracking-Search(csp):
return Recursive-Backtrack({}, csp)
function AC3(constraints):
queue = all arcs (A, B) in the problem # Start with all pairs (A, B) that
have constraints
1. Sense: Perceives something from the environment (e.g., light, sound).
2. Think: Updates its knowledge base with the new information and
queries it for the best action.
3. Act: Executes the chosen action and records it for future reasoning.
Example
Scenario:
Steps:
1. Perceive:
○ The agent receives the percept "It’s dark."
○ Tell(KB, "It’s dark at time t").
2. Reason:
○ The agent queries the KB: "What action should I take at time ttt?"
○ KB applies the rule and answers: "Turn on the light."
3. Act:
○ The agent records: "I turned on the light at time ttt."
○ Then performs the action: Turn on the light.
4. Repeat:
○ Next percept, next reasoning, next action.
2-Inference By Enumeration (slide 38):
1. TT-Entails Function
● This is the main function. It checks if KB⊨αKB \models αKB⊨α (if the
knowledge base entails the query).
● Input:
1. KB: The knowledge base.
2. α: The query (proposition to check if it follows from the KB).
● Steps:
1. Extract all the symbols (propositions) from KB and α
2. Call the recursive function Check-All to evaluate all possible
models (truth assignments).
2. Check-All Function
count = {rule: number of premises in rule for each rule in KB} # Count
remaining premises for each rule
if not inferred.get(p, False): # If the fact has not been processed yet
for rule in KB where p is a premise: # Check all rules where this fact is a
premise
Query (Q):
● Is D true?
Step-by-Step Execution
1. Initialization:
○ agenda = [A, B] (facts from KB).
○ inferred = {} (nothing processed yet).
○ count = { (A ∧ B → C): 2, (C → D): 1 }
(premises left to satisfy for each rule).
2. Process A:
○ p=A, remove A from agenda.
○ Mark A as processed: inferred = {A: true}.
○ Rule A ∧ B→C: Reduce count from 2 to 1.
3. Process B:
○ p=B, remove B from agenda.
○ Mark B as processed: inferred = {A: true, B:
true}.
○ Rule A∧B→C: Reduce count from 1 to 0 (all premises
satisfied).
○ Infer C: Add C to agenda.
4. Process C:
○ p=C, remove C from agenda.
○ Mark C as processed: inferred = {A: true, B: true,
C: true}.
○ Rule C→D Reduce count from 1 to 0 (all premises satisfied).
○ Infer D: Add D to agenda.
5. Process D:
○ p=D, remove D from agenda.
○ D=q: The query is proven true.
Final Result: