Agents

An Agentset is a collection of agents. There are three types of agents: patches, turtles, and observers:

The environment is a collection of agents. Initially, it consists of a single observer and an
N x M grid of stationary agents called patches.

Each patch is a square with a fixed position and size. The position of a patch is given by its row and column numbers (pxcor and pycor.) Its size is its width, measured in pixels. Every patch has the same size. In addition, a patch has a color (pcolor) and may have a label (plabel).

The environment may also contain mobile agents called turtles. Each turtle has a position (xcor and ycor). These need not be integers. Unlike a patch, the position of a turtle can change. A turtle also has a heading, a color, possibly a label, and a pen. The pen can be in the up state or the down state. When the pen is in the down state, the turtle changes the colors of the patches it moves across.

Patches and turtles may be given extra attributes by using the following declarations:

patches-own [price quality]
turtles-own [speed weight friendly?]

Agents may ask other agents to execute commands using the following syntax:

ask AGENT(S) [CMMD+]

Each agent executes these commands relative to its own context. For example, here's how the observer asks each turtle to move forward by ten patches:

ask turtles [fd 10]

Of course different turtles may end up in different places.

Here is how an agent can ask patch (0, 0) to change its color to red and turtle 5 to turn right by 90 degrees:

ask patch 0 0 [set pcolor red]
ask turtle 5 [rt 90]

The equivalent Java/C++ syntax might look like this:

patch[0][0].setPcolor(red);
turtle[5].rt(90);

Observers Commands

cp ; clear patches
ct ; clear turtles
ca ; clear all
crt N ; create N turtles

Patch Commands

Identification

patch ROW COL

patch-at-heading-and-distance HEADING DISTANCE

Orientation

neighbors

Turtle Commands

die
lt DEGREES
re DEGREES

other-turtles-here

pd
pu

patch-here

st
ht

Turtle and patch Commands

ask turtles with [self != myself] [ die ]

value-from AGENT [EXP] ; ask AGENT to execute EXP

distance other  ; distance from this agent to other

Agentset Commands

Display Commands

screen-size-x = 2 * screen-edge-x + 1
screen-size-y = 2 * screen-edge-y + 1

Example

Here's a procedure that will cause a turtle to draw a polygon with a specified number of sides of a specified length:

to draw-poly [numSides len]
  pd
  repeat numSides [fd len rt 360 / numSides]
  pu
end

Next, we create a single turtle and ask him to draw a hexagon:

Notice the jaggies. The problem is that the initial configuration of the environment is a 35 x 35 grid of 9 x 9 patches:

We can change this to a 241 x 241 grid of 1 x 1 patches:

Now the same command yields a finer polygon:

Breeds

We can create different breeds of Turtles. For example:

breeds [dogs cats]

dogs-own [bones]
cats-own [catnip]

to init
  ca
  create-custom-dogs 50
    [ set color yellow ]
  create-custom-cats 50
    [ set color green ]
end

to bark
  if breed = dogs [
    set bones (1 + bones)
    show bones
  ]
end

to meow
  if breed = cats [
    set catnip (1 + catnip)
    show catnip
  ]
end

to update
  ask dogs [bark]
  ask cats [meow]
end

Message Passing and Handling

We can imitate message passing and handling behavior by equipping each agent with a dispatch procedure:

to dispatch [msg]
   ifelse (msg = "m1") [handle-m1]
      [ifelse (msg = "m2") [handle-m2]
         [ifelse (msg = "m3") [handle-m3]
            [show  "unrecognized message: " + msg]]]
end

Agents ask other agents to dispatch a particular message:

ask turtle i [dispatch "m2"]

Of course an agent might delegate an unrecognized message to another agent. In this way we can model a form of inheritance.