swarm-support
[Top][All Lists]
Advanced

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

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


From: Jurgen van der Pol
Subject: [Swarm-Support] List problem: many bugs accessing a single list
Date: Tue, 27 Jul 2004 16:57:00 +0200

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;

}

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



reply via email to

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