Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Social Scientists Lecture 6 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson.

Similar presentations


Presentation on theme: "Programming for Social Scientists Lecture 6 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson."— Presentation transcript:

1 Programming for Social Scientists Lecture 6 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson

2 POL SCI 209-1 Cederman / Stefansson 2 Today's topics Graphical User Interfaces (GUIs) –graphs –control panels Example 1: GraphIPD Program that endows EvolIPD with a dynamic graph and a control panel. Example 2: Elevator

3 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

4 POL SCI 209-1 Cederman / Stefansson 4 GraphIPD: Adding GUI main EZGraph ModelSwarm popList Tournament newList winner ControlPanel Observer Swarm

5 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 Averager Collection

6 POL SCI 209-1 Cederman / Stefansson 6 Graph IPD: File Structure main.m Model- Swarm.m Tourna- ment.m Tourna- ment.h Model- Swarm.h Observer- Swarm.m Observer- Swarm.h Player.m Player.h

7 POL SCI 209-1 Cederman / Stefansson 7 GraphIPD: main.m #import #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; }

8 POL SCI 209-1 Cederman / Stefansson 8 GraphIPD: ObserverSwarm.h @interface ObserverSwarm : GUISwarm { int displayFrequency; id displayActions; id displaySchedule; id modelSwarm; id numGraph; } +createBegin: (id) aZone; -createEnd; -buildObjects; -buildActions; -activateIn: (id) swarmContext; @end

9 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

10 POL SCI 209-1 Cederman / Stefansson 10 Step I: Initializing + createBegin: aZone { ObserverSwarm *obj; obj = [super createBegin: aZone]; obj->displayFrequency = 1; return obj; }

11 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

12 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; }

13 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

14 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]; }

15 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 EZ Distribution Probe

16 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

17 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

18 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:

19 POL SCI 209-1 Cederman / Stefansson 19 Creating an EZBin Data is fed into Histogram by -setCollection: collection and -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: num and the range of values by -setUpperBound: -setLowerBound:

20 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

21 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

22 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?... n Two types of elevators: Type 0: "standard" Car remains where it is after use Type 1: "modified" Car returns to zero after use

23 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.

24 POL SCI 209-1 Cederman / Stefansson 24 Theoretical average waiting times for elevator types 0 and 1 n = number of stories

25 POL SCI 209-1 Cederman / Stefansson 25 Elevator: main.m int 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; }

26 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

27 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; }

28 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

29 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; }...

30 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


Download ppt "Programming for Social Scientists Lecture 6 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson."

Similar presentations


Ads by Google