swarm-support
[Top][All Lists]
Advanced

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

Re: Predation Model


From: Darren Schreiber
Subject: Re: Predation Model
Date: Fri, 30 Mar 2001 20:27:40 -0800


First, let me plug the use of the canvas tool. While I haven't gotten around to learning Java yet, I have used the canvas tool in a few of my projects. Many of the models I have seen use Grid 2d or some variation of it. But, the great thing about canvas is the ability to have agents move across a continuous 2d space rather than a discrete space like a grid. I think it is relatively easy library to use and has some nice features built in.

For instance, you can label agents, color them, change the thickness of the circle outline, and draw lines connecting one agent to another. All of this enables the programmer to place much more information at the hands of a user. It has really helped me to visualize the events in my party formation model. (See a simple example of using canvas at http://zia.hss.cmu.edu/econ/homework00/2/cocktail.html or my party formation model at http://www.bol.ucla.edu/~dschreib)

And for those who haven't seen Star Logo, it is a pretty neat simulation environment that has neither the start up costs (it is being taught in Jr. High and High Schools) nor the strengths of Swarm.

Here is the site for the StarLogoT distribution

http://www.ccl.sesp.northwestern.edu/cm/starlogoT/

And the site for the StarLogo distribution.

http://el.www.media.mit.edu/groups/el/Projects/starlogo/


        Darren



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

--

_____________________________________________

                 Darren Schreiber
                  Attorney at Law
                 Graduate Student
             Political Science, UCLA
                address@hidden
        http://www.bol.ucla.edu/~dschreib

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