Press Ctrl+D to draw

Drawing Tools

Log in for saved annotations

1px

Table of Contents

No headings in this section

3.8  Problems

Problem 3.1  Propositional Logic for the Hazardous Warehouse

Goal: Encode the Hazardous Warehouse environment in propositional logic and use logical inference to determine safe squares.

Setup: Consider a simplified \(3 \times 3\) warehouse grid. The robot starts at \((1,1)\). The environment contains:

  • At least one damaged floor section (locations unknown)
  • At least one malfunctioning forklift (locations unknown)
  • One package (location unknown)

Use the following propositional symbols:

  • \(D_{i,j}\): Damaged floor at \((i,j)\)
  • \(F_{i,j}\): Forklift at \((i,j)\)
  • \(P_{i,j}\): Package at \((i,j)\)
  • \(C_{i,j}\): Creaking perceived at \((i,j)\)
  • \(R_{i,j}\): Rumbling perceived at \((i,j)\)
  • \(S_{i,j}\): Square \((i,j)\) is safe

Tasks:

  1. Encode the physics: Write propositional sentences that express:
    • Creaking at a location iff damaged floor in an adjacent square
    • Rumbling at a location iff forklift in an adjacent square
    • A square is safe iff it has no damaged floor and no forklift
  2. Initial knowledge: Write sentences expressing:
    • The starting square \((1,1)\) is safe
    • At least one square has damaged floor
    • At least one square has the forklift
  3. Scenario reasoning: The robot is at \((1,1)\) and perceives no creaking and no rumbling.
    • Write the percept as propositional sentences
    • Use logical inference to determine which squares are provably safe
    • Show your reasoning step by step
  4. Exploration: The robot moves to \((2,1)\) and perceives creaking but no rumbling.
    • Add the new percepts to the knowledge base
    • Determine what new conclusions can be drawn about damaged floor locations
    • Which squares are now known to be dangerous? Which remain uncertain?

Deliverables:

  • Written propositional sentences for parts 1-2
  • Step-by-step logical derivations for parts 3-4
  • A diagram showing the \(3 \times 3\) grid with safe/dangerous/unknown labels after each observation

Problem 3.2  First-Order Logic Translation

Goal: Practice translating natural language statements about the Hazardous Warehouse into first-order logic.

Predicates available: - \(\text{Damaged}(l)\): Location \(l\) has damaged floor - \(\text{Forklift}(l)\): The forklift is at location \(l\) - \(\text{Package}(l)\): The package is at location \(l\) - \(\text{Safe}(l)\): Location \(l\) is safe to enter - \(\text{Adjacent}(l_1, l_2)\): Locations \(l_1\) and \(l_2\) are adjacent - \(\text{Creaking}(l)\): Creaking is perceived at \(l\) - \(\text{Rumbling}(l)\): Rumbling is perceived at \(l\) - \(\text{Visited}(l)\): The robot has visited \(l\) - \(\text{At}(r, l)\): Robot \(r\) is at location \(l\) - \(\text{Carrying}(r, p)\): Robot \(r\) is carrying package \(p\)

Translate each statement to FOL:

  1. "Every location adjacent to a damaged floor has creaking."

  2. "The robot should never enter an unsafe location."

  3. "If a location has no creaking and no rumbling, all adjacent locations are safe."

  4. "There is exactly one forklift in the warehouse."

  5. "If the robot is carrying the package and the robot is at the exit, the mission is complete."

  6. "A location is dangerous if it has damaged floor or if the forklift is there."

  7. "Every visited location is safe." (This reflects the fact that the robot survived visiting it.)

  8. "If rumbling is heard at every location adjacent to \(L\), then the forklift is at \(L\)."

Bonus: For each translation, identify whether the statement uses universal quantification, existential quantification, or both.

Problem 3.3  Forward and Backward Chaining Trace

Goal: Trace through forward chaining and backward chaining algorithms on a small knowledge base.

Knowledge Base:

Facts: - \(\text{Adjacent}(L_1, L_2)\) - \(\text{Adjacent}(L_1, L_4)\) - \(\text{Adjacent}(L_2, L_1)\) - \(\text{Adjacent}(L_2, L_3)\) - \(\text{Adjacent}(L_2, L_5)\) - \(\text{Adjacent}(L_4, L_1)\) - \(\text{Adjacent}(L_4, L_5)\) - \(\neg\text{Creaking}(L_1)\) - \(\text{Creaking}(L_2)\) - \(\neg\text{Rumbling}(L_1)\) - \(\neg\text{Rumbling}(L_2)\) - \(\text{Safe}(L_1)\) (starting square)

Rules: - R1: \(\forall l.\, \neg\text{Creaking}(l) \Rightarrow \forall l'.\, \text{Adjacent}(l, l') \Rightarrow \neg\text{Damaged}(l')\) - R2: \(\forall l.\, \neg\text{Rumbling}(l) \Rightarrow \forall l'.\, \text{Adjacent}(l, l') \Rightarrow \neg\text{Forklift}(l')\) - R3: \(\forall l.\, \neg\text{Damaged}(l) \land \neg\text{Forklift}(l) \Rightarrow \text{Safe}(l)\)

Grid layout (for reference):

L4 -- L5
|     |
L1 -- L2 -- L3

Tasks:

  1. Forward chaining: Starting from the initial facts, trace the forward chaining process.

    • What new facts are derived in each iteration?
    • Continue until no new facts can be derived.
    • List all derived facts in order.
  2. Backward chaining: Query: Is \(L_5\) safe?

    • Trace the backward chaining process step by step.
    • Show which rules are used and what subgoals are generated.
    • Does the query succeed or fail? Why?
  3. Unification: For each rule application in your traces, explicitly show the substitution (unifier) used.

  4. Comparison: Which approach (forward or backward) examined fewer rules/facts for this query? When would the other approach be preferable?

Problem 3.4  Building a Knowledge-Based Agent (Guided)

Goal: Follow the walkthrough in Section 3.6: Building a Knowledge-Based Agent to build your own knowledge-based agent for the Hazardous Warehouse using Z3.

Setup: You will need z3-solver (install via pip install z3-solver) and hazardous_warehouse_env.py in your working directory.

Tasks:

  1. Setup and exploration: Create a new Python file. Import Bool, Bools, Or, And, Not, Solver, unsat from z3, and HazardousWarehouseEnv from hazardous_warehouse_env.py. Verify you can:

    • Create boolean variables: P, Q = Bools('P Q')
    • Add a biconditional: s.add(P == Q) and a fact: s.add(P)
    • Check satisfiability: s.check() should return sat
    • Inspect the model: s.model() should show P = True, Q = True
    • Implement the z3_entails function from Section 3.6: Building a Knowledge-Based Agent using push/pop. Verify that after adding P == Q and P, the solver entails Q.
  2. Symbols and physics: Write the Bool variable helper functions (damaged, forklift_at, creaking_at, rumbling_at, safe) and build_warehouse_kb() as described in Section 3.6: Building a Knowledge-Based Agent. After building the solver, verify it is satisfiable (solver.check() returns sat).

  3. Manual reasoning: Before building the full agent, use your solver to replicate the reasoning from Section 3.2: The Hazardous Warehouse Environment:

    • Create the solver with build_warehouse_kb()
    • TELL the solver the percepts at \((1,1)\): no creaking, no rumbling
    • ASK whether \((2,1)\) is safe. ASK whether \((1,2)\) is safe. Both should return True.
    • TELL the solver the percepts at \((2,1)\): creaking, no rumbling
    • ASK whether \((3,1)\) is safe. What about \(\neg\mathit{OK}_{3,1}\)? What does the solver conclude about \((2,2)\)?
    • TELL the solver the percepts at \((1,2)\): rumbling, no creaking
    • ASK again about \((3,1)\), \((2,2)\), and \((1,3)\). The solver should now identify the damaged floor and forklift locations.
  4. Agent loop: Implement the full WarehouseKBAgent class with the perceive \(\to\) tell \(\to\) ask \(\to\) act cycle described in Section 3.6: Building a Knowledge-Based Agent. Include path planning (BFS through safe squares) and the action conversion logic.

  5. Testing: Run your agent on the example layout (using configure_rn_example_layout from hazardous_warehouse_viz.py). Does it retrieve the package and exit successfully? Record the number of steps and total reward.

  6. Reflection: In 2–3 sentences, describe a situation where the agent either gets stuck (cannot find the package) or behaves conservatively (avoids squares that a human would risk). What additional reasoning capability would help?

Deliverables: Your Python file with the agent implementation, plus a brief summary (replace the template's README.md file) of your results and observations from tasks 3, 5, and 6.

Bonus: Extend the agent to use the emergency shutdown device. The agent should reason about the forklift's location and, when it can identify the forklift along a line from its current position, fire the shutdown device before proceeding.

Problem 3.5  Building a FOL Agent with Z3 (Guided)

Goal: Follow the walkthrough in Section 3.7: Building a FOL Agent with Z3 to extend the propositional agent from Section 3.6: Building a Knowledge-Based Agent to use quantified first-order logic, and compare the two encodings.

Setup: You will need z3-solver (install via pip install z3-solver), hazardous_warehouse_env.py, and hazardous_warehouse_viz.py in your working directory. You should have a working propositional agent from problem 3.4.

Tasks:

  1. FOL domain setup: Create a new Python file. Import DeclareSort, Function, BoolSort, Const, ForAll, Exists, Distinct from z3 (in addition to the Or, And, Not, Solver, unsat you already know from Section 3.6: Building a Knowledge-Based Agent). Verify you can:

    • Create a sort: Location = DeclareSort('Location')
    • Create a predicate: P = Function('P', Location, BoolSort())
    • Create constants: L1 = Const('L1', Location)
    • Write a quantified sentence: ForAll(L, P(L)) where L = Const('L', Location)
    • Add the sentence to a solver and check satisfiability.
  2. Quantified physics rules: Implement build_warehouse_kb_fol() as described in Section 3.7: Building a FOL Agent with Z3. This includes:

    • Location constants for each grid square
    • Domain closure (ForAll(L, Or([L == loc[...] for ...])) and Distinct)
    • Closed-world adjacency facts
    • Three quantified physics rules: creaking, rumbling, and safety
    • Verify the solver is satisfiable after building the KB.
  3. Manual reasoning: Replicate the reasoning walkthrough from Section 3.6.14: Manual Walkthrough using the FOL KB:

    • Build the KB with build_warehouse_kb_fol()
    • TELL the solver: no creaking, no rumbling at \((1,1)\)—using self.preds['Creaking'](loc[(1,1)]) instead of creaking_at(1, 1)
    • ASK: is \((2,1)\) safe? Is \((1,2)\) safe? (Both should return True.)
    • TELL: creaking, no rumbling at \((2,1)\)
    • ASK: is \((3,1)\) safe? Is \((2,2)\) safe? (Both should be unknown.)
    • TELL: no creaking, rumbling at \((1,2)\)
    • ASK: is \((2,2)\) safe? Is \((3,1)\) dangerous? Is \((1,3)\) dangerous? (All True.)
    • Verify your results match the propositional agent's conclusions.
  4. Full agent: Implement the WarehouseZ3Agent class with the same decision loop as Section 3.6: Building a Knowledge-Based Agent. The key difference is using FOL predicates applied to location constants instead of grounded Bool variables. Run it on the example layout using configure_rn_example_layout. Record the number of steps and total reward. Confirm the results are identical to the propositional agent.

  5. Domain closure investigation (challenge): Remove the domain closure axiom from build_warehouse_kb_fol() and re-run the manual reasoning from task 3. Which entailment queries now fail? Explain why Z3 can construct a satisfying model where no real grid square is damaged, even when creaking is perceived. Restore the axiom when done.

  6. Reflection: In 3–4 sentences, compare the propositional and FOL encodings. Address: (a) How does rule count scale with grid size for each approach? (b) Which encoding is more readable? (c) What is the role of domain closure, and why doesn't the propositional encoding need it?

Deliverables: Your Python file with the FOL agent, the output trace from task 4, and the written comparison from task 6.

Problem 3.6  Propositional vs. First-Order Expressiveness

Goal: Understand the expressiveness gap between propositional and first-order logic.

Part A: Propositional Encoding Cost

Consider an \(n \times n\) warehouse grid. Count the number of propositional sentences needed to encode:

  1. The adjacency relation (which squares are adjacent to which)
  2. The creaking rule: "Creaking at \(L\) iff damaged floor adjacent to \(L\)"
  3. The safety rule: "Safe at \(L\) iff no damaged floor and no forklift at \(L\)"

Express your answers as functions of \(n\). What happens as \(n\) grows to 100?

Part B: FOL Comparison

Write the same rules in first-order logic. How many sentences are needed regardless of \(n\)?

Part C: Translation Challenge

Can the following statement be expressed in propositional logic? If so, how? If not, why not?

"There is exactly one damaged floor section in the entire warehouse."

Write the statement in FOL. Then attempt a propositional encoding for a \(3 \times 3\) grid. How does the complexity scale?

Part D: Practical Implications

A real warehouse might be \(100 \times 100\) with 10,000 locations. Discuss:

  1. Is propositional encoding practical at this scale?
  2. What restrictions on FOL would make inference tractable?
  3. How might a hybrid approach work (FOL for general rules, propositionalization for local reasoning)?

Problem 3.7  Model Checking and Entailment

Goal: Practice model checking by enumerating interpretations.

Setup: Consider a tiny \(2 \times 2\) warehouse with squares \(A\), \(B\), \(C\), \(D\) arranged as:

C -- D
|    |
A -- B

Use propositional symbols: - \(D_X\): Damaged floor at square \(X\) (for \(X \in \{A, B, C, D\}\)) - \(C_X\): Creaking at square \(X\)

Knowledge Base: - \(C_A \Leftrightarrow D_B \lor D_C\) (creaking at A iff damage at adjacent B or C) - \(C_B \Leftrightarrow D_A \lor D_D\) (creaking at B iff damage at adjacent A or D) - \(\neg D_A\) (starting square A is safe) - \(\neg C_A\) (no creaking at A) - \(C_B\) (creaking at B)

Tasks:

  1. List all propositional symbols in this KB.

  2. Enumerate all interpretations (there are \(2^n\) for \(n\) symbols—use only the 4 damage symbols since creaking is determined by the rules).

  3. Filter models: Which interpretations satisfy all sentences in the KB?

  4. Check entailment: Does the KB entail \(D_D\)? Does it entail \(\neg D_B\)? Does it entail \(D_B \lor D_C\)?

  5. Practical reflection: With 4 damage symbols, we have 16 interpretations. How many interpretations would a \(10 \times 10\) warehouse have? Is model enumeration practical at that scale?