Skip to content
← Notes

Lesson 05 · Artificial intelligence · 12 min

Agents and PEAS

An agent senses and acts. PEAS is how you write down what it's for. I fill in the table my notebook left empty, then race three agents in the same dusty world.

My handwritten notes: Performance measure, task environments and an empty PEAS table

After this you can

  • Write a PEAS description for any agent, including your own projects
  • Classify an environment: observable, deterministic, episodic, static, discrete
  • Tell reflex, model-based, goal-based, utility-based and learning agents apart

An agent is anything that perceives its environment through sensors and acts on it through actuators. A thermostat is one. So is a self-driving car, a spam filter, and you. The last lesson ended with the idea that AI means building agents that do the right thing. This one asks the obvious next question: right according to whom, and measured how?

My page answers with a definition and a table. The definition: a performance measure “defines the criterion for success of an agent's behaviour.” The table is headed PEAS, with Taxi Driver in the first row, and every cell is empty. I'll fill it in below.

Rational, not perfect

A rational agent picks the action expected to score best on its performance measure, given everything it has perceived so far and whatever it knew to begin with. That's narrower than it sounds. It isn't omniscient: it can only act on what it has seen. And it isn't judged on luck: a driver who checks the mirror and is hit by something nobody could have seen still made the rational choice.

The performance measure comes first, and it's easy to get wrong. Reward a cleaning robot for the amount of dirt it sucks up, and the best strategy is to dump the dirt back out and suck it up again. The standard advice is to measure what you want the world to look like, not how you think the agent should behave: a clean floor, not dirt collected.

Three agents, one world

Here's that robot in a corridor of eight rooms. Dust settles at random, and all three agents get exactly the same dust at exactly the same moments. Each one can only sense the room it's standing in. The performance measure is the share of time the rooms were clean.

Stay put

0% clean · 0 moves

If my room is dirty, suck. Otherwise do nothing.

Simple reflex

0% clean · 0 moves

If my room is dirty, suck. Otherwise move left or right at random.

Model-based

0% clean · 0 moves

Remember when I last saw each room. Suck if dirty, else head for the room I haven't checked longest.

tick 0 / 160
Score: the share of room-ticks that were clean, the performance measure. Moves: the energy it spent. Same rooms, same dust, three different agents.

The agent that stays put never wastes a move and scores the worst. If the measure were “spend as little energy as possible,” it would win. The reflex agent wanders at random, so it often doubles back over rooms it just cleaned. The model-based agent has the same sensors but keeps one extra piece of state, when it last saw each room, and that memory alone makes it cleaner with fewer moves. Press New world a few times: the order rarely changes.

the two moving agents, as codepython
import randomdef reflex_vacuum_agent(percept):    """Condition-action rules on the current percept only. No memory."""    location, dirty = percept    if dirty:        return "suck"    return random.choice(["left", "right"])class ModelBasedVacuumAgent:    """Same sensors, plus a memory of when it last saw each room."""    def __init__(self, rooms):        self.last_seen = {r: -1 for r in range(rooms)}        self.t = 0    def __call__(self, percept):        location, dirty = percept        self.t += 1        self.last_seen[location] = self.t        if dirty:            return "suck"        # Head for the room it has checked least recently, nearest first.        others = [r for r in self.last_seen if r != location]        def worth(r):            return (self.t - self.last_seen[r]) - 2 * abs(r - location)        target = max(others, key=worth)        return "right" if target > location else "left"

PEAS: the table, filled in

Before you design an agent, write down its task environment. PEAS is the checklist: Performance measure, Environment, Actuators, Sensors. Here is my notebook's taxi row, plus three agents closer to home, including two of my own projects.

AgentPerformanceEnvironmentActuatorsSensors
Taxi driverSafe, fast, legal, comfortable trips; profit; little disruption to other road usersRoads, traffic, pedestrians, police, passengers, weatherSteering, accelerator, brake, indicators, horn, displayCameras, radar, speedometer, GPS, engine sensors, microphone
CropScanCorrect diagnoses; few confident wrong answers; honest “not a crop”; useful adviceFarmers, leaves of three crops, light, phones, no signalScreen: diagnosis, confidence, treatment, care planPhone camera, the gallery, weather data when online
Ride dispatcherShort pickup times, drivers kept busy, fair fares, few cancellationsRiders, drivers, traffic, the city's roads at rush hourMatch a rider to a driver, set the price, send notificationsGPS from every phone, ride requests, cancellations, ratings
Fraud checkerFraud caught, honest payments rarely blocked, decisions in millisecondsPayments, customers, fraudsters who adaptApprove, decline, hold for review, ask for a codeAmount, merchant, device, location, payment history

What kind of environment?

The second half of a task description is the environment's properties. They decide which kind of agent can cope.

PropertyThe easy sideThe hard side
ObservableFully: the sensors see everything that mattersPartially: some of the state is hidden
AgentsSingleMulti: others are cooperating or competing
DeterministicThe next state follows from the actionStochastic: outcomes are uncertain
EpisodicEach decision stands aloneSequential: today's choice changes tomorrow
StaticThe world waits while the agent thinksDynamic: it changes anyway
DiscreteA finite set of states and actionsContinuous: positions, speeds, time
ObservableAgentsOutcomesEpisodesChangesStates
ChessFullyMultiDeterministicSequentialStaticDiscrete
Taxi drivingPartiallyMultiStochasticSequentialDynamicContinuous
CropScanPartiallySingleStochasticEpisodicStaticContinuous
Ride dispatchPartiallyMultiStochasticSequentialDynamicDiscrete

CropScan's episodic column is a big part of why a single neural network works for it: each photo is judged on its own and nothing carries over. A taxi gets the hard side of every column, and that's why self-driving took decades.

Five kinds of agent

The agent types build on each other. Each one adds a single capability to the one before.

  • Simple reflex

    Condition-action rules on the current percept: if hot, pull back. Fast and fine when the environment is fully observable. The example on my AI page.

  • Model-based

    Keeps internal state about the parts of the world it can't see right now, like the vacuum's memory of each room.

  • Goal-based

    Knows what it's trying to reach and plans a sequence of actions to get there. A route planner. This is where search comes in.

  • Utility-based

    Goals are pass or fail; utility is a score. A taxi doesn't only want to arrive, it trades speed against safety and cost.

  • Learning

    Improves from feedback: a critic judges how it did, and a learning element changes the agent. CropScan's model was learned from labelled photos rather than written by hand.

Try it

01Write PEAS for a phone app you speak to in Twi that sets reminders.

Show the answer
Performance: reminders set correctly on the first try, understood across accents, few false triggers. Environment: the user, background noise, code-switching between Twi and English, the phone's calendar. Actuators: the speaker, the screen, calendar entries. Sensors: the microphone, the clock, the calendar.

02Classify a crossword puzzle's environment.

Show the answer
Fully observable, single agent, deterministic, sequential (each word constrains others), static, discrete.

03The reflex vacuum gets a new rule: if the room is clean, always move right, and turn around at the end. Is it still a simple reflex agent?

Show the answer
Not quite. Remembering which way it's going is internal state, so it's now a (very small) model-based agent. It will sweep more evenly than the random one.

Further reading

07 · Contact

Write to me.

I'm open to software engineering and data roles, and to freelance work. If you have a process that still runs on copy and paste, I'd like to hear about it.