Running
[Agent practical 9 of 9]


Let's think about the things we may want to adjust in our model and set up externally.


Possible candidates include:

We'll leave the neighbourhood size for the moment and concentrate on the Nibbler eating.


At the moment the eating rate is hardwired into the code, so we'll have to change that to an instance variable, and we'll also have to get it passed into our agents using the constructor. In our Model we'd add a eatingRate instance variable with the value we want, followed up with an instance variable in the Nibblers and a change in the constructor to connect the two.

In addition, let's add another variable that says when Nibblers are full up. We'll have to treat this the same way, but let's make this an optional behaviour that we can turn on or off from the command line. We'll do this by initially setting the value to -1 and then checking whether this has been changed by the command line argument when enacting behaviour. We'll also need some kind of stomach to fill.

In Model:

private double eatingRate = 10.0;
private double fullUp = -1.0;

----------------------------------

private void buildAgents() {

   for (int i = 0; i < numberOfAgents; i++) {
      agents.add(
        new Nibbler(world,agents,eatingRate,fullUp)
      );
   }

}

In Nibbler:

private double eatingRate = 10.0;
private double fullUp = -1.0;

private double stomach = 0.0;

----------------------------------

public Nibbler (Environment worldIn, ArrayList agentsIn, double eatingRateIn, double fullUpIn) {

   super(worldIn, agentsIn);
   x = (int)(Math.random() * world.getWidth());
   y = (int)(Math.random() * world.getHeight());
   eatingRate = eatingRateIn;
   fullUp = fullUpIn;

}

Here then, are our new Nibbler eating methods. We're going to add a new method that checks whether the agents are full. If they are, they return to the bottom of the model for a rest, regurgitation, or whatever, and start again.

private void interactWithEnvironment () {

   if (world.getDataValue(x, y) > eatingRate) {
      world.setDataValue(x, y, world.getDataValue(x, y) - eatingRate);
      stomach = stomach + eatingRate;
   } else {
      stomach = stomach + world.getDataValue(x, y);
      world.setDataValue(x, y, 0);
   }
   if (fullUp != -1) checkFull(); // Wouldn't run unless fullUp set.

}

private void checkFull() {

   if (stomach >= fullUp) {
      stomach = 0;
      x = (world.getWidth() - 1) / 2;
      y = world.getHeight() - 1;
   }
}

Don't worry about cutting and pasting these in for the moment -- we'll give you a fresh copy in a bit.


For now, let's go on to look at what we do with our command line.