Python vacuum agent: trace perception and memory

A vacuum exercise skips the memory problem if its policy can inspect the whole world. Trace only the current percept into policy memory, then apply the chosen action to the simulator.

The memory policy produces MoveRight, CleanHere, then Stop from the original fixture. After cleaning, the world is clean while memory still says Right dirty until the next percept. The local simulation shows those separate records and compares a reflex policy that moves left instead of stopping on its third step.

Python vacuum agent: trace perception and memory
Python vacuum agent: trace perception and memory
Open the full example ↗

Work through the example

Advance twice. Step two observes Right dirty, chooses CleanHere and leaves Right dirty in last-observed memory while simulator truth becomes clean. Advance again: the new clean percept updates memory, enabling Stop. Reset and select Reflex policy to compare the same first two actions followed by MoveLeft. Python source for the memory policy is included for native reproduction.

At every step, write the percept before the action and the simulator effect afterward. Last-observed memory can lag behind world truth. The clean action does not predictively rewrite memory under this policy. Keep the static-world assumption explicit; new dirt appearing at an unobserved location would require a different stopping/revisit contract rather than an unqualified guarantee of cleanliness.

Give the policy a narrow input

AIMA's agent documentation describes programs that take percepts and choose actions, with internal models maintained separately. It distinguishes a simple reflex program using the current percept from a model-based program using percept and state. Adopt that conceptual boundary here; the policy below is an original exercise specification, not copied AIMA code. AIMA agents and environments.

Use a static fictional world with two locations:

Record Left Right
Initial simulator truth clean dirty
Initial policy memory UNKNOWN UNKNOWN

The agent starts at Left. No spontaneous dirt appears during this bounded fixture. At each step, the simulator provides only the current (location, status) percept to the policy. Keep the environment dictionary out of the policy's arguments and accessible state; the test design should check that boundary.

UNKNOWN means the policy has not observed that location. It is not a synonym for clean, dirty, or a missing value to silently replace with clean.

Define the original memory policy

The memory policy first records the observed status at the current location. It then applies these ordered rules:

  1. If the current percept says dirty, choose CleanHere.
  2. Otherwise, if both locations were last observed clean, choose Stop.
  3. Otherwise, move to the other location.

Only the current observation updates memory. For this exercise, the policy does not predict that a cleaning action succeeded and write clean immediately. That is a deliberate last-observed-memory contract, not a claim that every model-based agent must use this update method.

The simulator applies the action afterward. Moving changes location. CleanHere changes the current world's status to clean. Stop ends this trace without changing the world.

Trace three steps without merging the columns

Step Current percept Memory after observation Chosen action Simulator state after action
1 (Left, clean) Left clean; Right UNKNOWN MoveRight At Right; Left clean, Right dirty
2 (Right, dirty) Left clean; Right dirty CleanHere At Right; Left clean, Right clean
3 (Right, clean) Left clean; Right clean Stop At Right; both locations clean

After step 1, the policy knows Left was clean but has no observation of Right. It moves rather than stopping.

Step 2 is the important boundary. The simulator changes Right to clean after CleanHere, but the policy's last observation of Right is still dirty. Its memory remains dirty until the next percept arrives. If a trace shows both records changing together without explaining a prediction update, it no longer follows this exercise's policy.

Recreate this example

Build one self-contained responsive HTML example titled “Python vacuum agent: trace perception and memory” for readers completing the stated task. Return the complete HTML with embedded CSS and JavaScript. Use no remote fonts, libraries, images, requests, account connections, or saved-state claims.

Static world Left clean, Right dirty, start Left; both memories UNKNOWN. Original policy accepts only location/status, writes current observation, cleans if dirty, stops if both last observed clean, otherwise moves. Apply action afterward to world. Buttons Step, Reset, Memory policy, Reflex policy. Reflex dirty cleans else moves, with no stop condition. Show step/percept/memory/action/post-world separately; initial no action. Memory trace MoveRight, CleanHere, Stop; step2 world Right clean yet memory Right dirty. Include concise Python policy source. Reset world, memory and location; completed Stop cannot advance.

Use a two-location simulation desk with separate world, percept and last-observed memory records design with a compact header. Keep the actual sample data, controls, and useful default result visible early on a 360px-wide screen. Use readable text at least 16px, generous paragraph spacing, strong contrast, semantic labels, visible keyboard focus, and wrapping source blocks. Stack records on narrow screens rather than forcing a wide table.

Wire each action to an explicit type=button click handler using element selectors; the host permits scripts but blocks form submission. Update the visible result when the described action occurs. Show specific error feedback for invalid or missing input and clear stale feedback after dependent inputs change. Use plain text when displaying user values.

A local JavaScript trace and copyable Python policy do not execute Python, train AI or control a physical cleaner. Keep the source fixture and worked teacher or developer example distinct from actual user records. Include the domain-specific explanation and next check beside the result. Provide no fake download, send, deployment, approval, or persistence button. Everything operates locally for the current page session.

Review the prepared copy in NOS

Compare the memory and reflex traces at their third percept, then implement the policy in Python. NOS can render the prepared trace explanation while eligible static teaching notes are revised visually. Keep Python input-boundary and state-transition tests with the native source.

Keep reading