1 / 30

Programming for Social Scientists Lecture 6

Programming for Social Scientists Lecture 6. UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson. Today's topics. Graphical User Interfaces (GUIs) graphs control panels Example 1: GraphIPD

oren
Download Presentation

Programming for Social Scientists Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming for Social ScientistsLecture 6 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt 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 POL SCI 209-1 Cederman / Stefansson

  3. 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 The Graphical User Interface POL SCI 209-1 Cederman / Stefansson

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

  5. 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 Creating a graph Agent Collection Averager POL SCI 209-1 Cederman / Stefansson

  6. Graph IPD: File Structure Player.h Tourna- ment.h Observer- Swarm.h Model- Swarm.h Observer- Swarm.m Model- Swarm.m Player.m Tourna- ment.m main.m 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. Step III: Building actions -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; } 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. Objectbase Probe EZGraph Entropy Averager EZBin EZ Distribution 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 POL SCI 209-1 Cederman / Stefansson

  16. 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 Using EZGraph POL SCI 209-1 Cederman / Stefansson

  17. Creating an EZGraph The axis labels, set with -setAxisLabelsX: str Y: str 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 POL SCI 209-1 Cederman / Stefansson

  18. 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: Using EZBin POL SCI 209-1 Cederman / Stefansson

  19. Creating an EZBin The number of bins is determined with -setNumBins: num and the range of values by -setUpperBound: -setLowerBound: The axis labels, set with -setAxisLabelsX: str Y: str The title, made with: -setTitle: aString Data is fed into Histogram by -setCollection: collection and -setProbedSelector:M(method) POL SCI 209-1 Cederman / Stefansson

  20. 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 Other utility objects in Analysis 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 Apartment building with elevator: What is the average waiting time? • Two types of elevators: • Type 0: "standard" Car remains where it is after use • Type 1: "modified" Car returns to zero after use n ... 3 2 1 0 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 n = number of stories 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; } 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 POL SCI 209-1 Cederman / Stefansson

More Related