[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Swarm-Support] working with (indexed) lists: beginner's Q
From: |
Paul Johnson |
Subject: |
Re: [Swarm-Support] working with (indexed) lists: beginner's Q |
Date: |
Sun, 02 May 2004 23:52:37 -0500 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040116 |
If I understand, you want the bug to be able to review a list of options
and find one? I do things like this all the time.
In the Bug, create an IVAR
in Bug.h:
id cargoList;
- (void)setCargoList: aList;
In Bug.m, at the top include the header file for the Cargo class
#import "Cargo.h"
//use a method to set that list:
- (void)setCargoList: aList
{
cargoList = aList;
}
Now the bug can look at the cargoList to find what it wants.
It is recommended to traverse a collection like this:
cargoIndex = [cargoList begin: [self getZone]]; //note, in Model swarm,
//use begin: self, in Bug,
//use this
Cargo * anObject; //specific declaration for clarity
for (anObject = [cargoIndex next]; [cargoIndex getLoc]==Member;
anObject= [cargoIndex next])
{
//do something to cargo objects
}
[cargoIndex drop];
All the other stuff about making the changes show on the grid is really
a separate problem.
An exercise along these lines is from my modeling class: write a new
class "Exterminator" and have it kill some bugs and make the thing show
on the graphs. One of the students even put little pictures of
Exterminators.
http://lark.cc.ku.edu/~pauljohn/ps909/Agent-Based_Models/
The exterminator assignment is
Exercise9.1.txt
and I have some student results in a directory. I think there is also
one in myExterminator.
Jurgen van der Pol wrote:
Hi Swarmists,
A beginners question on working with lists. The user/refbooks only get
me this far and I'm not quite succeeding so would appreciate some hints.
Building on simpleobserverbug2 (BIG thanks, whomever...) , I have a 2d
world (GUI, display, EZGraph etc, all is working, I so proud!) in which
bugs represent trucks: if empty go to a depot, if at depot, pick up
cargo and deliver it. What I want the model to do is for a bug, when
arriving at the depot, look on a (for all bugs to 'see') list of cargo
and get one (later: bid on), based on a number of conditions. The bug
needs to 'look' at the cargo objects to get one that matches certain
creiteria (e.g. value, weigth etc) and, once chosen, get the cargo's
destination from the cargo object & get paid a value (also instilled in
the cargo object when delivering it. Hope intentions are somewhat clear.
I have observerswarm.h/m, modelswarm.h/m, bug.h/m, cargo.h/m etc.
My modelswarm.m - buildObjects builds me a number of cargo objects, and
the bug objects. Both are put on a list:
//--8<-----
cargoList = [List create: self];
for (y = 0; y < 1; y++) // just one to begin with, this will
of course be different once it 'works'
{
aCargo = [Cargo createBegin: self];
[aCargo setWorld: world];
aCargo = [aCargo createEnd];
[aCargo setX: 20 Y: 20 V: 100]; // again for test purposes
hard data here, later different. V = value
cargoID = cargoID + 1;
[aCargo setID: cargoID];
[cargoList addLast: aCargo]; // the cargo list
}
//--8<-----
I also make an index of cargoList here:
//--8<-----
cargoIndex = [cargoList begin: [self getZone]];
//--8<-----
This all seems to go OK.
The bugs are also familiar:
//--8<-----
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];
[aBug getMyCargoList: cargoIndex]; // *** here I
(think I)'m passing the cargoIndex to an (id) variable in the bug
[bugList addLast: aBug];
}
//--8<-----
Also seems to work. Stuff then goes on to display the bug on-screen,
etc. The cargo is not displayed in any way.
Then - buildActions does (a.o)
//--8<-----
modelActions = [ActionGroup create: self];
[modelActions createActionForEach: bugList message: M(goAhead)];
//--8<-----
The goAhead method sits in bug.m and is basically an or/or:
//--8<-----
- goAhead
{
if (haveEaten == 0) // while not loaded - switch
[self getCargo]; // get to the depot
else
[self deliverCargo]; // deliver the cargo at the destination
return self;
}
//--8<-----
- getCargo sets the destination to the coordinates of the depot and
makes it go there
- deliverCargo is where the delivery action is.
I'm not quite sure how to go on next. I thing I know how to pass either
the cargoList or the cargoIndex to the individual bugs.
What I have trouble with is getting access to the cargo lists (and
particularly to and individual cargo object's variables) from within the
bug. In bug.m:
//--8<-----
- deliverCargo
{
id temp;
temp = [myCargoList next]; //*** can this work? Is this
correct? What have I just done?
if (deliveryPointSet == 0)
{
[self setDestinationX: [temp getXDest] Y: [temp getYDest]];
//*** ditto? is temp a cargo instance or not???
deliveryPointSet = 1;
}
//--8<-----
where - getXDest and - getYDest live in cargo.h/m and simply return the
X, Y values of the cargo instance:
//--8<-----
- getXDest
{
return xPos;
}
- getYDest
{
return yPos;
}
//--8<-----
Now, can this work in any way? I know that the end result code will be
different, but I need to get the basics going first. Am I on the right
track? Should I pass the cargoList or the cargoIndex to the bug? In both
cases, how then to access the elements contained therein, and the
variables they hold? Apparently my ObjC/Swarm knowledge is still not
advanced enough to get this down quickly. I do know I'm close, though.
Any (off-list if this is too trivial) pointers are greatly appreciated!
Cheers, TIA,
Jurgen.
_______________________________________________
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