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: