swarm-support
[Top][All Lists]
Advanced

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

one trick for javaswarm, If you need dynamically adding or removing agen


From: Hu, Jianjun
Subject: one trick for javaswarm, If you need dynamically adding or removing agents, use createFActionForEachHeterogeneous$call rather than createFActionEachHomogeneous
Date: Wed, 26 Jun 2002 18:10:16 -0400

Hi, Buck,

Great thanks.

When I change from createFActionEachHomogeneous to
createFActionForEachHeterogeneous$call,  the new born agents moves!!!

haha.. it works!


Jianjun





-----Original Message-----
From: address@hidden
[mailto:address@hidden Behalf Of William T. Stockhausen
Sent: Wednesday, June 26, 2002 5:59 PM
To: address@hidden
Subject: RE: Is this the defects of Javaswarm? the swarm kernel neglect new
added agents!


Hi,

I solved a similar problem (back in January, apparently) to what you are
facing, with help from Marcus.  Apparently assigning a
createFActionEachHomogeneous-type of action to a list assigns the action
only to the members of the list that exist when the action is created.  When
you add members to the list after the model starts to run, the added members
aren't sent the message to perform the action, even though they're part of
the list.  You need to use createFActionForEachHeterogeneous.

Here's a bit of my original mesage:
WS> It appears, however, that the FActionForEach is "assigned" to each
WS> agent only once (at the start of the simulation), so that even
WS> though agents are moved from list1 to list2, they continue to act
WS> (in my case) as if they were on list1. Should this be the case, or
WS> have I missed something?

and Marcus' reply:
There's one FCall object that is allocated for the list, and the list itself
is frozen at create time.  This avoids the cost of iterating through
lists and the costs of dispatching the method to the target object.
So, createFActionForEachHomogeneous$call is too specialized for what
you want to do.  The Heterogeneous version should work, though.


This is what I ended up doing. The following are code snippets from a simple
model of planktonic transport and settlement for marine larvae.  Larvae
(AgentPLs) are advected over a grid of settlement sites of varying
suitability, migrate up and down in the water column, and can "decide" to
settle to the benthos.  Larvae are created at the borders of the world grid
and advected across it; larvae which would be advected out without settling
are droppped to save memory (advection is in one direction only).  I keep a
list for larvae still in the water column (llPlanktonic) and one for those
settled to the bottom (llBenthic). [the comments which start with "<--" are
not in the code; I added them here for clarity]

Set up the model actions in the buildActions part of the Model Swarm:
    modelActions = new ActionGroupImpl (getZone ());

    //create habitat-level actions
    System.out.println ("Creating bhMap Actions\n");
    try {
      modelActions.createActionTo$message
        (bhMap, new Selector (bhMap.getClass (), "stepRule", false));
    } catch (Exception e) {
      System.err.println ("Exception stepRule: " + e.getMessage ());
    }

    //create agent-level actions
    System.out.println ("Creating AgentPL Actions\n");
    try {
      AgentPL proto = (AgentPL) llPlanktonic.get (0);
      System.out.println ("Creating Actions: Got agent.\n");
      Selector sel = new Selector (proto.getClass (), "stepRule", false);
<--stepRule for inidivdual larvae
      System.out.println ("Creating Actions: Got selector.\n");
      FArguments fArg = new FArgumentsImpl (this, sel);
      System.out.println ("Creating Actions: Got fArg.\n");
      FCall fCall = new FCallImpl (this, proto, sel, fArg);
      System.out.println ("Creating Actions: Got fCall.\n");
      actionForEach =
        modelActions.createFActionForEachHeterogeneous$call
<--assigning the actions to members of llPlanktonic
        (llPlanktonic, fCall);
      System.out.println ("Creating Actions: Got actionForEach.\n");
    } catch (Exception e) {
      System.err.println ("Exception AgentPL Actions: " + e.getMessage ());
      e.printStackTrace (System.err);
    }

    //create modelSwarm-level actions
    System.out.println ("Creating modelSwarm Actions\n");
    try {
      modelActions.createActionTo$message
        (this, new Selector (this.getClass (), "stepRule", false));
<--steprule for the ModelSwarm
    } catch (Exception e) {
      System.err.println ("Exception stepRule: " + e.getMessage ());
    }
    .......

In the Model Swarm-level "stepRule", agents in the llPlanktonic list get
checked to see if they settled to the bottom or left the model grid--if the
former, they're switched to the llBenthic list; if the latter, they are
dropped from the model:
    /** update Swarm-level constructs */
  public Object stepRule () {
    lPA = 0;
    lBA = 0;
    lFluxOut = 0;
    ListIterator liP = llPlanktonic.listIterator();
    while (liP.hasNext()){
      lPA++;
      AgentPL agent = (AgentPL) liP.next();
      if (agent.getSettlementState()) {
        lBA++;
        llBenthic.add(agent);                      <--larva settled, so add
agent to Benthic list
        liP.remove();                              <--remove it from
Planktonic list
      }
      if (agent.getOnGrid () == 0) {
        lFluxOut++;
        liP.remove();                              <--larva exited grid, so
remove from list
        agent.drop();                              <--and drop
        agent = null;
      }
    }

    // createAgents(20);
    createAgentFlux();                             <--create new larva
(those entering the grid, see next snippet)

    pdMap.computeMap(llPlanktonic);
    bdMap.computeMap(llBenthic);
    return this;
  }  // public Object stepRule ()
//--------------------------------------------------------------------------
--------

Sniippet for agent creation during model run:

  /** Create agents and assign them to plankton list */
  private Object createAgentFlux() {
    //System.out.println ("Creating flux of AgentPLs\n");
    int i, j, x, y;
    double dMn;
    double r;
    Poisson rPoissonX,rPoissonY;

    // create flux in y direction
    // compute the expected number of agents crossing unit length in x in
time dDT
    dMn = Math.abs(dPlanktonicDensity*dVy*dDT);

    // compute the probability associated with 0-5 agents crossing
    rPoissonY = new Poisson(dMn,0,5);

    y = 0;
    if (dVy < 0) {
      y = iWorldY;
    }
    lFluxInY = 0;
    for (x = 0; x < iWorldX; x++) {
      // determine number of agents entering model space in cell x,y through
      // the outer y face (using a Poisson distribution)
      i = rPoissonY.getRnd();
      lFluxInY = lFluxInY + (long) i;
      for (j=1; j<=i; j++) {
        // Create the agent, with a standard Java constructor
        AgentPL agent =  new AgentPL ();

        // Add the agent to the end of the planktonic list.
        llPlanktonic.add (agent);

        // Now initialize the rest of the agent's state.
        agent.setDrawColor((byte) 2);
        agent.setTimeStep(dDT);
        agent.setBenthicMap(bhMap);
        agent.setHorizontalAdvection(dVx,dVy);
        agent.setDepth(dDepth);
        agent.setVerticalMotion(dSwim,dSink);
        agent.setDelays(dDelayAtBottom,dDelayAtSurface);
        agent.setSettlementProbabilities(dProbSettleCobble,dProbSettleSand);
        r = Globals.env.uniformDblRand.getDoubleWithMin$withMax (0.0, 1.0);
        agent.setVerticalState(r);
        r = Globals.env.uniformDblRand.getDoubleWithMin$withMax (0.0, 1.0);
        dPosX = ((double) x) + r;
        r = Globals.env.uniformDblRand.getDoubleWithMin$withMax (0.0, 1.0);
        dPosY = ((double) y) + dVy*dDT*r;
        agent.setHorizontalPosition(dPosX, dPosY);
      }
    }

    // create flux in x direction
    // compute the expected number of agents crossing unit length in y in
time dDT
    dMn = Math.abs(dPlanktonicDensity*dVx*dDT);

    // compute the probability associated with 0-5 agents crossing
    rPoissonX = new Poisson(dMn,0,5);

    x = 0;
    if (dVx < 0) {
      x = iWorldX;
    }
    lFluxInX = 0;
    for (y = 0; y < iWorldY; y++) {
      // determine number of agents entering model space in cell x,y through
      // the outer y face (using a Poisson distribution)
      i = rPoissonX.getRnd();
      lFluxInX = lFluxInX + (long) i;
      for (j=1; j<=i; j++) {
        // Create the agent, with a standard Java constructor
        AgentPL agent =  new AgentPL ();
<--create new agents

        // Add the agent to the end of the planktonic list.
        llPlanktonic.add (agent);
<--add to Plankton list

        // Now initialize the rest of the agent's state.
        agent.setDrawColor((byte) 2);
        agent.setTimeStep(dDT);
        agent.setBenthicMap(bhMap);
        agent.setHorizontalAdvection(dVx,dVy);
        agent.setDepth(dDepth);
        agent.setVerticalMotion(dSwim,dSink);
        agent.setDelays(dDelayAtBottom,dDelayAtSurface);
        agent.setSettlementProbabilities(dProbSettleCobble,dProbSettleSand);
        r = Globals.env.uniformDblRand.getDoubleWithMin$withMax (0.0, 1.0);
        agent.setVerticalState(r);
        r = Globals.env.uniformDblRand.getDoubleWithMin$withMax (0.0, 1.0);
        dPosX = ((double) x) + dVx*dDT*r;
        r = Globals.env.uniformDblRand.getDoubleWithMin$withMax (0.0, 1.0);
        dPosY = ((double) y) + r;
        agent.setHorizontalPosition(dPosX, dPosY);
      }
    }
    System.out.println ("Agent flux = ["+lFluxInX+","+lFluxInY+"]");
    return this;
  } // private Object createAgentFlux()
//--------------------------------------------------------------------------
--------

Hope this helps.


Buck Stockhausen
***********************************************************************
*William T. Stockhausen, PhD                e-mail: address@hidden     *
*Senior Marine Scientist                    voice : 804-684-7643      *
*Virginia Institute of Marine Science       fax   : 804-684-7250      *
*College of William and Mary                http://www.vims.edu/~buck *
*Greate Road                                                          *
*Gloucester Point, VA 23062-1346                                      *
***********************************************************************

-----Original Message-----
From: address@hidden
[mailto:address@hidden Behalf Of Hu, Jianjun
Sent: Wednesday, June 26, 2002 1:13 PM
To: address@hidden
Subject: RE: Is this the defects of Javaswarm? the swarm kernel neglect
new added agents!



Then I am wondering why when I started the simulation and during running,
when I add new agents,
these agents were just  neglected by the swarm kernel.
        tadpoleLayer.agentList.add(newTadpole );
Do you have any java example that you add or remove agents during a
simulation?

thanks

Jianjun







-----Original Message-----
From: address@hidden
[mailto:address@hidden Behalf Of Paul E Johnson
Sent: Wednesday, June 26, 2002 11:51 AM
To: address@hidden
Subject: Re: Is this the defects of Javaswarm? the swarm kernel neglect new
added agents!


In Objective-C, I have no trouble with createActionForEach usages when
the agent list changes. Have  you tried any diagnostics to make sure
your list of agents does actually change?  I bet that is where the
problem is.  If you remove an agent from a list, and drop that agent,
then if swarm tries to do something to that agent, it should find a nil
and the whole sim should abort.

Now, I'm remembering a problem from Swarm-2.1 java and maybe it would
account for your trouble. I discovered this in January, 2001, and I
think the word "yell" is in my swarm-support email trial. IT has since
been fixed, but here was the deal. If you had a createActionForEach and
it was supposed to go through a list, it would quit as soon as it found
a nil and it did not issue a warning message.  I found several lists
partially processed.  But if you are using a current Swarm, that does
not happen.

Hu, Jianjun wrote:
> Thanks Paul.
>
> I think one solution maybe swarm kernel exposes its iterator to the user,
> then we can use this iterator to update the agent list.
> and then the swarm kernel can track the changes of agents.
>
> Jianjun Hu
>

--
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
1541 Lilac Lane, Rm 504
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66044-3177           FAX: (785) 864-5700


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



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


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



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