swarm-support
[Top][All Lists]
Advanced

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

Re: Predation Model


From: Marcus G. Daniels
Subject: Re: Predation Model
Date: 30 Mar 2001 16:55:33 -0800
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4

>>>>> "RL" == Rob Leclerc <Rob> writes:

RL> Given that Swarm only has a discrete 2D gird by default, now
RL> allowing for multiple objects at the same X,Y coordinate, I
RL> originally coded my model for this. 

Well, this is a somewhat tangential reply (more visualization than
representation), but here's an example of how to put agents on a
Canvas such that two or more agents can be in the same place. It
requires recent a swarm.jar & DLLs.

And as far as Grid2d goes, you can just as easily put a List on a cell
as an agent..

import swarm.Globals;
import swarm.simtoolsgui.GUISwarmImpl;
import swarm.defobj.Zone;

import swarm.gui.Canvas;
import swarm.gui.CanvasC;
import swarm.gui.CanvasImpl;
import swarm.gui.CanvasCImpl;
import swarm.gui.OvalNodeItem;
import swarm.gui.OvalNodeItemC;
import swarm.gui.OvalNodeItemS;
import swarm.gui.OvalNodeItemCImpl;
import swarm.gui.OvalNodeItemImpl;

import swarm.activity.ScheduleImpl;
import swarm.activity.Schedule;
import swarm.activity.Activity;
import swarm.objectbase.Swarm;
import swarm.Selector;

import java.util.LinkedList;
import java.util.List;

public class TestCanvas extends GUISwarmImpl {
    List bugList;
    Schedule schedule;

    final int xMax = 500;
    final int yMax = 500;

    static void main (String args[]) {
        Globals.env.initSwarm ("TestCanvas", "0.0", "address@hidden",
                               args);

        TestCanvas testCanvas = new TestCanvas (Globals.env.globalZone);
        
        testCanvas.buildObjects ();
        testCanvas.buildActions ();
        testCanvas.activateIn (null);
        testCanvas.go ();
    }

    static int bugCount = 0;

    class Bug { 
        OvalNodeItem oval;
    
        Bug (Canvas canvas) {
            OvalNodeItemC ovalC =
                new OvalNodeItemCImpl (new OvalNodeItemImpl ());
            ovalC.createBegin (canvas.getZone ());
            ovalC.setCanvas (canvas);
            ovalC.setString ("Bug" + bugCount);

            int x =
                Globals.env.uniformIntRand.getIntegerWithMin$withMax (0, xMax);
            int y =
                Globals.env.uniformIntRand.getIntegerWithMin$withMax (0, yMax);

            ovalC.setX$Y (x, y);
            oval = (OvalNodeItem) ovalC.createEnd ();
            move ();
            bugCount++;
        }
    
        public void move () {
            int xdelta =
                Globals.env.uniformIntRand.getIntegerWithMin$withMax (-5, 5);
            int ydelta =
                Globals.env.uniformIntRand.getIntegerWithMin$withMax (-5, 5);
            int x = oval.getX ();
            int y = oval.getY ();

            if (x + xdelta >= 0 && y + ydelta >= 0
                && x + xdelta < xMax && y + ydelta < yMax)
                oval.moveX$Y (xdelta, ydelta);
        }
    }

    public TestCanvas (Zone aZone) {
        super (aZone);
    }
    
    public Object buildObjects () {
        super.buildObjects ();
        
        CanvasC canvasC = new CanvasCImpl (new CanvasImpl ());
        canvasC.createBegin (getZone ());
        Canvas canvas = (Canvas) canvasC.createEnd ();

        canvas.setHeight (xMax);
        canvas.setWidth (yMax);
        canvas.setWindowTitle ("Agents");
        canvas.pack ();

        bugList = new LinkedList ();

        bugList.add (new Bug (canvas));
        bugList.add (new Bug (canvas));

        return this;
    }

    public Object buildActions () {
        super.buildActions ();

        schedule = new ScheduleImpl (getZone (), 1);

        try {
            schedule.at$createActionForEach$message
                (0,
                 bugList,
                 new Selector (Bug.class,
                               "move",
                               false));

            schedule.at$createActionTo$message
                (0,
                 getActionCache (),
                 new Selector (getActionCache ().getClass (),
                               "doTkEvents",
                               false));
        } catch (Exception e) {
            e.printStackTrace (System.err);
            System.exit (1);
        }

        return this;
    }

    public Activity activateIn (Swarm swarmContext) {
        super.activateIn (swarmContext);

        schedule.activateIn (this);
        return getActivity ();
    }
}


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of 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]