50 Agent-Based Modeling and Simulation with Swarm
Swarm
Sub-Swarm
agent
scheduler
Interface
Model
Probes
Sub-sub-Swarm
Probes
Probes
FIGURE 3.7:
Recursive structures in Swarm.
var=10.2
FIGURE 3.8:
Probes.
3.2 Tutorial
This tutorial is an example of an a gent which does r andom walks and
consists of 5 steps shown in Table 3.1.
3.2.1 simpleCBug
The first progra m is a procedural simulation. After generating a bug agent,
it is placed on a two-dimensional plane (of the size worldXSize×worldYSize=
80 × 80) on the grid. The initial pos itio n of the bug is xPos= 40,yPos= 40.
Then a random walk is performed 100 times. Let us explain the program
briefly. First,
import swarm.*;
is a kind of charm that imports useful packages of random numbe rs, etc. Under
that is the main function, where the program starts. Main takes the argument
Multi-Agent Simulation Based on Swarm 51
Method execution
Input values of variables
Probe object class
Close probe
FIGURE 3.9:
Probe and GUI.
(args) from the command-line, and the function initSwarm below checks the
args and performs the necessary initialization.
Globals.env.initSwarm("bug", "0.2", "foo@nifty.com", args);
Here, we specify the “default file” (file named bug.scm) of the “ parameter set-
tings.” 0.2 is the version name. Other than that, settings such as the address of
the author, etc., are spec ified. This is detailed in the section simpleSwarmBug3.
Then, the initial coordinates of the bug are displayed, and function
Globals.env.uniformIntRand.getIntegerWithMin$withMax(-1,1);
generates the random numbers (integers; -1,0,1), which are a dded to xPos and
yPos, enabling a random walk.
xPos = (xPos + worldXSize) % worldXSize;
yPos = (yPos + worldYSize) % worldYSize;
Note that if the bug is outside the field, wrapping becomes necessary (i.e.,
% worldXSize).
3.2.2 simpleObjCBug and simpleObjCBug2
These two are meant to verify the object-oriented programming, and have
no sp e c ific significance with regard to Swarm.
Let us first look at simpleObjCBug. It is the simplest version of the object-
oriented program. Except for the main function, it consists of only one class
and its insta nce . The main function impor ts the basic library of Swarm,
launches the initSwarm, and performs the memory allocation.
Class Bug is defined in Bug.java:
52 Agent-Based Modeling and Simulation with Swarm
Derive the
price average
Derive the cost
average
Probe
Probe
Agent
Collection
Graph
widget
FIGURE 3.10: GUI widgets.
public class Bug extends SwarmObjectImpl {
This is to declare the class Bug as the subc lass of SwarmObjectImp1. This
allows the use of the following function:
construction: memory allocation
drop: memory creation and annihilation
In fact, in Bug.java, the following definition is the constructor:
public Bug(Zone aZone){
super(aZone);
}
Zone is the memory space of Swarm. Since super is the function, it executes
the constructor of SwarmObjectImp1, which is the super class of Bug.
Described in the main function,
Bug aBug=new Bug(Globals.env.globalZone);
aBug.setX$Y(xPos,yPos);
aBug.setWorldSizeX$Y(worldXSize,worldYSize);
are the typical procedures for the generation of an “instanc e .” Firs t, we gen-
erate an “instance” of class Bug using new,” which is then assig ned to a vari-
able aBug. Memory allocation takes place here. In the next two steps, nec e s-
sary parameter setting takes place. These methods for the Bug are defined in
Bug.java.
Next, let us move on to simpleObjCBug2. In this program, the world
where bug walks around and eats the bait is cr e ated. This world is defined as
Multi-Agent Simulation Based on Swarm 53
TABLE 3.1: Tutorial contents.
simpleCBug Procedural programming
simpleObjCBug 1 Object-oriented programming
2 Bug and FoodSpace interaction
simpleSwarmBug 1 Object r e sp onsible for the simulation
Schedule
2 Multiple Bugs
3 Parameter settings with files
simpleObserverBug 1 Display on the 2-dimensional plane
2 Probe to acce ss the object
simpleExperBug Multiple models
Hierarchical schedules
Graphical display
FoodSpace, and is the subclass of Discrete2dImp1. The following lines, inside
FoodSpace.java,
public FoodSpace(Zone aZone,int x,int y){
super(aZone,x,y);
}
contain a constructor, in which super executes the constructor of
Discrete2dImp1.
FoodSpace inherits the internal variables and methods fro m
Discrete2dImp1. It makes the retention and retrieval of values of the two-
dimensional spac e possible. The newly added method by FoodSpace is only
seedFoodWithProb. Note that the getSizeX, getDoubleWithMin$withMax
methods used in this definition are inherited from Discrete2dImp1. In
seedFoodWithProb, at each point in the FoodSpace a random number be-
tween 0 and 1 is g e nerated. If the value of the position is les s than seedProb,
it changes the value to 1 (that is, putValue$atX$Y(1,x,y)). This is to
achieve the bait at concentration of seedProb. In this program, the variable
(foodSpace) of FoodSpace is defined as an internal variable of Bug. It ho lds
the world in which it moves. In the part below the method step,” if the new
location has bait, it eats the bait (substitutes the va lue of 0) and displays that
position.
if (foodSpace.getValueAtX$Y(xPos,yPos) == 1){
foodSpace.putValue$atX$Y(0,xPos,yPos);
System.out.println("I found food at X = " + xPos + " Y = "
+ yPos +"!");
}
54 Agent-Based Modeling and Simulation with Swarm
+buildObjects()
+buildActions()
+activateIn(in context)
-worldXSize
-worldYSize
-seedProb
-Schedule
ModelSwarm
+step()
Bug
FoodSpace
+getActivity()
Swarm
+run()
<<interface>>
Activity
1
1
11
FIGURE 3.11:
Class hierarchy in simpleSwarmBug.
3.2.3 simpleSwarmBug
The typicality of Swarm can be shown with this program. In the
simpleSwarmBug of the third phase, the object ModelSwarm is introduced
(Fig. 3.11). It manages all the models (object of Bug and FoodSpace, e tc.,
parameters such as the size of the world, schedule, etc.). Often, Model be-
comes the nested structure of multiple swarms.
The generation of Swarm is done by the following steps:
1. construction: initialization of memory and parameters
2. buildObjects: construction of all agents and objects in the model
3. buildActions: sequence of events and definition of schedule
4. activate: beginning of the execution and merging into the top level swarm
The above steps correspond to the fo llowing lines in the simpleSwarmBug.
java:
observerSwarm=new ObserverSwarm(Globals.env.globalZone);
observerSwarm.buildObjects();
observerSwarm.buildActions();
observerSwarm.activateIn(null);
The details of these will be explained below.
3.2.3.1 Initialization
The constructor of ModelSwarm.java is as follows:
1: public ModelSwarm(Zone aZone){
2: super(aZone);
3:
4: worldXSize = 80;
5: worldYSize = 80;
6: seedProb = 0.5;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.22.34