swarm-support
[Top][All Lists]
Advanced

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

Re: Synchronous scheduling


From: Marcus G. Daniels
Subject: Re: Synchronous scheduling
Date: 22 Apr 2001 19:00:54 -0600
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.7

>>>>> "HJY" == Ho Jung Yoo <address@hidden> writes:

HJY> Yes, the model will eventually have stage structure and spatial
HJY> structure, such that individuals advance through 3 stages with
HJY> one dispersal event in the interim, before reproducing and dying
HJY> (if they live that long).  So, ultimately, I will probably need
HJY> to keep track of individuals as agents. 

MGD> I would suggest you have a `nextGenerationMothList' list that you
MGD> build, and assign to the `mothList' variable when it is time for
MGD> the next generation to start.

HJY> Can you give an example of how to tell mothList when the next
HJY> generation should start? 

I was thinking of something like this... 

#import <simtools.h>
#import <objectbase/SwarmObject.h>
#import <objectbase/Swarm.h>
#import <collections.h>

id <RandomBitDist> randomBitDist;

#define COUNT 20

@interface Bug: SwarmObject
{
  unsigned sn;
}
+ create: aZone setSn: (unsigned)sn;
@end

@implementation Bug
+ create: aZone setSn: (unsigned)theSn
{
  Bug *obj = [self create: aZone];

  obj->sn = theSn;
  return obj;
}

- (void)live: (id <List>)nextGenerationList
{
  if ([randomBitDist getCoinToss])
    {
      printf (" %02u", sn); fflush (stdout);
      [nextGenerationList addLast: self];
    }
}

@end

@interface ModelSwarm: Swarm
{
  id <List> populationList;
  id <List> nextGenerationList;

  id <Schedule> reproduceSchedule;
  id <Schedule> generationSchedule;

}
- buildObjects;
- buildActions;
- (void)reproduce;
- (void)nextGeneration;
@end

@implementation ModelSwarm
- buildObjects
{
  [super buildObjects];

  populationList = [List create: self];

  {
    unsigned i;

    for (i = 0; i < COUNT; i++)
      [populationList addLast: [Bug create: self setSn: i]];
  }

  nextGenerationList = [List create: self];

  return self;
}

- (void)reproduce
{
  id <Index> index = [populationList beginPermuted: scratchZone];
  id bug;

  printf ("Survivors: ");

  for (bug = [index next]; [index getLoc] == Member; bug = [index next])
    [bug live: nextGenerationList];
    
  [index drop];
  printf ("\n");
}

- (void)nextGeneration
{
  id <List> temp;
  unsigned count = [nextGenerationList getCount];

  printf ("Swapping nextGenerationList and populationList, count: %u\n",
          count);
  [populationList removeAll];
  temp = nextGenerationList;
  nextGenerationList = populationList;
  populationList = temp;

  if (count == 0)
    [getCurrentSwarmActivity () terminate];
}

- buildActions
{
  [super buildActions];

  reproduceSchedule = [Schedule create: self setRepeatInterval: 1];
  generationSchedule = [Schedule create: self setRepeatInterval: 1];

  [reproduceSchedule at: 0
                     createActionTo: self
                     message: M(reproduce)];

  [generationSchedule at: 0
                      createActionTo: self
                      message: M(nextGeneration)];

  return self;
}

- (id <Activity>)activateIn: (id <Swarm>)swarm
{
  [super activateIn: swarm];
  
  [reproduceSchedule activateIn: self];
  [generationSchedule activateIn: self];

  return [self getActivity];
}

@end

int
main (int argc, const char **argv)
{
  initSwarmBatch (argc, argv);

  randomBitDist = [RandomBitDist create: globalZone
                                 setGenerator: randomGenerator];
  {
    id <Swarm> modelSwarm = [ModelSwarm create: globalZone];

    [modelSwarm buildObjects];
    [modelSwarm buildActions];
    
    [[modelSwarm activateIn: nil] run];
  }

  return 0;
}

/*
Local Variables:
compile-command: "$SWARMHOME/bin/libtool-swarm --mode=link gcc -D_GNU_SOURCE -o 
GenerationSwap -O2 -g -Wno-import -I$SWARMHOME/include/swarm 
-L$SWARMHOME/lib/swarm GenerationSwap.m -lswarm -lobjc -ldl -lc"
End:
*/

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