Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for Social Scientists Lecture 9 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 9 UCLA Political Science 209-1: Programming for Social Scientists Winter 1999 Lars-Erik Cederman & Benedikt Stefansson."— Presentation transcript:

1 Programming for Social Scientists Lecture 9 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 Running experiments Graph File input/output Code example: ExperIPD –Based on ProbeIPD from last week

3 POL SCI 209-1 Cederman / Stefansson 3 ExperIPD: Running experiments main ModelSwarm ExperSwarm Graph ControlPanel

4 POL SCI 209-1 Cederman / Stefansson 4 Changes to ProbeIPD... New ExperSwarm controls experiment Use command line parameter to choose between Observer and ExperSwarm Read loop parameters from file Graph created from x,y data Using OutFile to create model.setup and datafiles

5 POL SCI 209-1 Cederman / Stefansson 5 Changes to main.m int main(int argc,const char ** argv) {... initSwarm(1,argv); if (argc == 1) { observerSwarm = [ObserverSwarm create: globalZone]; [observerSwarm buildObjects]; [observerSwarm buildActions]; [observerSwarm activateIn: nil]; [observerSwarm go]; } else { experSwarm = [ExperSwarm create: globalZone]; [experSwarm buildObjects]; [experSwarm run]; } return 0;}

6 POL SCI 209-1 Cederman / Stefansson 6 Switching on command line args initSwarm(1,argv); if (argc == 1) {... The argc variable and argv array come from the command line We 'trick' initSwarm to think there are no arguments (so it doesn't parse argv) argc==1 means there were no arguments

7 POL SCI 209-1 Cederman / Stefansson 7 ExperSwarm.h @interface ExperSwarm : GUISwarm { id modelSwarm; char * parameterName; double lowerBoundParameter; double upperBoundParameter; double incrementParameter; int lowerBoundSeed; int upperBoundSeed; int numGenerations; id graph; id data; } +createBegin: (id) aZone; -createEnd; -buildObjects; -run; @end ExperSwarm is set up to read a parameter name to loop over and the bounds and increments for the loop It also displays results on a Graph and saves to file

8 POL SCI 209-1 Cederman / Stefansson 8 Pseudo code for experiments for(parameter values) { for(random seeds) { SAVE setupfile FOR model CREATE model,averager,file LOAD model setupfile for(generations) RUN model UPDATE average,graph,file DROP model,averager } SHOW graph SAVE file The ExperSwarm has a exper.setup file which determines parameter to loop over and parameter values It communicates with ModelSwarm through loop.setup file which

9 POL SCI 209-1 Cederman / Stefansson 9 Example exper.setup file @begin numGenerations20 parameterName numIter lowerBoundParameter 1 upperBoundParameter 5 incrementParameter 1 lowerBoundSeed 8251777 upperBoundSeed 8251787 @end By saving the loop parameter name as string we can choose which of instance variables in model to loop over

10 POL SCI 209-1 Cederman / Stefansson 10 Handy classes from Simtools ObjectLoader –Loads instance variable values from file ObjectSaver –Saves instance variable values to file OutFile –Supports printing to file InFile –Reading from files Use OutFile to create an loop.setup for model that contains values of loop parameters ObjectLoader used to prime ModelSwarm from –model.setup (STATIC) –loop.setup (DYNAMIC) Also use OutFile to create data file

11 POL SCI 209-1 Cederman / Stefansson 11 Creating the loop.setup file for(p=pl;p<pu;p+=pi) { for(s=sl;s<su;s++) {... sprintf(string,"@begin\nrandomSeed %d\n%s %f\n@end\n",s,n,p); outFile=[OutFile create: self withName: "loop.setup"]; [outFile putString: string]; [outFile drop];... } 1 2

12 POL SCI 209-1 Cederman / Stefansson 12 Creating the loop.setup file (cont) The sprintf() function has same syntax as printf() except that result ends up in string (here str): i=3 sprintf(str,"Hello %d !",i) The string str now holds: Hello 3 ! OutFile prints strings or values to file: put{TYPE}: val Where {TYPE} is one of the C variable types (char,int,double...) or string,tab,newLine 1 2

13 POL SCI 209-1 Cederman / Stefansson 13 OutFile and InFile Basic idea that InFile opens file and “gets” strings and OutFile opens file and “puts” same Both can deal with any var type (only String and Int shown) In addition methods to insert delimiter (“tab”), skip line, go to next line etc. Some methods in InFile: -(int) getWord: (char *) aWord -(int) getInt: (int *) anInt; -(int) skipLine...and OutFile: -putString: (char *) aString -putInt: (int) anInt -putTab: (char) aChar -putNewLine

14 POL SCI 209-1 Cederman / Stefansson 14 Major GUI classes ZoomRaster –Resizeable raster Graph –Basic x,y graph Histogram –Basic histogram Colormap –Map between integer values and colors RasterGraph Histogram Widget ZoomRaster Colormap

15 POL SCI 209-1 Cederman / Stefansson 15 Creating a simple line graph Creating graph (in -buildObjects) graph = [Graph createBegin: self]; graph = [graph createEnd]; [graph setTitle: parameterName]; [graph setWidth: 400 Height: 300]; data = [graph createElement]; [data setLabel: "average payoff"]; Adding data to graph (in -run) [data addX: parameter Y: value]; Displaying graph (in -run) [graph pack]; Basic Graph class creates a widget with title and dimensions For each dataset we create an "element" Series of x,y values then fed to "element"


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

Similar presentations


Ads by Google