Data++
[Agent practical 2 of 9]


This practical we're going to simplify the initialisation of our agent array, add some behaviour to our agents, and then get our model to run! We'll also fill our Environment with some data.


 

Our sequence this practical will be:

In subsequent practicals, we'll make the behaviour more sophisticated. For now, we'll concentrate on the general structure of the code needed.


 

Start, as last practical, by copying your old files to a new directory: \java\src\unpackaged\framework2\. Keep a copy of the old files in last practical's directory: don't delete them, but work on the new copies.


 

First, let's make the agents array more flexible.

If you managed to set up your agent array properly last time, you should have something like the following, somewhere in your Model class main block:

Agent[] agents = new Agent[3];
agents[0] = new Agent();
agents[1] = new Agent();
agents[2] = new Agent();

This kind of thing is ok for small arrays, but won't work for arrays of a million agents. In addition, we've hardwired in the size of the array in a way that stops us from dynamicly changing it, for example by allowing the user to pick the number of agents.

Let's make this more flexible. First let's deal with the hardwiring. As we don't currently have a windows-based interface for our program, we're going to have to do some hardwiring somewhere, but the usual thing in these circumstances to collect all the hardwiring in one place, so you can easily find the values later and change them without having to search the code.

Given this, let's make ourselves a new variable that contains the number of agents we want:

int numberOfAgents = 3;

For the moment, put this as the first line inside the main block. We'll then need to adjust the line:

Agent[] agents = new Agent[3];

so it becomes:

Agent[] agents = new Agent[numberOfAgents];

As usual, as soon as you've made your changes, compile the code to check it.

Putting variables like this one at the top of our code in one place means that if we shift our array building code somewhere else in main we'll always be able to find the place where we set the size of the array. In general, it is good practice to put all the major variables associated with setting up a model or tool in one place so you can easily find them and adjust their values.

Note that because the code in a block runs a line at a time from the top of the block to the bottom (loops, branches, etc. excepted), we must make sure we make the variable first before we use it.

Doomed: This wouldn't work at all:

Agent[] agents = new Agent[numberOfAgents];
int numberOfAgents = 3;


 

Having done this, we'll now use a loop to fill our array.

This kind of thing:

agents[0] = new Agent();
agents[1] = new Agent();
agents[2] = new Agent();

would be crazy for large arrays. Fix this now so it uses a single (rather than nested) for-loop to set up the array. Note that you'll need a standard counting for-loop (i.e. one that has a counting variable); the alternative for-each-loops need the objects to be present and countable to work, so you can't use them for adding objects to an array (or, infact, taking objects out of an array).

Give it a go replacing the above with a for-loop; as usual, there are useful examples on the Key Ideas page for the last lecture. Once you think you've done it, add the line:

System.out.println(agents[0].x);

after your loop has closed, and compile/run the code to check it works. Note that temporarily adding in little printlns like this to test small bits of code is both common and useful. As soon as you're sure the code works, you can remove them (we'll also look at more formal testing methods later in the course).

Doomed: Once you've got it working, try increasing numberOfAgents. How big can you make the array? Does the program slow down any as the size increases? What kind of debugging messages can you get out of the JVM? How might you solve these issues?


Once you're happy with that, we'll move on to setting up our environment.