swarm-modeling
[Top][All Lists]
Advanced

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

Re: Caution on Boids


From: Marcus G. Daniels
Subject: Re: Caution on Boids
Date: 07 Apr 2000 16:35:57 -0700
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4

>>>>> "RB" == Robert Bell <address@hidden> writes:

RB> Any idea where the problem is? 

Note: I'm am *not* volunteering to maintain this app.

1. PriorityQueue has leaks.  I avoided the leaks by putting them in a
   temporary workspace zone.

2. A calculation object in [Boid -visible:] was not dropped.

3. An index wasn't dropped in [Polygon -createItem].

4. The Tcl/Tk coordinate string was not dropped in [Polygon -erase].

Other problems:

A. +create: was used in several places where +createBegin: was called for.

B. Lack of handling for missing nodes in a PriorityQueue, resulting in
   nil_method exceptions.

diff -w -c boid/Boid.h boid-new/Boid.h
*** boid/Boid.h Wed Jun  2 11:47:48 1999
--- boid-new/Boid.h     Fri Apr  7 17:01:32 2000
***************
*** 12,20 ****
    double cruiseDistance;    // [m] Default value is twice the bodylength.
  
    PriorityQueue *visibleBoids[NUMOBJECTTYPES]; // known objects sorted by 
distance
  }
  
! + (Boid *)create: aZone;
  - createEnd;
  - init: (int) id : (int) type
        : (id <Vector>) p
--- 12,21 ----
    double cruiseDistance;    // [m] Default value is twice the bodylength.
  
    PriorityQueue *visibleBoids[NUMOBJECTTYPES]; // known objects sorted by 
distance
+   id queueZone;
  }
  
! + (Boid *)createBegin: aZone;
  - createEnd;
  - init: (int) id : (int) type
        : (id <Vector>) p
diff -w -c boid/Boid.m boid-new/Boid.m
*** boid/Boid.m Thu Nov 11 10:24:29 1999
--- boid-new/Boid.m     Fri Apr  7 17:02:00 2000
***************
*** 6,27 ****
  
  @implementation Boid
  
! + (Boid *)create: aZone
  {
    int i;
!   Boid *obj=[super create: aZone];
  
    obj->acceleration = [Vector create: aZone]; // initially {0,0}
    obj->flockCenter  = [Vector create: aZone];
  
-   for (i=0; i<NUMOBJECTTYPES; i++)
-     obj->visibleBoids[i] = [PriorityQueue create: aZone];
    return obj;
  }
  
  - (void) drop
  {
    int i;
    for (i=0; i<NUMOBJECTTYPES; i++)
      [visibleBoids[i] drop];
    
--- 6,39 ----
  
  @implementation Boid
  
! static void
! replaceQueues (Boid *obj)
  {
    int i;
! 
!   if (obj->queueZone)
!     [obj->queueZone drop];
! 
!   obj->queueZone = [Zone create: [obj getZone]];
!   for (i=0; i<NUMOBJECTTYPES; i++)
!     obj->visibleBoids[i] = [PriorityQueue create: obj->queueZone];
! }
! 
! + (Boid *)createBegin: aZone
! {
!   Boid *obj=[super createBegin: aZone];
  
    obj->acceleration = [Vector create: aZone]; // initially {0,0}
    obj->flockCenter  = [Vector create: aZone];
+   replaceQueues (obj);
   
    return obj;
  }
  
  - (void) drop
  {
    int i;
+ 
    for (i=0; i<NUMOBJECTTYPES; i++)
      [visibleBoids[i] drop];
    
***************
*** 63,82 ****
      dx = [calculation getX]; if (dx<0) dx *= -1.0;
      dy = [calculation getY]; if (dy<0) dy *= -1.0;
      
!     if (dx>worldX/2) {
        if ([calc getX] < [position getX])
        [calc setX: [calc getX] + worldX];
        else
        [calc setX: [calc getX] - worldX];
      }
!     if (dy>worldY/2) {
        if ([calc getY] < [position getY])
        [calc setY: [calc getY] + worldY];
        else
        [calc setY: [calc getY] - worldY];
      }
      [[calculation init: calc] sub: position];
      res = [calculation getLength];
    }
    if (res > cruiseDistance &&
        [calculation angle: velocity] > 1.0471967) // pi/3 radians is our FOV
--- 75,99 ----
      dx = [calculation getX]; if (dx<0) dx *= -1.0;
      dy = [calculation getY]; if (dy<0) dy *= -1.0;
      
!     if (dx>worldX/2)
!       {
          if ([calc getX] < [position getX])
            [calc setX: [calc getX] + worldX];
          else
          [calc setX: [calc getX] - worldX];
        }
!     
!     if (dy>worldY/2)
!       {
          if ([calc getY] < [position getY])
            [calc setY: [calc getY] + worldY];
          else
            [calc setY: [calc getY] - worldY];
        }
+     
      [[calculation init: calc] sub: position];
      res = [calculation getLength];
+     [calc drop];
    }
    if (res > cruiseDistance &&
        [calculation angle: velocity] > 1.0471967) // pi/3 radians is our FOV
***************
*** 98,123 ****
  
  - updateVisibilityList
  {
-   id <Index> index;
-   SimObject *member;
-   int i, bType;
-   double dist;
    //  printf("Boid\tupdateVisibilityList\n");
  
!   index  = [[model getObjectList] begin: [self getZone]];
  
-   for (i=0; i<NUMOBJECTTYPES; i++)
-     [visibleBoids[i] removeAll];
    [flockCenter init];
  
    for (member = [index next]; [index getLoc] == Member; member = [index next])
!     if (member != self && (dist = [self visible: member])>0.0) {
!       bType = [member getObjectType];
        [visibleBoids[bType] addObject: member with: dist];
        if (bType == objectType)
        [flockCenter add: [member getPosition]];
      }
    [index drop];
  
    if( [visibleBoids[objectType] getCount] )
      [flockCenter div: (double) [visibleBoids[objectType] getCount]];
--- 115,146 ----
  
  - updateVisibilityList
  {
    //  printf("Boid\tupdateVisibilityList\n");
  
!   replaceQueues (self);
  
    [flockCenter init];
  
+   {
+     SimObject *member;
+     id <Index> index;
+ 
+     index  = [[model getObjectList] begin: [self getZone]];
      for (member = [index next]; [index getLoc] == Member; member = [index 
next]) 
!       {
!         double dist;
! 
!         if (member != self && (dist = [self visible: member])>0.0)
!           {
!             int bType = [member getObjectType];
! 
              [visibleBoids[bType] addObject: member with: dist];
              if (bType == objectType)
                [flockCenter add: [member getPosition]];
            }
+       }
      [index drop];
+   }
  
    if( [visibleBoids[objectType] getCount] )
      [flockCenter div: (double) [visibleBoids[objectType] getCount]];
***************
*** 186,192 ****
  
    [calculation init];
  
!   if ((obs = [visibleBoids[OBSTACLE] getLast]) != nil) {
      // Obstacle with lowest priority == closest seen
    
      mag = 1.0 - [obs avoid: position with: calculation]/[self getProbeLength];
--- 209,215 ----
  
    [calculation init];
  
!   if ((obs = [visibleBoids[OBSTACLE] getLast])) {
      // Obstacle with lowest priority == closest seen
    
      mag = 1.0 - [obs avoid: position with: calculation]/[self getProbeLength];
***************
*** 210,216 ****
    double dist;
  
    [calculation init];
!   if ((boid = [visibleBoids[objectType] getLast]) != nil) {
      // Boid with lowest priority == closest seen
      [[calculation init: [boid getPosition]] sub: position];
      dist = [calculation getLength];
--- 233,239 ----
    double dist;
  
    [calculation init];
!   if ((boid = [visibleBoids[objectType] getLast])) {
      // Boid with lowest priority == closest seen
      [[calculation init: [boid getPosition]] sub: position];
      dist = [calculation getLength];
***************
*** 228,234 ****
    Boid *boid;   
  
    [calculation init];
!   if ((boid = [visibleBoids[objectType] getLast]) != nil) {
      // Boid with lowest priority == closest seen
      [calculation init: [boid getVelocity]];
      [calculation mult: 0.05]; // importance factor of velocityMatching
--- 251,257 ----
    Boid *boid;   
  
    [calculation init];
!   if ((boid = [visibleBoids[objectType] getLast])) {
      // Boid with lowest priority == closest seen
      [calculation init: [boid getVelocity]];
      [calculation mult: 0.05]; // importance factor of velocityMatching
***************
*** 258,264 ****
    switch (objectType) {
    default:
    case PREY:
!     if ((obj = [visibleBoids[PREDATOR] getLast]) != nil) { // lowest priority 
== closest seen
        dist = [self visible: obj];
        [[calculation init: position] sub: [obj getPosition]];
        if (dist < 0) { // pretty far away
--- 281,287 ----
    switch (objectType) {
    default:
    case PREY:
!     if ((obj = [visibleBoids[PREDATOR] getLast])) { // lowest priority == 
closest seen
        dist = [self visible: obj];
        [[calculation init: position] sub: [obj getPosition]];
        if (dist < 0) { // pretty far away
***************
*** 270,276 ****
      }
      break;
    case PREDATOR:
!     if ((obj = [visibleBoids[PREY] getLast]) != nil) { // lowest priority == 
closest seen
        [[calculation init: [obj getPosition]] sub: position]; // vector 
towards PREY
        mag = 1.2 * (1.0 - [calculation getLength]/[self getProbeLength]);
        [calculation setLength: mag];
--- 293,299 ----
      }
      break;
    case PREDATOR:
!     if ((obj = [visibleBoids[PREY] getLast])) { // lowest priority == closest 
seen
        [[calculation init: [obj getPosition]] sub: position]; // vector 
towards PREY
        mag = 1.2 * (1.0 - [calculation getLength]/[self getProbeLength]);
        [calculation setLength: mag];
***************
*** 287,292 ****
--- 310,316 ----
    //  double dt = 1.0;
    
    //  [position print];
+ 
    [super step];     // p' = p0 + v0 * dt
    [self navigator]; // calculate new acceleration vector
  
diff -w -c boid/ModelSwarm.m boid-new/ModelSwarm.m
*** boid/ModelSwarm.m   Thu Nov 11 10:16:27 1999
--- boid-new/ModelSwarm.m       Fri Apr  7 15:50:41 2000
***************
*** 54,60 ****
    //  printf ("ModelSwarm\tbuildObjects\n");
  
    [super buildObjects];
- 
    // build the list to keep track of the agents
    objectList  = [List create: self];
  
--- 54,59 ----
***************
*** 71,77 ****
      if (type == PREDATOR)
        type = [uniformIntRand getIntegerWithMin: 0 withMax: NUMBOIDTYPES-1];
  
!     agent = [Boid create: self];
      [agent init: ++agentID : type
           : [position init: x : y]
           : [velocity init: vx : vy]
--- 70,76 ----
      if (type == PREDATOR)
        type = [uniformIntRand getIntegerWithMin: 0 withMax: NUMBOIDTYPES-1];
  
!     agent = [Boid createBegin: self];
      [agent init: ++agentID : type
           : [position init: x : y]
           : [velocity init: vx : vy]
***************
*** 81,101 ****
      [objectList addLast: agent];
    }
    [velocity init]; // reset to {0.0, 0.0}
!   agent = [SimObject create: self];
    [agent init: ++agentID : OBSTACLE
         : [position init: worldXSize/4 : worldYSize/2.0] : velocity : 20.0];
    [agent initModel: self];
    [agent createEnd];
    [objectList addLast: agent];
    
!   agent = [SimObject create: self];
    [agent init: ++agentID : OBSTACLE
         : [position init: worldXSize/2 : worldYSize/4.0] : velocity : 20.0];
    [agent initModel: self];
    [agent createEnd];
    [objectList addLast: agent];
    
!   agent = [SimObject create: self];
    [agent init: ++agentID : OBSTACLE
         : [position init: worldXSize/2 : worldYSize/2.0] : velocity : 20.0];
    [agent initModel: self];
--- 80,100 ----
      [objectList addLast: agent];
    }
    [velocity init]; // reset to {0.0, 0.0}
!   agent = [SimObject createBegin: self];
    [agent init: ++agentID : OBSTACLE
         : [position init: worldXSize/4 : worldYSize/2.0] : velocity : 20.0];
    [agent initModel: self];
    [agent createEnd];
    [objectList addLast: agent];
    
!   agent = [SimObject createBegin: self];
    [agent init: ++agentID : OBSTACLE
         : [position init: worldXSize/2 : worldYSize/4.0] : velocity : 20.0];
    [agent initModel: self];
    [agent createEnd];
    [objectList addLast: agent];
    
!   agent = [SimObject createBegin: self];
    [agent init: ++agentID : OBSTACLE
         : [position init: worldXSize/2 : worldYSize/2.0] : velocity : 20.0];
    [agent initModel: self];
diff -w -c boid/Polygon.m boid-new/Polygon.m
*** boid/Polygon.m      Wed Jun  2 13:50:51 1999
--- boid-new/Polygon.m  Fri Apr  7 16:02:34 2000
***************
*** 30,35 ****
--- 30,36 ----
        sprintf (eval, "%s %.0f %.0f", eval, [vector getX], [vector getY]);
      }
      [globalTkInterp eval: eval];
+     [index drop];
  
      item = strdup ([globalTkInterp result]);
    }
***************
*** 39,45 ****
--- 40,50 ----
  - erase
  {
    if (item)
+     {
        [globalTkInterp eval: "%s delete %s", [canvas getWidgetName], item];  
+       XFREE (item);
+       [vectorList deleteAll];
+     }
    return self;
  }
   
diff -w -c boid/PriorityQueue.m boid-new/PriorityQueue.m
*** boid/PriorityQueue.m        Wed Jun  2 11:47:49 1999
--- boid-new/PriorityQueue.m    Fri Apr  7 16:34:05 2000
***************
*** 71,81 ****
      // aHigher->parent == self
      // aHigher->higher->parent == aHigher
      lower->size =
!       (int) [lower->lower  getCount] +
!       (int) [lower->higher getCount] + 1;
      size =
!       (int) [lower  getCount] +
!       (int) [higher getCount] + 1;
    }
    return self;
  }
--- 71,81 ----
      // aHigher->parent == self
      // aHigher->higher->parent == aHigher
      lower->size =
!       (lower->lower ? [lower->lower  getCount] : 0) +
!       (lower->higher ? [lower->higher getCount] : 0) + 1;
      size =
!       (lower ? [lower  getCount] : 0) +
!       (higher ? [higher getCount] : 0) + 1;
    }
    return self;
  }
***************
*** 107,117 ****
      // aLower->parent == self
      // aLower->lower->parent == aLower
      higher->size =
!       (int) [higher->lower  getCount] +
!       (int) [higher->higher getCount] + 1;
      size =
!       (int) [lower  getCount] +
!       (int) [higher getCount] + 1;
    }
    return self;
  }
--- 107,117 ----
      // aLower->parent == self
      // aLower->lower->parent == aLower
      higher->size =
!       (higher->lower ? [higher->lower  getCount] : 0) +
!       (higher->higher ? [higher->higher getCount] : 0) + 1;
      size =
!       (lower ? [lower  getCount] : 0) +
!       (higher ? [higher getCount] : 0) + 1;
    }
    return self;
  }
diff -w -c boid/SimObject.h boid-new/SimObject.h
*** boid/SimObject.h    Wed Jun  2 11:47:49 1999
--- boid-new/SimObject.h        Fri Apr  7 15:34:52 2000
***************
*** 36,42 ****
    double radius;
  }
  
! + (SimObject *)create: aZone;
  - createEnd;
  - (void) drop;
  
--- 36,42 ----
    double radius;
  }
  
! + (SimObject *)createBegin: aZone;
  - createEnd;
  - (void) drop;
  
diff -w -c boid/SimObject.m boid-new/SimObject.m
*** boid/SimObject.m    Thu Nov 11 10:16:55 1999
--- boid-new/SimObject.m        Fri Apr  7 15:59:33 2000
***************
*** 8,16 ****
  
  @implementation SimObject
  
! + (SimObject *)create: aZone
  {
!   SimObject *obj=[super create:aZone];
    
    obj->calculation= [Vector create: aZone];
    obj->oldPosition= [Vector create: aZone];
--- 8,16 ----
  
  @implementation SimObject
  
! + (SimObject *)createBegin: aZone
  {
!   SimObject *obj=[super createBegin:aZone];
    
    obj->calculation= [Vector create: aZone];
    obj->oldPosition= [Vector create: aZone];


                  ==================================
   Swarm-Modelling is for discussion of Simulation and Modelling techniques
   esp. using 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]