programming for social scientists lecture 6 ucla political science 209-1: programming for social...
Post on 21-Dec-2015
214 views
TRANSCRIPT
Programming for Social Scientists
Lecture 6UCLA Political Science 209-1: Programming for Social
Scientists
Winter 1999
Lars-Erik Cederman & Benedikt Stefansson
POL SCI 209-1 Cederman / Stefansson
2
Today's topics
Graphical User Interfaces (GUIs)– graphs– control panels
Example 1: GraphIPDProgram that endows EvolIPD with a dynamic
graph and a control panel.
Example 2: Elevator
POL SCI 209-1 Cederman / Stefansson
3
The Graphical User Interface
• Objects provided to create and manage – Line graphs
– Histograms
– Raster images
– Digraphs
• Data collection, calculation and updating is provided through support objects to the GUI widgets
POL SCI 209-1 Cederman / Stefansson
4
GraphIPD: Adding GUImain
EZGraph
ModelSwarmpopList
Tournament
newListwinner
ControlPanel
ObserverSwarm
POL SCI 209-1 Cederman / Stefansson
5
Creating a graph
• EZGraph class sets up line graph for either– Single agent or object
– Collection of agents
• In both cases need– Name of sequence
– Type of sequence {Count, Total, Average, Min, Max}
– Target (object or collection)
– Name of method to get data
Agent
AveragerCollection
POL SCI 209-1 Cederman / Stefansson
6
Graph IPD: File Structure
main.mModel-Swarm.m
Tourna-ment.m
Tourna-ment.h
Model-Swarm.h
Observer-Swarm.m
Observer-Swarm.h
Player.m
Player.h
POL SCI 209-1 Cederman / Stefansson
7
GraphIPD: main.m
#import <simtools.h>
#import "ObserverSwarm.h"
int main(int argc,const char ** argv) {
ObserverSwarm * observerSwarm;
initSwarm(argc, argv);
observerSwarm = [ObserverSwarm create: globalZone];
[observerSwarm buildObjects];
[observerSwarm buildActions];
[observerSwarm activateIn: nil];
[observerSwarm go];
return 0;
}
POL SCI 209-1 Cederman / Stefansson
8
GraphIPD: ObserverSwarm.h
@interface ObserverSwarm : GUISwarm {
int displayFrequency;
id displayActions;
id displaySchedule;
id modelSwarm;
id <EZGraph> numGraph;
}
+createBegin: (id) aZone;
-createEnd;
-buildObjects;
-buildActions;
-activateIn: (id) swarmContext;
@end
POL SCI 209-1 Cederman / Stefansson
9
Creating an ObserverSwarm
• createBegin,createEnd – Initialize memory and parameters
• buildObjects– Build ModelSwarem
– Build graphs, rasters and probes
• buildActions– Define order and timing of GUI events
• activate
POL SCI 209-1 Cederman / Stefansson
10
Step I: Initializing
+ createBegin: aZone {
ObserverSwarm *obj;
obj = [super createBegin: aZone];
obj->displayFrequency = 1;
return obj;
}
POL SCI 209-1 Cederman / Stefansson
11
Step II: Creating objects
-buildObjects {
[super buildObjects];
modelSwarm = [ModelSwarm createBegin: self];
modelSwarm = [modelSwarm createEnd];
[modelSwarm buildObjects];
// cont'd
POL SCI 209-1 Cederman / Stefansson
12
Step II: Creating objects (cont'd)
numGraph = [EZGraph createBegin: self];
[numGraph setTitle: "Number of all-C"];
[numGraph setAxisLabelsX: "Time" Y: "Number"];
numGraph = [numGraph createEnd];
[numGraph createSequence: "all-C" withFeedFrom: modelSwarm andSelector: M(getNum0)];
[numGraph createSequence: "TFT" withFeedFrom: modelSwarm andSelector: M(getNum1)];
[numGraph createSequence: "aTFT" withFeedFrom: modelSwarm andSelector: M(getNum2)];
[numGraph createSequence: "all-D" withFeedFrom: modelSwarm andSelector: M(getNum3)];
[controlPanel setStateStopped];
return self;
}
POL SCI 209-1 Cederman / Stefansson
13
-buildActions {
[super buildActions];
displayActions = [ActionGroup create: self];
[displayActions createActionTo: modelSwarm message: M(step)];
[displayActions createActionTo: numGraph message: M(step)];
[displayActions createActionTo: actionCache message: M(doTkEvents)];
displaySchedule = [Schedule createBegin: self];
[displaySchedule setRepeatInterval: 1];
displaySchedule = [displaySchedule createEnd];
[displaySchedule at: 0 createAction: displayActions];
return self;
}
Step III: Building actions
POL SCI 209-1 Cederman / Stefansson
14
Step IV: Activating
-activateIn: (id) swarmContext {
[super activateIn: swarmContext];
// If modelSwarm also has activity schedule:
// [modelSwarm activateIn: self];
[displaySchedule activateIn: self];
return [self getSwarmActivity];
}
POL SCI 209-1 Cederman / Stefansson
15
Major Analysis classes
• EZGraph– Combines several objects to
gather and display data on line graph
• EZBin– Does same for histogram
• Averager, Entropy and EZDistribution– Collect statistics from
collection of objects
Averager
EZGraph
EZBin
Objectbase
Entropy
EZDistribution
Probe
POL SCI 209-1 Cederman / Stefansson
16
Using EZGraph
• EZGraph is a wrapper around several objects
• Allows creation and setup to be achieved in one fell swoop
• After setting title and labels must add one or more sequences
• Necessary steps:1) create an instance
2) setTitle: titleString
3) setAxisLabelsX: str Y: str
4) Add sequence(s) – With feed from object
– Or feed from collection: AverageSequence
TotalSequence
MinSequence MaxSequence
POL SCI 209-1 Cederman / Stefansson
17
Creating an EZGraph
A sequence with feed from collection is created with-createASequence: aString withFeedFrom: collection andSelector: M(method)where A is one of Average, Total, Min, Max or Count
aString
The title, made with:-setTitle: aString
The axis labels, set with-setAxisLabelsX: str Y: str
POL SCI 209-1 Cederman / Stefansson
18
Using EZBin
• EZBin, is also a wrapper around several objects
• This type of graph needs to be told the number of bins for histogram and interval in which values lie
• Necessary steps:1) create an instance
2) setTitle: titleString
3) setCollection: collection
4) setProbedSelector:M(method)
5) setBinNum: n
6) setUpperBound: and setLowerBound:
POL SCI 209-1 Cederman / Stefansson
19
Creating an EZBin
Data is fed into Histogram by-setCollection: collectionand-setProbedSelector:M(method)
The title, made with:-setTitle: aString
The axis labels, set with-setAxisLabelsX: str Y: str
The number of bins is determined with-setNumBins: numand the range of values by-setUpperBound: -setLowerBound:
POL SCI 209-1 Cederman / Stefansson
20
Other utility objects in Analysis
• Averager– Gets feed from
collection and calculates:
• Average
• Total
• Min
• Max
• Count
• Entropy– Gets feed from
collection and calculates entropy assuming data is probabilities
• EZDistribution– Subclass of EZBin,
gives acces to underlying distribution of data
POL SCI 209-1 Cederman / Stefansson
21
Homework Week 6
1. Modify GraphIPD sample program:a. Add a second graph plotting the average payoff
of the players over time
b. Replace frequency graph with histogram showing the frequency of the four strategies dynamically
2. Extend Elevator sample program to n-tenant case
POL SCI 209-1 Cederman / Stefansson
22
Homework Week 6: Exercise 2
0
1
2
3
Apartment
building with
elevator:
What is the
average waiting
time?
...
nTwo types of elevators:•Type 0: "standard" Car remains where it is after use•Type 1: "modified" Car returns to zero after use
POL SCI 209-1 Cederman / Stefansson
23
Assignment:a. Derive theoretical waiting times for n=2.
b. Generalize the program Elevator from two-tenant case to any n.
c. Simulate the estimated waiting time for both elevator types.
d. Plot the waiting times as a function of n.
e. Which elevator type minimizes waiting?
*f. Derive theoretical waiting times for n.
POL SCI 209-1 Cederman / Stefansson
24
Theoretical average waiting times for elevator types 0 and 1
EW(n,0) 1
3n(n2 1)
EW(n,1) n 1
4
n = number of stories
POL SCI 209-1 Cederman / Stefansson
25
Elevator: main.mint main(int argc, const char ** argv) {
...
for (eType = 0; eType < 2; eType++) {
[tenant1 init: 1];
[tenant2 init: 2];
[elevator init: 2 type: eType];
for (repl = 0; repl < n; repl++) {
if ([uniformIntRand getIntegerWithMin: 0 withMax: 1])
[tenant1 move: elevator];
else
[tenant2 move: elevator];
}
printf("Type: %d Time: %10.6f \n",
eType,
(double) ([tenant1 getTime]+[tenant2 getTime])/(double) n);
}
return 0;
}
POL SCI 209-1 Cederman / Stefansson
26
Elevator: Tenant.h...
@interface Tenant: SwarmObject {
int floor, homeFloor;
int waitingTime, trips;
}
-init: (int) n;
-setFloor: (int) f;
-(BOOL)isAtHome;
-(int)getTime;
-move: (id) e;
@end
POL SCI 209-1 Cederman / Stefansson
27
Elevator: Tenant.m...-init: (int) n { homeFloor = n; if ([uniformIntRand getIntegerWithMin: 0 withMax: 1]) floor = homeFloor; else floor = 0; waitingTime = 0; trips = 0; return self;}...-move: (id) e { waitingTime = waitingTime + [e callAtFloor: floor]; if ([self isAtHome]) [e take: self toFloor: 0]; else [e take: self toFloor: homeFloor]; trips++; return self;}
POL SCI 209-1 Cederman / Stefansson
28
Elevator: Elevator.h...
@interface Elevator: SwarmObject {
int floor;
int type;
}
-init: (int) nFloors type: (int) eType;
-(int)getFloor;
-(int)callAtFloor: (int) f; // returns waiting time!
-take: (id) t toFloor: (int) f;
@end
POL SCI 209-1 Cederman / Stefansson
29
Elevator: Elevator.m (1)...
@implementation Elevator
-init: (int) nFloors type: (int) eType {
type = eType;
if (type)
floor = 0;
else {
if ([uniformIntRand getIntegerWithMin: 0 withMax: 1])
floor = 0;
else
floor = [uniformIntRand getIntegerWithMin: 1 withMax: nFloors];
}
return self;
}
...
POL SCI 209-1 Cederman / Stefansson
30
Elevator: Elevator.m (2)...
-(int)callAtFloor: (int) f {
int wait;
wait = abs(floor-f);
floor = f;
return wait;
}
-take: (id) t toFloor: (int) f {
[t setFloor: f];
if (type==1)
floor = 0;
else
floor = f;
return self;
}
@end