swarm-modeling
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bugs' reproduction


From: Enrico Curiotto
Subject: bugs' reproduction
Date: Mon, 11 Mar 2002 12:19:14 -0800 (PST)

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.
                  ==================================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]