Skip to main content

Variables and Hill Climbing in AgentCubes

AgentCubes · Agents that remember, and agents that follow a scent

TipToday’s question

So far our agents have reacted only to what they see right now. How can an agent remember something, like how long it has been sick? And how can an agent find something it can’t see, like a robot hunting for people across the whole world?

NoteSoftware

Open AgentCubes Online at agentsheets.com and sign in with the class account. This builds on Meet AgentCubes and on the attributes and plot from Spreading of Fire in AgentCubes.

Part 1: Variables

An AgentCubes world seen from above: a sandy desert with people, a few sick people, wolves, butterflies, and one cactus.

The desert world: people, wolves, and butterflies, with a cactus as the controller.

AgentCubes has two kinds of variables.

Kind Looks like Belongs to Example
Attribute (agent variable) days_sick Each agent has its own copy How long this person has been sick
Global variable @sick The whole simulation; one copy everyone shares How many people are sick right now

The @ sign at the front is how you tell them apart.

An attribute: days_sick

AgentCubes rules for the person agent, while-running. Rule 1: if see healthy and next-to sick greater than 0 and percent-chance 50, then change to sick and set days_sick to 0. Rule 2: else if see healthy and percent-chance 90, then move-random-on sand. Rule 3: else if see sick and test days_sick greater than 20 and percent-chance 30, then change to healthy. Rule 4: else if see sick and percent-chance 50, then set days_sick to days_sick plus 1 and move-random-on sand. Rule 5: else if see sick, then set days_sick to days_sick plus 1.

Read the person’s rules as sentences (ignore Set p to 0; it’s left over from another model):

  1. If I’m healthy, a sick person is next to me, and a 50% chance comes up, then I get sick and start my days_sick count at 0.
  2. Otherwise, if I’m healthy, then 90% of the time I take a random step.
  3. Otherwise, if I’m sick, I’ve been sick more than 20 ticks, and a 30% chance comes up, then I recover and become healthy again.
  4. Otherwise, if I’m sick, then half the time I add 1 to days_sick and take a random step.
  5. Otherwise, if I’m sick, I add 1 to days_sick and stay put.

Notice that a recovered person is just healthy again, so they can catch it a second time.

Every sick person keeps their own days_sick. One person might be on day 3 while another is on day 18. That’s why it has to be an attribute: it describes one agent, not the whole world. The test condition is how a rule checks an attribute’s value.

Rules 4 and 5 both count the day. Whether a sick person moves or not, one more tick has passed.

Global variables: @healthy and @sick

AgentCubes rule for the cactus, while-running: set @healthy to agents_with_shape healthy; set @sick to agents_with_shape sick; plot-to-window @healthy in window people representing healthy in blue; plot-to-window @sick in window people representing sick in red.

The cactus is the controller. Every tick it counts the healthy and sick people and stores the counts in @healthy and @sick. Then it plots both in a window called people: blue for healthy, red for sick.

A count of the whole world doesn’t belong to any one person, so it’s a global. Any agent can read @sick. For example, a person could stay home (stop moving) when @sick gets large.

Part 2: Hill climbing

An AgentCubes world in 3D: a large brick floor with about ten people scattered across it and one robot.

The hill-climbing world: people scattered on a brick floor, with the killer robot hunting them.

A killer robot wants to catch people, but it can only look at the four cells next to it. How does it know which way to go?

The answer is a scent. Each person gives off a strong smell. The smell spreads out across the floor and gets weaker with distance. The robot just keeps stepping toward wherever the smell is stronger, which is uphill. That’s hill climbing.

Every person is a source

AgentCubes rules for the innocent person. while-running rule 1: if see person and next-to robot greater than 0, then erase. Rule 2: else if see person, then hill-climb x in four directions, Von Neumann neighborhood. when-creating-new-agent: set p to 1000.

When a person is created, it sets its attribute p to 1000. That’s the strong scent at the source. While it runs:

  1. If the robot is next to me, then I’m erased. Caught!
  2. Otherwise, I hill-climb x (more on x below).

The floor spreads the scent

AgentCubes rule for the ground, while-running: if once-every 0.05 sec, then set p to the quantity p up plus p down plus p left plus p right, divided by 4, and set x to the quantity x up plus x down plus x left plus x right, divided by 4.

Each piece of ground, 20 times a second, sets its own p to the average of its four neighbors’ p. p[up] means “the p of the agent in the cell above me.”

That first line is diffusion. A cell next to a person (p = 1000) gets a big average. A cell next to that gets a slightly smaller one, and so on. Run it, and the scent spreads out from every person like heat spreading through a metal plate. Close to a person p is high, and far away it’s low.

The second line does the same thing for a second scent, x, which comes from the robot (see below). So two smells spread across the floor at once.

Notice this is the same kind of rule as the fire: each cell looks only at its four neighbors, and a pattern across the whole floor emerges.

The robot climbs the hill

AgentCubes rules for the killer robot. while-running: if see robot, then hill-climb p in four directions, Von Neumann neighborhood. when-creating-new-agent: set x to negative 1000.

hill-climb p looks at the four neighboring cells and moves to the one with the highest p. Step by step, the robot walks uphill on the scent, toward the nearest person (or the biggest crowd), even though it never sees a person until it’s right next to one.

Hill climbing is a simple search strategy: you don’t need a map; you just keep taking the step that looks best from where you are.

The people flee

When the robot is created, it sets its x to −1000. The floor spreads x just like p, so x is very negative near the robot and close to 0 far away.

Each person hill-climbs x. Climbing up a negative scent means stepping to the neighbor where x is closest to 0, which is the one farthest from the robot. The same hill-climb action that makes the robot chase makes the people run. The only difference is the sign of the source.

  • Why make the robot’s scent negative instead of +1000? What would the people do if it were +1000?
  • Watch a run. Do people escape forever, or do they get trapped? What happens when a person runs into a corner or a wall?

Hill climbing can get stuck

Put two people far apart, with the robot exactly halfway between them. Which way does it go? Now picture a scent landscape with a small bump and a much bigger hill farther away. A robot on top of the small bump sees every neighbor as lower, so it stops. That’s a local maximum. Hill climbing finds a peak, not always the best one.

Try it

  • Immunity. Right now rule 3 changes a sick person back to healthy. Instead, change to a new recovered shape that nothing can infect. Add @recovered to the cactus and plot it in a third color. Now you have SIR again. How does the plot change?
  • Stay home. Add a rule so a sick person doesn’t move when @sick is more than 5. Does the outbreak slow down?
  • Faster scent. Change once-every 0.05 on the ground to 0.5. Can the robot still find people?
  • Two robots. Add a second robot. Do they chase the same person or split up?
ImportantWhat we practiced
  • An attribute belongs to one agent, so every agent keeps its own value, like days_sick.
  • A global variable starts with @ and is shared by the whole simulation, like the counts @healthy and @sick.
  • Diffusion: averaging your neighbors, over and over, spreads a value smoothly across the world.
  • Hill climbing: step toward the neighbor with the highest value. It’s simple and needs no map, but it can stop on a local maximum.
Back to top