[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Swarm-Support] (no subject)
From: |
LRA |
Subject: |
Re: [Swarm-Support] (no subject) |
Date: |
Tue, 13 Dec 2005 07:20:30 -0800 |
User-agent: |
Mozilla Thunderbird 1.0.7 (Windows/20050923) |
mariem siala wrote:
Hi !
We can build agents in Model or Observer.
But How an agent can create another agent? Have you an example?
Hello,
Yes, agents can create new agents. You can program agents to call their
own "create" or constructor methods to reproduce.
There is one tricky part: You cannot add (or remove) agents from the
model's list of agents while the model's schedule is acting on that
list. (If the model is going through its list of all the StupidBugs and
telling them each to "grow", by using "createActionForEach", the bugs
cannot add new bugs to that list until the createActionForEach has
finished.)
Instead, new agents are usually put on a list of new agents. The model
then must have a scheduled action that takes the new agents off that
list and put them on the master agent list.
Do you want an example in Objective-C or Java Swarm?
Steve Railsback
public class StupidBug {
...
public void grow() {
HabitatCell myCell =
(HabitatCell)myCellWorld.getObjectAtX$Y(myX,myY);
double cellFoodAvailable = myCell.getCellFoodAvailable();
double foodEaten = Math.min(cellFoodAvailable,
modelSwarm.getBugMaxConsumptionRate());
myCell.setCellFoodAvailable(cellFoodAvailable - foodEaten);
mySize += foodEaten;
// For version 12, add reproduction to the grow method
// When we create offspring, we use a new StupidBug constructor
method
// New bugs can die during their creation (if they do not find
// a vacant cell) so we need to be careful ordering cleanup
// of bug lists in the model swarm
if (mySize >= 10.0) {
// First make 5 more bugs
StupidBug newBug;
for (int i = 1; i <= 5; i++) {
newBug = new StupidBug (myBugWorld,
myCellWorld, modelSwarm, myX, myY);
modelSwarm.add(newBug);
}
// Then die
myBugWorld.putObject$atX$Y(null, myX, myY);
modelSwarm.remove(this);
}
return;
}
...
// Here is the new constructor method used for reproduction
public StupidBug (Grid2dImpl aBugSpace, Grid2dImpl aCellSpace,
StupidModelSwarm aModelSwarm, int momsX, int momsY) {
myBugWorld = aBugSpace;
myCellWorld = aCellSpace;
modelSwarm = aModelSwarm;
mySize = 0.0;
neighborCellList = new ListImpl(modelSwarm);
int testX, testY, testCellX, testCellY;
int moveDistance = 3;
HabitatCell newCell;
// To find a new location, make a list of available cells
// This approach is copied from "move"
neighborCellList.removeAll();
for (testX = momsX - moveDistance; testX <= momsX + moveDistance;
testX++) {
for (testY = momsY - moveDistance; testY <= momsY + moveDistance;
testY++) {
// check the coordinates because Swarm does not
// provide a toroidal space
testCellX = xnorm(testX);
testCellY = ynorm(testY);
// If there is no bug at this location, put the
cell
// at the location on the neighbor list.
if (myBugWorld.getObjectAtX$Y(testCellX,
testCellY) == null)
neighborCellList.addLast(myCellWorld.getObjectAtX$Y(testCellX,
testCellY));
}
}
// If no vacant neighbor cells were found, the new bug dies
if (neighborCellList.getCount() == 0) {
modelSwarm.remove(this);
return;
}
// Else the new bug moves to a randomly chosen vacant cell
else {
newCell = (HabitatCell) neighborCellList.atOffset
(Globals.env.uniformIntRand.getIntegerWithMin$withMax
(0, neighborCellList.getCount()-1));
myX = newCell.getMyX();
myY = newCell.getMyY();
myBugWorld.putObject$atX$Y(this, myX, myY);
// the new bug gets put on the bug list in 'grow'
}
return;
}
--
Lang Railsback & Associates
250 California Ave.
Arcata CA 95521
(707) 822-0453