swarm-support
[Top][All Lists]
Advanced

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

Re: [Swarm-Support] List problem: many bugs accessing a single list


From: Paul Johnson
Subject: Re: [Swarm-Support] List problem: many bugs accessing a single list
Date: Tue, 27 Jul 2004 10:40:18 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040510

Dear Jurgen:

This can be done! Over the years I have asked student to do exercises like this, one of which is creating an Exterminator to kill bugs. Look in this folder under Exterminator or something like that.

http://lark.cc.ku.edu/~pauljohn/ps909/Agent-Based_Models

In your context, I don't think this should be trouble. But from your comments I can't tell exactly what is going wrong. Here's what I would try first.

First, you have the Cargo objects and when they are created (say, in ModelSwarm), you add them to a cargoList. Then inside each agent, there's

- (void)setCargoList: aList
{
    cargoList = aList;
}

And in the ModelSwarm, when the truck/bugs are created, one of the commands will be

  [aBug setCargoList: cargoList];

Note I use the same name "cargoList" for the IVAR in the ModelSwarm and in the Cargo class, because it makes my life simpler.

That way, each agent has access to the list.

Now, in the cargo object, add a variable "blocked" or something like that. In what you have below, I don't see such a thing. Just a YES or NO variable will do, say

BOOL blocked;  //a new IVAR

and you need these methods


- (void)setBlocked: (BOOL)val
{
        blocked = val;
}

- (BOOL)getBlocked
{
        return blocked;
}

Now, when some agent wants to grab a piece of cargo, it looks at one from the cargo list, and then asks that object if it is taken.

If you still have this "blocked" idea in mind, then I suppose an agent would have to iterate through the list until it finds a nonTaken item. I don't have the C manual in front of me, but I'd use a while loop to iterate like this in order to grab the first one:

{
   Cargo * anObject;
   id index = [cargoList begin: [self getZone]];
   BOOL foundOne = NO;
   while (foundOne == NO)
        {
            anObject = [index next];
            if ( [anObject getBlocked] == NO )
                {
                   myCargo = anObject;  //use IVAR myCargo to point to
                                        //cargo object.
                   [anObject setTaken: YES];
                   foundOne = YES;
                }
         }
}


The other approach, which I think would be more realistic, would be to have a list of "notTakenCargo" and then put things on and off of it. An agent could interact with the cargoList and remove items when they are in its possession. Or you could have an ObjectGrid2d and place unblocked objects on it (like food for bugs).

pj


Jurgen van der Pol wrote:
Ola Dearest Swarmists,

I need a bit of help, please. I'm strugging a bit with lists & indexes.

What I want is for the agents in my simulation (representing trucks, moving on a 2d lattice) to have access to a for-all-available, single 'list of cargo'. Now, tinkering onwards from on SimpleObserverBug, what I have is is cargo.h/m: the cargo object which does little else than fill itself with a number of (randomised) variables representing aspect of cargo (desitiantion coordinates, value, size, weight, due date, etc). I have bug.h/m (too lazy to rename to truck.h/m) as truck object to do the chores of picking up/delivering the cargo. I have modelswarm.h/m to build me the various items & observerswarm.h/m for the rest. As in SimpleObserverBug.

What I -want- done is that the instances of the bugs get 'access' to a single list of cargo objects and can interact with it. What I mean is that a bug can access the cargo list, 'grab' a cargo off that list (i.e. instill the various variables in itself) and 'block' that cargo piece for other bugs (so no two bugs can get hold of the same piece of cargo). I can't make this happen: local bugs looking & acting upon a single, global list. What I fail at is to get the bugs access the cargo list. Maybe this is because all real action is done within bug.h/m. The go-between that I have is that I give each bug a local -copy- of the cargo list, but this gets funny results as the '(de)blocking' does nog work as it should be.

Below are some code snippets (yes, I'm a horrible coder) as I have it now. I'd welcome some expert suggestions as to how I can get to the point that I have 'local' bugs accessing & interacting with a single, indexed, 'global' cargo list.

Thanks for any tips!
Cheers
Jurgen.


in modelswarm.m:
//-------------
- buildObjects

    // making the cargo objests & list
    cargoList = [List create: self];
    for (y = 0; y < numberOfCargo; y++)
    {
        aCargo = [Cargo createBegin: self];
        [aCargo setWorld: world];
        aCargo = [aCargo createEnd];
[aCargo setX: [uniformIntRand getIntegerWithMin: 0 withMax: worldXSize] // the destination X Y: [uniformIntRand getIntegerWithMin: 0 withMax: worldYSize] // the destination Y V: [uniformIntRand getIntegerWithMin: 1 withMax: 100] // value of cargo (EURO) W: [uniformIntRand getIntegerWithMin: 1 withMax: 100] // weight of cargo (kg) S: [uniformIntRand getIntegerWithMin: 1 withMax: 500] // size of cargo (L*W*H in m) D: [uniformIntRand getIntegerWithMin: 60 withMax: 4320] // when is it due at desination? (in minutes, between 1 and max 3 days) B: [uniformIntRand getIntegerWithMin: 60 withMax: 4320] // when is it perished? (in minutes, between 1 and max 3 days)
                    ];
cargoID = cargoID + 1;

        [aCargo setID: cargoID];
[aCargo deAllocate]; // make sure it's 'accessible'

        [cargoList addLast: aCargo];
    }

    // Make index of cargoList

    cargoIndex = [cargoList begin: [self getZone]];

    // Now, create a bunch of bugs (a.k.a. trucks) to live in the world

  bugList = [List create: self];

    for (y = 0; y < numberOfTrucks; y++)
    {
        aBug = [Bug createBegin: self];
        [aBug setWorld: world Food: food];
        aBug = [aBug createEnd];
[aBug setX: [uniformIntRand getIntegerWithMin: 0 withMax: worldXSize] Y: [uniformIntRand getIntegerWithMin: 0 withMax: worldYSize]];
        bugID = bugID + 1;
        [aBug setID: bugID];

// now pass the cargolist to the bug. Each bug will now have the -same- list.
        [aBug setMyCargoList: cargoList];
[bugList addLast: aBug];
    }


- buildActions

  modelActions = [ActionGroup create: self];
[modelActions createActionForEach: bugList message: M(goAhead)] // Here we go to bug!
//-------------

in cargo.m:
//-------------
// this sets the various variables in a cargo object
- setX: (int)x Y: (int)y V: (int)v W: (int)w S: (int)s D: (int)d B: (int)b
{
    xPos = x;            // y destination coordinate
    yPos = y;            // x destination coordinate
    value = v;            // value in EURO
    weight = w;            // weight in kg
    size = s;            // size = L*W*H
    duedate = d;        // when to deliver?
    bestbeforedate = b; // when is it perished?
return self;
}
//-------------


in bug.m:
//-------------
- goAhead // this is where modelswarm enters bug
{

if (haveEaten == 0) // while not loaded (recycling old simpleobserverbug2 variable here)
        [self getCargo];            // get to the depot for cargo
    else
        [self deliverCargo2];    // deliver the cargo at the destination

    return self;

}

- (void) setMyCargoList: aList // this gets the cargolist from modelswarm
{
myCargoList = aList; // make bug's local list myCargoIndex = [myCargoList begin: [self getZone]]; // make local index of list
}

- setMyCargo            // to get variables from cargo object into bug
{
    myCargo = [myCargoIndex next];        // get next cargo object on list
if ([myCargoIndex getLoc]==End) // are we at the end of the list?
        [myCargoIndex setLoc: Start];   // then go back to the beginning!

if ([myCargo getAllocationState] == 0) // is it NOT already allocated (by another truck)?
        {
[myCargo Allocate]; // this is important, it effectively 'blocks' other truck's access to this specific cargo object
        }
        else
        {
            myCargo = [myCargoIndex next]; // get next cargo object on list
                if ([myCargoIndex getLoc]==End) // EOL check
                [myCargoIndex setLoc: Start];   // if so, go back to front
        }

xDest = [myCargo getXDest]; // ok, got a cargo object, now read the x destination from it yDest = [myCargo getYDest]; // ok, got a cargo object, now read the y destination from it

    return self;
}

- deliverCargo2 // ok, trying out the list passing around stuff here.
{
    if (deliveryPointSet == 0) // i.e. we're an empty truck
        {
            [self setMyCargo];        // ok, get stuff from cargo object
[self setDestinationX: xDest Y: yDest]; // get & set my destination
            deliveryPointSet = 1;
        }

    [self setVector];                    // set my bearing
    [self step];                        // make me step on the 2d world

  if ([world getObjectAtX: xNew Y: yNew] == nil)
    {
      [world putObject: nil atX: xPos Y: yPos];
      xPos = xNew;
      yPos = yNew;
      [world putObject: self atX: xNew Y: yNew];
    }
    else
        {
            [self stepRandom]; // to avoid collisions
            collisions++; // just to see how often random steps were made
        }

    if ([self checkThere] == 1) // are we at destination?
        {
            haveEaten--;     //just recycling an old SOBug var here...
            deliveryPointSet = 0;
            credits = credits + [myCargo getCargoValue];
[myCargo deAllocate]; // this is important, it effectively 'frees' this specific cargo object so other bugs can access it

        }

    return self;

}

//-------------

_______________________________________________
Support mailing list
address@hidden
http://www.swarm.org/mailman/listinfo/support


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


reply via email to

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