Vacuum Agents: Runnable Demo
Lecture 2b: Designing Intelligent Agents
Compare a simple reflex agent with a model-based reflex agent that remembers which rooms are clean. Both agents receive only the current room and its dirt status. The simulator, not the agent, knows the whole world.
Download vacuum_agents_demo.py
Run the demonstration
The file uses only Python built-ins; no packages or imports are needed.
In Jupyter Notebook or Google Colab, paste the complete file into a cell and run it. The script checks its results and prints two comparisons: no new dirt, then dirt reappearing in A at the start of step 5.
Complete Python code
This is the complete content of the downloadable file.
"""Lecture 2b: compare reflex and memory-based vacuum agents.
Run with Python 3. No packages or imports are required.
True means dirty; False means clean; None means unknown to the agent.
"""
class VacuumWorld:
def __init__(self, start="A", dirt=None):
self.position = start
self.dirt = {"A": True, "B": True} if dirt is None else dict(dirt)
def perceive(self):
room = self.position
return room, self.dirt[room]
def apply(self, action):
if action == "Suck":
self.dirt[self.position] = False
elif action == "Left":
self.position = "A"
elif action == "Right":
self.position = "B"
elif action != "NoOp":
raise ValueError(f"Unknown action: {action}")
class ReflexAgent:
def act(self, percept):
room, dirty = percept
if dirty:
return "Suck"
return "Right" if room == "A" else "Left"
class MemoryAgent:
def __init__(self):
self.seen = {"A": None, "B": None}
def act(self, percept):
room, dirty = percept
self.seen[room] = dirty
if dirty:
self.seen[room] = False # Predict a successful cleaning action.
return "Suck"
if all(value is False for value in self.seen.values()):
return "NoOp"
return "Right" if room == "A" else "Left"
def run_agent(agent, start="A", dirt=None, steps=8, redirty_at=None):
world = VacuumWorld(start, dirt)
trace, score = [], 0.0
for step in range(1, steps + 1):
if step == redirty_at:
world.dirt["A"] = True
percept = world.perceive()
action = agent.act(percept)
world.apply(action)
clean = sum(not value for value in world.dirt.values())
effort = int(action != "NoOp")
score += clean - 0.25 * effort
trace.append((step, percept, action, clean, effort))
return trace, score
def show_comparison(redirty_at=None):
reflex_trace, reflex_score = run_agent(ReflexAgent(), redirty_at=redirty_at)
memory_trace, memory_score = run_agent(MemoryAgent(), redirty_at=redirty_at)
if redirty_at is None:
label = "No new dirt"
else:
label = f"Room A becomes dirty at step {redirty_at}"
print(f"\n{label}")
print(
"Step | Reflex action | Memory action | "
"Clean rooms (reflex / memory)"
)
for reflex, memory in zip(reflex_trace, memory_trace):
row = (
f"{reflex[0]:4} | {reflex[2]:13} | "
f"{memory[2]:13} | {reflex[3]} / {memory[3]}"
)
print(row)
print(f"Scores: reflex={reflex_score:.2f}, memory={memory_score:.2f}")
print("Non-idle actions:", sum(row[4] for row in reflex_trace), "/",
sum(row[4] for row in memory_trace))
def self_check():
cases = 0
for start in ("A", "B"):
for dirty_a in (False, True):
for dirty_b in (False, True):
dirt = {"A": dirty_a, "B": dirty_b}
reflex_trace, reflex_score = run_agent(
ReflexAgent(), start, dirt
)
memory_trace, memory_score = run_agent(
MemoryAgent(), start, dirt
)
assert reflex_trace[-1][3] == memory_trace[-1][3] == 2
assert memory_trace[-1][2] == "NoOp"
assert memory_score >= reflex_score
assert dirt == {"A": dirty_a, "B": dirty_b}
cases += 1
assert run_agent(ReflexAgent())[1] == 12.0
assert run_agent(MemoryAgent())[1] == 13.25
assert run_agent(ReflexAgent(), redirty_at=5)[1] == 12.0
assert run_agent(MemoryAgent(), redirty_at=5)[1] == 9.25
assert MemoryAgent().act(("A", False)) == "Right"
print(
f"Checks passed: {cases} starting worlds, "
"baseline scores, and new-dirt case."
)
if __name__ == "__main__":
self_check()
show_comparison()
show_comparison(redirty_at=5)Running result
Running the complete file above produces:
Checks passed: 8 starting worlds, baseline scores, and new-dirt case.
No new dirt
Step | Reflex action | Memory action | Clean rooms (reflex / memory)
1 | Suck | Suck | 1 / 1
2 | Right | Right | 1 / 1
3 | Suck | Suck | 2 / 2
4 | Left | NoOp | 2 / 2
5 | Right | NoOp | 2 / 2
6 | Left | NoOp | 2 / 2
7 | Right | NoOp | 2 / 2
8 | Left | NoOp | 2 / 2
Scores: reflex=12.00, memory=13.25
Non-idle actions: 8 / 3
Room A becomes dirty at step 5
Step | Reflex action | Memory action | Clean rooms (reflex / memory)
1 | Suck | Suck | 1 / 1
2 | Right | Right | 1 / 1
3 | Suck | Suck | 2 / 2
4 | Left | NoOp | 2 / 2
5 | Suck | NoOp | 2 / 1
6 | Right | NoOp | 2 / 1
7 | Left | NoOp | 2 / 1
8 | Right | NoOp | 2 / 1
Scores: reflex=12.00, memory=9.25
Non-idle actions: 8 / 3World and score
- Rooms A and B are adjacent. Both are initially dirty; the agent starts in A.
- Sensors report
(room, dirty), whereTruemeans dirty andFalsemeans clean. - Moves and cleaning always succeed.
NoOpmeans no operation: an idle step. - Both agents run for eight steps. After each action, add one point per clean room and subtract 0.25 points for a non-idle action.
- Agent memory begins with
Nonefor each room, meaning unknown. The memory agent assumes cleaned rooms remain clean.
Expected results
| Scenario | Reflex score | Memory score | Final clean rooms: reflex / memory |
|---|---|---|---|
| No new dirt | 12.00 | 13.25 | 2 / 2 |
| A becomes dirty at step 5 | 12.00 | 9.25 | 2 / 1 |
In the baseline, both agents accumulate 14 clean-room steps. The reflex agent takes eight non-idle actions; the memory agent takes three. When new dirt appears, the idle memory agent is in B and never sees the change in A.
Try another starting world
In a separate file in the same directory, or in a notebook after defining the classes:
If the complete demo is already in your notebook, omit the import line. Each trace row contains (step, percept, action, clean_rooms, effort). Create a fresh agent for every independent run so it does not inherit memory from a previous world.
Questions to investigate
- What happens when both rooms start clean? How does the memory agent establish that fact?
- Why must the stopping test use
value is False, rather thannot value? What happens to an unknown room represented byNone? - How do the scores change if the action penalty is zero?
- Can periodically checking the other room help when dirt reappears? What extra cost does it introduce?
- What would need to change if cleaning sometimes failed?
Built-in checks
The self_check() function tests all eight combinations of starting location and room dirt. It checks that both agents eventually clean both rooms in the static world, the memory agent becomes idle, the supplied initial map is not changed, and the baseline and new-dirt scores match the table above.
This is a small deterministic teaching example, not a proof of optimality for either policy.