swarm-support
[Top][All Lists]
Advanced

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

Re: [Swarm-Support] Multiple Agents in One Cell


From: Crile Doscher
Subject: Re: [Swarm-Support] Multiple Agents in One Cell
Date: Thu, 25 Aug 2005 12:35:39 -0400

Thanks for that, Steve. That's an excellent point about using two separate space objects. That might be a relatively easy way around this. Below I'll try to post some of the code that *I think* is relevant - I'll admit to being a bit unsure about what will be most helpful to post. Is there a better way to post this? A tarball perhaps? Nonetheless, a brief summary of what's below:

Selection from Cell.m: -createEnd and -createCollection - a la Opinion, the list is created here at the end of cell creation. Plus -set and -getElevLevel, elevlevel being the elevation value, and -insertItem, borrowed from MultiGrid2d to insert an agent into a cell's list.

Selections from CellSurface.m: -fillWithCells (adds data to cells) and -findSteepestSlope (determines the best cell for the primary agent to move to - where the current failure seems to occur)

Selection from DataFile.m: -inputData (reads in elevation values from an ascii raster grid file)

Selected code from Cell.m:
//Cell.m a swarmobject that fills in each point in the cellSurface lattice

//cell objects hold variables for GIS data layers. They can change these

//variables as well as communicate with neighboring cells in the lattice.

//cells can also implement cellular automata-like functions.

//Cell.m is based on Paul Box's Biox model

//changes made by Crile Doscher, September 04, for Drop

#import "Cell.h"

#import "ModelSwarm.h"

@implementation Cell

- createEnd

{

   [self createCollection];  //This creates the list

   return [super createEnd];

}

- (id) createCollection

{

   //Borrowed from MulitGrid2d

   list = [List create: [self getZone]];

   return list;

}

-step {

   // here put things that all cells do at each time step. Especially,

   // place here functions that involve doing things and sending

   // messages to neighbor cells

   return self;

}

/////////////////////////////////////////////////////////////////////////

//here is where we put methods for setting and retrieving variables

//specific to our model. Replace these with your own

-setElevLevel: (float) e { //ElevLevel is the elevation value in each cell

   elevLevel = e;

   //printf("Cell.m: elevLevel = %f\n", elevLevel);

   return self;

}

   -(float) getElevLevel {

   return elevLevel;

}



///////////////////////////////////////////////////////////////////////////

- (BOOL) insertItem: obj //19 August 05 - CD added to get agents into lists

   {

       [list addFirst: obj];

       return YES;

   }

@end

From CellSurface.m - this method creates the cells

-fillWithCells {

   int incx, incy;

   Cell * cell;


   printf ("Initializing the lattice...\n\n ");


   cellList = [List create: [self getZone]];

//first create empty cell objects and place them in the lattice

   for (incy = 0; incy < ysize; incy++)

       for (incx = 0; incx < xsize; incx++) {


           // allocate memory for a temporary cell

           cell = [Cell createBegin: [self getZone]];

           //tell the cell where it is

           [cell setModel: modelSwarm andCellSurface: self];

           [cell setX: incx Y: incy];

           cell = [cell createEnd];


           //place the cell at its point in the lattice

           [self putObject: cell atX: incx Y: incy];

           [cellList addLast: cell];


       }

printf("cellSurface, created %d x %d cells.\n", ysize, xsize);

return self;

}

Also from CellSurface.m - this method determines which cell is the best one to move to - roughly based on HeatBugs movement

//New method to find the steepest slope adapted from Heatbugs. Search the 9 cell
//neighbourhood for the steepest slope.

- (ElevValue)findSteepestSlopeAtX: (int *) px Y: (int *) py

   {

       float maxSlope;

       float elevHere;

       int x, y;

       id <List>slopeList;

       id cell, bestCell;

       int offset;

       printf("Inside findSteepestSlopeAtX now\n");

       elevHere = [[self getCellAtX: *px Y: *py] getElevLevel];

       printf("CellSurface.m: x = %d, y = %d\n", *px, *py);

       printf("CellSurface.m: elevHere = %f\n\n", elevHere);

       maxSlope = 0; //not sure if this is the best place to do this...

       //Now scan through the world, finding the slopes to the 8 cell

       //neighbours, determing the best, or the best equal

       slopeList = [List create: [self getZone]];

       for (y = *py -1; y <= *py + 1; y++) {

           for (x = *px - 1; x <= *px + 1; x++) {

               float elevThere;

               float slopeValue;

               elevThere = [[self getCellAtX: x Y: y] getElevLevel];

               //elevThere = (int) elevThere;

//elevThere = [self getValueAtX: (x+xsize)%xsize Y: (y+ysize)%ysize];

               printf ("@ (%d, %d) = %f ", x, y, elevThere);

if (x == *px || y == *py) { //if the cell we're in is N,S,E or W, don't divide by 1.414

               slopeValue = elevHere - elevThere;

printf("CellSurface.m:slopeValue = %f (loop1)\n", slopeValue);

           }

       else

           {

slopeValue = (elevHere - elevThere)/1.414; //if the cell is on the diagonal, divide by 1.414

printf("CellSurface.m:slopeValue = %f (loop2)\n", slopeValue);

           }

       if (slopeValue > maxSlope) {

       cell = [[[ElevCell create: [self getZone]] setX:x] setY:y];

       [slopeList deleteAll];

       [slopeList addLast: cell];

       maxSlope = slopeValue;

   }


           printf("CellSuface.m:maxSlope = %f\n", maxSlope);

           if (slopeValue == maxSlope) {

           cell = [[[ElevCell create: [self getZone]] setX: x] setY: y];

           [slopeList addLast: cell];

       } //close else loop

   } //close x loop

} //close y loop

Code below deleted for brevity...

From DataFile.m - this method imports elevation values from the ascii raster
grid file

-inputData: (int) f {

   int incx, incy, x, y;

   int data1, result; //for debugging

   //float data2;

float nzmgX, nzmgY; //nzmg = New Zealand Map Grid - local coordinate system



   //id list; //needed to create list in this method 24/8/05


   fileID = f;


   printf ("Importing data from fileID:%d %s..\n", fileID, fileName);


   // first put nodatavalues in all the cells. Can be overwritten in

   //the next steps if a cell contains information

   printf("Putting nodata values in all the cells. fileID = %d\n", fileID);


   //printf("cellSurface = %s\n",[cellSurface getInstanceName]);

   for (incy = 0; incy < ysize; incy++){

       for (incx = 0; incx < xsize; incx++) {

           if (fileID == 0)

           [[cellSurface getCellAtX: incx Y: incy]

           setElevLevel: noDataValue];   //defined in cell.hm

data1 = [[cellSurface getCellAtX: incx Y: incy] getElevLevel]; //next three lines her for diagnostics

printf("Elevation here (data1) = %d\n", data1); //This returns 2147483648 consistently

           printf("noDataValue = %d\n", noDataValue);

           }

       }


      printf ("Scanning through file for data points\n");

       for (incy=0; incy <numRows; incy++) {

          for (incx=0; incx < numCols; incx++) {


//scan the datapoint - datapoint is the elevation value from the ascii file

               float datapoint;

               int datapoint2;

               datapoint = [self getDataPoint];

               //printf("datapoint = %f\n", datapoint);

               //datapoint2 = (int) datapoint;

               //printf("datapoint2 = %d\n", datapoint2);

               //if its within the space, convert the coords to swarm

               //and input the data



               if (fileID == 0)

[[cellSurface getCellAtX: x Y: y] setElevLevel: dataPoint];

data2 = [[cellSurface getCellAtX: x Y: y] getElevLevel]; //more diagnostics here

//printf("Elevation here (data2) = %f\n", data2); //This currently returning "NaN"



//Testing out list creation here instead of in Cell: -createEnd

//list = [List create: [self getZone]]; //implmenting this here hasn't helped

           }

       //else if not within space, do nothing but scan the

       //next datapoint

       // move ahead one cell

       //printf ("Still in the inputData method...");

       nzmgX = nzmgX + cellSize;

       //printf("x = %f, y = %f\n", nzmgX, nzmgY);

     }

}

//end else (data overlaps main data set)

printf ("successfully scanned data file\n\n");


return self;

}

----- Original Message ----- From: "Steve Railsback" <address@hidden>
To: "Swarm Support" <address@hidden>
Sent: Thursday, August 25, 2005 11:24 AM
Subject: Re: [Swarm-Support] Multiple Agents in One Cell


Crile Doscher wrote:
Hi there SwarmList,
...

So, have I been sloppy in creating my lists? My gut feeling is that my cells should be able to hold both elevations and lists and I simply haven't implemented this well. Could the order in which the list is added and elevation values read in be causing a problem? Any thoughts?

Crile: I think you will have to post your cell code and the methods that call it for anyone to help.

Also: Remember that you can use two separate space objects. If your Discrete2D holding elevations works already, you could just leave it alone and create a separate MultiGrid2d to keep the agents. The agents would then look in the elevation space to get their elevation.

Steve R.

--
Lang Railsback & Assoc.
250 California Ave.
Arcata, California 95521
(707) 822-0453
_______________________________________________
Support mailing list
address@hidden
http://www.swarm.org/mailman/listinfo/support




reply via email to

[Prev in Thread] Current Thread [Next in Thread]