[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bugs' reproduction
From: |
Alex Lancaster |
Subject: |
Re: bugs' reproduction |
Date: |
11 Mar 2002 16:11:56 -0800 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.1 |
Enrico,
Please make followups to swarm-support. swarm-support is the
"technical list" for the day-to-day usage of Swarm, swarm-modelling is
for more general issues of modelling.
Please see:
http://www.swarm.org/community-mailing-lists.html
for descriptions.
Thanks,
Alex
>>>>> "PT" == Pietro Terna <address@hidden> writes:
PT> A suggestion: myBugSpace.getObjectAtX$Y(newX, newY)
PT> points to the other bug, which has to be able to perform a method
PT> like 'getSex()' retunuing a 0/1 integer value.
PT> Pietro
PT> At 12.19 11/03/02 -0800, you wrote:
>> Hi, I'me learning Swarm with java, studying the "Simplebug
>> tutorial". To do some practise I'm making some changes to
>> SimpleObserverBug3, trying to simulate the reproduction between
>> bugs (pretending they were mammals). For doing so I've added the
>> integer variable "bugSex" to the SimpleBug class, its value can be
>> either 0 or 1 indicating "female" or "male". The reproduction
>> would happen just if a male walks on a cell occupied by a female
>> (or viceversa). The method to create one bug does work and is
>> called "crea1Bug". While I know the value of the variable bugSex
>> of the agent who "randomwalks", how can I get the variable bugSex
>> of the agent who stands ?
>>
>> At the moment the simulation works but the reproduction does not
>> depend upon the sex. To introduce sex dependence reproduction I
>> suppose I've to change the part signed with "*****" in the
>> randomwalk method, but I don't know how. Any help ?
>>
>> Thanks Enrico
>>
>> The code of the SimpleBug class is the following:
>>
>>
>> // SimpleBug.java // Defines the class for our SimpleBug agents/
>>
>> import swarm.Globals; import swarm.defobj.Zone; import
>> swarm.objectbase.SwarmObjectImpl;
>>
>> import swarm.space.Discrete2dImpl; import swarm.space.Grid2dImpl;
>>
>> import swarm.gui.Raster; import swarm.gui.ZoomRasterImpl;
>>
>> public class SimpleBug extends SwarmObjectImpl { // These instance
>> variables keep track of a given bug's foodspace, // position,
>> hardiness and identity. We also save the dimensions // of the
>> foodspace so that we can make fewer calls to the // getSizeX() and
>> getSizeY() methods in the bug's randomWalk(). public FoodSpace
>> myFoodSpace; public Grid2dImpl myBugSpace; public int xPos; public
>> int yPos; public int bugNumber; public int bugHardiness; public int
>> bugSex; public ModelSwarm modelSwarm;
>>
>>
>> int worldXSize; int worldYSize;
>>
>> // bugColor records the color that this bug is to use when drawing
>> // itself on the GUI display raster. public byte bugColor;
>>
>> // haveEaten keeps track of whether the bug has eaten on its most
>> // recent walk and periodsSinceEaten keeps track of just that.
>> public boolean haveEaten; public int periodsSinceEaten = 0;
>>
>> // Constructor to create a SimpleBug object in Zone aZone and to //
>> place it in the foodspace and bugspace, fSpace and bSpace, at //
>> the specified X,Y location. The bug is also given a numeric id, //
>> bNum. public SimpleBug(Zone aZone, FoodSpace fSpace, Grid2dImpl
>> bSpace, int X, int Y, int bNum, int bHardiness, int bSex,
>> ModelSwarm mSwarm) { // Call the constructor for the bug's parent
>> class. super(aZone);
>>
>> // Decide the sex of the Bug bSex =
>> Globals.env.uniformIntRand.getIntegerWithMin$withMax(0, 1); if
>> (bSex == 1) setBugColor((byte)4); else if (bSex == 0)
>> setBugColor((byte)5);
>>
>>
>>
>> // Record the bug's foodspace, bugspace, initial position and // id
>> number. myFoodSpace = fSpace; myBugSpace = bSpace; worldXSize =
>> myFoodSpace.getSizeX(); worldYSize = myFoodSpace.getSizeY(); xPos =
>> X; yPos = Y; bugNumber = bNum; bugHardiness = bHardiness; bugSex =
>> bSex; modelSwarm = mSwarm; }
>>
>> // This is the method to have the bug take a random walk backward
>> // (-1), forward (+1), or not at all (0) in first the X and then //
>> the Y direction. The randomWalk method uses //
>> getIntegerWithMin$withMax() to return an integer between a //
>> minimum and maximum value, here between -1 and +1. //
>> Globals.env.uniformRand is an instance of the class //
>> UniformIntegerDistImpl, instantiated by the call to //
>> Globals.env.initSwarm in StartSimpleBug. Note that the bug's //
>> world is a torus. If the bug walks off the edge of its //
>> rectangular world, it is magically transported (via the modulus //
>> operator) to the opposite edge. If on its walk the bug finds //
>> food, it eats it and turns on the haveEaten flag so it can //
>> report its feast if asked. Note that before the bug actually //
>> moves, we must check to see that there is no other bug at the //
>> destination cell. If there is, the this bug just stays put.
>>
>> public void randomWalk() {
>>
>>
>> int newX, newY;
>>
>> int walkBugSex = bugSex;
>>
>> // Decide where to move. newX = xPos +
>>
>> Globals.env.uniformIntRand.getIntegerWithMin$withMax(-1, 1); newY =
>> yPos +
>>
>> Globals.env.uniformIntRand.getIntegerWithMin$withMax(-1, 1); newX =
>> (newX + worldXSize) % worldXSize; newY = (newY + worldYSize) %
>> worldYSize;
>>
>> // Is there a bug at the new position already? If not, put a //
>> null at this bug's current position and put this bug at the // new
>> position. if (myBugSpace.getObjectAtX$Y(newX, newY) == null) {
>> myBugSpace.putObject$atX$Y(null, xPos, yPos); xPos = newX; yPos =
>> newY; myBugSpace.putObject$atX$Y(this, xPos, yPos); } ***** else if
>> (myBugSpace.getObjectAtX$Y(newX, newY) != this)
>>
>> {
>>
>> modelSwarm.crea1Bug();
>>
>> }
>>
>>
>> // If there is food at this cell, eat it and record the //
>> fact. Otherwise, increment periodsSinceEaten and change the //
>> bugColor to yellow (3). if (myFoodSpace.getValueAtX$Y(xPos, yPos)
>> == 1) { myFoodSpace.putValue$atX$Y(0, xPos, yPos); haveEaten =
>> true; periodsSinceEaten = 0; if (bugSex == 1) setBugColor((byte)4);
>> else if (bugSex == 0) setBugColor((byte)5);
>>
>> } else { haveEaten = false; ++periodsSinceEaten; if
>> (periodsSinceEaten >= bugHardiness/2) setBugColor((byte)3); }
>>
>> // Now check to see if we're still alive! If not, we tell //
>> modelSwarm that we've died. if (periodsSinceEaten >= bugHardiness)
>> modelSwarm.bugDeath(this); }
>>
>> // Method to report the bug's position to the console. public void
>> reportPosition() { System.out.println("Bug " + bugNumber + " is at
>> " + xPos + ", " + yPos + " sesso: " + bugSex); } // Method to
>> report if the bug has eaten. public boolean reportIfEaten() { if (
>> haveEaten ) System.out.println("Bug " + bugNumber + " has found
>> food at " + xPos + ", " + yPos + "sesso: " + bugSex);
>>
>> return haveEaten; }
>>
>> // These are methods that allow the bug to draw itself on the GIU
>> // raster display object. The first tells the bug what color it //
>> should use, the second draws the bug at its current location.
>> public Object setBugColor(byte c) { bugColor = c; return this; }
>>
>> public Object drawSelfOn (Raster r) { r.drawPointX$Y$Color (xPos,
>> yPos, bugColor); return this; } }
>>
>>
>>
>> __________________________________________________ Do You Yahoo!?
>> Try FREE Yahoo! Mail - the world's greatest free email!
>> http://mail.yahoo.com/
>>
>>
>> ================================== Swarm-Modelling is for
>> discussion of Simulation and Modelling techniques esp. using Swarm.
>> For list administration needs (esp. [un]subscribing), please send a
>> message to <address@hidden> with "help" in the body of the
>> message. ==================================
PT> ==================================
PT> Swarm-Modelling is for discussion of Simulation and Modelling
PT> techniques esp. using Swarm. For list administration needs
PT> (esp. [un]subscribing), please send a message to
PT> <address@hidden> with "help" in the body of the message.
PT> ==================================
--
Alex Lancaster | e-mail: address@hidden
Swarm Development Group | web: http://www.swarm.org
-------------------------------------------------------------------
==================================
Swarm-Modelling is for discussion of Simulation and Modelling techniques
esp. using Swarm. For list administration needs (esp. [un]subscribing),
please send a message to <address@hidden> with "help" in the
body of the message.
==================================