swarm-support
[Top][All Lists]
Advanced

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

Re: Activity bug or Paul's mistake?


From: Benedikt Stefansson
Subject: Re: Activity bug or Paul's mistake?
Date: Fri, 03 Mar 2000 13:07:01 -0700

Conceptually, each object that is referenced by a schedule must exist at
the time you pass the (pointer to) the object to the schedule. That only
stands to reason.

If you are sticking with your strategy of creating the action call to
agent2 at create time you have the option of creating a placeholder or
proxy for the agent which exists when the schedule is created.

Sounds like a silly way to solve the problem so how about this, when you
create agent2 have the creator send a message to the schedule and schedule
future method calls  to agent2.

That is after all one of the main advantages of the action libraries
-schedules are inherently  dynamic, unlike for-next loops.

Regards,
Benedikt


Paul Johnson wrote:

> Dear everybody:
>
> I could direct this question just to Marcus, but I'm so proud of my work
> that I am imposing it on all of you (and the archive) too.
>
> Last week I came across a problem in a program and I thought it was
> happening because I did not understand how to pass objects by reference.
> But that had nothing to do with it.  After many hours of struggle, I've
> finally reproduced the problem in a smallish test program, that I'm
> including at the end of this note.
>
> The kernel of the problem is this.  Should I be able to put objects into
> a schedule when they are created/altered by previous steps in that
> schedule.  The ModelSwarm has 2 agents, agent and agent2.  The first is
> created in buildObjects, the second is created "on the fly" by the
> scheduled action #2 below.  That action does run, the printout to the
> console confirms that agent2 is created.  However, when item #4 runs, it
> sees agent2 as nil still.  This problem came up in another project and
> it has driven me crazy because I kept getting "frozen" objects in
> schedules.
>
> - buildActions
> {
>
>   [super buildActions];
>
>   schedule = [[[Schedule createBegin: self]
>                setRepeatInterval: 1]
>                createEnd];
>   [schedule at: 0 createActionTo: agent  message: M(printValue)];
>   [schedule at: 0  createActionTo: self message:
> M(createAgent:Arg:):agent2:(id)2];  //#2
>   [schedule at: 0 createActionTo: self message: M(updateAgent:):agent];
>   [schedule at: 0  createActionTo: self message:
> M(updateAgent:):agent2]; //#4
>   [schedule at: 0 createActionTo: agent  message: M(printValue)];
>   [schedule at: 0 createActionTo: agent2  message: M(printValue)];
>   return self;
> }
>
> Should this work or have I constructed a monster, or both?
>
> #import <simtools.h>
> #import <analysis.h>
> #import <defobj/Create.h>
> #import <objectbase.h>
> #import <simtoolsgui/GUISwarm.h>
> #import <random.h>
>
> @interface Agent: CreateDrop
> {
>   int ident;
>   double value[2];
> }
> - setAgentValue: (double*)value;
>
> - updateValue;
> - printValue;
> - (double)getValue1;
>
> - setID: (int)x;
>
> @end
>
> @implementation Agent
> - setAgentValue: (double*)theValue
> {
>   value[0] = theValue[0];
>   value[1] = theValue[1];
>   return self;
> }
>
> - updateValue
> {
>   int i;
>   for (i=0; i<2;i++)
>     {
>     value[i]= [uniformDblRand getDoubleWithMin: 0L withMax: 10];
>     }
>   return self;
> }
>
> -(double)getValue1
> {
>   return value[0];
> }
>
> -setID: (int)x
> {
>   ident=x;
>   return self;
> }
>
> - printValue
> {
>   int i;
>   printf("Agent %d value is\n", ident);
>   for(i=0; i<2; i++)
>     printf("%f ", value[i]);
>   printf("\n");
>   return self;
> }
>
> @end
>
> @interface ModelSwarm: Swarm
> {
>   id <Schedule> schedule;
>     id <List> list;
>   id agent, agent2, agent3, agent4;
> }
> - createEnd;
> - updateAgent: ag;
> - createAgent: ag Arg: (int) integ;
> - buildObjects;
> - buildActions;
>
> @end
>
> @implementation ModelSwarm
>
> #define AGENT(val) [[[Agent createBegin: globalZone] setAgentValue: val]
> createEnd]
>
> - createEnd
> {
>   [super createEnd];
>
>    return self;
> }
>
> -buildObjects
> {
>   int i;
>   double val[2];
>   [super buildObjects];
>   for(i=0;i<2;i++)
>     {
>     val[i]= [uniformDblRand getDoubleWithMin: 0L withMax: 10];
>     }
>
>   agent= AGENT ( val  );
>    [agent setID: 1];
>
>   for(i=0;i<2;i++)
>     val[i]= [uniformDblRand getDoubleWithMin: 0L withMax: 10];
>
>   return self;
> }
>
> -createAgent: ag Arg: (int) integ
> {
>   ag= [Agent create: self];
>   [ag setID: integ];
>
>   printf("Agent %d Exists, but has no value set yet.  Here is its xprint
> \n", integ);
>   xprint(ag);
>   printf("Here is Agent %d's response to the printValue
> message\n",integ);
>   [ag printValue];
>   printf("end of agent creation \n");
>   return self;
> }
>
> - updateAgent:ag
> {
>   int i;
>   double val[2];
>
>   printf("Update agent to: \n");
>
>    for(i=0;i<2;i++)
>     val[i]= [uniformDblRand getDoubleWithMin: 0L withMax: 10];
>    [ag setAgentValue: val];
>    [ag printValue];
>    return self;
> }
>
> - buildActions
> {
>   [super buildActions];
>
>   schedule = [[[Schedule createBegin: self]
>                setRepeatInterval: 1]
>                createEnd];
>   [schedule at: 0 createActionTo: agent  message: M(printValue)];
>   [schedule at: 0  createActionTo: self message:
> M(createAgent:Arg:):agent2:(id)2];
>   [schedule at: 0 createActionTo: self message: M(updateAgent:):agent];
>   [schedule at: 0  createActionTo: self message:
> M(updateAgent:):agent2];
>   [schedule at: 0 createActionTo: agent  message: M(printValue)];
>   [schedule at: 0 createActionTo: agent2  message: M(printValue)];
>   return self;
> }
>
> - activateIn: swarmContext
> {
>   [super activateIn: swarmContext];
>   [schedule activateIn: self];
>   return [self getActivity];
> }
> @end
>
> @interface ObserverSwarm: GUISwarm
> {
>   id modelSwarm;
> }
> +createBegin: aZone;
> -buildObjects;
> -buildActions;
> -activateIn: aZone;
> @end
>
> @implementation ObserverSwarm
>
> +createbegin: aZone
> {
>
>
>   return [super createBegin: aZone];
>
> }
>
> -buildObjects
> {
>   [super buildObjects];
>    modelSwarm=[ModelSwarm create: self];
>   [controlPanel setStateStopped];
>   [modelSwarm buildObjects];
>   return self;
> }
> -buildActions
> {
>   [super buildActions];
>   [modelSwarm buildActions];
>   return self;
> }
>
> -activateIn: aZone
> {
>   [super activateIn: aZone];
>   [modelSwarm activateIn: self];
>     return [self getActivity];
> }
>
> int
> main (int argc, const char **argv)
> {
>   id swarm;
>
>   initSwarm (argc, argv);
>
>   swarm = [ObserverSwarm create: globalZone];
>
>   [swarm buildObjects];
>   [swarm buildActions];
>   [swarm activateIn: nil];
>   [swarm go];
>
>   return 0;
> }
>
> /*
> Local Variables:
> compile-command: "$SWARMHOME/bin/libtool-swarm --mode=link gcc
> -D_GNU_SOURCE -o testapp -g -Wno-import \
>  -I$SWARMHOME/include -I$SWARMHOME/include/swarm -L$SWARMHOME/lib \
>  -L$SWARMHOME/lib/swarm PJActivityProblem.m -lswarm -lobjc "
> End:
> */
>
> --
> Paul E. Johnson                       email: address@hidden
> Dept. of Political Science           http://lark.cc.ukans.edu/~pauljohn
> University of Kansas                  Office: (785) 864-9086
> Lawrence, Kansas 66045                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.

--
Benedikt Stefansson      | address@hidden
CASA, Inc.               | Ph : (505) 988-8807 x101
Santa Fe, NM 87501       | Fax: (505) 988-3440




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