Paul Johnson Feb. 17, 2000 POLS 909 Swarm Beginner Exercises. I was struggling to think of things a person can do to gain a basic level of comfort with Swarm and Objective-C. I don't want to rely on any advanced Swarm programming tricks, I want the basics of objects, methods, and so forth. So here is what I have come up with. After you finish reading/working through the Swarm tutorial, you should be able to manage these things, if you make some effort. Begin with any "relatively advanced" swarm application, such as simpleObserverBug2 or heatbugs. Then try these things. 1. Create a new method in the agent class (Bug or such). I always create a "sayHi" method in my agents, and it has printf statements in there that report the agent's variables. After you have created a method that does that (whatever you call it), then in your "step" method, or any other method that gets executed, add a message that calls your new method. When your program runs, you should see output in your console. Question: if you put the new method in Bug.m, but not in Bug.h, does it run? (You may get a compiler warning about the method not being declared, but the model should run, even though the method is not in the interface.) You may have to remove -Werror from Makefile.common in order to make it compile, though. 2. In your ModelSwarm, try to schedule the agent to execute the new method you created. Do that in the buildActions method. Note that, if you want this to work, you have to add your new method to the interface file for Bug.h or whatever. If you don't have that method in the interface, part 1 may still work, but for sure this step won't work. 3. The method you created in 1 probably did not have any return value you wanted to catch. Now write a new method in your Bug class that returns an integer or a floating point value. I don't care what that value is. Then in your step method name a variable, and set it equal to the return value from your new method, as in int newVariable; newVariable=[self newFunction]; Then put a printf statement that prints newVariable to the console. 4. After that works, modify your newFunction so that it takes some arguments. It could take as arguments any variables that are available inside the scope of your step method. Then fiddle the newFunction's innards to do something with those values and return a result. Harder/Trickier things to try. 5. Does your ModelSwarm have a "getList" (or similar name) method that returns a list of all its objects? Most of them do have that, and it is used primarily by the Observer, which asks the model swarm to give it a list for plotting. If you don't have a getList kind of method in ModelSwarm, make one. Then try this. Figure out a way that the Bug class can ask the model swarm for a list, and let the Bug keep that list. Once you think you have the ability to retrieve the list in the Bug, and suppose you call that list "bugList", then you can do this in your Bug to see if it worked: [bugList forEach: M(sayHi)]; This, of course, assumes you have a sayHi method in your bug class. 6. If this seems to indicate that you do indeed have the list copied into your Bug, then try this in your Bug class. You can put this in the step method or in another method, as long as the method gets executed sometime. Let's grab the first bug in the list, firstbug=[buglist atOffset: 0]; Then tell that bug to execute some methods, such as sayHi. 7. If you got part 6, next write a method that can grab an agent from the list, possibly at random, using the select methods from the NSelect class in the simtools library. You must import simtools.h and the NSelect method is declared like this: + (void)select: (int)n from: aCollection into: bCollection; Notice that select is a class method, so if you have "buglist" in place of aCollection and some "otherList" you created in place of bCollection, you can execute it like this: [NSelect select: 1 from: bugList into: otherList]; There is an easier way to select a single member, you could use the uniform integer random variable to pick an integer, k, between 0 and N-1 and then do aBug = [bugList atOffset: k]; But I like NSelect for other purposes, so you could try it too.