Multi-Agent Simulation Based on Swarm 55
7: bugDensity = 0.1;
8: }
The super in the second line executes the constructor of the super class
SwarmImp and allocates the memory. In lines 4–7, initial values are being
assigned. Its own object (this) can be omitted here. More pre c isely, this
should be written as follows:
4: this.worldXSize = 80;
4: this.worldXSize = 80;
5: this.worldYSize = 80;
6: this.seedProb = 0.5;
7: this.bugDensity = 0.1;
Here, let us explain the aZone in the first line. In Swarm, all the objects
are created in the memory reg ion called zone.” Methods for allocating and
freeing the memory fo r that purpose are provided. Memory for “instance” and
it’s internal variables are also supplied. Moreover, “zone” keeps track of all
the objects created there, and memory can be re-used simply by dropping the
zone.” In other words, a signal to all objects can be sent for self-destruction.
The three main parts of zone are summarized as follows:
1. The initSwarm(...) function inside the main,” executes various func-
tions tha t generate global memo ry areas.
2. In modelSwarm=newModelSwarm(Globals.env.globalZone) inside the
main function, instances are created.
3. ModelSwarm class’ constructor modelSwarm() is executed.
3.2.3.2 Construction of Agents
This is defined in buildObjects of ModelSwarm.java. The purpose of this
method is to generate the instance of the class required for the representation
of objects in simulation, and to set the parameters in them. As we can see
from the definition of setFoodSpace, it is a n advantage of object-oriented
programming that the functions for the settings of variables are ready thro ugh
inheritance.
3.2.3.3 Construction of Schedul es
A schedule is constructed by the following method called buildActions
in ModelSwarm.java.
1: public Object buildActions(){
2: modelSchedule=new ScheduleImpl(this,1);
3: try {
4: modelSchedule.at$createActionTo$message(0,aBug,
56 Agent-Based Modeling and Simulation with Swarm
5: new Selector(Class.forName("Bug"),"step",false));
6: } catch (Exception e) {
7: System.out.println ("Exception: " + e.getMessage ());
8: System.exit(1);
9: }
10:
11: return this;
12: }
In the second line, the instance of the clas s Schedule is generated. The
second argument 1,” means “Execute this schedule at e ach time step.” If
we write the natural number n here, that schedule will be executed ev-
ery n time steps. In lines 4–5, an event is ass igned to the schedule. Here,
in the instance aBug of class Bug,” Action is gener ated which e x-
ecutes the method called step.” The fir st argument indicates the time
of first b oot. In other words, in this case it executes at time step 0.
Lines 6–9 are for handling err ors if the occurred. That means, by sending
at$createActionTo$message (start, instance, method) method to the
instance modelSchedule of schedule, Action is generated. Her e , the fir st ar -
gument start is the starting time of Action,” and an event c alled Action
is se t which sends method to instance.”
Furthermore, the method Selector(Class, string, boolean) retrieves
the method defined by the string-name string of the clas s Class.” The
third ar gument boolean is false when it retrieves the methods of Java and
true in the case of Objective-C.
3.2.3.4 Activation of Swarm
In the main function, by calling
modelSwarm.activateIn(null);
a schedule gets activated. Since there is only one Swarm in simpleSwarmBug,
the argument becomes null.” Specifically, the following method is called:
public Activity activateIn(Swarm context){
super.activateIn (context);
modelSchedule.activateIn(this);
return getActivity ();
}
Merge and activation of schedule are initiated by this method.
3.2.3.5 State of Execution
As the program starts, fir st the ModelSwarm is executed, and ModelSwarm
generates Bug and FoodSpace (method: buildObjects).
In the body of the simulation, Bug r e peats the method step.” This is
Multi-Agent Simulation Based on Swarm 57
simpleSwarmBug
ModelSwarm
FoodSpace
Bug
create()
create()
seedFoodWithProb()
create()
setFoodSpace()
setWorldSizeX$Y()
setX$Y()
*step()
getValueAtX$Y()
putValue$atX$Y()
activateIn()
getActivity()
run()
buildObjects()
buildActions()
FIGURE 3.12:
Execution sequence in simpleSwarmBug.
implemented (in the method buildActions”) by registering action in the
schedule: “Send the step method execution mess age to the Bug instance”
while “starting at time 0 and repeating with a time interva l of 1.” Schedule is
activated by the method activateIn,” and is executed by run.” The state
of exe c utio n is shown in Fig. 3.12.
3.2.4 simpleSwarmBug2
Multiple age nts are handled in this program. Since multiple bugs are han-
dled as a set, the instance bugList of class List is used (Fig. 3.13). The
concentration of bugs is set by the variable bugDensity.”
if (Globals.env.uniformDblRand.getDoubleWithMin$withMax(0.0,1.0)
< bugDensity){
58 Agent-Based Modeling and Simulation with Swarm
main
bugList
World
ModelSwarm
Food
Space
FIGURE 3.13: simpleSwarmBug2.
contains that creation part, in which rando m variables from 0 to 1 are gener-
ated. If that value is smaller than the bugDensity,” then an instance of Bug
is created and initialized as shown below:
aBug=new Bug(this);
aBug.setWorld$Food(world,food);
aBug.setX$Y(x,y);
In the following line:
bugList.addLast(aBug);
a newly created instance is being added at the back of bugList. Moreover,
reportBug=(Bug)bugList.removeFirst();
bugList.addFirst(reportBug);
substitutes the variable reportBug at the front of the list. As will be men-
tioned in a later section, reportBug is used for outputting the position.
At the same time, multiple action messages are orga nized into one for
activation, and then implemented as the class instance of ActionGroupImp1,”
which is the implementation of the ActionGroup interface. This is useful for
denoting the concurrent execution for multiple bugs. For this purp ose, we
need to dec lare the variable modelActions in
ActionGroup modelActions;
and then, using the buildActions method, the following instance is created:
modelActions=new ActionGroupImpl(this);
After that, in the following lines:
Multi-Agent Simulation Based on Swarm 59
modelActions.createActionForEach$message(bugList,
new Selector(Class.forName("Bug"),"step",false));
modelActions.createActionTo$message(reportBug,
new Selector(Class.forName("Bug"),"report",false));
multiple actions are defined. Here, through createActionForeach$message,
action executes fo r all the elements of bugList. Mor e over, methods
(report) for other instances (reportBug) are also added.
Next, schedule is created with the following lines:
modelSchedule=new ScheduleImpl(this,1);
modelSchedule.at$createAction(0,modelActions);
Here, the frequency of schedule execution is 1 time step, starting at time
0. To ensure that there is only o ne bug in a particula r location, world is
created as the instance of class Grid2d.” For the method buildObjects
inside ModelSwarm.java, cre ation and initializa tion are done by the following
lines (each is filled with null”):
world=new Grid2dImpl(this,worldXSize,worldYSize);
world.fillWithObject(null);
For the method step in Bug,” using the following lines:
if (world.getObjectAtX$Y(newX,newY) == null){
world.putObject$atX$Y(null,xPos,yPos);
xPos = newX;
yPos = newY;
world.putObject$atX$Y(this,newX,newY);
}
the past position (xPos,yPos) is cleared (null), and the ins tance of Bug is
described in a new location (newX,newY). In the following if loop,
if (foodSpace.getValueAtX$Y(xPos,yPos) == 1){
foodSpace.putValue$atX$Y(0,xPos,yPos);
haveEaten=1;
}
if there is a bait in the new loca tion, the bug eats it (resets to 0), and makes the
haveEaten flag 1. This fla g is used to determine the dis play in the method
report.”
3.2.5 simpleSwarmBug3
This is not much different from simpleSwarmBug3.” The only difference
is that it reads the default parameters from an external file for initialization.
The description for this is in the following line of the main function:
..................Content has been hidden....................

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