Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 46 ยท Intelligent Agents

Agents and Environments

Learn what an intelligent agent is: percepts, actions, agent function versus program, rational behavior, performance measures, PEAS descriptions and a runnable vacuum agent.

Almost every AI system, from a thermostat to a self-driving car, fits one simple picture: something that perceives its surroundings through sensors and acts on them through actuators. We call that something an agent and its surroundings the environment. The picture is deliberately generic, which is exactly why it is useful: it lets us talk about a chess program, a robot vacuum and a trading bot with the same vocabulary.

In this lesson you will learn that vocabulary, what it means for an agent to be rational, how to describe a task precisely with a PEAS description, and how to write a first agent that runs in a simulated vacuum world.

Percepts, actions and the agent loop

A percept is what the agent senses at one moment. A percept sequence is the complete history of everything it has sensed so far. An action is something the agent can do to change the world. The agent runs in a loop forever: sense, decide, act, and the environment responds.

graph LR
    ENV["Environment"] -- "percepts (sensors)" --> AG["Agent"]
    AG -- "actions (actuators)" --> ENV

The agent and environment form a closed loop: what the agent does changes what it senses next.

Some examples:

Agent Sensors (percepts) Actuators (actions)
Robot vacuum Bump sensor, dirt sensor, wheel encoders Wheel motors, brush, suction
Chess program The current board, the opponent's move Moving a piece
Thermostat Temperature reading Switch the heating on or off
Spam filter The text of an email Move to inbox or spam folder

Agent function versus agent program

Two ideas are easy to confuse.

  • The agent function is a mathematical mapping from every possible percept sequence to an action. It describes behavior completely, but is usually an infinite table you could never write out.
  • The agent program is the finite piece of code that implements the function, running on some physical hardware. It is what you actually write.

Think of the function as the specification and the program as an implementation. Two different programs can implement the same function, just as two different sorting algorithms compute the same mapping from lists to sorted lists. A tiny program can replace a gigantic table, which is the whole point of AI programming: find compact code that behaves well.

What does "rational" mean?

A rational agent chooses the action that is expected to maximize its performance measure, given the percept sequence so far and whatever built-in knowledge it has. Three things deserve attention.

  1. The performance measure belongs to the designer, not the agent. It scores how good a sequence of environment states is. Choose it carefully. If you reward a vacuum agent for "amount of dirt sucked", it can learn to dump dirt on the floor and clean it again. A better measure rewards each clean square at each time step.
  2. Rational is not omniscient. An agent cannot know the future or what it has not sensed. Crossing the road after looking both ways is rational even if a meteor lands on you.
  3. Rational is not the same as successful. Rationality is about expected outcomes given the available information. Gathering information (looking before crossing), exploring and learning are themselves rational actions.

Tip: When a system behaves strangely, ask whether the agent is wrong or the performance measure is. Many AI failures are correct optimization of a badly chosen objective.

Describing a task with PEAS

Before writing any code, specify the task environment with four headings, known as PEAS: Performance measure, Environment, Actuators, Sensors. Writing this down forces you to be precise.

Automated taxi Medical diagnosis assistant Vacuum cleaner
Performance Safe, fast, legal, comfortable trip, low fuel use Healthy patient, low cost, few lawsuits Clean floor, little energy, little noise
Environment Roads, traffic, pedestrians, weather Patients, doctors, hospital Rooms, furniture, dirt, pets
Actuators Steering, accelerator, brake, horn, display Questions, test requests, diagnoses Wheels, brushes, suction
Sensors Cameras, lidar, GPS, speedometer, microphone Symptoms typed in, test results Cameras, dirt detector, bump sensors

A good PEAS table also exposes difficulty. The taxi's performance measure has conflicting parts (fast versus safe), and its environment is open-ended and unpredictable. The vacuum's task is far easier, which is why we start there.

A first agent: the two-square vacuum world

The classic toy world has two squares, A and B. Each is either clean or dirty. The agent sits on one square and can perceive only its own location and whether that square is dirty. It can Suck, move Right or Left, or do nothing. A reflex agent here just reacts to the current percept using condition-action rules:

  • If the current square is dirty, suck.
  • Otherwise move to the other square.
Python A reflex vacuum agent in a two-square world

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the trace. The agent sucks at A, moves right, sucks at B, and then keeps shuttling between the two clean squares forever. It reached a clean world, yet it keeps wasting motion. Whether that matters depends entirely on the performance measure, which is the next experiment.

Comparing agents with a performance measure

To judge an agent we run it in many environments and average the score. The performance measure below gives +1 per clean square per step and -1 for each move (energy cost). We try all four possible starting dirt patterns at both starting locations and compare three agents.

Python Scoring three vacuum agents

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Run it and you should see averages of 10.25 for reflex, 15.00 for lazy and 8.38 for the seeded random agent. Surprising? The lazy agent never cleans the square it is not standing on, yet it wins, because the reflex agent keeps shuttling between clean squares and pays the movement penalty on every step. Now change both score -= 1 lines to score -= 0: the averages become 19.25 for reflex, 15.00 for lazy and 13.75 for random, and the ranking flips. That is the lesson: the best agent depends on the performance measure, and "smart" has no meaning without one.

What comes next

A reflex agent decides from the current percept alone. Better agents remember, set goals and weigh trade-offs, and the right design depends on the kind of environment. Those distinctions are the subject of the next lesson. Later lessons build the algorithms that let an agent choose actions by thinking ahead, starting with search.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between an agent function and an agent program?

    The agent function is the abstract mapping from percept histories to actions. The agent program is a finite implementation of that mapping.
  2. 2. Who defines the performance measure?

    The measure evaluates sequences of environment states from the designer's point of view. A badly chosen measure leads to behavior that is optimal but not what you wanted.
  3. 3. Which statement about rationality is correct?

    Rationality concerns expected outcomes given the available information. It is not omniscience, and information gathering can itself be rational.
  4. 4. What do the letters in PEAS stand for?

    PEAS describes a task environment: the performance measure, the environment, and the agent's actuators and sensors.

Summary

  • An agent perceives through sensors and acts through actuators in a continuous loop with its environment.
  • The agent function is the ideal mapping from percept sequences to actions; the agent program is its finite implementation.
  • A rational agent maximizes expected performance given its percepts and knowledge; this is not perfection or omniscience.
  • The performance measure comes from the designer and must be chosen with care.
  • PEAS (performance, environment, actuators, sensors) is a checklist for specifying any task.
  • A reflex vacuum agent shows how different performance measures favor different agents.

Next: Types of Environments and Agents.