Intelligent agents and environments
Model an agent that perceives its environment and chooses actions, using a simple reflex vacuum cleaner.
On this page
Functions, dictionaries, and loops.
- Model an agent that perceives its environment and chooses actions, using a simple reflex vacuum cleaner.
- Predict the sample output and explain the state changes.
- Complete the practice task and check the boundary cases.
The concept
Modern AI textbooks organize the field around the idea of an agent: something that perceives its environment through sensors and acts on it through actuators. An agent function maps the history of percepts to an action, and an agent program is the code that implements it. A rational agent chooses the action expected to maximize its performance measure, given what it has perceived so far. The task is described by its PEAS: Performance measure, Environment, Actuators, and Sensors.
Environments differ in ways that shape the design: fully or partially observable, deterministic or stochastic, episodic or sequential, static or dynamic, discrete or continuous, and one agent or several. Agent designs range from simple reflex agents, which use only the current percept, through model-based agents that keep an internal state, to goal-based agents, utility-based agents, and learning agents.
The classic example is a vacuum cleaner in a world of two squares, A and B. The reflex agent follows two rules: if the current square is dirty, suck, otherwise move to the other square. It works well here because the world is small and fully observable. Notice that the agent never stops: it keeps moving even after everything is clean, wasting effort. Whether that matters depends on the performance measure, which is why defining the measure is part of the design.
Code example
def reflex_agent(location, dirty):
if dirty:
return "suck"
return "right" if location == "A" else "left"
def run(world, location, steps):
actions = []
for _ in range(steps):
action = reflex_agent(location, world[location])
actions.append(action)
if action == "suck":
world[location] = False
elif action == "right":
location = "B"
else:
location = "A"
return actions
world = {"A": True, "B": True}
print(run(world, "A", 4))
print(world)['suck', 'right', 'suck', 'left']
{'A': False, 'B': False}Follow the code
Open the walkthrough to inspect the example one step at a time.
Show guided walkthrough
def reflex_agent(location, dirty): if dirty: return "suck" return "right" if location == "A" else "left" def run(world, location, steps): actions = [] for _ in range(steps): action = reflex_agent(location, world[location]) actions.append(action) if action == "suck": world[location] = False elif action == "right": location = "B" else: location = "A" return actions world = {"A": True, "B": True}print(run(world, "A", 4))print(world)Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| Percept: the agent's current square and whether it is dirty. The agent program turns it into an action. | percept = (A, dirty) | — | |
| The environment changes: sucking cleans the current square. | A is clean | — | |
| With A clean, the rule is to move right, so the agent is now at B. | A is clean | — | |
| Actions over four steps: clean A, move to B, clean B, then move left again. | A is clean | ['suck', 'right', 'suck', 'left'] | |
| Both squares are clean, so the agent performed its task, though it keeps moving afterwards. | A is clean | ['suck', 'right', 'suck', 'left']
{'A': False, 'B': False} |
Behind the scenes
How Python processes this code
Python runs this source through its interpreter. It creates objects for values and resolves names as each statement runs.
Cost and common mistakes
Each decision is O(1) in this tiny world. Real agents face large state spaces, which is why search and learning methods are needed.
- A rational agent maximizes expected performance given its percepts. It is not required to know everything or to succeed every time.
- Without internal state, a reflex agent cannot tell that the whole world is already clean, so it cannot decide to stop.
- The performance measure belongs to the designer and the task, not to the agent. A badly chosen measure produces behaviour you did not intend.
Try it yourself
How would you change the performance measure so the agent is penalized for moving after the world is clean?
Reveal answer and reasoning
Charge a cost per move and reward clean squares per time step. Then the best behaviour is to stop once both squares are clean, which a model-based agent that remembers the state can do.