# Lab 4: The agent's internal state versus the true world — step-by-step guide

## Context

This laboratory accompanies Lecture 1 (slides 36–42) of 5DV248 Trustworthy Human-AI Interaction and follows AIMA Section 2.4.3. Lab 3 ended with a frozen reflex agent: without a location sensor, no condition-action rule matched. The slides' remedy is the model-based reflex agent, which keeps an internal state updated by two models: the **transition model** (how the next state depends on the current state and the agent's action — "what my actions do") and the **sensor model** (how the world is reflected in percepts). This lab shows both models at work, side by side with reality.

The world is the same corridor of four squares as in Lab 3. The agent still perceives only the dirt status of the square it occupies; it never sees its location. Its strategy is dead reckoning: it applies the transition model to its own previous actions to estimate where it is, and it remembers which squares it believes are clean.

## Objective

The objective is to understand the internal state not as an abstract box in the slide diagram, but as a data structure with a specific content and a specific update rule. Concretely, you will observe three things:

1. Memory repairs the failure of Lab 3: with an exact transition model, the agent sweeps the corridor systematically and stops (NoOp) exactly when its internal state says the world is clean — something a stateless agent can never decide.
2. The internal state is the agent's only source of truth. The agent stops because the belief says the world is clean, regardless of whether the world actually is clean.
3. When the transition model lies (moves silently fail ten percent of the time), the belief drifts away from the true world. The agent may then declare victory over a dirty corridor.

## The interface

- The left strip is the true world, which you can see and the agent cannot. Fog hides every square except the one the agent occupies. The blue dot is the agent's true position.
- The right strip is the agent's internal state, its belief map. Grey cells are unknown, blue cells are believed clean. The purple dashed dot is the agent's believed position — its own estimate, which need not coincide with the true position.
- Step executes one full UPDATE-STATE cycle; Run repeats automatically; Reset restores the initial corridor (three dirty squares) and empties the belief.
- The checkbox "Moves sometimes fail" makes the world betray the transition model: with probability ten percent a move silently fails, so the agent's estimate of its position becomes wrong and the belief is updated on a false assumption.
- The UPDATE-STATE log records, at every step, the percept, the believed position after applying the transition model, and the chosen action. This log is the pseudocode of slide 42 executed line by line.
- The statistics line compares the true dirt remaining with the believed dirty or unknown squares; the red warning appears when the belief no longer matches the world (state drift).

## Your goal as a student

You are the designer and the auditor of the agent's memory. Proceed as follows, writing down your prediction before each run:

1. Run the agent with the checkbox off. Predict first which squares the belief map will mark, and in which order. Then describe how the agent's behaviour differs from the frozen reflex agent of Lab 3, and identify the exact moment in the log where internal state — not perception — determines the action.
2. Explain the stopping rule: why can the agent return NoOp here, while no simple reflex agent in Lab 3 ever could? Which part of the internal state makes this decision possible?
3. Now reset, switch on "Moves sometimes fail", and run several episodes. Watch the purple dot (believed position) separate from the blue dot (true position). Find a run in which the agent declares the world clean while the true world still contains dirt. Take a screenshot of the belief map and the true world at that moment.
4. Read the UPDATE-STATE log of the drifting run. Identify the exact step where the error entered the internal state, and name which of the two models was wrong (transition or sensor) and why.
5. Repair proposal: which single additional percept would let UPDATE-STATE correct the drift? Examples to consider: a wall detector at the corridor ends, a distance sensor, an odometer that reports failures. Argue for one, and explain which half of UPDATE-STATE (transition model or sensor model) your proposal strengthens.
6. Bridge to the GAMA practical: open the firefighter code from the lecture. The block `perceive target:self` maintains the beliefs `water_predicate` and `no_water_predicate`. Identify one statement in the firefighter code that plays the role of our transition model (expectations about the effects of the agent's own actions) and one that plays the role of the sensor model. Bring your answers to the seminar.

## What to submit or discuss

A short reflection (half a page) containing: the answer to step 2 (why memory enables stopping), your screenshot and drift analysis from steps 3–4 (where the error entered the belief), and your repair proposal from step 5 with its classification into transition-model or sensor-model support. Connect your answer to slides 39–40: an internal state is only as trustworthy as the two models that maintain it.