Data++
[Agent practical 2 of 9]


Finally, having got our arrays set up, we're now in a position to get our model running.


 

Ultimately we'll put a stopping condition into our model for our model to check each iteration. However, for the moment we'll just stick with a fairly simple agent behaviour, and we'll run our model for a fixed number of iterations. We probably want this anyhow, in the end, as it's always good to have a backup plan incase the stopping condition is never reached.

At the moment, your Model class should look something like this (with proper code in the place of comments):

class Model {
   public static void main (String args[]) {
      // Code to set up the agent array
      // Loop to fill the agent array
      // Code to set up the Environment
      // Nested loops to fill the Environment's data array with 255s.
      // Possibly some loops to print the data array.
      // (We'll put some code here to run the model.)
   }
}

What we now need is some code to run our model. First up, we need to decide how many iterations we're going to run. At the top of your main block, just after int numberOfAgents = 3;, make the following variable:

int numberOfIterations = 10;

Again, by having this at the top of our main block, we know where it is if we want to change it later.

Now, where we have "We'll put some code here to run the model" in the above outline, let's start building up our model.

 


Our model algorithm is going to look like this:

// Loop through iterations with counter i.
//    Loop through agents with counter j.
//       get agents to move
//       get agents to eat
//    } End agents loop.
// } End iterations loop.

For the moment, we'll keep each part of this as simple as possible. Rather than getting the agents to move or eat, we'll just get them to report their location. Using standard counting for-loops, can you get the above to work, but with the following only in the centre of the loops:

System.out.println("iteration = " + i + " agent " + j + " located at " + agents[j].x + " " + agents[j].y);

Add your code where indicated, and compile and run the model. Although the agents are just reporting their location at the moment, they are still kind of doing something, so we have the foundations of our model up and running!

Doomed: Note that if we're just interested in looping through the agents, and we'll always have the same number, it's actually clearer to use a for-each-loop as the inner loop, thus:

for (Agent agent: agents) {
   System.out.println("iteration = " + i + " agent located at " + agent.x + " " + agent.y);
}

This is probably clearer, and prevents you making mistakes with the array notation, but because you can't add or remove agents while the list is being cycled through, it would prevent you having agents that reproduce or die. While it is complicated to sort out (because the array length changes, as does the counter), there isn't this restriction on standard counting for-loops. Even though we're not going to implement reproduction or death in this model, we'll use the standard loop here so you have it if you need it in your own projects.

 


Once you're happy with the above, let's add some behaviour to our agents.