Documentation Set for Swarm 2.2
Prev | Chapter 4. Mag 4x:Sketch of Code | Next

# 4.2. Defining an Agent

The agents are typically the real focus of a modeling effort. Most of the work in a simulation comes in defining the agent behavior so that the computer agents resemble the real world phenomena you are trying to create.

In the case of Heatbugs, agents are pretty simple. Here is their definition, from Heatbug.h:

    @interface Heatbug: SwarmObject {
     double unhappiness; // my current unhappiness
     int x, y; // my spatial coordinates
     HeatValue idealTemperature; // my ideal temperature
     HeatValue outputHeat; // how much heat I put out
     float randomMoveProbability; // chance of moving randomly

     Grid2d * world; // the world I live in
     int worldXSize, worldYSize; // how big that world is
     HeatSpace * heat; // the heat for the world
     Color bugColor; // my colour (display)
    }

    -setWorld: (Grid2d *) w Heat: (HeatSpace *) h; // which world are we in?
    -createEnd;

    -(double) getUnhappiness;

    -setIdealTemperature: (HeatValue) i;
    -setOutputHeat: (HeatValue) o;
    -setRandomMoveProbability: (float) p;
    -setX: (int) x Y: (int) y; // bug's position
    -setBugColor: (Color) c; // bug's colour (display)

    -step;

    -drawSelfOn: (id <Raster>) r;

Heatbug is a subclass of SwarmObject. SwarmObjects have very little behavior of their own - they are defined as the root class of most objects and control computer science aspects like memory allocation and probability.

Heatbug carry with them a variety of state variables. For instance, each Heatbug has a notion of its ideal temperature, which will affect it's behavior. In addition, Heatbugs have variables that let them know about the world: these agents are storing references to the HeatSpace object, for example.

Most of the Heatbug methods have to do with setting up the agents state - the inputs to a Heatbug. Every heatbug must set up its world and heat objects, via the `setWorld:Heat:` method. In addition when Heatbugs are created they have their ideal temperature set, their output heat, etc. Heatbugs are also observable. Heatbugs define a `getUnhappiness` method - the unhappiness is the major measurable aspect of a heatbug, how well optimized it is at the moment. They also have a `drawSelfOn` method that directs the heatbug to draw itself on the specified graphics widget.

Finally, and most importantly, a Heatbug has a `step` method. `step` is where the Heatbugs behavior is defined: every time the Heatbug is told to step it performs its internal calculations, choosing where to move. Each heatbug is told to `step` when appropriate by the model schedule. The code for `step` is the real intellectual input into the model, and is worth reading as an example of an agent's behavior.

Prev | Home | Next
Mag 4x:Sketch of Code | Up | Building Agents