Introducing Swarm and Schedules

This is the same story of the Students, their fingerprints, and the Policeman.

The only really huge difference is that we introduce the Swarm scheduling system. Doing that requires some touching up in Test.java and Model.java.

Oh, and to build/run this, you need to have the Swarm simulation system installed, including the java support. If you have that, it should be as easy as opening a terminal, changing into the directory where you find this code, and type:

# javacswarm *.java
# javaswarm Test
That uses the free, redistributable Java environment known as Kaffe, but if you instead want to run with the commercial Sun JDK, then install the JDK and then Swarm. Then building should be easy, like
# jdkswarm *.java
# javaswarm Test
(javacswarm and jdsaswarm replace javac, in case you did not notice). The model should run, give you some printout, and you can control how it runs by using some built-in command line options. Try this command:
# javaswarm Test -s
You should see the model run again, except this time it will have a different outcome. The -s option causes the random number generator to pick a new starting spot (based on time). If you don't add that -s, every run of this model is exactly like the one before.

One warning. If you run this in Microsoft Windows, your program my be really slow because printing to the terminal window in Windows is really really slow. I think that, if you use Emacs and open a shell in there (Alt-x shell), you can run the program in there, and it is OK.

Test.java

You will see one big difference in the Test.java class. There, the main method assumes the flavor of a Swarm model. There are two things that happen. First, there is the Globals.env.initSwarm() method. That creates a number of handy objects, like random number generators (see below). Second, notice a sequence of commands that tell the created instance of the model to buildObjects(), then buildActions(), then activateIn(), and finally to run the resulting stream of actions.

There's one bit of complexity to warn you about. Swarm uses a "zone" system for memory. When you create a Swarm object, you have to tell it what memory zone to use. The zone that is created by the initSwarm() method is called Globals.eng.globalZone, and when you create objects in the Test.java class, you can use that zone. In lower level classes, you can create objects, and you can use the return from the getZone() method as a zone. Or, if your class is a Swarm or GUISwarm, it is a zone, and so you can use "this" as the zone.

Model.java

Model is now subclassed from SwarmImpl. The "Impl" is short for Implementation. If you were writing a model in Objective-C, you would simply subclass from Swarm, but because we are working in Java, we use SwarmImpl. Note that the import statements include imports for both Swarm and SwarmImpl. The buildActions() method is a Swarm thing. Objects of type Swarm have built-in scheduling capability, and part of that capability is the ability to integrate actions from different levels into a meaningful sequence in time. In the buildActions() method, one creates a schedule, throws actions onto it, and then that schedule is integrated into the overall time scheme by the activateIn() method. The "go" method tells the model to run through its paces.

Please note that activateIn() refers to a hierarchical concept. A lower level swarm can be "activated in" a higher level swarm, which means that when the higher swarm ticks ahead one time step, then the next thing that happens is that all the actions set for that time in the lower swarm are run. How precisely they are ordered is going to depend on the way the model is designed, but I hope you get the idea. The higher level does not take a step ahead until the lower level's actions are all completed. It is possible to design a Swarm model with many hierarchical levels.

Of course, since this is an elementary model there is no hierarchy to speak of. I've got one Model level where the agents are created and their actions are managed from that model level. We just iterate through the list a few times, then randomly designate a criminal and find out if the cop can track down the suspect. But the beauty of Swarm is that you don't have to do things in such a limited fashion, if you don't want to. There is commentary in Model.java to try to explain more about how the scheduling works.

One other minor change. Instead of the random number generators from Java, which are of uncertain quality, we use random numbers from the Swarm libraries. One of the things that happens when you initialize the Swarm model (look at the main function in Test.java) is the creation of a default object Globals.env.randomGenerator as well as some built in distribution objects that can give out numbers from random distributions by converting input from the randomGenerator. Here, the Random class from the Java libraries is replaced by Swarm's uniformIntRand object, which can give us random integers in any inerval we specify.

The style of the method names of the random number draws may be surprising to new comers. The dollar sign acts like a "spacer" to help us to remember the arguments that are needed in parentheses. Consider:

int x = Globals.env.uniformIntRand.getIntegerWithMin$withMax(0,N-1);
There's only one dollar sign, but it prompts us to note that we need 2 arguments, one for the minimum value (WithMin) and one for the maximum value (withMax). I rather like these dollar signs as reminders, but, in all honesty, I think they are only a vestige of the Objective-C origins of Swarm. In Objective-C, the syntax looks like this:
[uniformIntRand getIntegerWithMin: 0 withMax: N-1];
The java native interface is used to "expose" these methods from Objective-C to programs that are written in java. The translation introduces the dollar signs.