Title: Swarmfest 99 Tutorial
1Swarmfest 99Tutorial
- Session II Simplebug demo
- Benedikt Stefansson
- ltbenedikt_at_ucla.edugt
2Second session
- Tutorial Simplebug
- Start From C to Objective-C to Swarm
- Adding GUI Probes, raster image, graphs
- Creating and controlling full experiment run
- Topics covered
- Managing objects
- Swarms, activity library, probes, GUI
3Chapters in Simplebug tutorial
- simpleCBug Simple C code
- simpleObjCBug Bug is now Objective-C class
- simpleObjCBug2 Adds grid holding food
- simpleSwarmBug Adds a ModelSwarm
- simpleSwarmBug2 Many bugs...
- simpleSwarmBug3 Reads parameters from file
- simpleObserverBug Adds ObserverSwarm GUI
- simpleObserverBug2 Adds more sophisticated GUI
- simpleExperBug Runs multiple experiments
4Bug in C
import ltsimtools.hgt void main (int argc, const
char argv) int worldXSize 80 //
Maximum X value int worldYSize 80 //
Maximum Y value int xPos 40 // Bug's
starting position int yPos 40 int i
initSwarm(argc,argv) printf("I started at X
d Y d \n\n", xPos, yPos) for(i 0 i lt
100 i) // Random movement in X and Y
(possibly 0) xPos xPos uniformIntRand
getIntegerWithMin -1 withMax 1 yPos
yPos uniformIntRand getIntegerWithMin -1
withMax 1 // Take move modulo maximum
coordinate values xPos (xPos worldXSize)
worldXSize yPos (yPos worldYSize)
worldYSize printf( "I moved to X d Y
d \n", xPos, yPos) return 0
1
2
3
4
5Bug in C - explanations
- Importing header file gives us access to random
number obj. - Each C program must have main function, which can
take 2 args - argc of command line args
- argv value of command line args
- initSwarm() function checks command line args and
revs up Swarm engine - The program uses default random number
distributions to choose new location for bug
1
3
2
4
6Bug in ObjC
- The simplest version of an Objective-C program
- A main( ) function
- One class, one instance Bug
- main( ) imports base library of Swarm and fires
up initSwarm( ) which gives us libraries for
memory allocation etc.
main
Instance vars
xPos,yPos worldXSize, worldYSize
-setX Y -setWorldSizeX Y -step
Methods
7Bug in ObjC The bug as object
- Inheriting from the ObjectBase class Bug knows
these tricks - create Allocate memory
- drop Deallocate and die
- First instruct class to create instance of
itself. Associate instance with aBug - aBugBug createglobalZone
- Then set parameters in the instance, aBug
- aBug setX xPos Y yPos
- aBug setWorldXSize worldXSize Y worldYSize
8Typical create phase
aBug Bug createBegin globalZone
Allocate memory etc.
Set all necessary parameters
- aBug setWorldSizeX xsize Y ysize
- aBug setFoodSpace foodSpace
- aBug setX xPos Y yPos
aBug aBug createEnd
Finish initialization and return proper instance
of object
9Bug in ObjC II The FoodSpace
- Here create a world for the bug to roam around in
and eat - The world is defined in FoodSpace, a subclass of
Discrete2d - Discrete2d manages a lattice of integer values
main
Bug
FoodSpace
By passing the Bug a pointer to FoodSpace it can
talk to (i.e. forage in) the other object
10The FoodSpace class
- By subclassing from Discrete2d FoodSpace inherits
several Ivars and about 16 methods to store and
retrieve values in 2d space - The only addition is a method to fill space with
food
-
-seedFoodWithProb(float)seedProb int x,y
for (y 0 y lt ysize y) for (x 0 x lt
xsize x) if (uniformDblRand
getDoubleWithMin 0.0 withMax
1.0ltseedProb) self putValue 1
atX x Y y
return self
Inherited variables
Inherited method
11The power of inheritance
- Defobj
- Root class of Swarm. Memory management, create,
drop etc. - SwarmObject
- Adds support for probes
- Discrete2d
- 2d lattice management
Defobj
SwarmObject
Discrete2d
FoodSpace
12Bug in Swarm The ModelSwarm
- An instance of the Swarm class can manage a
model world - Facilitates the creation of agents and
interaction model - Model can have many Swarms, often nested
main
ModelSwarm
Bug
FoodSpace
13Creating a Swarm
- I. createBegin,createEnd
- Initialize memory and parameters
- II. buildObjects
- Build all the agents and objects in the model
- III. buildActions
- Define order and timing of events
- IV. activate
- Merge into top level swarm or start Swarm running
14Step I Initializing
1
- createBegin (id) aZone
- ModelSwarm obj
-
- obj super createBegin aZone
- obj-gtworldXSize 80
- obj-gtworldYSize 80
- obj-gtseedProb 0.1
- obj-gtxPos 40
- obj-gtyPos 40
-
- return obj
2
3
4
5
15Details on createBegin method
- The indicates that this is a class method as
opposed to - which indicates an instance method - ModelSwarm obj indicates to compiler that obj
is statically typed
- super ...
- Executes createBegin method in the super class of
obj (Swarm) and returns an instance of ModelSwarm - obj-gtvarNameval
- Abuses OOP paradigm and pokes values directly
into object
1
4
3
5
16Memory zones
2
- The Defobj super class provides facilities to
create and drop an object - allocate and
deallocate memory - Each object is created in a memory zone
- Effectively this means that the underlying
mechanism provides enough memory for the
instance, its variables and methods. - The zone also keeps track of all objects created
in it and allows you to reclaim memory simply by
dropping a zone. It will signals to all objects
in it to destroy themselves
17Where did that zone come from?
Executes various functions in defobj and
simtools which create a global memory zone among
other things
- In main.m initSwarm (argc, argv)
In main.m modelSwarm ModelSwarm create
globalZone
create method is implemented in defobj,
superclass of the Swarm class and it calls the
createBegin method in ModelSwarm
In ModelSwarm.m createBegin
18Step II Building the agents
- -buildObjects
- foodSpace FoodSpace createBegin
globalZone - foodSpace setSizeX worldXSize Y worldYSize
- foodSpace foodSpace createEnd
- foodSpace seedFoodWithProb seedProb
-
- aBug Bug createBegin globalZone
- aBug setWorldSizeX worldXSize Y worldYSize
- aBug setFoodSpace foodSpace
- aBug setX xPos Y yPos
- aBug aBug createEnd
- return self
19Details on the buildObjects phase
- The purpose of this method is to create each
instance of objects needed at the start of
simulation, and then to pass parameters to the
objects - It is good OOP protocol to provide setX methods
for each parameter X we want to set, as in aBug
setFoodSpace foodSpace
20Why createBegin vs. create?
- Using createBegin, createEnd is appropriate when
we want a reminder that the object needs to
initialize something, calculate or set (usually
this code is put in the createEnd method). - Always use createBegin with createEnd to avoid
messy problems (object returned o.w. not valid) - But create is perfectly fine if we just want
just to create an object without further ado
21Step III Building schedules
- -buildActions
- modelScheduleSchedule createBegin self
- modelSchedule setRepeatInterval 1
- modelSchedule modelSchedule createEnd
- modelSchedule at 0
- createActionTo aBug
- message M(step)
- return self
22Schedules
t1
t2
t3
- Schedules define event in terms of
- Time of first invocation
- Target object
- Method to call
- Repeat interval
bug eat
bug eat
1
2
1
schedule at t createActionTo
agent message M(method)
2
3
3
4
23ActivityGroups
t1
t2
t3
- ActivityGroups group together events at same
timestep - Schedule then executes group
bug eat
bug eat
bug sleep
food grow
24Implementation
- schedule1Schedule createBegin self
- schedule1 setRepeatInterval 2
- schedule1schedule1 createEnd
- schedule1 at 1 createActionTo bug message
M(eat) - actionGroupActionGroup createBegin self
- actionGroup createEnd
- actionGroup createActionTo bug message
M(sleep) - actionGroup createActionTo food message
M(grow) - schedule2Schedule createBegin self
- schedule2schedule2 createEnd
- schedule2 at 2 createAction actionGroup
t1
t2
t3
25Step IV Activating the Swarm
- -activateIn (id) swarmContext
- super activateIn swarmContext
-
- modelSchedule activateIn self
- return self getActivity
26Activation of schedule(s)
There is only one Swarm so we activate it in nil
- In main.mmodelSwarm activateIn nil
This one line could set in motion complex scheme
of merging and activation
-activateIn (id) swarmContext
modelSchedule activateIn self
27Bug in Swarm II More bugs
- To manage more than one bug add
- World, a Grid2d instance that enforces one bug
per location - bugList, to keep track of bugs as a collection
main
ModelSwarm
Food Space
World
bugList
28Creating a population of agents
1
- Create collection objects (Grid2d, List) to keep
track of agents - In for loop create each instance initialize
- Then put each agent on list,grid
- (create world)
- bugListList create self getZone
- for(y0yltworldYSizey)
- for(x0xltworldXSizex)
- if(uniformDblRand . ltbugDensity)
- aBug Bug createBegin self
- aBug setWorld world
- Food foodSpace
- aBug setX x Y y
- aBug aBug createEnd
-
-
-
- world putObject aBug atX x Y y
- bugList addLast aBug
-
2
3
29Activity and collections
- We now have a collection of agents - bugs
- Schedule now takes as target the collection
object - a List instance - The collection object passes on the message to
each bug
- - buildActions
-
- modelActions ActionGroup create self
- modelActions createActionForEach bugList
message M(step) - modelActions createActionTo reportBug
message M(report) - modelSchedule Schedule createBegin self
- modelSchedule setRepeatInterval 1
- modelSchedule modelSchedule createEnd
- modelSchedule at 0 createAction
modelActions - return self
-
30Bug in Swarm III Loading state
- The model.setup file
- _at_begin
- worldXSize 80
- worldYSize 80
- seedProb 0.9
- bugDensity 0.01
- _at_end
- ObjectLoader needs
- target object
- name of file
- ObjectLoader
- Reads values of ivars from file
- ObjectSaver
- Writes values of ivars to file
- Any instance vars not mentioned in infile
unchanged
Needed
Needed
31Bug with Observer Adding GUI
main
Observer Swarm
Model Swarm
Object2dDisplay
Raster
World
Food Space
bugList
Colormap
Value2dDisplay
32Creating an ObserverSwarm
- createBegin,createEnd
- Initialize memory and parameters
- buildObjects
- Build ModelSwarm
- Build graphs, rasters and probes
- buildActions
- Define order and timing of GUI events
- activate
33Step I Initializing
- createBegin aZone
- ObserverSwarm obj
-
- obj super createBegin aZone
- obj-gtdisplayFrequency 1
- return obj
34Step II Creating objects
- - buildObjects
- super buildObjects
- modelSwarm ModelSwarm create self
- controlPanel setStateStopped
- modelSwarm buildObjects
-
- (create Colormap)
- (create Raster)
- (create foodDisplay)
- (create bugDisplay)
- return self
Boldfaced items are only placeholders
35Step III Building schedules
-buildActions super buildActions
modelSwarm buildActions (create ActionGroup)
(create foodDisplay action) (create
bugDisplay action) (create worldRaster
action) (create GUI update action) (create
Schedule) (create action to
ActionGroup) return self
36Step IV Activating the Swarms
- - activateIn swarmContext
- super activateIn swarmContext
- modelSwarm activateIn self
- displaySchedule activateIn self
- return self getSwarmActivity
-
37Integration of Swarm activities
Sub-Swarm
ModelSwarm
ObserverSwarm
Swarm kernel
38Multilevel activation
In main.mtopSwarm activateIn nil
-activateIn (id) swarmContext
schedule activateIn self subSwarm activateIn
self
-activateIn (id) swarmContext
schedule activateIn self
39Merging two Swarms
- main( ) creates ObserverSwarm
- ObserverSwarm creates ModelSwarm as a subswarm in
own memory zone - ModelSwarm creates agents and activates self in
ObserverSwarm
modelSwarm create self modelSwarm
buildObjects modelSwarm buildActions modelSw
arm activateIn self
Observer
-create aZone -buildObjects -buildActions -act
ivateIn swarmContext
Model
40Managing the Raster display
- ZoomRaster displays data from lattice
- We will display the food distribution from
FoodSpace and ask bugs to draw location - In addition to ZoomRaster need the 3 classes to
the right
- ColorMap
- Associates a number with a color in palette
- Value2dDisplay
- Maps array of x,y int data to raster
- Object2dDisplay
- Feeds data from agents captures mouseclick
41What do these new objects do?
bugList
Object2dDisplay
Colormap
world
Holds x,y locations of bugs as array of ids
Value2dDisplay
food
Holds x,y location of food as array of 0,1
ModelSwarm
ObserverSwarm
Raster
42Bug with Observer II Add probes
main
Probe
Probe
Observer Swarm
Model Swarm
bugList
43How the probes come in...
A custom ProbeMap with 1 MessageProbes
Model Swarm
Observer Swarm
A custom ProbeMap with 4 VariableProbes
ControlPanel is provided by kernel
44Brief overview of probes
- Two major uses for probes
- To interface with an object
- To create a GUI to an object
- Interface with an object of two types
- VarProbe Probes an instance variable
- MessageProbe Probes a method
- GUI utilities
- ProbeMap Collection of Var and MessageProbes
45Creating graphic probe to object
A ProbeMap
- Check out instance of EmtpyProbeMap
- Attach VarProbe or MessageProbe to each variable
or message to appear on GUI - Put each probe on ProbeMap
- Ask probeDisplayManager to create actual widget
1
MessageProbe
2
3
VarProbes
Object
4
46Creating ModelSwarms probe
- probeMap EmptyProbeMap createBegin self
- probeMap setProbedClass self class
- probeMap probeMap createEnd
-
- probeMap addProbe probeLibrary
- getProbeForVariable "worldXSize
- inClass self class
-
- probeLibrary setProbeMap probeMap
- For self class
- (then call probeDisplayManager
createProbeDisplayFor modelSwarm in Observer)
1
2
3
4
47Final frontier Experiment Bug
main
Probe
Experiment Swarm
Observer Swarm
Graph
Model
48Multilevel activation
In main.mobserverSwarm activateIn nil
-activateIn (id) swarmContext
ObserverSwarm
schedule activateIn self experimentSwarm
activateIn self
-activateIn (id) swarmContext
ExperSwarm
schedule activateIn self modelSwarm
activateIn self
-activateIn (id) swarmContext
ModelSwarm
schedule activateIn self
49Two approaches to experiments
- Use external program to execute Swarms
- Use Swarm to control experiment runs
External program
50Two approaches Pros and cons
- Use Swarm to control experiment runs
- Pros
- All processing is done within Swarm framework
- Can use GUI interactively
- Cons
- Sequential - cant parallelize in present form
- Use external program to execute Swarms
- Pros
- Can do poor mans paralellization on
multiprocessor machines - Cons
- Requires knowledge of tools outside Swarm
framework